-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_user_model.py
117 lines (83 loc) · 3.22 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
"""User model tests."""
# Run via this command:
# python -m unittest test_user_model.py
import os
from unittest import TestCase
from sqlalchemy import exc
from models import db, User, List
os.environ['DATABASE_URL'] = "postgresql:///whichani-test"
from app import app
db.create_all()
class UserModelTestCase(TestCase):
"""Test user model."""
def setUp(self):
"""Create test client, add sample data."""
db.drop_all()
db.create_all()
user1 = User.signup("test1", "testpw1", "[email protected]", None)
user2 = User.signup("test2", "testpw2", "[email protected]", None)
user1.user_id = 111111
user2.user_id = 222222
db.session.commit()
user1 = User.query.get(111111)
user2 = User.query.get(222222)
self.user1 = user1
self.user2 = user2
self.client = app.test_client()
def tearDown(self):
res = super().tearDown()
db.session.rollback()
return res
def test_user_model(self):
"""Does basic model work?"""
u = User(
email="[email protected]",
username="testuser",
password="HASHED_PASSWORD"
)
db.session.add(u)
db.session.commit()
# User should have no liked lists or lists
self.assertEqual(len(u.liked_lists), 0)
self.assertEqual(len(u.lists), 0)
########################################
# Authenticating
def test_wrong_username(self):
self.assertFalse(User.authenticate("wrongusername", "testpw1"))
def test_wrong_password(self):
self.assertFalse(User.authenticate("test1", "wrongpassword"))
def test_authentication(self):
user = User.authenticate(self.user1.username, "password")
self.assertIsNotNone(user)
#############################
# Signup
def test_signup(self):
user = User.signup("testuser", "testpassword", "[email protected]", None)
user.user_id = 333333
db.session.commit()
user = User.query.get(333333)
self.assertIsNotNone(user)
self.assertEqual(user.username, "testuser")
self.assertEqual(user.email, "[email protected]")
# checks that pw is encrypted
self.assertNotEqual(user.password, "testpassword")
def test_invalid_username(self):
user = User.signup(None, "testpassword", "[email protected]", None)
user.user_id = 333333
with self.assertRaises(exc.IntegrityError) as context:
db.session.commit()
def test_invalid_password(self):
with self.assertRaises(ValueError) as context:
user = User.signup("testuser", None, "[email protected]", None)
with self.assertRaises(ValueError) as context:
user = User.signup("testuser", "", "[email protected]", None)
#############################
# Updating password
def test_update_password(self):
"""Test updating user password."""
user = User.signup('testuser', 'password', '[email protected]', None)
db.session.commit()
user = User.update_password('testuser', 'newpassword')
db.session.commit()
user = User.authenticate('testuser','newpassword')
self.assertTrue(user)