-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_user_model.py
136 lines (99 loc) · 4.46 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
134
135
136
"""User model tests."""
# run these tests like:
#
# python -m unittest test_user_model.py
from unittest import TestCase
from sqlalchemy.exc import IntegrityError
from models import db, User, SavedSearch
from app import app
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql:///restroom-finder-test'
db.create_all()
class UserModelTestCase(TestCase):
"""Test model for users."""
def setUp(self):
"""Create test client, add sample data."""
User.query.delete()
SavedSearch.query.delete()
# create test user to test the various methods and unique restraints
u1 = User(
email="[email protected]",
password="HASHED_PASSWORD",
firstname="Joe",
lastname="Smith"
)
db.session.add(u1)
db.session.commit()
self.test_user1 = u1
self.client = app.test_client()
def test_user_model(self):
"""Does create basic model work?"""
u = User(
email="[email protected]",
password="HASHED_PASSWORD",
firstname="Tim",
lastname="Tester"
)
db.session.add(u)
db.session.commit()
# User should have no searches
self.assertEqual(len(u.searches), 0)
def test_repr_method(self):
"""Does the repr method work as expected?"""
self.assertEqual(self.test_user1.__repr__(), f"<User - id: {self.test_user1.id}, email: {self.test_user1.email}, name: {self.test_user1.lastname}, {self.test_user1.firstname}>")
def test_register_method_valid(self):
"""Does the register method work as expected given valid input and credentials?"""
user = User.register(
email="[email protected]",
password="HASHED_PASSWORD",
firstname="Tom",
lastname="Jones"
)
db.session.commit()
# Does User.register successfully create a new user given valid credentials?
self.assertIsInstance(user.id, int)
self.assertEqual(user.email, '[email protected]')
self.assertEqual(user.firstname, 'Tom')
self.assertEqual(user.lastname, 'Jones')
def test_register_method_invalid(self):
"""Does the sign_up method work as expected given invalid input/credentials?"""
cases = [
{'email': '[email protected]', 'password': 'HASHED_PASSWORD', 'firstname': 'Test', 'lastname': 'Person1'}, # duplicate email
{'email': None, 'password': 'HASHED_PASSWORD', 'firstname': 'Test', 'lastname': 'Person2'} # null email
]
# Does User.create fail to create a new user if any of the validations (e.g. uniqueness, non-nullable fields) fail?
for case in cases:
user = User.register(
email=case['email'],
password=case['password'],
firstname=case['firstname'],
lastname=case['lastname']
)
failed = False
try:
db.session.commit()
except IntegrityError as ie:
failed = True
self.assertIsInstance(ie, IntegrityError)
db.session.rollback()
self.assertEqual(failed, True)
def test_authenticate_valid(self):
"""Does the User.authenticate method work as expected given valid username and password?"""
user = User.register(
email='[email protected]',
password='password123',
firstname='Test',
lastname='User'
)
db.session.commit()
good_user = User.authenticate(email='[email protected]', password="password123")
# Does User.authenticate successfully return a user when given a valid email and password?
self.assertIsInstance(good_user.id, int)
self.assertEqual(good_user.email, '[email protected]')
self.assertEqual(good_user.firstname, 'Test')
self.assertEqual(good_user.lastname, 'User')
# Does User.authenticate fail to return a user when the email is invalid?
bad_user = User.authenticate(email='[email protected]', password="password123")
self.assertEqual(bad_user, False)
# Does User.authenticate fail to return a user when the password is invalid?
bad_user = User.authenticate(email='[email protected]', password="wrongPassword")
self.assertEqual(bad_user, False)