-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_app.py
271 lines (206 loc) · 9.32 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
import os
import unittest
import json
from flask_sqlalchemy import SQLAlchemy
from app import create_app
from models import setup_db, db, Movie, Actor, db_drop_and_create_all
casting_assistant_jwt = "Bearer {}".format(os.environ.get('CASTING_ASSISTANT_JWT'))
casting_director_jwt = "Bearer {}".format(os.environ.get('CASTING_DIRECTOR_JWT'))
executive_producer_jwt = "Bearer {}".format(os.environ.get('EXECUTIVE_PRODUCER_JWT'))
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
self.database_path = os.environ['TEST_DATABASE_URL']
setup_db(self.app, self.database_path)
# binds the app to the current context
with self.app.app_context():
self.db = SQLAlchemy()
self.db.init_app(self.app)
# drop and create all tables
db_drop_and_create_all()
# sample actor to be used for test
self.new_actor_1 = {
'name': 'Gal Gadot',
'gender': 'female',
'age': 35,
}
self.new_actor_2 = {
'name': 'Daisy Ridley',
'gender': 'female',
'age': 30,
}
self.update_actor = {
'name': 'Daisy Ridley',
'gender': 'female',
'age': 28,
}
self.new_movie_1 = {
'title': 'Mission: Impossible 7',
'release_date': '2021-11-19',
}
self.new_movie_2 = {
'title': 'Matrix',
'release_date': '2021-12-22',
}
self.update_movie = {
'title': 'The Matrix 4',
'release_date': '2021-12-22',
}
def tearDown(self):
"""Executed after reach test"""
pass
"""
TODO
Write at least one test for each test for successful operation
and for expected errors.
"""
"""
Test API endpoint for actors
"""
def test_post_actors(self):
res = self.client().post('/actors', json=self.new_actor_1, headers={"Authorization": (casting_director_jwt)})
data = json.loads(res.data)
# check status and status message
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
self.assertTrue(data['actor'])
def test_get_actors(self):
res = self.client().get('/actors', headers={"Authorization": (casting_assistant_jwt)})
data = json.loads(res.data)
# check status and status message
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
self.assertTrue(len(data['actors']) >= 0)
def test_patch_actors(self):
res = self.client().post('/actors', json=self.new_actor_1, headers={"Authorization": (executive_producer_jwt)})
res = self.client().patch('/actors/1', json=self.update_actor, headers={"Authorization": (executive_producer_jwt)})
data = json.loads(res.data)
# check status and status message
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
self.assertTrue(len(data['actor']) == 1)
def test_delete_actors(self):
res = self.client().post('/actors', json=self.new_actor_1, headers={"Authorization": (executive_producer_jwt)})
res = self.client().post('/actors', json=self.new_actor_2, headers={"Authorization": (executive_producer_jwt)})
res = self.client().delete('/actors/2', headers={"Authorization": (executive_producer_jwt)})
data = json.loads(res.data)
# check status and status message
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
self.assertTrue(data['delete'] == 2)
"""
Test API endpoint for movies
"""
def test_post_movies(self):
res = self.client().post('/movies', json=self.new_movie_1, headers={"Authorization": (executive_producer_jwt)})
data = json.loads(res.data)
# check status and status message
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
self.assertTrue(data['movie'])
def test_get_movies(self):
res = self.client().get('/movies', headers={"Authorization": (casting_assistant_jwt)})
data = json.loads(res.data)
# check status and status message
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
self.assertTrue(len(data['movies']) >= 0)
def test_patch_movies(self):
res = self.client().post('/movies', json=self.new_movie_2, headers={"Authorization": (executive_producer_jwt)})
res = self.client().patch('/movies/1', json=self.update_movie, headers={"Authorization": (executive_producer_jwt)})
data = json.loads(res.data)
# check status and status message
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
self.assertTrue(len(data['movie']) == 1)
def test_delete_movies(self):
res = self.client().post('/movies', json=self.new_movie_1, headers={"Authorization": (executive_producer_jwt)})
res = self.client().post('/movies', json=self.new_movie_2, headers={"Authorization": (executive_producer_jwt)})
res = self.client().delete('/movies/2', headers={"Authorization": (executive_producer_jwt)})
data = json.loads(res.data)
# check status and status message
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
self.assertTrue(data['delete'] == 2)
"""
Test Error behaviour for /actors
"""
# Test non-existent actor
def test_404_get_request_non_existent_actor(self):
res = self.client().get('/actors/1000', headers={"Authorization": (executive_producer_jwt)})
data = json.loads(res.data)
# check status and status message
self.assertEqual(res.status_code, 404)
self.assertEqual(data['success'], False)
self.assertEqual(data['message'], 'resource not found')
# Test non-existent actor update
def test_404_update_actors(self):
res = self.client().patch('/actors/1000', json=self.update_actor, headers={"Authorization": (executive_producer_jwt)})
data = json.loads(res.data)
self.assertEqual(res.status_code, 404)
self.assertFalse(data['success'])
self.assertEqual(data['message'], 'resource not found')
# Test non-existent actor deletion
def test_404_delete_request_non_existent_actor(self):
res = self.client().delete('/actors/1599', headers={"Authorization": (executive_producer_jwt)})
data = json.loads(res.data)
# check status and status message
self.assertEqual(res.status_code, 404)
self.assertEqual(data['success'], False)
self.assertEqual(data['message'], 'resource not found')
# Test get actor without RBAC permission
def test_401_get_actors(self):
res = self.client().get('/actors')
data = json.loads(res.data)
self.assertEqual(res.status_code, 401)
self.assertFalse(data['success'])
# Test actor creation without RBAC permission
def test_401_post_actors(self):
res = self.client().post('/actors', json=self.new_actor_2)
data = json.loads(res.data)
self.assertEqual(res.status_code, 401)
self.assertFalse(data['success'])
"""
Test Error behaviour for /movies
"""
# Test non-existent movie
def test_404_get_request_non_existent_movie(self):
res = self.client().get('/movies/1000', headers={"Authorization": (executive_producer_jwt)})
data = json.loads(res.data)
# check status and status message
self.assertEqual(res.status_code, 404)
self.assertEqual(data['success'], False)
self.assertEqual(data['message'], 'resource not found')
# Test non-existent movie update
def test_404_patch_movies(self):
res = self.client().patch('/movies/1500', json=self.update_movie, headers={"Authorization": (executive_producer_jwt)})
data = json.loads(res.data)
self.assertEqual(res.status_code, 404)
self.assertFalse(data['success'])
self.assertEqual(data['message'], 'resource not found')
# Test non-existent movie deletion
def test_404_delete_request_non_existent_movie(self):
res = self.client().delete('/movies/1500', headers={"Authorization": (executive_producer_jwt)})
data = json.loads(res.data)
# check status and status message
self.assertEqual(res.status_code, 404)
self.assertEqual(data['success'], False)
self.assertEqual(data['message'], 'resource not found')
# Test get actor without RBAC permission
def test_401_get_movies(self):
res = self.client().get('/movies')
data = json.loads(res.data)
self.assertEqual(res.status_code, 401)
self.assertFalse(data['success'])
# Test movie creation without RBAC permission
def test_401_post_movies(self):
res = self.client().post('/movies', json=self.new_movie_2)
data = json.loads(res.data)
self.assertEqual(res.status_code, 401)
self.assertFalse(data['success'])
# Make the tests conveniently executable
if __name__ == "__main__":
unittest.main()