forked from realpython/discover-flask
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
74 lines (62 loc) · 2.48 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from app import app
import unittest
class FlaskTestCase(unittest.TestCase):
# Ensure that Flask was set up correctly
def test_index(self):
tester = app.test_client(self)
response = tester.get('/login', content_type='html/text')
self.assertEqual(response.status_code, 200)
# Ensure that the login page loads correctly
def test_login_page_loads(self):
tester = app.test_client(self)
response = tester.get('/login')
self.assertIn(b'Please login', response.data)
# Ensure login behaves correctly with correct credentials
def test_correct_login(self):
tester = app.test_client()
response = tester.post(
'/login',
data=dict(username="admin", password="admin"),
follow_redirects=True
)
self.assertIn(b'You were logged in', response.data)
# Ensure login behaves correctly with incorrect credentials
def test_incorrect_login(self):
tester = app.test_client()
response = tester.post(
'/login',
data=dict(username="wrong", password="wrong"),
follow_redirects=True
)
self.assertIn(b'Invalid Credentials. Please try again.', response.data)
# Ensure logout behaves correctly
def test_logout(self):
tester = app.test_client()
tester.post(
'/login',
data=dict(username="admin", password="admin"),
follow_redirects=True
)
response = tester.get('/logout', follow_redirects=True)
self.assertIn(b'You were logged out', response.data)
# Ensure that main page requires user login
def test_main_route_requires_login(self):
tester = app.test_client()
response = tester.get('/', follow_redirects=True)
self.assertIn(b'You need to login first.', response.data)
# Ensure that logout page requires user login
def test_logout_route_requires_login(self):
tester = app.test_client()
response = tester.get('/logout', follow_redirects=True)
self.assertIn(b'You need to login first.', response.data)
# Ensure that posts show up on the main page
def test_posts_show_up_on_main_page(self):
tester = app.test_client()
response = tester.post(
'/login',
data=dict(username="admin", password="admin"),
follow_redirects=True
)
self.assertIn(b'Hello from the shell', response.data)
if __name__ == '__main__':
unittest.main()