-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_user_model.py
153 lines (106 loc) · 4.3 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
"""User model tests."""
# run these tests like:
#
# python -m unittest test_user_model.py
import os
from unittest import TestCase
from sqlalchemy import exc
from models import db, User, Message, Follows
# BEFORE we import our app, let's set an environmental variable
# to use a different database for tests (we need to do this
# before we import our app, since that will have already
# connected to the database
os.environ['DATABASE_URL'] = "postgresql:///warbler-test"
# Now we can import app
from app import app
# Create our tables (we do this here, so we only create the tables
# once for all tests --- in each test, we'll delete the data
# and create fresh new clean test data
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", "[email protected]", "testpw1", None)
user2 = User.signup("test2", "[email protected]", "testpw2", None)
user1.id = 111111
user2.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 messages & no followers
self.assertEqual(len(u.messages), 0)
self.assertEqual(len(u.followers), 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)
########################################
# Following
def test_following(self):
self.user1.following.append(self.user2)
db.session.commit()
self.assertEqual(len(self.user2.followers), 1)
self.assertEqual(len(self.user1.following), 1)
self.assertEqual(self.user2.followers[0].id, self.user1.id)
self.assertEqual(self.user1.following[0].id, self.user2.id)
def test_is_following(self):
self.user1.following.append(self.user2)
db.session.commit()
self.assertTrue(self.user1.is_following(self.user2))
self.assertFalse(self.user2.is_following(self.user1))
def test_is_followed_by(self):
self.user1.following.append(self.user2)
db.session.commit()
self.assertTrue(self.user2.is_followed_by(self.user1))
self.assertFalse(self.user1.is_followed_by(self.user2))
#############################
# Signup
def test_signup(self):
user = User.signup("testuser", "[email protected]", "testpassword", None)
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, "[email protected]", "testpassword", None)
user.id = 333333
with self.assertRaises(exc.IntegrityError) as context:
db.session.commit()
def test_invalid_email(self):
user = User.signup("testuser", None, "testpassword", None)
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", "[email protected]", None, None)
with self.assertRaises(ValueError) as context:
user = User.signup("testuser", "[email protected]", "", None)