-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_user_model.py
135 lines (96 loc) · 3.47 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
"""User model tests."""
# run these tests like:
#
# python -m unittest test_user_model.py
import os
from sqlalchemy.exc import IntegrityError
from unittest import TestCase
from flask_bcrypt import Bcrypt
from models import db, User, Follows, Like, Message
os.environ['DATABASE_URL'] = "postgresql:///warbler_test"
from app import app
# instantiate Bcrypt to create hashed passwords for test data
bcrypt = Bcrypt()
db.drop_all()
db.create_all()
class UserModelTestCase(TestCase):
def setUp(self):
Like.query.delete()
Message.query.delete()
Follows.query.delete()
User.query.delete()
hashed_password = (bcrypt
.generate_password_hash("password")
.decode('UTF-8')
)
u1 = User(
username="u1",
email="[email protected]",
password=hashed_password,
image_url=None,
)
u2 = User(
username="u2",
email="[email protected]",
password=hashed_password,
image_url=None,
)
db.session.add_all([u1, u2])
db.session.commit()
self.u1_id = u1.id
self.u2_id = u2.id
self.client = app.test_client()
def tearDown(self):
db.session.rollback()
def test_user_model(self):
u1 = User.query.get(self.u1_id)
# User should have no messages & no followers
self.assertEqual(len(u1.messages), 0)
self.assertEqual(len(u1.followers), 0)
# #################### Following tests
def test_user_follows(self):
u1 = User.query.get(self.u1_id)
u2 = User.query.get(self.u2_id)
u1.following.append(u2)
db.session.commit()
self.assertEqual(u2.following, [])
self.assertEqual(u2.followers, [u1])
self.assertEqual(u1.followers, [])
self.assertEqual(u1.following, [u2])
def test_is_following(self):
u1 = User.query.get(self.u1_id)
u2 = User.query.get(self.u2_id)
u1.following.append(u2)
db.session.commit()
self.assertTrue(u1.is_following(u2))
self.assertFalse(u2.is_following(u1))
def test_is_followed_by(self):
u1 = User.query.get(self.u1_id)
u2 = User.query.get(self.u2_id)
u1.following.append(u2)
db.session.commit()
self.assertTrue(u2.is_followed_by(u1))
self.assertFalse(u1.is_followed_by(u2))
# #################### Signup Tests
def test_valid_signup(self):
u3 = User.signup("u3", "[email protected]", "password", None)
self.assertEqual(u3.username, "u3")
self.assertEqual(u3.email, "[email protected]")
self.assertNotEqual(u3.password, "password")
# Bcrypt strings should start with $2b$
self.assertTrue(u3.password.startswith("$2b$"))
def test_invalid_signup(self):
# Assert that user signup raises an integrity error when we make a user
# with the same username
with self.assertRaises(IntegrityError):
User.signup("u1", "[email protected]", "password", None)
db.session.commit()
# #################### Authentication Tests
def test_valid_authentication(self):
u1 = User.query.get(self.u1_id)
u = User.authenticate("u1", "password")
self.assertEqual(u, u1)
def test_invalid_username(self):
self.assertFalse(User.authenticate("bad-username", "password"))
def test_wrong_password(self):
self.assertFalse(User.authenticate("u1", "bad-password"))