-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
344 lines (303 loc) · 16.3 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
import os
import unittest
import json
from flask_sqlalchemy import SQLAlchemy
from app import create_app
from models import setup_db, Actor, Movie
casting_assistant_token = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IkxYN1hWX2JYaUw1bGRCNGtCOU1FZiJ9.eyJpc3MiOiJodHRwczovL215LWNhc3RpbmctYWdlbmN5LWNhcHN0b25lLnVzLmF1dGgwLmNvbS8iLCJzdWIiOiJhdXRoMHw1ZjI4YTVhMTMxYmVkNTAwOTkwNDM5ZWMiLCJhdWQiO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YwPjLcXm8ohuvXqziPlmg4jv7jArarqgOQ"
def set_header(role_token):
return {'Authorization': 'Bearer {}'.format(role_token)}
class CapstoneTestCase(unittest.TestCase):
def addInitialData(self):
actor = Actor(name="initial actor")
movie = Movie(title="initial movie", release_date="1/1/2000")
self.db.session.add(actor)
self.db.session.add(movie)
self.db.session.commit()
self.db.session.close()
def setUp(self):
self.app = create_app()
self.client = self.app.test_client
self.database_name = "capstone_test"
self.database_path = "postgresql://postgres:temppass@localhost:5432/{}".format(self.database_name)
setup_db(self.app, self.database_path)
with self.app.app_context():
self.db = SQLAlchemy()
self.db.init_app(self.app)
self.addInitialData()
def tearDown(self):
pass
#______________________________________________________________________________________
def test_actor_list(self):
res = self.client().get('/actors', headers=set_header(casting_assistant_token))
data = json.loads(res.data)
self.assertTrue(data['success'])
self.assertTrue(data['actors'])
self.assertEqual(res.status_code, 200)
def test_actor_list_failure_404(self):
actors = Actor.query.all()
for actor in actors:
actor.delete()
res = self.client().get('/actors', headers=set_header(casting_assistant_token))
data = json.loads(res.data)
self.assertFalse(data['success'])
self.assertEqual(data['error'], 404)
self.assertEqual(data['message'], "resource not found")
self.assertEqual(res.status_code, 404)
def test_movie_list(self):
res = self.client().get('/movies', headers=set_header(casting_assistant_token))
data = json.loads(res.data)
self.assertTrue(data['success'])
self.assertTrue(data['movies'])
self.assertEqual(res.status_code, 200)
def test_movie_list_failure_404(self):
movies = Movie.query.all()
for movie in movies:
movie.delete()
res = self.client().get('/movies', headers=set_header(casting_assistant_token))
data = json.loads(res.data)
self.assertFalse(data['success'])
self.assertEqual(data['error'], 404)
self.assertEqual(data['message'], "resource not found")
self.assertEqual(res.status_code, 404)
def test_get_actor(self):
actor = Actor.query.filter(Actor.name == "test actor")
actor_id = actor[0].id
res = self.client().get('/actors/' + str(actor_id), headers=set_header(casting_assistant_token))
data = json.loads(res.data)
self.assertTrue(data['success'])
self.assertEqual(data['id'], actor_id)
self.assertEqual(res.status_code, 200)
def test_get_actor_failure_400(self):
res = self.client().get('/actors/0', headers=set_header(casting_assistant_token))
data = json.loads(res.data)
self.assertFalse(data['success'])
self.assertEqual(data['error'], 400)
self.assertEqual(data['message'], "bad request")
self.assertEqual(res.status_code, 400)
def test_get_actor_failure_404(self):
res = self.client().get('/actors/9999999999', headers=set_header(casting_assistant_token))
data = json.loads(res.data)
self.assertFalse(data['success'])
self.assertEqual(data['error'], 404)
self.assertEqual(data['message'], "resource not found")
self.assertEqual(res.status_code, 404)
def test_get_movie(self):
movie = Movie.query.filter(Movie.title == "test movie")
movie_id = movie[0].id
res = self.client().get('/movies/' + str(movie_id), headers=set_header(casting_assistant_token))
data = json.loads(res.data)
self.assertTrue(data['success'])
self.assertEqual(data['id'], movie_id)
self.assertEqual(res.status_code, 200)
def test_get_movie_failure_400(self):
res = self.client().get('/movies/0', headers=set_header(casting_assistant_token))
data = json.loads(res.data)
self.assertFalse(data['success'])
self.assertEqual(data['error'], 400)
self.assertEqual(data['message'], "bad request")
self.assertEqual(res.status_code, 400)
def test_get_movie_failure_404(self):
res = self.client().get('/movies/9999999999', headers=set_header(casting_assistant_token))
data = json.loads(res.data)
self.assertFalse(data['success'])
self.assertEqual(data['error'], 404)
self.assertEqual(data['message'], "resource not found")
self.assertEqual(res.status_code, 404)
def test_add_actor(self):
res = self.client().post('/actors/add', headers=set_header(casting_director_token), json={
'name': "test actor"
})
data = json.loads(res.data)
self.assertTrue(data['success'])
self.assertEqual(data['name'], "test actor")
self.assertEqual(res.status_code, 200)
def test_add_actor_failure_422(self):
res = self.client().post('/actors/add', headers=set_header(casting_director_token), json={
'age': 123
})
data = json.loads(res.data)
self.assertFalse(data['success'])
self.assertEqual(data['error'], 422)
self.assertEqual(data['message'], "unprocessable")
self.assertEqual(res.status_code, 422)
def test_add_movie(self):
res = self.client().post('/movies/add', headers=set_header(executive_producer_token), json={
'title': "test movie",
'release_date': "1/1/2000"
})
data = json.loads(res.data)
self.assertTrue(data['success'])
self.assertEqual(data['title'], "test movie")
self.assertEqual(data['release_date'], "1/1/2000")
self.assertEqual(res.status_code, 200)
def test_add_movie_failure_422(self):
res = self.client().post('/movies/add', headers=set_header(executive_producer_token), json={
'title': "test movie",
})
data = json.loads(res.data)
self.assertFalse(data['success'])
self.assertEqual(data['error'], 422)
self.assertEqual(data['message'], "unprocessable")
self.assertEqual(res.status_code, 422)
def test_modify_actor(self):
self.client().post('/actors/add', headers=set_header(casting_director_token), json={
'name': "test actor modify"
})
actor = Actor.query.filter(Actor.name == "test actor modify")
actor_id = actor[0].id
res = self.client().patch('/actors/' + str(actor_id) + '/edit', headers=set_header(casting_director_token), json={
'name': "actor modified"
})
data = json.loads(res.data)
self.assertTrue(data['success'])
self.assertEqual(data['name'], "actor modified")
self.assertEqual(res.status_code, 200)
def test_modify_actor_failure_400(self):
res = self.client().patch('/actors/0/edit', headers=set_header(casting_director_token), json={
'name': "actor modified"
})
data = json.loads(res.data)
self.assertFalse(data['success'])
self.assertEqual(data['error'], 400)
self.assertEqual(data['message'], "bad request")
self.assertEqual(res.status_code, 400)
def test_modify_actor_failure_404(self):
res = self.client().patch('/actors/99999999/edit', headers=set_header(casting_director_token), json={
'name': "actor modified"
})
data = json.loads(res.data)
self.assertFalse(data['success'])
self.assertEqual(data['error'], 404)
self.assertEqual(data['message'], "resource not found")
self.assertEqual(res.status_code, 404)
def test_modify_actor_failure_422(self):
self.client().post('/actors/add', headers=set_header(casting_director_token), json={
'name': "test actor modify"
})
actor = Actor.query.filter(Actor.name == "test actor modify")
actor_id = actor[0].id
res = self.client().patch('/actors/' + str(actor_id) + '/edit', headers=set_header(casting_director_token), json={
'age': "hello"
})
data = json.loads(res.data)
self.assertFalse(data['success'])
self.assertEqual(data['error'], 422)
self.assertEqual(data['message'], "unprocessable")
self.assertEqual(res.status_code, 422)
def test_modify_movie(self):
self.client().post('/movies/add', headers=set_header(executive_producer_token), json={
'title': "test movie modify",
'release_date': "1/1/2000"
})
movie = Movie.query.filter(Movie.title == "test movie modify")
movie_id = movie[0].id
res = self.client().patch('/movies/' + str(movie_id) + '/edit', headers=set_header(casting_director_token), json={
'title': "movie modified"
})
data = json.loads(res.data)
self.assertTrue(data['success'])
self.assertEqual(data['title'], "movie modified")
self.assertEqual(res.status_code, 200)
def test_modify_movie_failure_400(self):
res = self.client().patch('/movies/0/edit', headers=set_header(casting_director_token), json={
'title': "movie modified"
})
data = json.loads(res.data)
self.assertFalse(data['success'])
self.assertEqual(data['error'], 400)
self.assertEqual(data['message'], "bad request")
self.assertEqual(res.status_code, 400)
def test_modify_movie_failure_404(self):
res = self.client().patch('/movies/999999999/edit', headers=set_header(casting_director_token), json={
'title': "test movie modify",
'release_date': "1/1/2000"
})
data = json.loads(res.data)
self.assertFalse(data['success'])
self.assertEqual(data['error'], 404)
self.assertEqual(data['message'], "resource not found")
self.assertEqual(res.status_code, 404)
def test_modify_movie_failure_422(self):
self.client().post('/movies/add', headers=set_header(executive_producer_token), json={
'title': "test movie modify",
'release_date': "1/1/2000"
})
movie = Movie.query.filter(Movie.title == "test movie modify")
movie_id = movie[0].id
res = self.client().patch('/movies/' + str(movie_id) + '/edit', headers=set_header(casting_director_token), json={
'release_date': "hello"
})
data = json.loads(res.data)
self.assertFalse(data['success'])
self.assertEqual(data['error'], 422)
self.assertEqual(data['message'], "unprocessable")
self.assertEqual(res.status_code, 422)
def test_delete_actor(self):
self.client().post('/actors/add', headers=set_header(casting_director_token), json={
'name': "test actor delete"
})
actor = Actor.query.filter(Actor.name == "test actor delete")
actor_id = actor[0].id
res = self.client().delete('/actors/' + str(actor_id), headers=set_header(casting_director_token))
data = json.loads(res.data)
self.assertTrue(data['success'])
self.assertEqual(data['id'], actor_id)
self.assertEqual(res.status_code, 200)
def test_delete_actor_failure_400(self):
res = self.client().delete('/actors/0', headers=set_header(casting_director_token))
data = json.loads(res.data)
self.assertFalse(data['success'])
self.assertEqual(data['error'], 400)
self.assertEqual(data['message'], "bad request")
self.assertEqual(res.status_code, 400)
def test_delete_actor_failure_404(self):
res = self.client().delete('/actors/99999999', headers=set_header(casting_director_token))
data = json.loads(res.data)
self.assertFalse(data['success'])
self.assertEqual(data['error'], 404)
self.assertEqual(data['message'], "resource not found")
self.assertEqual(res.status_code, 404)
def test_delete_movie(self):
self.client().post('/movies/add', headers=set_header(executive_producer_token), json={
'title': "test movie delete",
'release_date': '1/1/2000'
})
movie = Movie.query.filter(Movie.title == "test movie delete")
movie_id = movie[0].id
res = self.client().delete('/movies/' + str(movie_id), headers=set_header(executive_producer_token))
data = json.loads(res.data)
self.assertTrue(data['success'])
self.assertEqual(data['id'], movie_id)
self.assertEqual(res.status_code, 200)
def test_delete_movie_failure_400(self):
res = self.client().delete('/movies/0', headers=set_header(executive_producer_token))
data = json.loads(res.data)
self.assertFalse(data['success'])
self.assertEqual(data['error'], 400)
self.assertEqual(data['message'], "bad request")
self.assertEqual(res.status_code, 400)
def test_delete_movie_failure_404(self):
res = self.client().delete('/movies/9999999', headers=set_header(executive_producer_token))
data = json.loads(res.data)
self.assertFalse(data['success'])
self.assertEqual(data['error'], 404)
self.assertEqual(data['message'], "resource not found")
self.assertEqual(res.status_code, 404)
def test_role_failure_assistant(self):
res = self.client().post('/actors/add', headers=set_header(casting_assistant_token), json={
'name': "test actor"
})
data = json.loads(res.data)
self.assertEqual(res.status_code, 403)
def test_role_failure_director(self):
res = self.client().post('/movies/add', headers=set_header(casting_director_token), json={
'title': "test movie",
'release_date': "1/1/2001"
})
data = json.loads(res.data)
self.assertEqual(res.status_code, 403)
if __name__ == "__main__":
unittest.main()