@@ -24,8 +24,7 @@ class TestViews(SetUp):
24
24
def test_status_view (self , mock_db_status ):
25
25
mock_db_status .return_value .get_status .return_value = (10 , 2 , 500 )
26
26
response = self .client .get (reverse ("status" ))
27
- self .assertEqual (response .status_code , 200 )
28
- self .assertTemplateUsed (response , "blog/status_page.html" )
27
+ self .assertResponseAndTemplate (response , "blog/status_page.html" )
29
28
30
29
# def test_all_posts_view_shows_pagination(self):
31
30
# response = self.client.get("/all-posts/")
@@ -43,23 +42,20 @@ def test_status_view(self, mock_db_status):
43
42
44
43
def test_all_posts_view (self ):
45
44
response = self .client .get (reverse ("all-posts" ))
46
- self .assertEqual (response .status_code , 200 )
47
- self .assertTemplateUsed (response , "blog/all_posts.html" )
45
+ self .assertResponseAndTemplate (response , "blog/all_posts.html" )
48
46
self .assertIsInstance (response .context ["posts" ][0 ], Post )
49
47
50
- def test_home_view_anonymous_user (self ): # TODO add check for draft post
48
+ def test_home_view_anonymous_user (self ):
51
49
response = self .client .get (reverse ("home" ))
52
- self .assertEqual (response .status_code , 200 )
53
- self .assertTemplateUsed (response , "blog/home.html" )
50
+ self .assertResponseAndTemplate (response , "blog/home.html" )
54
51
self .assertIsInstance (response .context ["posts" ][0 ], Post )
55
52
56
53
def test_home_view_admin_user (self ):
57
- # Access using admin_user (should get posts in draft mode)
58
54
self .client .login (
59
55
username = self .admin_user .username , password = self .admin_user_password
60
56
)
61
57
response = self .client .get (reverse ("home" ))
62
- self .assertEqual (response . status_code , 200 )
58
+ self .assertResponseAndTemplate (response , "blog/home.html" )
63
59
64
60
def test_home_view_htmx_request (self ):
65
61
headers = {"HTTP_HX-Request" : "true" , "HTTP_HX-Trigger" : "TEST" }
@@ -75,8 +71,7 @@ def test_home_view_htmx_request(self):
75
71
def test_post_detail_view_anonymous (self ):
76
72
test_post_detail_url = reverse ("post-detail" , args = [self .first_post .slug ])
77
73
response = self .client .get (test_post_detail_url )
78
- self .assertEqual (response .status_code , 200 )
79
- self .assertTemplateUsed (response , "blog/post/post_detail.html" )
74
+ self .assertResponseAndTemplate (response , "blog/post/post_detail.html" )
80
75
81
76
def test_post_detail_view_anonymous_draft_post (self ):
82
77
draft_post_detail_url = reverse ("post-detail" , args = [self .draft_post .slug ])
@@ -97,8 +92,7 @@ def test_post_create_view_has_correct_context_template_and_form(self):
97
92
username = self .admin_user .username , password = self .admin_user_password
98
93
)
99
94
response = self .client .get (reverse ("post-create" ))
100
- self .assertEqual (response .status_code , 200 )
101
- self .assertTemplateUsed (response , "blog/post/add_post.html" )
95
+ self .assertResponseAndTemplate (response , "blog/post/add_post.html" )
102
96
self .assertIsInstance (response .context ["form" ], PostForm )
103
97
self .assertEqual (response .context ["title" ], "Create a New Post" )
104
98
@@ -161,8 +155,7 @@ def test_update_post_view_has_correct_context_template_and_form(self):
161
155
)
162
156
post1_update_url = reverse ("post-update" , args = [self .first_post .slug ])
163
157
response = self .client .get (post1_update_url )
164
- self .assertEqual (response .status_code , 200 )
165
- self .assertTemplateUsed (response , "blog/post/edit_post.html" )
158
+ self .assertResponseAndTemplate (response , "blog/post/edit_post.html" )
166
159
self .assertIsInstance (response .context ["form" ], PostForm )
167
160
self .assertEqual (response .context ["title" ], f"Edit { self .first_post .title } " )
168
161
@@ -351,8 +344,7 @@ def test_comment_delete_different_user_blocked(self):
351
344
def test_category_view_anonymous (self ):
352
345
category_url = reverse ("blog-category" , args = [self .default_category .slug ])
353
346
response = self .client .get (category_url )
354
- self .assertEqual (response .status_code , 200 )
355
- self .assertTemplateUsed (response , "blog/categories.html" )
347
+ self .assertResponseAndTemplate (response , "blog/categories.html" )
356
348
self .assertEqual (response .context ["category" ], self .default_category )
357
349
self .assertIsInstance (response .context ["posts" ][0 ], Post )
358
350
@@ -380,10 +372,8 @@ def test_category_view_htmx_request(self):
380
372
381
373
382
374
def test_search_view_blank (self ):
383
- # Empty page if user didn't search for anything and manually typed in the search url (get)
384
375
response = self .client .get (reverse ("blog-search" ))
385
- self .assertEqual (response .status_code , 200 )
386
- self .assertTemplateUsed (response , "blog/search_posts.html" )
376
+ self .assertResponseAndTemplate (response , "blog/search_posts.html" )
387
377
388
378
def test_search_view_anonymous (self ):
389
379
# If anonymous, should be able to find a post
@@ -406,8 +396,7 @@ def test_search_view_staff(self):
406
396
407
397
def test_register_view_happy_path (self ):
408
398
response = self .client .get (reverse ("register" ))
409
- self .assertEqual (response .status_code , 200 )
410
- self .assertTemplateUsed (response , "users/register.html" )
399
+ self .assertResponseAndTemplate (response , "users/register.html" )
411
400
self .assertIsInstance (response .context ["form" ], UserRegisterForm )
412
401
413
402
data = {
@@ -451,8 +440,7 @@ def test_profile_view(self):
451
440
username = self .admin_user .username , password = self .admin_user_password
452
441
)
453
442
response = self .client .get (reverse ("profile" ))
454
- self .assertEqual (response .status_code , 200 )
455
- self .assertTemplateUsed (response , "users/profile.html" )
443
+ self .assertResponseAndTemplate (response , "users/profile.html" )
456
444
self .assertIsInstance (response .context ["p_form" ], ProfileUpdateForm )
457
445
self .assertIsInstance (response .context ["u_form" ], UserUpdateForm )
458
446
@@ -489,34 +477,29 @@ def test_profile_view_edit_invalid(self):
489
477
490
478
def test_login_view (self ):
491
479
response = self .client .get (reverse ("login" ))
492
- self .assertEqual (response .status_code , 200 )
493
- self .assertTemplateUsed (response , "users/login.html" )
480
+ self .assertResponseAndTemplate (response , "users/login.html" )
494
481
495
482
def test_logout_view (self ):
496
483
self .client .login (
497
484
username = self .admin_user .username , password = self .admin_user_password
498
485
)
499
486
response = self .client .post (reverse ("logout" ))
500
- self .assertEqual (response .status_code , 200 )
501
- self .assertTemplateUsed (response , "users/logout.html" )
487
+ self .assertResponseAndTemplate (response , "users/logout.html" )
502
488
503
489
def test_password_rest_view (self ):
504
490
response = self .client .get (reverse ("password_reset" ))
505
- self .assertEqual (response .status_code , 200 )
506
- self .assertTemplateUsed (response , "users/password_reset.html" )
491
+ self .assertResponseAndTemplate (response , "users/password_reset.html" )
507
492
508
493
def test_password_reset_done_view (self ):
509
494
response = self .client .get (reverse ("password_reset_done" ))
510
- self .assertEqual (response .status_code , 200 )
511
- self .assertTemplateUsed (response , "users/password_reset_done.html" )
495
+ self .assertResponseAndTemplate (response , "users/password_reset_done.html" )
512
496
513
497
# def test_password_reset_confirm_view(self):
514
498
# # TODO
515
499
516
500
def test_password_reset_complete (self ):
517
501
response = self .client .get (reverse ("password_reset_complete" ))
518
- self .assertEqual (response .status_code , 200 )
519
- self .assertTemplateUsed (response , "users/password_reset_complete.html" )
502
+ self .assertResponseAndTemplate (response , "users/password_reset_complete.html" )
520
503
521
504
def test_sitemap_view (self ):
522
505
response = self .client .get (reverse ("django.contrib.sitemaps.views.sitemap" ))
0 commit comments