-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_app.py
317 lines (245 loc) · 11.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
import os
import unittest
import json
from flask_sqlalchemy import SQLAlchemy
from datetime import date
from app import create_app
from models import setup_db, Actors, Movies
from config import bearer_tokens
class MyapphsbkTestCase(unittest.TestCase):
"""This class represents the myapphsbk test case"""
def setUp(self):
"""Define test variables and initialize app."""
self.app = create_app()
self.client = self.app.test_client
self.database_name = "mycapstone"
self.database_path = "postgres://{}/{}".format(
'postgres:postgres@localhost:5432', self.database_name)
setup_db(self.app, self.database_path)
self.producer_auth_header = {'Authorization': '{}'.format(
bearer_tokens['executive_producer'])}
self.director_auth_header = {
'Authorization': '{}'.format(bearer_tokens['casting_director'])}
self.assistant_auth_header = {
'Authorization': '{}'.format(bearer_tokens['casting_assistant'])}
self.new_actor = {
'name': 'Ben Afleck',
'age': 48,
'gender': 'male'
}
self.new_actor_fail = {
'name': 'Banji',
'age': 'really old',
'gender': 'male'
}
self.new_movie = {
'title': 'Apocalypse the END',
'release_date': '2020-09-24',
'country': 'Global'
}
self.new_movie_fail = {
'title': 'Apocalypse the END',
'release_date': 'hopefully never',
'country': 'Global'
}
self.update_actor = {
'age': 45
}
self.update_actor_fail = {
'age': 'i dont know'
}
self.update_movie = {
'country': 'UK'
}
self.update_movie_fail = {
'country': 24
}
# 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
"""
TODO
Write at least one test for each test for successful
operation and for expected errors.
"""
# GET Actors success and failure
def test_get_actors(self):
res = self.client().get('/actors', headers=self.producer_auth_header)
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
self.assertTrue(data['actors'])
def test_404_sent_requesting_unique_actor(self):
res = self.client().get('/actors/1', headers=self.producer_auth_header)
data = json.loads(res.data)
self.assertEqual(res.status_code, 405)
self.assertEqual(data['success'], False)
self.assertEqual(data['message'], 'Method Not Allowed')
def test_get_movies(self):
res = self.client().get('/movies', headers=self.producer_auth_header)
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
self.assertTrue(data['movies'])
def test_404_sent_requesting_unique_movie(self):
res = self.client().get('/movies/1', headers=self.producer_auth_header)
data = json.loads(res.data)
self.assertEqual(res.status_code, 405)
self.assertEqual(data['success'], False)
self.assertEqual(data['message'], 'Method Not Allowed')
""" Delete tests for actors and movies using executive producer RBAC """
@unittest.skip("need a new actor_id otherwise will fail")
def test_delete_actor(self):
res = self.client().delete('/actors/11',
headers=self.producer_auth_header)
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
self.assertEqual(data['delete'], 11)
def test_404_if_actor_does_not_exist(self):
res = self.client().delete('/actors/1000',
headers=self.producer_auth_header)
data = json.loads(res.data)
self.assertEqual(res.status_code, 404)
self.assertEqual(data['success'], False)
self.assertEqual(data['message'], 'resource not found')
@unittest.skip("need a new movie_id otherwise will fail")
def test_delete_movie(self):
res = self.client().delete('/movies/11',
headers=self.producer_auth_header)
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
self.assertEqual(data['delete'], 11)
def test_404_if_movie_does_not_exist(self):
res = self.client().delete('/movies/1000',
headers=self.producer_auth_header)
data = json.loads(res.data)
self.assertEqual(res.status_code, 404)
self.assertEqual(data['success'], False)
self.assertEqual(data['message'], 'resource not found')
"""Post tests for actors and movies """
def test_create_new_actor(self):
res = self.client().post('/actors', json=self.new_actor,
headers=self.producer_auth_header)
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
self.assertTrue(data['actors'])
def test_422_if_actor_creation_fails(self):
res = self.client().post('/actors', json=self.new_actor_fail,
headers=self.producer_auth_header)
data = json.loads(res.data)
self.assertEqual(res.status_code, 422)
self.assertEqual(data['success'], False)
def test_create_new_movie(self):
res = self.client().post('/movies', json=self.new_movie,
headers=self.producer_auth_header)
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
self.assertTrue(data['movies'])
def test_422_if_movie_creation_fails(self):
res = self.client().post('/movies', json=self.new_movie_fail,
headers=self.producer_auth_header)
data = json.loads(res.data)
self.assertEqual(res.status_code, 422)
self.assertEqual(data['success'], False)
""""Patch tests """
def test_update_actor_exist(self):
res = self.client().patch('/actors/6', json=self.update_actor,
headers=self.producer_auth_header)
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
self.assertTrue(data['actor'])
def test_update_actor_nonexistant(self):
res = self.client().patch('/actors/1000',
headers=self.producer_auth_header)
data = json.loads(res.data)
self.assertEqual(res.status_code, 422)
self.assertEqual(data['success'], False)
self.assertEqual(data['message'], 'unprocessable')
def test_update_movie_exist(self):
res = self.client().patch('/movies/5', json=self.update_movie,
headers=self.producer_auth_header)
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
self.assertTrue(data['movies'])
def test_update_movie_nonexistant(self):
res = self.client().patch('/movies/1000',
headers=self.producer_auth_header)
data = json.loads(res.data)
self.assertEqual(res.status_code, 422)
self.assertEqual(data['success'], False)
self.assertEqual(data['message'], 'unprocessable')
'''RBAC test of atleast two tests for each role.
Test for Executive producer
already done above. Below we look at the RBAC tests for
casting_assitant and casting_director '''
def test_get_actors(self):
res = self.client().get('/actors',
headers=self.assistant_auth_header)
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
self.assertTrue(data['actors'])
def test_delete_actor(self):
res = self.client().delete('/actors/13',
headers=self.assistant_auth_header)
data = json.loads(res.data)
self.assertEqual(res.status_code, 403)
self.assertEqual(data['success'], False)
self.assertEqual(data['message'], 'Permission not found.')
def test_create_new_movie(self):
res = self.client().post('/movies', json=self.new_movie,
headers=self.assistant_auth_header)
data = json.loads(res.data)
self.assertEqual(res.status_code, 403)
self.assertEqual(data['success'], False)
self.assertEqual(data['message'], 'Permission not found.')
# Casting Director permissions check
def test_get_actors(self):
res = self.client().get('/actors', headers=self.director_auth_header)
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
self.assertTrue(data['actors'])
def test_create_new_actor(self):
res = self.client().post('/actors', json=self.new_actor,
headers=self.director_auth_header)
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
self.assertTrue(data['actors'])
def test_update_movie_exist(self):
res = self.client().patch('/movies/5', json=self.update_movie,
headers=self.director_auth_header)
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
self.assertTrue(data['movies'])
def test_create_new_movie(self):
res = self.client().post('/movies', json=self.new_movie,
headers=self.director_auth_header)
data = json.loads(res.data)
self.assertEqual(res.status_code, 403)
self.assertEqual(data['success'], False)
self.assertEqual(data['message'], 'Permission not found.')
def test_delete_movie(self):
res = self.client().delete('/movies/9',
headers=self.director_auth_header)
data = json.loads(res.data)
self.assertEqual(res.status_code, 403)
self.assertEqual(data['success'], False)
self.assertEqual(data['message'], 'Permission not found.')
# Make the tests conveniently executable
if __name__ == "__main__":
unittest.main()