Coverage for articles/tests/test_views.py: 100.00%
81 statements
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-13 16:37 -0700
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-13 16:37 -0700
1import pytest
2from django.urls import reverse
3from pytest_django.asserts import (
4 assertContains,
5 assertTemplateUsed,
6)
8from ..models import Article
9from ..views import (
10 article_create,
11 article_delete,
12 article_detail,
13 article_update,
14)
16# pytestmark = pytest.mark.django_db
19@pytest.mark.django_db
20def test_article_create_view(rf, admin_user):
21 form_data = { 1f
22 "title": "A news article",
23 "slug": "",
24 "tags": "testingishard, placeholder",
25 "status": "PB",
26 "body": "This is serious news",
27 }
28 # Make a request for our new message
29 request = rf.post(reverse("article_new"), form_data) 1f
30 # Add an authenticated user
31 request.user = admin_user 1f
32 # Use the request to get the response
33 response = article_create(request) 1f
34 # Test that the response is valid
35 assert response.status_code == 302 1f
38@pytest.mark.django_db
39def test_article_detail_view(rf, article, admin_user):
40 # Get the request
41 url = f"{article.publish}/{article.slug}/" 1g
42 request = rf.get(url) 1g
43 request.user = admin_user 1g
44 # Use the request to get the response
45 response = article_detail( 1g
46 request,
47 year=article.publish.year,
48 month=article.publish.month,
49 day=article.publish.day,
50 article=article.slug,
51 )
52 # Test that the response is valid
53 assertContains(response, article.title) 1g
56"""
57@pytest.mark.django_db
58def test_article_list_view(rf, ten_articles, admin_user):
59 # Get the request
60 request = rf.get(reverse("article_list"))
61 request.user = admin_user
62 # Use the request to get the response
63 response = article_list(request)
64 html = response.content.decode("utf-8")
65 for i in range(10):
66 i += 1
67 assert f"A Tiny Test Article {i}" in html
68 # Test that the response is valid
69 assert response.status_code == 200
70 assertContains(response, "Latest News Articles")
71 # assert response.content[:].number == 10
72 # assert "is_paginated" in response.context
73 # assert (response.context("is_paginated") is True)
74 # assert len(response.context["article_list"]) == 5
75"""
78@pytest.mark.django_db
79def test_article_delete(rf, article):
80 request = rf.post( 1d
81 reverse("article_delete", kwargs={"pk": article.id}),
82 )
83 request.user = article.author 1d
84 callable_obj = article_delete 1d
85 response = callable_obj(request, pk=article.id) 1d
86 assert request.method == "POST" 1d
87 assert response.status_code == 302 1d
90@pytest.mark.django_db
91def test_article_delete_bad_author(rf, article, user):
92 request = rf.get( 1h
93 reverse("article_delete", kwargs={"pk": article.id}),
94 )
95 request.user = user 1h
96 callable_obj = article_delete 1h
97 response = callable_obj(request, pk=article.id) 1h
98 assert response.status_code == 200 1h
101@pytest.mark.django_db
102def test_article_update(rf, article):
103 """POST request to message_update updates a message
104 and redirects.
105 """
106 form_data = { 1b
107 "title": article.title,
108 "status": article.status,
109 "tags": "newtag, thistag, thenews",
110 "body": "This is the new article body",
111 }
112 url = reverse("article_update", kwargs={"pk": article.id}) 1b
113 # Make a request for our updated message
114 request = rf.post(url, form_data) 1b
115 request.user = article.author 1b
116 callable_obj = article_update 1b
117 response = callable_obj(request, pk=article.id) 1b
119 # Check that the message body has been changed
120 article.refresh_from_db() 1b
121 text = Article.published.last() 1b
122 assert response.status_code == 302 1b
123 assert text.author == article.author 1b
126@pytest.mark.django_db
127def test_article_create(client, user):
128 client.login(email=user.email, password="P@s5word") 1i
129 response = client.get("/articles/new/") 1i
130 assert response.status_code == 200 1i
131 assertTemplateUsed(response, "articles/article_new.html") 1i
134@pytest.mark.django_db
135def test_article_list_pagination(rf, client, ten_articles, user):
136 client.login(email=user.email, password=user.password) 1j
137 # response = rf.get("articles/?page=2")
138 response = client.get("/articles/") 1j
139 assert response.status_code == 200 1j
140 # assertTemplateUsed(response, "articles/article_list.html")
141 # assert response.context["is_paginated"]
142 # assertContains(response.content, "is_paginated")
143 # assert response.context["is_paginated"] is True
144 # assert len(response.context["article_list"]) == 5
147@pytest.mark.django_db
148def test_comment_add(client, article, user):
149 client.login(email=user.email, password="P@s5word") 1k
150 # form = "templates/articles/includes/comment_form.html"
151 response = client.post( 1k
152 reverse("comment_add", kwargs={"article_id": article.id}),
153 {
154 "name": "John Doe",
155 "email": "johndoe@example.com",
156 "body": "This is a new comment",
157 },
158 )
160 assert response.status_code, 200 1k
163"""
164 def test_pagination_is_five(self):
165 self.assertEqual(self.response.status_code, 200)
166 self.assertTrue("is_paginated" in self.response.context)
167 self.assertTrue(self.response.context["is_paginated"] is True)
168 self.assertEqual(len(self.response.context["article_list"]), 5)
170 def test_lists_all_articles(self):
171 self.client.login(email="johndoe@example.com", password="secret")
172 # Get second page and confirm it has (exactly) the remaining 3 items
173 response = self.client.get("articles/?page=2")
174 self.assertEqual(self.response.status_code, 200)
175 self.assertTrue("is_paginated" in response.context)
176 self.assertTrue(self.response.context["is_paginated"] is True)
177 self.assertEqual(len(self.response.context["article_list"]), 2)
178"""
181@pytest.mark.django_db
182def test_sitemap(client, ten_articles):
183 response = client.get("/sitemap.xml") 1e
184 xml = response.content.decode("utf-8") 1e
185 expected_articles = [a for a in ten_articles] 1e
186 assert response.status_code == 200 1e
187 assert len(expected_articles) == 10 1e
188 assert "<lastmod>" in xml 1e
191@pytest.mark.django_db
192def test_rssfeed(client, ten_articles):
193 response = client.get(reverse("article_feed")) 1c
194 xml = response.content.decode("utf-8") 1c
195 expected_articles = [a for a in ten_articles] 1c
196 assert len(expected_articles) == 10 1c
197 assert response["Content-Type"] == "application/rss+xml; charset=utf-8" 1c
198 assert response.status_code == 200 1c
199 assert "<title>news</title>" in xml 1c