generated from ScottGuthart/flaskloginapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
84 lines (66 loc) · 2.23 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
import unittest
from app import create_app, db, current_app
from app.models import User
from config import Config
class TestConfig(Config):
TESTING = True
SQLALCHEMY_DATABASE_URI = 'sqlite://'
WTF_CSRF_ENABLED = False
class BasicTestCase(unittest.TestCase):
def setUp(self):
self.app = create_app(TestConfig)
self.app_context = self.app.app_context()
self.app_context.push()
db.create_all()
def tearDown(self):
db.session.remove()
db.drop_all()
self.app_context.pop()
def test_login(self):
tester = current_app.test_client(self)
response = tester.get('/auth/login', content_type='html/text')
self.assertEqual(response.status_code, 200)
class UserModelCase(unittest.TestCase):
def setUp(self):
self.app = create_app(TestConfig)
self.app_context = self.app.app_context()
self.app_context.push()
db.create_all()
def tearDown(self):
db.session.remove()
db.drop_all()
self.app_context.pop()
def test_password_hashing(self):
u = User(username="susan")
u.set_password('cat')
self.assertFalse(u.check_password('dog'))
self.assertTrue(u.check_password('cat'))
class FlaskAppTestCase(unittest.TestCase):
def setUp(self):
self.app = create_app(TestConfig)
self.app_context = self.app.app_context()
self.app_context.push()
self.client = self.app.test_client()
db.create_all()
def tearDown(self):
db.session.remove()
db.drop_all()
self.app_context.pop()
def login(self, username, password):
return self.client.post('/auth/login', data=dict(
username=username,
password=password), follow_redirects=True)
def logout(self):
return self.client.get('/auth/logout', follow_redirects=True)
def test_login_logout(self):
u = User(username='susan')
u.set_password('cat')
u.confirmed = True
db.session.add(u)
db.session.commit()
rv = self.login('susan', 'cat')
assert b'Logout' in rv.data
rv = self.logout()
assert b'Login' in rv.data
if __name__ == "__main__":
unittest.main(verbosity=2)