-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_app.py
350 lines (266 loc) · 16 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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import os
import unittest
import json
from flask_sqlalchemy import SQLAlchemy
from app import create_app
from models import Movies,Actors,setup_db
class CastingAgencyTestCase(unittest.TestCase):
"""This class represents the casting agency test case"""
def setUp(self):
"""Define test variables and initialize app."""
self.app = create_app()
self.client = self.app.test_client
setup_db(self.app)
#tokens for getting access:
self.casting_assistant=os.environ['casting_assistant']
self.casting_director=os.environ['casting_director']
self.executive_producer=os.environ['executive_producer']
self.new_actor={'name':'steven seagal',
'age':69,
'gender':'male'
}
self.new_movie={'title':'Tinker Tailor Soldier Spy',
'release_date':'2011-09-16'
}
self.app.app_context().push()
def tearDown(self):
"""Executed after reach test"""
pass
################ RBAC TESTS #############
#Casting assistant tests:
def test_casting_assistant_allowed_actions(self):
'''tests the allowed actions fot the casting assistant role '''
get_actors=self.client().get('/actors',headers={'Authorization':"Bearer {}".format(self.casting_assistant)})
get_movies=self.client().get('/movies',headers={'Authorization':"Bearer {}".format(self.casting_assistant)})
self.assertEqual(get_actors.status_code,200)
self.assertEqual(get_movies.status_code,200)
def test_forbidden_actions_casting_assistant(self):
''' tests all the forbidden endpoints for the casting assistant role'''
post_actor=self.client().post('/actors',headers={'Authorization':"Bearer {}".format(self.casting_assistant)})
post_movie=self.client().post('/movies',headers={'Authorization':"Bearer {}".format(self.casting_assistant)})
delete_actor=self.client().delete('/actors/6',headers={'Authorization':"Bearer {}".format(self.casting_assistant)})
delete_movie=self.client().delete('/movies/6',headers={'Authorization':"Bearer {}".format(self.casting_assistant)})
patch_actor=self.client().patch('/actors/1',headers={'Authorization':"Bearer {}".format(self.casting_assistant)})
patch_movie=self.client().patch('/movies/1',headers={'Authorization':"Bearer {}".format(self.casting_assistant)})
self.assertEqual(post_actor.status_code,403)
self.assertEqual(post_movie.status_code,403)
self.assertEqual(delete_actor.status_code,403)
self.assertEqual(delete_movie.status_code,403)
self.assertEqual(patch_actor.status_code,403)
self.assertEqual(patch_movie.status_code,403)
# Casting Director tests :
def test_casting_director_allowed_actions(self):
''' tests the allowed actions for the casting assistant role'''
get_actors=self.client().get('/actors',headers={'Authorization':"Bearer {}".format(self.casting_director)})
get_movies=self.client().get('/movies',headers={'Authorization':"Bearer {}".format(self.casting_director)})
post_actor=self.client().post('/actors',headers={'Authorization':"Bearer {}".format(self.casting_director)})
delete_actor=self.client().delete('/actors/6',headers={'Authorization':"Bearer {}".format(self.casting_director)})
patch_actor=self.client().patch('/actors/1',headers={'Authorization':"Bearer {}".format(self.casting_director)})
patch_movie=self.client().patch('/movies/1',headers={'Authorization':"Bearer {}".format(self.casting_director)})
self.assertEqual(get_actors.status_code,200)
self.assertEqual(get_movies.status_code,200)
self.assertNotEqual(post_actor.status_code,401 and 403)
self.assertEqual(delete_actor.status_code,200)
self.assertNotEqual(patch_actor.status_code,401 and 403)
self.assertNotEqual(patch_movie.status_code,401 and 403)
def test_403_casting_director(self):
''' tests all the forbidden endpoints for the casting director role'''
post_movie=self.client().post('/movies',headers={'Authorization':"Bearer {}".format(self.casting_director)})
delete_movie=self.client().delete('/movies/6',headers={'Authorization':"Bearer {}".format(self.casting_director)})
self.assertEqual(post_movie.status_code,403)
self.assertEqual(delete_movie.status_code,403)
# Executive Producer
def test_executive_producer_allowed_actions(self):
'''tests allowed endpoints for the executive producer'''
get_actors=self.client().get('/actors',headers={'Authorization':"Bearer {}".format(self.executive_producer)})
get_movies=self.client().get('/movies',headers={'Authorization':"Bearer {}".format(self.executive_producer)})
post_actor=self.client().post('/actors',headers={'Authorization':"Bearer {}".format(self.executive_producer)})
post_movie=self.client().post('/movies',headers={'Authorization':"Bearer {}".format(self.executive_producer)})
delete_movie=self.client().delete('/movies/8',headers={'Authorization':"Bearer {}".format(self.executive_producer)})
delete_actor=self.client().delete('/actors/8',headers={'Authorization':"Bearer {}".format(self.executive_producer)})
patch_actor=self.client().patch('/actors/1',headers={'Authorization':"Bearer {}".format(self.executive_producer)})
patch_movie=self.client().patch('/movies/1',headers={'Authorization':"Bearer {}".format(self.executive_producer)})
self.assertEqual(get_actors.status_code,200)
self.assertEqual(get_movies.status_code,200)
self.assertNotEqual(post_actor.status_code,401 and 403)
self.assertNotEqual(post_movie.status_code,401 and 403)
self.assertEqual(delete_actor.status_code,200)
self.assertEqual(delete_movie.status_code,200)
self.assertNotEqual(patch_actor.status_code,401 and 403)
self.assertNotEqual(patch_movie.status_code,401 and 403)
########### ENPOINTS TESTS ###################
# Actors tests:
def test_200_get_actors(self):
'tests getting all actors in the expected format'
res=self.client().get('/actors',headers={"Authorization":"Bearer {}".format(self.executive_producer)})
data=json.loads(res.data)
actors=Actors.query.all()
actors_format_list=[i.format() for i in actors]
self.assertEqual(200,res.status_code)
self.assertEqual(len(actors),data['total_actors'])
self.assertListEqual(actors_format_list[:10], data['actors'])
def test_404_for_invalid_actors_page(self):
'''tests if the requested actors page is beyond valid index'''
res=self.client().get('/actors?page=9999',headers={"Authorization":"Bearer {}".format(self.executive_producer)})
data=json.loads(res.data)
self.assertEqual(res.status_code, 404)
self.assertFalse(data['success'])
self.assertEqual(data['message'],'resource not found')
def test_success_paginate_actors(self):
'''tests if the requested actors page works fine'''
res=self.client().get('/actors?page=1',headers={"Authorization":"Bearer {}".format(self.executive_producer)})
data=json.loads(res.data)
actors=Actors.query.all()
self.assertEqual(res.status_code, 200)
self.assertEqual(data['total_actors'], len(actors))
self.assertEqual(len(data['actors']), 10)
def test_200_delete_actor(self):
'''tests if deleting an actor works fine'''
res=self.client().delete('/actors/4',headers={"Authorization":"Bearer {}".format(self.executive_producer)})
data=json.loads(res.data)
deleted=Actors.query.filter(Actors.id==4).one_or_none()
actors=Actors.query.all()
self.assertTrue(data['success'])
self.assertEqual(deleted,None)
self.assertEqual(data['deleted'],4)
self.assertEqual(res.status_code,200)
self.assertEqual(len(actors),data['total_actors'])
def test_404_cant_delete_unexisted_actor(self):
'''tests if the requested actor does not exist'''
res=self.client().delete('/actors/9999',headers={"Authorization":"Bearer {}".format(self.executive_producer)})
data=json.loads(res.data)
actor=Actors.query.filter(Actors.id==9999).one_or_none()
self.assertEqual(res.status_code,404)
self.assertEqual(actor,None)
self.assertEqual(data['message'],'resource not found')
self.assertFalse(data['success'])
def test_200_add_actor(self):
'''tests if adding a new actor works fine '''
res=self.client().post('/actors',json=self.new_actor,headers={"Authorization":"Bearer {}".format(self.executive_producer)})
data=json.loads(res.data)
added_to_db=Actors.query.filter(Actors.name==self.new_actor['name']).one_or_none()
self.assertEqual(res.status_code,200)
self.assertNotEqual(added_to_db,None)
self.assertEqual(data['inserted'],added_to_db.name)
self.assertTrue(data['success'])
def test_422_no_repeated_actors_allowed(self):
''' tests an unprocessable unity when adding and already existing actor'''
res=self.client().post('/actors',json=self.new_actor,headers={"Authorization":"Bearer {}".format(self.executive_producer)})
data=json.loads(res.data)
actor=Actors.query.filter(Actors.name==self.new_actor['name']).all()
self.assertEqual(res.status_code,422)
self.assertNotEqual(0,len(actor))
self.assertFalse(data['success'])
def test_200_patch_actor(self):
'''tests if patching and actor works fine'''
res=self.client().patch('/actors/1',json={'name':'aymen boudabia','age':25},headers={"Authorization":"Bearer {}".format(self.executive_producer)})
data=json.loads(res.data)
modified=Actors.query.filter(Actors.id==1).one_or_none()
self.assertEqual(res.status_code,200)
self.assertTrue(data['success'])
self.assertEqual(data['updated'],1)
self.assertEqual(modified.name,'aymen boudabia')
self.assertEqual(modified.age,25)
def test_400_patch_actor_with_empty_body(self):
'''tests a bad request response by patching an actor with an empty body'''
res=self.client().patch('actors/1',json={},headers={"Authorization":"Bearer {}".format(self.executive_producer)})
data=json.loads(res.data)
self.assertFalse(data['success'])
self.assertEqual(res.status_code,400)
def test_400_patch_actor_with_invalid_age(self):
'''tests a bad request response by patching an actor's age with invalid data '''
res=self.client().patch('actors/1',json={'age':5.3},headers={"Authorization":"Bearer {}".format(self.executive_producer)})
data=json.loads(res.data)
self.assertFalse(data['success'])
self.assertEqual(res.status_code,400)
def test_400_patch_actor_with_invalid_gender(self):
'''tests a bad request response by patching an actor's gender with invalid data '''
res=self.client().patch('actors/1',json={'gender':'hotmail'},headers={"Authorization":"Bearer {}".format(self.executive_producer)})
data=json.loads(res.data)
self.assertFalse(data['success'])
self.assertEqual(res.status_code,400)
#Movies tests
def test_200_get_movies(self):
'tests getting all movies in the expected format'
res=self.client().get('/movies',headers={"Authorization":"Bearer {}".format(self.executive_producer)})
data=json.loads(res.data)
movies=Movies.query.all()
self.assertEqual(200,res.status_code)
self.assertEqual(len(movies),data['total_movies'])
self.assertEqual(list, type(data['movies']))
def test_404_for_invalid_movies_page(self):
'''tests if the requested movies page is beyond valid index'''
res=self.client().get('/movies?page=9999',headers={"Authorization":"Bearer {}".format(self.executive_producer)})
data=json.loads(res.data)
self.assertEqual(res.status_code, 404)
self.assertFalse(data['success'])
self.assertEqual(data['message'],'resource not found')
def test_success_paginate_movies(self):
'''tests if the requested movies page works fine'''
res=self.client().get('/movies?page=1',headers={"Authorization":"Bearer {}".format(self.executive_producer)})
data=json.loads(res.data)
movies=Movies.query.all()
self.assertEqual(res.status_code, 200)
self.assertEqual(data['total_movies'], len(movies))
self.assertEqual(len(data['movies']), 10)
def test_200_delete_movie(self):
'''tests if deleting a movie works fine'''
res=self.client().delete('/movies/4',headers={"Authorization":"Bearer {}".format(self.executive_producer)})
data=json.loads(res.data)
deleted=Movies.query.filter(Movies.id==4).one_or_none()
movies=Movies.query.all()
self.assertTrue(data['success'])
self.assertEqual(deleted,None)
self.assertEqual(data['deleted'],4)
self.assertEqual(res.status_code,200)
self.assertEqual(len(movies),data['total_movies'])
def test_404_cant_delete_unexisted_movie(self):
'''tests if the requested movie does not exist'''
res=self.client().delete('/movies/9999',headers={"Authorization":"Bearer {}".format(self.executive_producer)})
data=json.loads(res.data)
movie=Movies.query.filter(Movies.id==9999).one_or_none()
self.assertEqual(res.status_code,404)
self.assertEqual(movie,None)
self.assertEqual(data['message'],'resource not found')
self.assertFalse(data['success'])
def test_200_add_movie(self):
'''tests if adding a new movie works fine '''
res=self.client().post('/movies',json=self.new_movie,headers={"Authorization":"Bearer {}".format(self.executive_producer)})
data=json.loads(res.data)
added_to_db=Movies.query.filter(Movies.title==self.new_movie['title']).one_or_none()
self.assertEqual(res.status_code,200)
self.assertNotEqual(added_to_db,None)
self.assertEqual(data['inserted'],added_to_db.title)
self.assertTrue(data['success'])
def test_422_no_repeated_movies_allowed(self):
''' tests an unprocessable unity when adding and already existing movie'''
res=self.client().post('/movies',json=self.new_movie,headers={"Authorization":"Bearer {}".format(self.executive_producer)})
data=json.loads(res.data)
movie=Movies.query.filter(Movies.title==self.new_movie['title']).all()
self.assertEqual(res.status_code,422)
self.assertNotEqual(0,len(movie))
self.assertFalse(data['success'])
def test_200_patch_movie(self):
'''tests if patching a movie works fine'''
res=self.client().patch('/movies/1',json={'release_date':'1994-09-23'},headers={"Authorization":"Bearer {}".format(self.executive_producer)})
data=json.loads(res.data)
modified=Movies.query.filter(Movies.id==1).one_or_none()
self.assertEqual(res.status_code,200)
self.assertTrue(data['success'])
self.assertEqual(data['updated'],1)
self.assertEqual(modified.release_date,'1994-09-23')
def test_400_patch_movie_with_empty_body(self):
'''tests a bad request response by patching a movie with an empty body'''
res=self.client().patch('movies/1',json={},headers={"Authorization":"Bearer {}".format(self.executive_producer)})
data=json.loads(res.data)
self.assertFalse(data['success'])
self.assertEqual(res.status_code,400)
def test_400_patch_movie_with_invalid_date(self):
'''tests a bad request response by patching an movie's date with invalid data '''
res=self.client().patch('movies/1',json={'release_date':'20009-05-66'},headers={"Authorization":"Bearer {}".format(self.executive_producer)})
data=json.loads(res.data)
self.assertFalse(data['success'])
self.assertEqual(res.status_code,400)
# Make the tests conveniently executable
if __name__ == "__main__":
unittest.main()