-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_app.py
332 lines (262 loc) · 10.1 KB
/
test_app.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
import os
import sys
import unittest
import json
import datetime
from flask_sqlalchemy import SQLAlchemy
from app import create_app
from models import setup_db, Actor, Movie, db
class CastingAgencyTestCase(unittest.TestCase):
""" The Casting Agency test case """
def setUp(self):
""" initialize app and define teset variables """
self.app = create_app()
self.client = self.app.test_client
self.database_path = os.environ['TEST_DATABASE_URL']
setup_db(self.app, self.database_path)
db.create_all()
self.casting_assistant_header = {
'Authorization': "Bearer {}".format(
os.environ['CASTING_ASSISTANT_TOKEN'])
}
self.casting_director_header = {
'Authorization': "Bearer {}".format(
os.environ['CASTING_DIRECTOR_TOKEN'])
}
self.executive_producer_header = {
"Authorization": "Bearer {}".format(
os.environ['EXECUTIVE_PRODUCER_TOKEN'])
}
self.all_perms_header = self.executive_producer_header
self.unvalid_token_header = {
"Authorization": "invalid_token"
}
self.new_actor = {
"name": "Tom Cruise",
"age": 58,
"gender": "male"
}
self.new_movie = {
"title": "Tenet",
"release_date": "2020-8-12"
}
self.invalid_actor_json = {
"name": "unknown",
"agw": 33,
"gendr": "male"
}
self.invalid_movie_json = {
"title": "new_movie",
"relse_dat": "2022-3-6"
}
self.update_actor_json = {
"name": "Scarlett Johansson",
"gender": "female"
}
self.update_movie_json = {
"release_date": "2025-6-9"
}
self.update_movie_date = (2025, 6, 9)
# binds the app to the current context
with self.app.app_context():
self.db = SQLAlchemy()
self.db.init_app(self.app)
# create all tables
self.db.create_all()
def tearDown(self):
""" Executed after each test """
pass
'''
Tests for Auth
'''
# casting assistant tests
def test_get_actors_CA(self):
res = self.client().get(
"/actors", headers=self.casting_assistant_header)
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
self.assertTrue(len(data['actors']))
self.assertTrue(data["actors_count"])
def test_cannot_post_actors_CA(self):
res = self.client().post(
"/actors", headers=self.casting_assistant_header,
json=self.new_actor)
data = json.loads(res.data)
self.assertEqual(res.status_code, 403)
self.assertEqual(data['success'], False)
self.assertEqual(data['message']['code'], "Forbidden")
self.assertEqual(
data['message']['description'],
"permission not found")
# casting director tests
def test_patch_actor_CD(self):
res = self.client().patch(
'/actors/3', json=self.update_actor_json,
headers=self.casting_director_header)
data = json.loads(res.data)
actor = Actor.query.get(3)
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
self.assertEqual(data['actor']['id'], 3)
self.assertEqual(actor.name, self.update_actor_json['name'])
self.assertEqual(actor.gender, self.update_actor_json['gender'])
def test_cannot_delete_movie_no_auth_header_CD(self):
res = self.client().delete("/movies/1")
data = json.loads(res.data)
self.assertEqual(res.status_code, 401)
self.assertEqual(data['success'], False)
self.assertEqual(
data['message']['code'],
'authorization_header_missing')
self.assertEqual(
data['message']['description'],
'Authorization header is expected')
# executive producer tests
def test_delete_movie_EP(self):
res = self.client().delete(
"/movies/3", headers=self.executive_producer_header)
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
self.assertEqual(data['deleted'], 3)
def test_post_movie_unvalid_token_EP(self):
res = self.client().post(
"/movies", headers=self.unvalid_token_header,
json=self.new_movie)
data = json.loads(res.data)
self.assertEqual(res.status_code, 401)
self.assertEqual(data['success'], False)
self.assertEqual(data['message']['code'], "invalid_header")
self.assertEqual(
data['message']['description'],
"Authorization must start with 'Bearer'.")
'''
Tests for GET endpoints
'''
def test_get_actors(self):
res = self.client().get("/actors", headers=self.all_perms_header)
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
self.assertTrue(len(data['actors']))
self.assertTrue(data["actors_count"])
def test_get_actors_bad_url(self):
res = self.client().get('/actors/2', headers=self.all_perms_header)
data = json.loads(res.data)
self.assertEqual(res.status_code, 405)
self.assertEqual(data['success'], False)
def test_get_movies(self):
res = self.client().get('/movies', headers=self.all_perms_header)
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
self.assertTrue(len(data['movies']))
self.assertTrue(data["movies_count"])
def test_get_movies_bad_url(self):
res = self.client().get("/movies/3", headers=self.all_perms_header)
data = json.loads(res.data)
self.assertEqual(res.status_code, 405)
self.assertEqual(data['success'], False)
'''
Tests for POST endpoints
'''
def test_post_actor(self):
res = self.client().post(
"/actors", json=self.new_actor,
headers=self.all_perms_header)
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
self.assertTrue(data['created'])
def test_post_actor_bad_json(self):
res = self.client().post(
"/actors", json=self.invalid_actor_json,
headers=self.all_perms_header)
data = json.loads(res.data)
self.assertEqual(res.status_code, 422)
self.assertEqual(data['success'], False)
def test_post_movie(self):
res = self.client().post(
"/movies", json=self.new_movie,
headers=self.all_perms_header)
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
self.assertTrue(data['created'])
def test_post_movie_no_data(self):
res = self.client().post(
'/movies', headers=self.all_perms_header)
data = json.loads(res.data)
self.assertEqual(res.status_code, 400)
self.assertEqual(data['success'], False)
'''
Tests for PATCH endpoints
'''
def test_update_actor_info(self):
res = self.client().patch(
'/actors/1', json=self.update_actor_json,
headers=self.all_perms_header)
data = json.loads(res.data)
actor = Actor.query.get(1)
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
self.assertEqual(data['actor']['id'], 1)
self.assertEqual(actor.name, self.update_actor_json['name'])
self.assertEqual(actor.gender, self.update_actor_json['gender'])
def test_update_actor_info_no_data(self):
res = self.client().patch(
'/actors/1', headers=self.all_perms_header)
data = json.loads(res.data)
self.assertEqual(res.status_code, 400)
self.assertEqual(data['success'], False)
def test_update_movie_info(self):
res = self.client().patch(
'/movies/1', json=self.update_movie_json,
headers=self.all_perms_header)
data = json.loads(res.data)
movie = Movie.query.get(1)
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
self.assertEqual(data['movie']['id'], 1)
self.assertEqual(
movie.release_date, datetime.date(*self.update_movie_date))
def test_update_movie_info_no_movie(self):
res = self.client().patch(
'/movies/77', json=self.update_movie_json,
headers=self.all_perms_header)
data = json.loads(res.data)
self.assertEqual(res.status_code, 404)
self.assertEqual(data['success'], False)
'''
Tests for DELETE endpoints
'''
def test_delete_actor(self):
res = self.client().delete(
"/actors/2", headers=self.all_perms_header)
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
self.assertEqual(data['deleted'], 2)
def test_delete_actor_does_not_exist(self):
res = self.client().delete(
"/actors/33", headers=self.all_perms_header)
data = json.loads(res.data)
self.assertEqual(res.status_code, 404)
self.assertEqual(data['success'], False)
def test_delete_movie(self):
res = self.client().delete(
"/movies/2", headers=self.all_perms_header)
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
self.assertEqual(data['deleted'], 2)
def test_delete_movie_does_not_exist(self):
res = self.client().delete(
"/movies/99", headers=self.all_perms_header)
data = json.loads(res.data)
self.assertEqual(res.status_code, 404)
self.assertEqual(data['success'], False)
# Make the tests conveniently executable
if __name__ == "__main__":
unittest.main()