-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
107 lines (86 loc) · 3.58 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import unittest
from flask_testing import TestCase
from flask_login import current_user
from project import app, db
from project.models import User, BlogPost
class BaseTestCase(TestCase):
"""A base test case."""
def create_app(self):
app.config.from_object('config.TestConfig')
return app
def setUp(self):
db.create_all()
db.session.add(User("admin", "ad@min.com", "admin"))
db.session.add(BlogPost("Test post", "This is a test. Only a test.", "admin"))
db.session.commit()
def tearDown(self):
db.session.remove()
db.drop_all()
class FlaskTestCase(BaseTestCase):
# Ensure that Flask was set up correctly
def test_index(self):
response = self.client.get('/login', content_type='html/text')
self.assertEqual(response.status_code, 200)
# Ensure that main page requires user login
def test_main_route_requires_login(self):
response = self.client.get('/', follow_redirects=True)
self.assertIn(b'Please log in to access this page', response.data)
# Ensure that posts show up on the main page
def test_posts_show_up_on_main_page(self):
response = self.client.post(
'/login',
data=dict(username="admin", password="admin"),
follow_redirects=True
)
self.assertIn(b'This is a test. Only a test.', response.data)
class UserViewsTests(BaseTestCase):
# Ensure that the login page loads correctly
def test_login_page_loads(self):
response = self.client.get('/login')
self.assertIn(b'Please login', response.data)
# Ensure login behaves correctly with correct credentials
def test_correct_login(self):
with self.client:
response = self.client.post(
'/login',
data=dict(username="admin", password="admin"),
follow_redirects=True
)
self.assertIn(b'You were logged in', response.data)
self.assertTrue(current_user.name == "admin")
self.assertTrue(current_user.is_active())
# Ensure login behaves correctly with incorrect credentials
def test_incorrect_login(self):
response = self.client.post(
'/login',
data=dict(username="wrong", password="wrong"),
follow_redirects=True
)
self.assertIn(b'Invalid username or password.', response.data)
# Ensure logout behaves correctly
def test_logout(self):
with self.client:
self.client.post(
'/login',
data=dict(username="admin", password="admin"),
follow_redirects=True
)
response = self.client.get('/logout', follow_redirects=True)
self.assertIn(b'You were logged out', response.data)
self.assertFalse(current_user.is_active)
# Ensure that logout page requires user login
def test_logout_route_requires_login(self):
response = self.client.get('/logout', follow_redirects=True)
self.assertIn(b'Please log in to access this page', response.data)
# Ensure user can register
def test_user_registeration(self):
with self.client:
response = self.client.post('register/', data=dict(
username='test', email='test@gmail.com',
password='python', confirm='python'
), follow_redirects=True)
self.assertIn(b'Welcome to Flask!', response.data)
self.assertTrue(current_user.name == "test")
self.assertTrue(current_user.is_active)
if __name__ == '__main__':
unittest.main()