From b30901af692a8897b8e832f8eb78d8d9249928c9 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Thu, 3 Dec 2020 21:34:40 +0100 Subject: [PATCH] Test feed views --- articles/tests/test_feed_views.py | 32 +++++++++++++++++++ .../{test_articles.py => test_html_views.py} | 0 articles/views/feeds.py | 3 +- 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 articles/tests/test_feed_views.py rename articles/tests/{test_articles.py => test_html_views.py} (100%) diff --git a/articles/tests/test_feed_views.py b/articles/tests/test_feed_views.py new file mode 100644 index 0000000..358b1d3 --- /dev/null +++ b/articles/tests/test_feed_views.py @@ -0,0 +1,32 @@ +import pytest +from django.test import Client +from django.urls import reverse +from model_bakery import baker + +from articles.models import Article, Page, User +from articles.views.feeds import CompleteFeed + + +@pytest.mark.django_db +def test_can_access_feed(client: Client, published_article): + res = client.get(reverse("complete-feed")) + assert res.status_code == 200 + assert "application/rss+xml" in res["content-type"] + content = res.content.decode("utf-8") + assert published_article.title in content + + +@pytest.mark.django_db +def test_feed_limits_number_of_articles(client: Client, author: User): + baker.make(Article, 100, status=Article.PUBLISHED, author=author) + res = client.get(reverse("complete-feed")) + content = res.content.decode("utf-8") + assert content.count("") == CompleteFeed.FEED_LIMIT + + +@pytest.mark.django_db +def test_page_not_rendered_in_feed(client: Client, published_page: Page): + res = client.get(reverse("complete-feed")) + assert res.status_code == 200 + content = res.content.decode("utf-8") + assert published_page.title not in content diff --git a/articles/tests/test_articles.py b/articles/tests/test_html_views.py similarity index 100% rename from articles/tests/test_articles.py rename to articles/tests/test_html_views.py diff --git a/articles/views/feeds.py b/articles/views/feeds.py index 5c4e1fd..bf76344 100644 --- a/articles/views/feeds.py +++ b/articles/views/feeds.py @@ -5,6 +5,7 @@ from blog import settings class CompleteFeed(Feed): + FEED_LIMIT = 15 title = "Gab's Notes" link = settings.BLOG["base_url"] description = settings.BLOG["description"] @@ -12,7 +13,7 @@ class CompleteFeed(Feed): def items(self): return Article.without_pages.filter(status=Article.PUBLISHED).order_by( "-published_at" - )[:15] + )[: self.FEED_LIMIT] def item_title(self, item: Article): return item.title