-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_user_views.py
223 lines (168 loc) · 7.35 KB
/
test_user_views.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
"""User View tests."""
# run these tests like:
#
# FLASK_ENV=production python -m unittest test_message_views.py
import os
from unittest import TestCase
from models import db, connect_db, Message, User, FollowersFollowee, Reaction, Message
# 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, CURR_USER_KEY
# 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()
# Don't have WTForms use CSRF at all, since it's a pain to test
app.config['WTF_CSRF_ENABLED'] = False
class UserViewTestCase(TestCase):
"""Test views for user."""
def setUp(self):
"""Create test client, add sample data."""
User.query.delete()
Message.query.delete()
self.client = app.test_client()
self.testuser = User.signup(username="testuser",
email="[email protected]",
password="testuser",
image_url=None)
self.testuser2 = User.signup(username="testuser2",
email="[email protected]",
password="testuser",
image_url=None)
self.testuser4 = User.signup(username="testuser4",
email="[email protected]",
password="testuser",
image_url=None)
self.testuser.id = 1
self.testuser2.id = 2
self.testuser4.id = 4
db.session.commit()
self.f = FollowersFollowee(follower_id=1, followee_id=2)
self.f2 = FollowersFollowee(follower_id=2, followee_id=1)
self.m = Message(id=1, text="test message", user_id=2)
self.r = Reaction(user_id=1, message_id=1, reaction_type="sad")
db.session.add_all([self.f, self.f2, self.m, self.r])
db.session.commit()
def test_signup(self):
"""Can use signup?"""
with self.client as c:
# test get signup
resp = c.get("/signup")
self.assertEqual(resp.status_code, 200)
self.assertIn(b'Join Warbler today.', resp.data)
# test post signup
resp = c.post("/signup", data={"username": "testuser3",
"email": "[email protected]", "password": "testuser"})
# Make sure it redirects
self.assertEqual(resp.status_code, 302)
def test_login(self):
"""Can login?"""
with self.client as c:
# test login get
resp = c.get("/login")
self.assertEqual(resp.status_code, 200)
self.assertIn(b'Welcome back.', resp.data)
# test post login
resp = c.post(
"/login", data={"username": "testuser", "password": "testuser"})
# Make sure it redirects
self.assertEqual(resp.status_code, 302)
def test_logout(self):
"""Can logout?"""
with self.client as c:
with c.session_transaction() as sess:
sess[CURR_USER_KEY] = self.testuser.id
# test logout get
resp = c.get("/logout")
self.assertEqual(resp.status_code, 302)
def test_list_users(self):
"""Can list users?"""
with self.client as c:
with c.session_transaction() as sess:
sess[CURR_USER_KEY] = self.testuser.id
# test get
resp = c.get('/users')
self.assertIn(b'card-bio', resp.data)
# test on person
resp = c.get('/users?q=testuser')
self.assertIn(b'testuser', resp.data)
def test_show_user(self):
"""Can show user?"""
with self.client as c:
with c.session_transaction() as sess:
sess[CURR_USER_KEY] = self.testuser.id
# test get
resp = c.get(f'/users/{self.testuser.id}')
self.assertIn(b'testuser', resp.data)
self.assertIn(b'Edit Profile', resp.data)
def test_following_user(self):
"""Can show following user?"""
with self.client as c:
with c.session_transaction() as sess:
sess[CURR_USER_KEY] = self.testuser.id
# test get
resp = c.get(f'/users/{self.testuser.id}/following')
self.assertIn(b'testuser2', resp.data)
def test_followers_user(self):
"""Can show followers user?"""
with self.client as c:
with c.session_transaction() as sess:
sess[CURR_USER_KEY] = self.testuser.id
# test get
resp = c.get(f'/users/{self.testuser.id}/followers')
self.assertIn(b'testuser2', resp.data)
def test_reaction_user(self):
"""Can show reacted?"""
with self.client as c:
with c.session_transaction() as sess:
sess[CURR_USER_KEY] = self.testuser.id
# test get
resp = c.get(f'/users/{self.testuser.id}/reactions')
self.assertIn(b'test message', resp.data)
def test_follow(self):
"""Can follow?"""
with self.client as c:
with c.session_transaction() as sess:
sess[CURR_USER_KEY] = self.testuser.id
# test post
resp = c.post('/users/follow/4')
followerfollowee = FollowersFollowee.query.get((1, 4))
self.assertTrue(followerfollowee)
def test_unfollow(self):
"""Can Unfollow"""
with self.client as c:
with c.session_transaction() as sess:
sess[CURR_USER_KEY] = self.testuser.id
# test post
resp = c.post('/users/stop-following/2')
followerfollowee = FollowersFollowee.query.all()
self.assertEqual(len(followerfollowee), 1)
def test_show_profile(self):
"""Can show and update profile?"""
with self.client as c:
with c.session_transaction() as sess:
sess[CURR_USER_KEY] = self.testuser.id
# test get
resp = c.get('/users/profile')
self.assertIn(b'Edit Your Profile.', resp.data)
# test post
resp = c.post('/users/profile', data={"username": "testuser5", "email": "[email protected]",
"password": "testuser", "image_url": "/static/images/default-pic.png",
"header_image_url": "/static/images/warbler-hero.jpg", "bio": "bio"})
self.assertEqual(resp.status_code, 302)
user = User.query.get(self.testuser.id)
self.assertEqual(user.username, "testuser5")
self.assertEqual(user.email, "[email protected]")
def test_delete_user(self):
"""Can delete user?"""
with self.client as c:
with c.session_transaction() as sess:
sess[CURR_USER_KEY] = self.testuser.id
# test post
resp = c.post('/users/delete')
users = User.query.all()
self.assertEqual(len(users), 2)