From 7d4e11b096b9e1f56ddd4f4ad71de223e479e5d1 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Thu, 3 Dec 2020 21:15:48 +0100 Subject: [PATCH] Increase coverage for html views --- articles/tests/conftest.py | 13 +++++++++ articles/tests/test_articles.py | 52 +++++++++++++++++++++++++++++++-- articles/views/html.py | 2 +- 3 files changed, 63 insertions(+), 4 deletions(-) diff --git a/articles/tests/conftest.py b/articles/tests/conftest.py index c1448f6..e95b008 100644 --- a/articles/tests/conftest.py +++ b/articles/tests/conftest.py @@ -23,6 +23,19 @@ def published_article(author: User) -> Article: ) +@pytest.fixture() +@pytest.mark.django_db +def unpublished_article(author: User) -> Article: + return Article.objects.create( + title="Some interesting article title, but sorry it is not public yet", + status=Article.DRAFT, + author=author, + published_at=None, + slug="some-draft-article-slug", + content="## some draft article markdown\n\n[a draft article link](https://article.com)", + ) + + @pytest.fixture() @pytest.mark.django_db def published_page(author: User) -> Page: diff --git a/articles/tests/test_articles.py b/articles/tests/test_articles.py index 155a098..91feeb7 100644 --- a/articles/tests/test_articles.py +++ b/articles/tests/test_articles.py @@ -40,11 +40,57 @@ def test_only_title_shown_on_list(client: Client, author: User): @pytest.mark.django_db def test_access_article_by_slug(client: Client, published_article: Article): - res = client.get(reverse("article-detail", kwargs={"slug": published_article.slug})) + _test_access_article_by_slug(client, published_article) + + +@pytest.mark.django_db +def test_access_page_by_slug(client: Client, published_page: Page): + _test_access_article_by_slug(client, published_page) + + +def _test_access_article_by_slug(client: Client, item: Article): + res = client.get(reverse("article-detail", kwargs={"slug": item.slug})) assert res.status_code == 200 content = res.content.decode("utf-8") - assert published_article.title in content - assert published_article.get_formatted_content() in content + assert item.title in content + assert item.get_formatted_content() in content + + +@pytest.mark.django_db +def test_anonymous_cant_access_draft_detail( + client: Client, unpublished_article: Article +): + res = client.get( + reverse("article-detail", kwargs={"slug": unpublished_article.slug}) + ) + assert res.status_code == 404 + + +@pytest.mark.django_db +def test_user_can_access_draft_detail( + client: Client, author: User, unpublished_article: Article +): + client.force_login(author) + _test_access_article_by_slug(client, unpublished_article) + + +@pytest.mark.django_db +def test_anonymous_cant_access_drafts_list( + client: Client, unpublished_article: Article +): + res = client.get(reverse("drafts-list")) + assert res.status_code == 302 + + +@pytest.mark.django_db +def test_user_can_access_drafts_list( + client: Client, author: User, unpublished_article: Article +): + client.force_login(author) + res = client.get(reverse("drafts-list")) + assert res.status_code == 200 + content = res.content.decode("utf-8") + assert unpublished_article.title in content @pytest.mark.django_db diff --git a/articles/views/html.py b/articles/views/html.py index a35edd2..1c1d417 100644 --- a/articles/views/html.py +++ b/articles/views/html.py @@ -32,7 +32,7 @@ class ArticlesListView(BaseArticleListView): return context -class DraftsListView(BaseArticleListView, LoginRequiredMixin): +class DraftsListView(LoginRequiredMixin, BaseArticleListView): model = Article context_object_name = "articles" queryset = Article.objects.filter(status=Article.DRAFT)