Coverage for pages/tests/test_views.py: 97.33%

75 statements  

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

1from django.core.mail import BadHeaderError 1a

2from django.test import SimpleTestCase 1a

3from django.urls import resolve, reverse 1a

4 

5from ..views import ( 1a

6 AboutPageView, 

7 ContactView, 

8 HomePageView, 

9 SuccessView, 

10) 

11 

12 

13class HomePageTests(SimpleTestCase): 1a

14 def setUp(self): 1a

15 url = reverse("home") 1a

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

17 

18 def test_homepage_status_code(self): 1a

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

20 

21 def test_homepage_template(self): 1a

22 self.assertTemplateUsed(self.response, "pages/home.html") 1a

23 

24 def test_homepage_contains_correct_html(self): 1a

25 self.assertContains(self.response, "Django Polls app") 1a

26 

27 def test_homepage_does_not_contain_incorrect_html(self): 1a

28 self.assertNotContains(self.response, "Not the Homepage") 1a

29 

30 def test_homepage_url_resolves_homepageview(self): 1a

31 view = resolve("/") 1a

32 self.assertEqual( 1a

33 view.func.__name__, 

34 HomePageView.as_view().__name__, 

35 ) 

36 

37 

38class AboutPageTests(SimpleTestCase): 1a

39 def setUp(self): 1a

40 url = reverse("about") 1a

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

42 

43 def test_about_page_status_code(self): 1a

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

45 

46 def test_about_page_template(self): 1a

47 self.assertTemplateUsed(self.response, "pages/about.html") 1a

48 

49 def test_about_page_contains_correct_html(self): 1a

50 self.assertContains(self.response, "About Page") 1a

51 

52 def test_about_page_does_not_contain_incorrect_html(self): 1a

53 self.assertNotContains(self.response, "Not the About page") 1a

54 

55 def test_about_page_url_resolves_about_pageview(self): 1a

56 view = resolve("/about/") 1a

57 self.assertEqual( 1a

58 view.func.__name__, 

59 AboutPageView.as_view().__name__, 

60 ) 

61 

62 

63class ContactViewTests(SimpleTestCase): 1a

64 def setUp(self): 1a

65 url = reverse("contact") 1a

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

67 

68 def test_contact_page_status_code(self): 1a

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

70 

71 def test_contact_page_url_resolves_contactpageview(self): 1a

72 view = resolve("/contact/") 1a

73 self.assertEqual( 1a

74 view.func.__name__, 

75 ContactView.__name__, 

76 ) 

77 

78 def test_contact_page_template(self): 1a

79 self.assertTemplateUsed(self.response, "pages/contact.html") 1a

80 

81 def test_contact_page_contains_correct_html(self): 1a

82 self.assertContains(self.response, "Contact Us") 1a

83 

84 def test_contact_page_does_not_contain_incorrect_html(self): 1a

85 self.assertNotContains(self.response, "Home Page") 1a

86 

87 def test_post_success(self): 1a

88 self.client.post( 1a

89 "/contact/", 

90 data={ 

91 "from_email": "john@example.com", 

92 "subject": "Test Email", 

93 "messages": "This is a test", 

94 }, 

95 ) 

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

97 

98 def test_header_injection(self): 1a

99 error_occured = True 1a

100 try: 1a

101 self.client.post( 1a

102 "/contact/", 

103 data={ 

104 "from_email": "joe@example.com", 

105 "subject": "Subject\nInjectionTest", 

106 "message": "This is a test of a BadHeaderError", 

107 }, 

108 ) 

109 error_occured = False 1a

110 except BadHeaderError: 

111 error_occured = True 

112 self.assertFalse(error_occured) 1a

113 

114 

115class SuccessViewTests(SimpleTestCase): 1a

116 def setUp(self): 1a

117 url = reverse("success") 1a

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

119 

120 def test_success_page_status_code(self): 1a

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

122 

123 def test_success_page_template(self): 1a

124 self.assertTemplateUsed(self.response, "pages/success.html") 1a

125 

126 def test_success_page_contains_correct_html(self): 1a

127 self.assertContains( 1a

128 self.response, 

129 "Thank you for your message.", 

130 ) 

131 

132 def test_success_page_does_not_contain_incorrect_html(self): 1a

133 self.assertNotContains(self.response, "Please Go Away") 1a

134 

135 def test_success_page_url_resolves_success_page_view(self): 1a

136 view = resolve("/success/") 1a

137 self.assertEqual( 1a

138 view.func.__name__, 

139 SuccessView.__name__, 

140 )