-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_user_model.py
173 lines (138 loc) · 5.8 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
"""User model tests."""
# run these tests like:
#
# python -m unittest test_user_model.py
import os
from unittest import TestCase
from sqlalchemy.exc import IntegrityError
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 views for messages."""
def setUp(self):
"""Create test client, add sample data."""
User.query.delete()
Message.query.delete()
Follows.query.delete()
self.client = app.test_client()
def tearDown(self):
""" Rollback transactions """
db.session.rollback()
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)
# check repr method
self.assertEqual(u.__repr__(),
f"<User #{u.id}: testuser, [email protected]>")
def test_user_follow(self):
""" Does the is_following and is_followed_by work?"""
u1 = User(
email="[email protected]",
username="testuser1",
password="HASHED_PASSWORD"
)
u2 = User(
email="[email protected]",
username="testuser2",
password="HASHED_PASSWORD"
)
db.session.add_all([u1, u2])
db.session.commit()
#u1 follows u2, u2 doesn't follow u1
follow = Follows(user_being_followed_id=u2.id, user_following_id=u1.id)
db.session.add(follow)
db.session.commit()
self.assertEqual(u1.is_following(u2), True)
self.assertEqual(u2.is_following(u1), False)
self.assertEqual(u2.is_followed_by(u1), True)
self.assertEqual(u1.is_followed_by(u2), False)
# delete u1 following u2
# print(type(u1.id), u1.id)
follow = Follows.query.get((u2.id, u1.id))
db.session.delete(follow)
db.session.commit()
self.assertEqual(u1.is_following(u2), False)
self.assertEqual(u2.is_following(u1), False)
self.assertEqual(u2.is_followed_by(u1), False)
self.assertEqual(u1.is_followed_by(u2), False)
def test_user_register(self):
""" Can we create new users?"""
new_user = User.signup(username="new.user",
email="[email protected]",
password="HASH_THIS_PASS",
image_url="https://vignette.wikia.nocookie.net/questionablecontent/images/7/7a/Yelling_Bird.png/revision/latest?cb=20100107084653")
# print(new_user)
db.session.commit()
self.assertIsNotNone(new_user.id)
self.assertEqual(f"{new_user}",
f"<User #{new_user.id}: new.user, [email protected]>")
def test_user_register_validation(self):
""" Can we create new users?"""
# pass in function
new_user = User.signup(username="new.user",
email="[email protected]",
password="HASH_THIS_PASS",
image_url="https://vignette.wikia.nocookie.net/questionablecontent/images/7/7a/Yelling_Bird.png/revision/latest?cb=20100107084653")
new_user = User.signup(username="new.user",
email="[email protected]",
password="HASH_THIS_PASS",
image_url=None)
try:
db.session.commit()
except IntegrityError as e:
# print(e)
self.assertIn("duplicate key value violates unique constraint \"users_email_key\"", f"{e}")
self.assertIsNone(new_user.id)
self.assertNotEqual(f"{new_user}",
f"<User #{new_user.id}: new.user, [email protected]>")
def test_user_authentication(self):
""" Can we login as a user?"""
user = self._create_test_user()
auth = User.authenticate("testuser42", "HASHED_PASSWORD")
# print("AUTH:",auth)
self.assertNotEqual(auth, False)
self.assertEqual(f"{auth}",
f"<User #{auth.id}: testuser42, [email protected]>")
def test_user_authentication_bad_username(self):
""" Will a bad username fail to create user? """
user = self._create_test_user()
# print(user)
auth = User.authenticate("fails", "HASHED_PASSWORD")
# print("AUTH:",auth)
self.assertEqual(auth, False)
def test_user_authentication_bad_password(self):
""" Will a bad password fail to create user? """
user = self._create_test_user()
# print(user)
auth = User.authenticate("testuser42", "PASSWORD_fails")
# print("AUTH:",auth)
self.assertEqual(auth, False)
def _create_test_user(self):
""" Create user to login with. """
u = User.signup(
email="[email protected]",
username="testuser42",
password="HASHED_PASSWORD",
image_url="https://vignette.wikia.nocookie.net/questionablecontent/images/7/7a/Yelling_Bird.png/revision/latest?cb=20100107084653"
)
db.session.commit()
return u