Coverage for posts/tests/test_views.py: 100.00%

102 statements  

« prev     ^ index     » next       coverage.py v7.16.1, created at 2026-09-22 12:02 -0700

1from datetime import datetime as dt 1a

2 

3import pytest 1a

4from django.contrib.auth import get_user_model 1a

5from django.test import TestCase 1a

6from django.urls import reverse 1a

7from pytest_django.asserts import ( 1a

8 assertContains, 

9) 

10 

11from ..models import Comment, Post 1a

12 

13# from ..sitemaps import PostSitemap 

14from ..views import ( 1a

15 PostCreateView, 

16 PostDeleteView, 

17 post_detail, 

18 post_list, 

19) 

20 

21# pytestmark = pytest.mark.django_db 

22 

23 

24@pytest.mark.django_db 1a

25def test_post_list_view(rf): 1a

26 # Get the request 

27 request = rf.get(reverse("post_list")) 1a

28 # Use the request to get the response 

29 response = post_list(request) 1a

30 # Test that the response is valid 

31 assert response.status_code == 200 1a

32 assertContains(response, "Welcome to django-blog") 1a

33 

34 

35@pytest.mark.django_db 1a

36def test_post_detail_view(rf, post): 1a

37 # Get the request 

38 url = f"{post.publish}/{post.slug}/" 1a

39 request = rf.get(url) 1a

40 # Use the request to get the response 

41 response = post_detail( 1a

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) 1a

50 

51 

52@pytest.mark.django_db 1a

53def test_post_create_view(rf, post, admin_user): 1a

54 form_data = { 1a

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) 1a

61 # Add an authenticated user 

62 request.user = admin_user 1a

63 # Use the request to get the response 

64 response = PostCreateView.as_view()(request) 1a

65 text = Post.published.last() 1a

66 # Test that the response is valid 

67 assert response.status_code == 200 1a

68 assert text.author == post.author 1a

69 

70 

71@pytest.mark.django_db 1a

72def test_message_delete(rf, post): 1a

73 request = rf.post( 1a

74 reverse("post_delete", kwargs={"pk": post.id}), 

75 ) 

76 request.user = post.author 1a

77 callable_obj = PostDeleteView.as_view() 1a

78 response = callable_obj(request, pk=post.id) 1a

79 assert response.status_code == 302 1a

80 

81 

82""" 1a

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) 

100 

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""" 

106 

107 

108class PostTests(TestCase): 1a

109 def setUp(self): 1a

110 self.user = get_user_model().objects.create_user( 1a

111 username="leopoldbloom", 

112 email="leopoldbloom@example.com", 

113 password="secret", 

114 ) 

115 

116 self.post = Post.objects.create( 1a

117 title="A good title", 

118 body="Nice body content", 

119 slug="a-good-title", 

120 author=self.user, 

121 ) 

122 

123 self.post2 = Post.objects.create( 1a

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") 1a

131 # self.slug_time = timezone.now() 

132 

133 """ 1a

134 def test___str__(self): 

135 assert self.post.__str__() == self.post.title 

136 assert str(self.post) == self.post.title 

137 

138 def test_post_content(self): 

139 self.assertEqual(f"{self.post.title}", "A good title") 

140 self.assertEqual(f"{self.post.slug}", "a-good-title") 

141 self.assertEqual(f"{self.post2.slug}", "a-good-second-title") 

142 self.assertEqual(f"{self.post.author}", "leopoldbloom") 

143 self.assertEqual(f"{self.post.body}", "Nice body content") 

144 self.assertEqual(f"{self.post.status}", "PB") 

145 

146 def test_get_absolute_url(self): 

147 self.assertEqual( 

148 self.post.get_absolute_url(), f"/posts/{self.slug_time}/{self.post.slug}/" 

149 ) 

150 

151 def test_post_detail_view(self): 

152 self.client.login(email="johndoe@example.com", password="secret") 

153 response = self.client.get(f"/posts/{self.slug_time}/{self.post.slug}/") 

154 self.assertEqual(response.status_code, 200) 

155 self.assertContains(response, "A good title") 

156 self.assertTemplateUsed(response, "posts/post_detail.html") 

157 

158 def test_post_create_view(self): 

159 self.client.login(email="johndoe@example.com", password="secret") 

160 response = self.client.get( 

161 reverse("post_new"), 

162 { 

163 "title": "New title", 

164 "body": "New text", 

165 "author": self.user.id, 

166 }, 

167 ) 

168 self.assertEqual(response.status_code, 302) 

169 self.assertEqual(Post.objects.last().title, "A good title") 

170 self.assertEqual(Post.objects.last().body, "Nice body content") 

171 

172 def test_post_delete_view(self): 

173 self.client.login(email="leopolbloom@example.com", password="secret") 

174 response = self.client.get(reverse("post_delete", args={self.post.id})) 

175 # response = self.client.get(reverse("post_delete", args="1")) 

176 self.assertEqual(response.status_code, 302) 

177 """ 

178 

179 def test_post_update_view(self): 1a

180 self.client.login(email="leopoldbloom@example.com", password="secret") 1a

181 response = self.client.get( 1a

182 reverse("post_edit", args={self.post.id}), 

183 # reverse("post_edit", args="1"), 

184 { 

185 "title": "Updated title", 

186 "body": "Updated text", 

187 }, 

188 ) 

189 self.assertEqual(response.status_code, 200) 1a

190 

191 

192class PostListViewTest(TestCase): 1a

193 def setUp(self): 1a

194 url = reverse("post_list") 1a

195 self.response = self.client.get(url) 1a

196 

197 self.user = get_user_model().objects.create_user( 1a

198 username="johndoe", 

199 email="johndoe@example.com", 

200 password="secret", 

201 ) 

202 

203 self.post = Post.objects.create( 1a

204 title="A good title", 

205 body="Nice body content", 

206 slug="a-good-title", 

207 author=self.user, 

208 ) 

209 

210 # Create posts for pagination tests 

211 number_of_posts = 10 1a

212 for post_id in range(number_of_posts): 1a

213 Post.objects.create( 1a

214 title="A Tiny Test Post {0}".format(post_id), 

215 slug="2023/7/15/a-tiny-test-post-{0}/".format(post_id), 

216 body="Some post content {0}".format(post_id), 

217 author=self.user, 

218 ) 

219 

220 def test_view_url_exists_at_desired_location(self): 1a

221 # response = self.client.get("/posts/") 

222 self.assertEqual(self.response.status_code, 200) 1a

223 

224 def test_view_url_accessible_by_name(self): 1a

225 self.assertEqual(self.response.status_code, 200) 1a

226 

227 def test_view_uses_correct_template(self): 1a

228 self.assertEqual(self.response.status_code, 200) 1a

229 self.assertTemplateUsed(self.response, "posts/post_list.html") 1a

230 

231 

232@pytest.mark.django_db 1a

233def test_comment_add(client, post, user): 1a

234 client.login(email=user.email, password="P@s5word") 1a

235 # form = "templates/articles/includes/comment_form.html" 

236 response = client.post( 1a

237 reverse("comment_add", kwargs={"post_id": post.id}), 

238 { 

239 "name": "John Doe", 

240 "email": "johndoe@example.com", 

241 "body": "This is a new comment", 

242 }, 

243 ) 

244 

245 assert response.status_code, 200 1a

246 

247 

248class CommentTests(TestCase): 1a

249 def setUp(self): 1a

250 self.user = get_user_model().objects.create_user( 1a

251 username="johndoe", 

252 email="johndoe@example.com", 

253 password="secret", 

254 ) 

255 

256 self.post = Post.objects.create( 1a

257 title="A good title", 

258 body="Nice body content", 

259 slug="a-good-title", 

260 author=self.user, 

261 ) 

262 

263 self.comment = Comment.objects.create( 1a

264 post=self.post, 

265 name="Ron Swoboda", 

266 email="ron@amazingmets.org", 

267 body="This is a comment", 

268 ) 

269 

270 def test_comment_content(self): 1a

271 self.assertEqual(f"{self.comment.name}", "Ron Swoboda") 1a

272 self.assertEqual(f"{self.comment.email}", "ron@amazingmets.org") 1a

273 self.assertEqual(f"{self.comment.body}", "This is a comment") 1a

274 

275 def test_comment_add_view(self): 1a

276 self.client.login(email="ron@amazingmets.org", password="secret") 1a

277 response = self.client.post( 1a

278 reverse("comment_add", args={self.post.id}), 

279 { 

280 "name": "Ron Swoboda", 

281 "email": "ron@amazingmets.org", 

282 "body": "This is a new comment", 

283 }, 

284 ) 

285 self.assertEqual(response.status_code, 200) 1a

286 self.assertEqual(Comment.objects.last().body, "This is a new comment") 1a

287 self.assertTrue(self.comment.email == self.comment.email) 1a

288 

289 

290@pytest.mark.django_db 1a

291def test_sitemap(client, ten_posts): 1a

292 response = client.get("/sitemap.xml") 1a

293 xml = response.content.decode("utf-8") 1a

294 expected_posts = [p for p in ten_posts] 1a

295 assert response.status_code == 200 1a

296 assert len(expected_posts) == 10 1a

297 assert "<lastmod>" in xml 1a

298 

299 

300@pytest.mark.django_db 1a

301def test_rssfeed(client, ten_posts): 1a

302 response = client.get(reverse("post_feed")) 1a

303 xml = response.content.decode("utf-8") 1a

304 expected_posts = [p for p in ten_posts] 1a

305 assert len(expected_posts) == 10 1a

306 assert response["Content-Type"] == "application/rss+xml; charset=utf-8" 1a

307 assert response.status_code == 200 1a

308 assert "<title>django-blog</title>" in xml 1a

309 

310 

311""" 1a

312class SitemapTests(TestCase): 

313 def setUp(self): 

314 # url = reverse("sitemap") 

315 url = "/sitemap.xml" 

316 self.response = self.client.get(url) 

317 

318 def test_view_url_exists_at_desired_location(self): 

319 self.assertEqual(self.response.status_code, 200) 

320 

321 

322def test_sitemap(rf): 

323 # url = "/sitemap.xml" 

324 url = reverse("django.contrib.sitemaps.views.sitemap") 

325 response = rf.post(url) 

326 # response = rf.get(url) 

327 # response = PostSitemap(request) 

328 assert response.status_code == 200 

329"""