-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_user_model.py
169 lines (122 loc) · 5.09 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
"""User model tests."""
# run these tests like:
#
# python -m unittest test_user_model.py
import os
from unittest import TestCase
from app import IntegrityError
from models import db, User, Message, Follows, LikedMessage
# 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()
app.config["TESTING"] = True
USER_DATA = {
"email": "[email protected]",
"username": "minestrone",
"password": "password",
"image_url": None,
}
USER_DATA2 = {
"email": "[email protected]",
"username": "bouillabaisse",
"password": "password",
"image_url": "www.image.com"
}
class UserModelTestCase(TestCase):
"""Test model instances."""
def setUp(self):
"""Create test client, add sample data."""
db.session.rollback()
User.query.delete()
Message.query.delete()
Follows.query.delete()
user1 = User.signup(**USER_DATA)
user2 = User.signup(**USER_DATA2)
db.session.add(user1)
db.session.add(user2)
db.session.commit()
self.client = app.test_client()
self.user1 = user1
self.user2 = user2
# def tearDown(self):
# 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)
def test_repr(self):
"""Test Repr returns correct username and email """
repr_string = repr(User(email=self.user1.email,
username=self.user1.username))
self.assertIn(f"{self.user1.username}, {self.user1.email}", repr_string)
def test_user_following(self):
""" Test that user following inlcludes user2 """
self.user1.following.append(self.user2)
db.session.commit()
self.assertIn(self.user2, self.user1.following)
def test_user_not_following(self):
"""Test that user1 is not folllowing user2"""
self.assertNotIn(self.user2, self.user1.following)
def test_user_followers(self):
"""Test user being followed by user2"""
self.user1.followers.append(self.user2)
db.session.commit()
self.assertIn(self.user2, self.user1.followers)
def test_user_not_follower(self):
"""Test that user2 is not following user1"""
self.assertNotIn(self.user2, self.user1.followers)
def test_successful_user_signup(self):
"""Test user signup with valid credentials"""
new_user = User.signup(username="testname",
email="[email protected]",
password="HASHED PASSWORD",
image_url=None)
db.session.add(new_user)
db.session.commit()
user_query = User.query.filter_by(username="testname").first()
self.assertEqual(new_user.username, user_query.username)
self.assertEqual(new_user.email, user_query.email)
self.assertEqual(new_user.password, user_query.password)
def test_invalid_user_signup(self):
"""Test User signup method fails with invalid credentials"""
try:
bad_user = User.signup(username="badtest", password="HASHED PASSWORD")
except TypeError:
signupError = "failure to assign user"
self.assertEqual(signupError, "failure to assign user")
def test_not_unique_user_signup(self):
"""Test User signup method fails with invalid credentials"""
with self.assertRaises(IntegrityError):
duplicate_user = User.signup(username="minestrone",
email="[email protected]",
password="HASHED PASSWORD",
image_url=None)
db.session.commit()
def test_authenticate_valid(self):
"""Test for valid authentication """
valid_user = User.authenticate(self.user1.username, USER_DATA["password"])
self.assertEqual(self.user1, valid_user)
def test_authenticate_invalid_password(self):
"""Test for invalid password authentication """
invalid_user = User.authenticate(self.user1.username, "badpassword")
self.assertFalse(invalid_user)
def test_authenticate_invalid_password(self):
"""Test for invalid password authentication """
invalid_user = User.authenticate("pasta e fagioli", USER_DATA["password"])
self.assertFalse(invalid_user)