-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_app.py
179 lines (129 loc) · 5.29 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
# Third party imports
import pytest
# Application imports
from app import app
from models import db, insert_dummy_data
from config import tokens
# Auth0 JWT tokens
casting_assistant_token = tokens.get('casting_assistant')
casting_director_token = tokens.get('casting_director')
executive_producer_token = tokens.get('executive_producer')
@pytest.fixture
def client():
db.drop_all()
db.create_all()
insert_dummy_data()
app.config['TESTING'] = True
client = app.test_client()
yield client
#---------------------------------
# Actor test cases
#---------------------------------
def test_get_all_actors(client):
headers = {'Authorization': casting_assistant_token}
res = client.get('/actors', headers=headers)
assert res.status_code == 200
assert res.json.get('actors') != None
def test_get_all_actors_without_token(client):
res = client.get('/actors')
assert res.status_code == 401
def test_create_actor(client):
headers = {'Authorization': casting_director_token}
body = {'name': 'Hazem', 'age':25, 'gender': 'male'}
res = client.post('/actors', json=body, headers=headers)
assert res.status_code == 201
assert res.json.get('name') == 'Hazem'
assert res.json.get('id') != None
def test_create_actor_without_permission(client):
headers = {'Authorization': casting_assistant_token}
body = {'name': 'Hazem', 'age':25, 'gender': 'male'}
res = client.post('/actors', json=body, headers=headers)
assert res.status_code == 401
def test_get_actor_by_id(client):
headers = {'Authorization': casting_director_token}
res = client.get('/actors/1', headers=headers)
assert res.status_code == 200
assert res.json.get('id') == 1
def test_get_unexisting_actor(client):
headers = {'Authorization': casting_director_token}
res = client.get('/actors/1000', headers=headers)
assert res.status_code == 404
def test_update_actor(client):
headers = {'Authorization': executive_producer_token}
body = {'age': '25'}
res = client.patch('/actors/1', json=body, headers=headers)
assert res.status_code == 200
assert res.json.get('age') == 25
def test_update_unexisting_actor(client):
headers = {'Authorization': executive_producer_token}
body = {'age': '25'}
res = client.patch('/actors/1000', json=body, headers=headers)
assert res.status_code == 404
def test_delete_actor(client):
headers = {'Authorization': casting_director_token}
res = client.delete('/actors/2', headers=headers)
assert res.status_code == 200
assert res.json.get('deleted') == 2
def test_delete_actor_without_permission(client):
headers = {'Authorization': casting_assistant_token}
res = client.delete('/actors/2')
assert res.status_code == 401
def test_delete_unexisting_actor(client):
headers = {'Authorization': casting_director_token}
res = client.delete('/actors/1000', headers=headers)
assert res.status_code == 404
# #---------------------------------
# # Movie test cases
# #---------------------------------
def test_get_all_movies(client):
headers = {'Authorization': casting_assistant_token}
res = client.get('/movies', headers=headers)
assert res.status_code == 200
assert res.json.get('movies') != None
def test_create_movie(client):
headers = {'Authorization': executive_producer_token}
body = {'title': 'Sherlock'}
res = client.post('/movies', json=body, headers=headers)
assert res.status_code == 201
assert res.json.get('title') == 'Sherlock'
assert res.json.get('id') != None
def test_create_movie_without_permission(client):
headers = {'Authorization': casting_director_token}
body = {'title': 'Sherlock'}
res = client.post('/movies', json=body, headers=headers)
assert res.status_code == 401
def test_create_existing_movie(client):
headers = {'Authorization': executive_producer_token}
body = {'title': 'Inside Out'}
res = client.post('/movies', json=body, headers=headers)
assert res.status_code == 422
def test_get_movie_by_id(client):
headers = {'Authorization': casting_assistant_token}
res = client.get('/movies/1', headers=headers)
assert res.status_code == 200
assert res.json.get('id') == 1
def test_get_unexisting_movie(client):
headers = {'Authorization': executive_producer_token}
res = client.get('/movies/1000', headers=headers)
assert res.status_code == 404
def test_update_movie(client):
headers = {'Authorization': casting_director_token}
body = {'title': 'lions'}
res = client.patch('/movies/1', json=body, headers=headers)
assert res.status_code == 200
assert res.json.get('id') == 1
assert res.json.get('title') == 'Lions'
def test_update_unexisting_movie(client):
headers = {'Authorization': executive_producer_token}
body = {'title': 'Batman'}
res = client.patch('/movies/1000',json=body, headers=headers)
assert res.status_code == 404
def test_delete_movie(client):
headers = {'Authorization': executive_producer_token}
res = client.delete('/movies/2', headers=headers)
assert res.status_code == 200
assert res.json.get('deleted') == 2
def test_delete_unexisting_movie(client):
headers = {'Authorization': executive_producer_token}
res = client.delete('/movies/1000', headers=headers)
assert res.status_code == 404