From 06b5e39b8db893f7dc3b4f87d8665442a58574c3 Mon Sep 17 00:00:00 2001 From: Victor Freire Date: Tue, 16 Jul 2024 22:46:01 -0300 Subject: [PATCH 1/2] change feed urls to follow custom domain if exists closes #52 --- main/models.py | 2 +- main/tests/test_api.py | 5 ++--- main/tests/test_feeds.py | 18 ++++++++++++++++++ 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/main/models.py b/main/models.py index ffe379fd..f1e3c046 100644 --- a/main/models.py +++ b/main/models.py @@ -238,7 +238,7 @@ def is_published(self): def get_absolute_url(self): path = reverse("post_detail", kwargs={"slug": self.slug}) - return f"//{self.owner.username}.{settings.CANONICAL_HOST}{path}" + return f"//{self.owner.blog_url}{path}" def get_proper_url(self): """Returns custom domain URL if custom_domain exists, else subdomain URL.""" diff --git a/main/tests/test_api.py b/main/tests/test_api.py index 01ccffeb..03a2977a 100644 --- a/main/tests/test_api.py +++ b/main/tests/test_api.py @@ -1,6 +1,5 @@ from datetime import date -from django.conf import settings from django.test import TestCase from django.urls import reverse @@ -675,7 +674,7 @@ def test_posts_get(self): "slug": "hello-world", "body": "## Hey\n\nHey world.", "published_at": "2020-01-01", - "url": f"{util.get_protocol()}//{self.user.username}.{settings.CANONICAL_HOST}/blog/hello-world/", + "url": f"{util.get_protocol()}//{self.user.blog_url}/blog/hello-world/", }, post_list, ) @@ -685,7 +684,7 @@ def test_posts_get(self): "slug": "bye-world", "body": "## Bye\n\nBye world.", "published_at": "2020-09-14", - "url": f"{util.get_protocol()}//{self.user.username}.{settings.CANONICAL_HOST}/blog/bye-world/", + "url": f"{util.get_protocol()}//{self.user.blog_url}/blog/bye-world/", }, post_list, ) diff --git a/main/tests/test_feeds.py b/main/tests/test_feeds.py index 739024b9..52424e5b 100644 --- a/main/tests/test_feeds.py +++ b/main/tests/test_feeds.py @@ -35,6 +35,24 @@ def test_rss_feed(self): f"//{self.user.username}.{settings.CANONICAL_HOST}/blog/{self.data['slug']}/", ) + def test_rss_feed_with_custom_domain(self): + # HELP ??? + response = self.client.get( + reverse("rss_feed"), + HTTP_HOST=self.user.username + "." + settings.CANONICAL_HOST, + ) + + self.assertEqual(response.status_code, 200) + self.assertEqual(response["Content-Type"], "application/rss+xml; charset=utf-8") + self.assertContains(response, self.data["title"]) + self.assertContains(response, self.data["slug"]) + self.assertContains(response, self.data["body"]) + self.assertContains( + response, + f"//{self.user.username}.{settings.CANONICAL_HOST}/blog/{self.data['slug']}/", + ) + + class RSSFeedDraftsTestCase(TestCase): """Tests draft posts do not appear in the RSS feed.""" From 4d44ac1c89846cc5ee939f1d87d88092242a456f Mon Sep 17 00:00:00 2001 From: Victor Freire Date: Wed, 17 Jul 2024 10:56:44 -0300 Subject: [PATCH 2/2] give testing a try --- main/tests/test_feeds.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/main/tests/test_feeds.py b/main/tests/test_feeds.py index 52424e5b..f9b9b727 100644 --- a/main/tests/test_feeds.py +++ b/main/tests/test_feeds.py @@ -36,10 +36,12 @@ def test_rss_feed(self): ) def test_rss_feed_with_custom_domain(self): - # HELP ??? + self.user.custom_domain = "mataroa-blog.com" + self.user.save() + response = self.client.get( reverse("rss_feed"), - HTTP_HOST=self.user.username + "." + settings.CANONICAL_HOST, + HTTP_HOST=self.user.custom_domain, ) self.assertEqual(response.status_code, 200) @@ -49,7 +51,7 @@ def test_rss_feed_with_custom_domain(self): self.assertContains(response, self.data["body"]) self.assertContains( response, - f"//{self.user.username}.{settings.CANONICAL_HOST}/blog/{self.data['slug']}/", + f"//{self.user.custom_domain}/blog/{self.data['slug']}/", )