-
Notifications
You must be signed in to change notification settings - Fork 2
/
tests.py
93 lines (76 loc) · 3.11 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
import unittest
from flask.ext.testing import TestCase
from flask.ext.login import current_user
from project import app, db
from project.models import User
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(BlogPost("Test post", "This is a test. Only a test."))
db.session.add(User("admin", "admin@admin.com", "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 posts show up on the main page
def test_posts_show_up_on_main_page(self):
response = self.client.post(
'/login',
data=dict(user="admin", password="admin"),
follow_redirects=True
)
self.assertIn(b'This is a test. Only a test.', response.data)
class UsersViewsTests(BaseTestCase):
# Ensure that the login page loads correctly
def test_login_page_loads(self):
response = self.client.get('/login')
self.assertIn(b'Login dude', response.data)
# Ensure login behaves correctly with correct credentials
def test_correct_login(self):
with self.client:
response = self.client.post(
'/login',
data=dict(user="admin", password="admin"),
follow_redirects=True
)
self.assertIn(b'You just 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(user="wrong", password="wrong"),
follow_redirects=True
)
self.assertIn(b'Oops. That username or password is incorrect.', response.data)
# Ensure logout behaves correctly
def test_logout(self):
with self.client:
self.client.post(
'/login',
data=dict(user="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 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 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)
if __name__ == '__main__':
unittest.main()