generated from hack4impact/flask-base
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_user_model.py
133 lines (113 loc) · 4.77 KB
/
test_user_model.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import time
import unittest
from app import create_app, db
from app.models import AnonymousUser, Permission, Role, User
class UserModelTestCase(unittest.TestCase):
def setUp(self):
self.app = create_app('testing')
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_setter(self):
u = User(password='password')
self.assertTrue(u.password_hash is not None)
def test_no_password_getter(self):
u = User(password='password')
with self.assertRaises(AttributeError):
u.password()
def test_password_verification(self):
u = User(password='password')
self.assertTrue(u.verify_password('password'))
self.assertFalse(u.verify_password('notpassword'))
def test_password_salts_are_random(self):
u = User(password='password')
u2 = User(password='password')
self.assertTrue(u.password_hash != u2.password_hash)
def test_valid_confirmation_token(self):
u = User(password='password')
db.session.add(u)
db.session.commit()
token = u.generate_confirmation_token()
self.assertTrue(u.confirm_account(token))
def test_invalid_confirmation_token(self):
u1 = User(password='password')
u2 = User(password='notpassword')
db.session.add(u1)
db.session.add(u2)
db.session.commit()
token = u1.generate_confirmation_token()
self.assertFalse(u2.confirm_account(token))
def test_expired_confirmation_token(self):
u = User(password='password')
db.session.add(u)
db.session.commit()
token = u.generate_confirmation_token(1)
time.sleep(2)
self.assertFalse(u.confirm_account(token))
def test_valid_reset_token(self):
u = User(password='password')
db.session.add(u)
db.session.commit()
token = u.generate_password_reset_token()
self.assertTrue(u.reset_password(token, 'notpassword'))
self.assertTrue(u.verify_password('notpassword'))
def test_invalid_reset_token(self):
u1 = User(password='password')
u2 = User(password='notpassword')
db.session.add(u1)
db.session.add(u2)
db.session.commit()
token = u1.generate_password_reset_token()
self.assertFalse(u2.reset_password(token, 'notnotpassword'))
self.assertTrue(u2.verify_password('notpassword'))
def test_valid_email_change_token(self):
u = User(email='[email protected]', password='password')
db.session.add(u)
db.session.commit()
token = u.generate_email_change_token('[email protected]')
self.assertTrue(u.change_email(token))
self.assertTrue(u.email == '[email protected]')
def test_invalid_email_change_token(self):
u1 = User(email='[email protected]', password='password')
u2 = User(email='[email protected]', password='notpassword')
db.session.add(u1)
db.session.add(u2)
db.session.commit()
token = u1.generate_email_change_token('[email protected]')
self.assertFalse(u2.change_email(token))
self.assertTrue(u2.email == '[email protected]')
def test_duplicate_email_change_token(self):
u1 = User(email='[email protected]', password='password')
u2 = User(email='[email protected]', password='notpassword')
db.session.add(u1)
db.session.add(u2)
db.session.commit()
token = u2.generate_email_change_token('[email protected]')
self.assertFalse(u2.change_email(token))
self.assertTrue(u2.email == '[email protected]')
def test_roles_and_permissions(self):
Role.insert_roles()
u = User(email='[email protected]', password='password')
self.assertTrue(u.can(Permission.GENERAL))
self.assertFalse(u.can(Permission.ADMINISTER))
def test_make_administrator(self):
Role.insert_roles()
u = User(email='[email protected]', password='password')
self.assertFalse(u.can(Permission.ADMINISTER))
u.role = Role.query.filter_by(
permissions=Permission.ADMINISTER).first()
self.assertTrue(u.can(Permission.ADMINISTER))
def test_administrator(self):
Role.insert_roles()
r = Role.query.filter_by(permissions=Permission.ADMINISTER).first()
u = User(email='[email protected]', password='password', role=r)
self.assertTrue(u.can(Permission.ADMINISTER))
self.assertTrue(u.can(Permission.GENERAL))
self.assertTrue(u.is_admin())
def test_anonymous(self):
u = AnonymousUser()
self.assertFalse(u.can(Permission.GENERAL))