-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
430 lines (277 loc) · 11.4 KB
/
tests.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
from server import app, get_podcasts
from unittest import TestCase, mock
import requests
from model import connect_to_db, db, example_data
from flask import session
class FlaskTestsBasic(TestCase):
""" Flask tests. """
def setUp(self):
""" Do before every test. """
self.client = app.test_client()
# Show Flask errors that occur during tests
app.config['TESTING'] = True
connect_to_db(app, "postgresql:///testdb")
db.create_all()
example_data()
def tearDown(self):
db.session.close()
db.drop_all()
def test_index(self):
""" Test homepage. """
result = self.client.get("/")
self.assertIn(b'Sign In', result.data)
class FlaskTestsLoggedIn(TestCase):
""" Tests for logged in vs not logged in. """
def setUp(self):
self.client = app.test_client()
app.config['TESTING'] = True
connect_to_db(app, "postgresql:///testdb")
db.create_all()
example_data()
with self.client as c:
with c.session_transaction() as sess:
sess['username'] = 'HillaryForPres'
self.client.post('/login',
data={'username': 'HillaryForPres', 'password': 'bill123'},
follow_redirects=True)
def tearDown(self):
db.session.close()
db.drop_all()
def test_show_playlists(self):
""" Test show_playlist function. """
result = self.client.get("/user")
self.assertIn(b'<h3 class="title"> Playlists', result.data)
def test_show_profile(self):
""" Tests that profile page loads correctly for user. """
result = self.client.get("/user", follow_redirects=True)
self.assertIn(b'HillaryForPres', result.data)
def test_get_podcasts(self):
""" Test handling iTunes API response. """
# -------This doesn't require user to be logged in------
# Move to tests basic
pass
class FlaskTestsLogInLogOut(TestCase):
""" Flask tests whether user login and logout function properly. """
def setUp(self):
""" Do before every test. """
self.client = app.test_client()
# Show Flask errors that occur during tests
app.config['TESTING'] = True
app.config['SECRET_KEY'] = 'MySecretKey'
connect_to_db(app, "postgresql:///testdb")
db.create_all()
example_data()
def tearDown(self):
db.session.close()
db.drop_all()
def test_login(self):
""" Test login form. """
with self.client as c:
result = c.post('/login',
data={'username': 'HillaryForPres', 'password': 'bill123'},
follow_redirects=True)
self.assertEqual(session['username'], 'HillaryForPres')
def test_bad_login(self):
""" Test login form. """
with self.client as c:
result = c.post('/login',
data={'username': 'UnregisteredUser', 'password': 'password'},
follow_redirects=True)
self.assertNotIn(b'UnregisteredUser', result.data)
def test_logout(self):
""" Test log out route. """
result = self.client.post("/logout",
follow_redirects=True)
self.assertNotIn(b'<a href="/user">Home</a>', result.data)
def test_registration_form(self):
""" Testing registration form route. """
result = self.client.get("/registration", method='GET')
self.assertIn(b'<form action="/registration" method="post">',
result.data)
def test_register(self):
""" Test that db updates to reflect new user. """
result = self.client.post("/registration",
method='POST',
data={'username': 'henrythepup',
'fname': 'Henry',
'lname': 'Louise',
'email': 'henrypup@gmail.com',
'password': 'bonesforever'},
follow_redirects=True)
self.assertIn(b'Sign In', result.data)
class FlaskTestsDatabase(TestCase):
""" Flask tests using database. """
def setUp(self):
""" Set up and connect to testdb. """
self.client = app.test_client()
app.config['TESTING'] = True
connect_to_db(app, 'postgresql:///testdb')
db.create_all()
example_data()
with self.client as c:
with c.session_transaction() as sess:
sess['username'] = 'HillaryForPres'
self.client.post('/login',
data={'username': 'HillaryForPres', 'password': 'bill123'},
follow_redirects=True)
def tearDown(self):
db.session.close()
db.drop_all()
def test_add_playlist(self):
""" Test add playlist route. """
result = self.client.post('/add-playlist',
data={'title': 'News'},
follow_redirects=True)
self.assertIn(b'Playlist added', result.data)
def test_add_track(self):
""" Test add track route. """
self.client.post('/add-playlist',
data={'title': 'News'})
result = self.client.post('/add-track',
data={'artist': 'Mark Maron',
'title': 'Some Interview',
'rss': 'thisaudiofile.mp3',
'playlist_title': 'News'},
follow_redirects=True)
self.assertIn(b'Some Interview', result.data)
def test_delete_track(self):
self.client.post('/add-playlist',
data={'title': 'News'})
self.client.post('/add-track',
data={'artist': 'Mark Maron',
'title': 'Some Interview',
'rss': 'thisaudiofile.mp3',
'playlist_title': 'News'})
result = self.client.post('/delete-track',
data={'track_id': 3},
follow_redirects=True)
self.assertNotIn(b'Some Interview', result.data)
# def test_upload_photo(self):
# import io
# result = self.client.post('/upload',
# method='POST',
# data={'file': (io.BytesIO(b"myimage.jpg"), 'myimage.jpg')},
# follow_redirects=True)
# self.assertIn(b'myimage.jpg', result.data)
def test_search_users(self):
result = self.client.get('/search-users',
data={'friend_username': 'HillaryForPres'},
follow_redirects=True)
self.assertIn(b'HillaryForPres', result.data)
# def test_add_friend(self):
# result = self.client.post('/add-friend',
# method='POST',
# data={'friend_username': 'HappyTrees'},
# follow_redirects=True)
# self.assertIn(b'HappyTrees', result.data)
def test_events_form(self):
result = self.client.get('/events',
data={'username': 'HillaryForPres'})
self.assertIn(b'Fresh Air', result.data)
def itunes_query(query):
url = "https://itunes.apple.com/search"
payload = {
'term': query,
'limit': 5,
'entity': 'podcast',
'kind': 'podcast-episode'
}
resp = requests.get(url, params=payload)
resp.raise_for_status()
return resp.content
def gpodder_request():
from mygpoclient import public
client = public.PublicClient()
if client.get_toplist():
toplist_results = client.get_toplist()
return toplist_results
def eventbrite_request():
from eventbrite import Eventbrite
eventbrite = Eventbrite('MYSECRETKEY')
data = {
'location.address': 94025,
'q': 'goat+yoga',
}
events = eventbrite.get('/events/search/', data=data)
return events
class FlaskTestsRequests(TestCase):
def setUp(self):
""" Set up. """
self.client = app.test_client()
app.config['TESTING'] = True
connect_to_db(app, 'postgresql:///testdb')
db.create_all()
example_data()
with self.client as c:
with c.session_transaction() as sess:
sess['username'] = 'HillaryForPres'
self.client.post('/login',
data={'username': 'HillaryForPres', 'password': 'bill123'},
follow_redirects=True)
def _mock_itunes_query(search_terms):
return "Fresh Air"
get_podcasts = _mock_itunes_query
def tearDown(self):
db.session.close()
db.drop_all()
def test_get_podcasts(self):
SEARCH_URL = 'http://jsonplaceholder.typicode.com/podcasts'
response = requests.get(SEARCH_URL)
if response.ok:
return response
else:
return None
# def test_itunes_query(self):
# result = self.client.get('/search',
# data={'search_input': 'fresh air'},
# follow_redirects=True)
# assertIn(b'Fresh Air', result.data)
class FlaskTestsRequestsCall(TestCase):
""" Flask tests using mocked API responses. """
def setUp(self):
""" Set up. """
self.client = app.test_client()
app.config['TESTING'] = True
with self.client as c:
with c.session_transaction() as sess:
sess['username'] = 'HillaryForPres'
def tearDown(self):
db.session.close()
def _mock_response(self,
status=200,
content="CONTENT",
json_data=None,
raise_for_status=None):
mock_resp = mock.Mock()
mock_resp.raise_for_status = mock.Mock()
if raise_for_status:
mock_resp.raise_for_status.side_effect = raise_for_status
mock_resp.status_code = status
mock_resp.content = content
if json_data:
mock_resp.json = mock.Mock(return_value=json_data)
return mock_resp
@mock.patch("requests.get")
def test_itunes_query(self, mock_get):
""" Test itunes query. """
mock_resp = self._mock_response(content="FRESH AIR")
mock_get.return_value = mock_resp
result = itunes_query('fresh+air')
self.assertEqual(result, "FRESH AIR")
self.assertEqual(mock_resp.status_code, 200)
@mock.patch("requests.get")
def test_gpodder_request(self, mock_get):
""" Test gpodder request. """
mock_resp = self._mock_response()
mock_get.return_value = mock_resp
# result = gpodder_request()
self.assertEqual(mock_resp.status_code, 200)
@mock.patch("requests.get")
def test_eventbrite_request(self, mock_get):
""" Test eventbrite request. """
mock_resp = self._mock_response()
mock_get.return_value = mock_resp
self.assertEqual(mock_resp.status_code, 200)
if __name__ == "__main__":
import unittest
unittest.main()