-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_server.py
325 lines (253 loc) · 11.2 KB
/
test_server.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
from urllib.request import urlopen
import json, requests
import unittest
from flask import Flask, session
# import flask_testing
from server import app
import config
from model import (Repo, User, Follower, Account,
Stargazer, Dislike,
Watcher, Contributor,
Language, RepoLanguage,
db, connect_to_db)
from test_model import example_data
# class LiveServerTests(flask_testing.LiveServerTestCase):
# """Test live server functions that do not use the DB."""
# def create_app(self):
# """Create app for flask_testing."""
# app = Flask(__name__)
# app.config['TESTING'] = True
# return app
# def setUp(self):
# """Connect to database, create tables, generate test data."""
# self.client = app.test_client()
# app.config["TESTING"] = True
# def test_server(self):
# response = urlopen(self.get_server_url())
# self.assertEqual(response.code, 200)
class ServerTests(unittest.TestCase):
"""Test server functions that do not use the DB."""
# def create_app(self):
# """Create app for flask testing."""
# app = Flask(__name__)
# app.config['TESTING'] = True
# return app
def setUp(self):
"""Connect to database, create tables, generate test data."""
self.client = app.test_client()
app.config["TESTING"] = True
def test_homepage(self):
"""Test homepage when user is logged out."""
result = self.client.get("/").data.decode("utf-8")
self.assertIn("Welcome!", result)
def test_about(self):
"""Test about when user is logged out."""
with self.client as c:
result = self.client.get("/about")
self.assertEqual(result.status_code, 302)
with c.session_transaction() as sess:
flash = dict(sess["_flashes"]).get("message")
self.assertIn("Oops!", flash)
def test_logged_out_route_auth(self):
"""Test auth redirect when user is logged out and not sending code&state."""
with self.client as c:
result = self.client.get("/about")
self.assertEqual(result.status_code, 302)
with c.session_transaction() as sess:
flash = dict(sess["_flashes"]).get("message")
self.assertIn("Oops!", flash)
def test_no_logout(self):
"""Test conditional navbar items when user is logged out."""
result = self.client.get("/").data.decode("utf-8")
self.assertNotIn("Logout", result)
self.assertNotIn("Recommendations", result)
self.assertIn("Login", result)
def test_logged_out_route_me(self):
"""Test route redirect when user is logged out."""
result = self.client.get("/me")
self.assertEqual(result.status_code, 302)
def test_logged_out_route_logout(self):
"""Test route redirect when user is logged out."""
result = self.client.get("/logout")
self.assertEqual(result.status_code, 302)
def test_logged_out_route_login(self):
"""Test route redirect when user is logged out."""
result = self.client.get("/login")
self.assertEqual(result.status_code, 302)
def test_logged_out_route_recs(self):
"""Test route redirect when user is logged out."""
result = self.client.get("/recs")
self.assertEqual(result.status_code, 302)
def test_logged_out_route_get_recs(self):
"""Test route redirect when user is logged out."""
result = self.client.get("/get_repo_recs")
self.assertEqual(result.status_code, 302)
def test_logged_out_route_add_star(self):
"""Test route redirect when user is logged out."""
result = self.client.post("/add_star")
self.assertEqual(result.status_code, 302)
def test_logged_out_route_remove_star(self):
"""Test route redirect when user is logged out."""
result = self.client.post("/remove_star")
self.assertEqual(result.status_code, 302)
def test_logged_out_route_check_star(self):
"""Test route redirect when user is logged out."""
result = self.client.post("/check_star")
self.assertEqual(result.status_code, 302)
class LoginLogoutTests(unittest.TestCase):
"""Test server session when logged in and logging out."""
# def create_app(self):
# """Create app for flask testing."""
# app = Flask(__name__)
# app.config['TESTING'] = True
# return app
def setUp(self):
"""Connect to database, create tables, generate test data."""
self.client = app.test_client()
app.config["TESTING"] = True
connect_to_db(app, config.TEST_DB_URI)
db.drop_all()
db.create_all()
example_data()
def tearDown(self):
"""Close session, drop db."""
db.session.close()
db.drop_all()
def test_profile(self):
"""Test showing profile when user is logged in."""
with self.client as c:
with c.session_transaction() as sess:
sess["user_id"] = 1
result = self.client.get("/me").data.decode("utf-8")
self.assertIn("Jane", result)
self.assertIn("jhacks", result)
self.assertIn("python-repo", result)
def test_no_login(self):
"""Test conditional navbar items when user is logged in."""
with self.client as c:
with c.session_transaction() as sess:
sess["user_id"] = 1
result = self.client.get("/").data.decode("utf-8")
self.assertIn("Logout", result)
self.assertIn("Recommendations", result)
self.assertNotIn("Login", result)
def test_login_redirect(self):
"""Test conditional navbar items when user is logged in."""
with self.client as c:
with c.session_transaction() as sess:
sess["user_id"] = 1
result = self.client.get("/login")
self.assertEqual(result.status_code, 302)
def test_auth_redirect(self):
"""Test conditional navbar items when user is logged in."""
with self.client as c:
with c.session_transaction() as sess:
sess["user_id"] = 1
result = self.client.get("/auth")
self.assertEqual(result.status_code, 302)
#TODO: Test login path with mocking?
def test_logout(self):
"""Test logout route."""
with self.client as c:
with c.session_transaction() as sess:
sess["user_id"] = "1"
result = self.client.get("/logout", follow_redirects=True).data.decode("utf-8")
self.assertNotIn("user_id", session)
self.assertNotIn("Logout", result)
self.assertNotIn("Recommendations", result)
self.assertIn("Login", result)
class TestRoutesWithDB(unittest.TestCase):
"""Test server session when logged in and logging out."""
# def create_app(self):
# """Create app for flask testing."""
# app = Flask(__name__)
# app.config['TESTING'] = True
# return app
def setUp(self):
"""Connect to database, create tables, generate test data."""
self.client = app.test_client()
app.config["TESTING"] = True
connect_to_db(app, config.TEST_DB_URI)
db.drop_all()
db.create_all()
example_data()
def tearDown(self):
"""Close session, drop db."""
db.session.close()
db.drop_all()
def test_user_profile_success(self):
"""Test showing another user profile when user is logged in."""
with self.client as c:
with c.session_transaction() as sess:
sess["user_id"] = 2
result = self.client.get("/user?login=jhacks").data.decode("utf-8")
self.assertIn("Jane", result)
self.assertIn("jhacks", result)
self.assertIn("python-repo", result)
def test_user_profile_id_success(self):
"""Test showing another user profile when user is logged in."""
with self.client as c:
with c.session_transaction() as sess:
sess["user_id"] = 2
result = self.client.get("/user?user_id=1").data.decode("utf-8")
self.assertIn("Jane", result)
self.assertIn("jhacks", result)
self.assertIn("python-repo", result)
def test_user_profile_casing_success(self):
"""Test showing another user profile when user is logged in."""
with self.client as c:
with c.session_transaction() as sess:
sess["user_id"] = 2
result = self.client.get("/user?login=jHACKS").data.decode("utf-8")
self.assertIn("Jane", result)
self.assertIn("jhacks", result)
self.assertIn("python-repo", result)
def test_user_profile_failure(self):
"""Test showing a profile request failure when user is logged in."""
with self.client as c:
with c.session_transaction() as sess:
sess["user_id"] = 2
result = self.client.get("/user?login=null")
with c.session_transaction() as sess:
flash = dict(sess["_flashes"]).get("message")
self.assertIn("Unable to find user", flash)
self.assertEqual(result.status_code, 302)
def test_user_profile_null_failure(self):
"""Test showing a profile request failure when user is logged in."""
with self.client as c:
with c.session_transaction() as sess:
sess["user_id"] = 2
result = self.client.get("/user")
with c.session_transaction() as sess:
flash = dict(sess["_flashes"]).get("message")
self.assertIn("Unable to find user", flash)
self.assertEqual(result.status_code, 302)
# def test_get_json_from_repos(self):
# """Test getting json of repos."""
# result = self.client.get("/get_repo_recs")
# self.assertEqual(result.status_code, 302)
def test_add_dislike(self):
with self.client as c:
with c.session_transaction() as sess:
sess["user_id"] = 3
sess["access_token"] = "foo"
data = {"repo_id": "2"}
result = self.client.post("/add_dislike",
data=json.dumps(data),
content_type="application/json")
self.assertEqual(result.status_code, 200)
self.assertEqual(1, Dislike.query.filter_by(repo_id=2, user_id=3).count())
def test_remove_dislike(self):
with self.client as c:
with c.session_transaction() as sess:
sess["user_id"] = 2
sess["access_token"] = "foo"
data = {"repo_id": "2"}
self.assertEqual(1, Dislike.query.filter_by(repo_id=2, user_id=2).count())
result = self.client.post("/remove_dislike",
data=json.dumps(data),
content_type="application/json")
self.assertEqual(result.status_code, 200)
self.assertEqual(0, Dislike.query.filter_by(repo_id=2, user_id=2).count())
if __name__ == "__main__":
unittest.main()