Coverage for posts/tests/test_views.py: 100.00%
102 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-05-02 19:56 -0700
« prev ^ index » next coverage.py v7.8.0, created at 2025-05-02 19:56 -0700
1from datetime import datetime as dt
3import pytest
4from django.contrib.auth import get_user_model
5from django.test import TestCase
6from django.urls import reverse
7from pytest_django.asserts import (
8 assertContains,
9)
11from ..models import Comment, Post
13# from ..sitemaps import PostSitemap
14from ..views import (
15 PostCreateView,
16 PostDeleteView,
17 post_detail,
18 post_list,
19)
21# pytestmark = pytest.mark.django_db
24@pytest.mark.django_db
25def test_post_list_view(rf):
26 # Get the request
27 request = rf.get(reverse("post_list")) 1g
28 # Use the request to get the response
29 response = post_list(request) 1g
30 # Test that the response is valid
31 assert response.status_code == 200 1g
32 assertContains(response, "Welcome to django-blog") 1g
35@pytest.mark.django_db
36def test_post_detail_view(rf, post):
37 # Get the request
38 url = f"{post.publish}/{post.slug}/" 1h
39 request = rf.get(url) 1h
40 # Use the request to get the response
41 response = post_detail( 1h
42 request,
43 year=post.publish.year,
44 month=post.publish.month,
45 day=post.publish.day,
46 post=post.slug,
47 )
48 # Test that the response is valid
49 assertContains(response, post.title) 1h
52@pytest.mark.django_db
53def test_post_create_view(rf, post, admin_user):
54 form_data = { 1b
55 "title": post.title,
56 "status": post.status,
57 "body": post.body,
58 }
59 # Make a request for our new post
60 request = rf.post(reverse("post_new"), form_data) 1b
61 # Add an authenticated user
62 request.user = admin_user 1b
63 # Use the request to get the response
64 response = PostCreateView.as_view()(request) 1b
65 text = Post.published.last() 1b
66 # Test that the response is valid
67 assert response.status_code == 200 1b
68 assert text.author == post.author 1b
71@pytest.mark.django_db
72def test_message_delete(rf, post):
73 request = rf.post( 1e
74 reverse("post_delete", kwargs={"pk": post.id}),
75 )
76 request.user = post.author 1e
77 callable_obj = PostDeleteView.as_view() 1e
78 response = callable_obj(request, pk=post.id) 1e
79 assert response.status_code == 302 1e
82"""
83@pytest.mark.django_db
84def test_post_update(rf, post):
85 # POST request to PostUpdateView updates a post
86 # and redirects.
87 #
88 form_data = {
89 "title": post.title,
90 # tags: post.tags,
91 "status": post.status,
92 "body": "This is the new post body",
93 }
94 url = reverse("post_edit", kwargs={"pk": post.id})
95 # Make a request for our new message
96 request = rf.post(url, form_data)
97 request.user = post.author
98 callable_obj = PostUpdateView.as_view()
99 response = callable_obj(request, pk=post.id)
101 # Check that the message body has been changed
102 post.refresh_from_db()
103 assert post.body == "This is the new post body"
104 assert response.status_code == 302
105"""
108class PostTests(TestCase):
109 def setUp(self):
110 self.user = get_user_model().objects.create_user(
111 username="leopoldbloom",
112 email="leopoldbloom@example.com",
113 password="secret",
114 )
116 self.post = Post.objects.create(
117 title="A good title",
118 body="Nice body content",
119 slug="a-good-title",
120 author=self.user,
121 )
123 self.post2 = Post.objects.create(
124 title="A good second title",
125 body="Nice body content for a second post",
126 # slug="a-good-title",
127 author=self.user,
128 status="DF",
129 )
130 self.slug_time = dt.now().strftime("%Y/%-m/%-d")
132 """
133 def test___str__(self):
134 assert self.post.__str__() == self.post.title
135 assert str(self.post) == self.post.title
137 def test_post_content(self):
138 self.assertEqual(f"{self.post.title}", "A good title")
139 self.assertEqual(f"{self.post.slug}", "a-good-title")
140 self.assertEqual(f"{self.post2.slug}", "a-good-second-title")
141 self.assertEqual(f"{self.post.author}", "leopoldbloom")
142 self.assertEqual(f"{self.post.body}", "Nice body content")
143 self.assertEqual(f"{self.post.status}", "PB")
145 def test_get_absolute_url(self):
146 self.assertEqual(
147 self.post.get_absolute_url(), f"/posts/{self.slug_time}/{self.post.slug}/"
148 )
150 def test_post_detail_view(self):
151 self.client.login(email="johndoe@example.com", password="secret")
152 response = self.client.get(f"/posts/{self.slug_time}/{self.post.slug}/")
153 self.assertEqual(response.status_code, 200)
154 self.assertContains(response, "A good title")
155 self.assertTemplateUsed(response, "posts/post_detail.html")
157 def test_post_create_view(self):
158 self.client.login(email="johndoe@example.com", password="secret")
159 response = self.client.get(
160 reverse("post_new"),
161 {
162 "title": "New title",
163 "body": "New text",
164 "author": self.user.id,
165 },
166 )
167 self.assertEqual(response.status_code, 302)
168 self.assertEqual(Post.objects.last().title, "A good title")
169 self.assertEqual(Post.objects.last().body, "Nice body content")
171 def test_post_delete_view(self):
172 self.client.login(email="leopolbloom@example.com", password="secret")
173 response = self.client.get(reverse("post_delete", args={self.post.id}))
174 # response = self.client.get(reverse("post_delete", args="1"))
175 self.assertEqual(response.status_code, 302)
176 """
178 def test_post_update_view(self):
179 self.client.login(email="leopoldbloom@example.com", password="secret") 1i
180 response = self.client.get( 1i
181 reverse("post_edit", args={self.post.id}),
182 # reverse("post_edit", args="1"),
183 {
184 "title": "Updated title",
185 "body": "Updated text",
186 },
187 )
188 self.assertEqual(response.status_code, 200) 1i
191class PostListViewTest(TestCase):
192 def setUp(self):
193 url = reverse("post_list")
194 self.response = self.client.get(url)
196 self.user = get_user_model().objects.create_user(
197 username="johndoe",
198 email="johndoe@example.com",
199 password="secret",
200 )
202 self.post = Post.objects.create(
203 title="A good title",
204 body="Nice body content",
205 slug="a-good-title",
206 author=self.user,
207 )
209 # Create posts for pagination tests
210 number_of_posts = 10
211 for post_id in range(number_of_posts):
212 Post.objects.create(
213 title="A Tiny Test Post {0}".format(post_id),
214 slug="2023/7/15/a-tiny-test-post-{0}/".format(post_id),
215 body="Some post content {0}".format(post_id),
216 author=self.user,
217 )
219 def test_view_url_exists_at_desired_location(self):
220 # response = self.client.get("/posts/")
221 self.assertEqual(self.response.status_code, 200) 1m
223 def test_view_url_accessible_by_name(self):
224 self.assertEqual(self.response.status_code, 200) 1n
226 def test_view_uses_correct_template(self):
227 self.assertEqual(self.response.status_code, 200) 1l
228 self.assertTemplateUsed(self.response, "posts/post_list.html") 1l
231@pytest.mark.django_db
232def test_comment_add(client, post, user):
233 client.login(email=user.email, password="P@s5word") 1j
234 # form = "templates/articles/includes/comment_form.html"
235 response = client.post( 1j
236 reverse("comment_add", kwargs={"post_id": post.id}),
237 {
238 "name": "John Doe",
239 "email": "johndoe@example.com",
240 "body": "This is a new comment",
241 },
242 )
244 assert response.status_code, 200 1j
247class CommentTests(TestCase):
248 def setUp(self):
249 self.user = get_user_model().objects.create_user(
250 username="johndoe",
251 email="johndoe@example.com",
252 password="secret",
253 )
255 self.post = Post.objects.create(
256 title="A good title",
257 body="Nice body content",
258 slug="a-good-title",
259 author=self.user,
260 )
262 self.comment = Comment.objects.create(
263 post=self.post,
264 name="Ron Swoboda",
265 email="ron@amazingmets.org",
266 body="This is a comment",
267 )
269 def test_comment_content(self):
270 self.assertEqual(f"{self.comment.name}", "Ron Swoboda") 1k
271 self.assertEqual(f"{self.comment.email}", "ron@amazingmets.org") 1k
272 self.assertEqual(f"{self.comment.body}", "This is a comment") 1k
274 def test_comment_add_view(self):
275 self.client.login(email="ron@amazingmets.org", password="secret") 1f
276 response = self.client.post( 1f
277 reverse("comment_add", args={self.post.id}),
278 {
279 "name": "Ron Swoboda",
280 "email": "ron@amazingmets.org",
281 "body": "This is a new comment",
282 },
283 )
284 self.assertEqual(response.status_code, 200) 1f
285 self.assertEqual(Comment.objects.last().body, "This is a new comment") 1f
286 self.assertTrue(self.comment.email == self.comment.email) 1f
289@pytest.mark.django_db
290def test_sitemap(client, ten_posts):
291 response = client.get("/sitemap.xml") 1d
292 xml = response.content.decode("utf-8") 1d
293 expected_posts = [p for p in ten_posts] 1d
294 assert response.status_code == 200 1d
295 assert len(expected_posts) == 10 1d
296 assert "<lastmod>" in xml 1d
299@pytest.mark.django_db
300def test_rssfeed(client, ten_posts):
301 response = client.get(reverse("post_feed")) 1c
302 xml = response.content.decode("utf-8") 1c
303 expected_posts = [p for p in ten_posts] 1c
304 assert len(expected_posts) == 10 1c
305 assert response["Content-Type"] == "application/rss+xml; charset=utf-8" 1c
306 assert response.status_code == 200 1c
307 assert "<title>django-blog</title>" in xml 1c
310"""
311class SitemapTests(TestCase):
312 def setUp(self):
313 # url = reverse("sitemap")
314 url = "/sitemap.xml"
315 self.response = self.client.get(url)
317 def test_view_url_exists_at_desired_location(self):
318 self.assertEqual(self.response.status_code, 200)
321def test_sitemap(rf):
322 # url = "/sitemap.xml"
323 url = reverse("django.contrib.sitemaps.views.sitemap")
324 response = rf.post(url)
325 # response = rf.get(url)
326 # response = PostSitemap(request)
327 assert response.status_code == 200
328"""