MCPcopy Create free account
hub / github.com/realpython/discover-flask / UserViewsTests

Class UserViewsTests

tests/test_users.py:55–98  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

53
54
55class UserViewsTests(BaseTestCase):
56
57 # Ensure that the login page loads correctly
58 def test_login_page_loads(self):
59 response = self.client.get('/login')
60 self.assertIn(b'Please login', response.data)
61
62 # Ensure login behaves correctly with correct credentials
63 def test_correct_login(self):
64 with self.client:
65 response = self.client.post(
66 '/login',
67 data=dict(username="admin", password="admin"),
68 follow_redirects=True
69 )
70 self.assertIn(b'You were logged in', response.data)
71 self.assertTrue(current_user.name == "admin")
72 self.assertTrue(current_user.is_active())
73
74 # Ensure login behaves correctly with incorrect credentials
75 def test_incorrect_login(self):
76 response = self.client.post(
77 '/login',
78 data=dict(username="wrong", password="wrong"),
79 follow_redirects=True
80 )
81 self.assertIn(b'Invalid username or password.', response.data)
82
83 # Ensure logout behaves correctly
84 def test_logout(self):
85 with self.client:
86 self.client.post(
87 '/login',
88 data=dict(username="admin", password="admin"),
89 follow_redirects=True
90 )
91 response = self.client.get('/logout', follow_redirects=True)
92 self.assertIn(b'You were logged out', response.data)
93 self.assertFalse(current_user.is_active())
94
95 # Ensure that logout page requires user login
96 def test_logout_route_requires_login(self):
97 response = self.client.get('/logout', follow_redirects=True)
98 self.assertIn(b'Please log in to access this page', response.data)
99
100
101if __name__ == '__main__':

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected