This repository has been archived by the owner on Mar 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_app.py
235 lines (179 loc) · 7.13 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
import os
import unittest
import json
from flask_sqlalchemy import SQLAlchemy
from app import create_app
from models import setup_db, Actor, Movie, db_drop_and_create, insert_seed_records
from datetime import date
casting_assistant_auth_header = {
"Authorization": "Bearer " + os.environ['CASTING_ASSISTANT_TOKEN']
}
casting_director_auth_header = {
"Authorization": "Bearer " + os.environ['CASTING_DIRECTOR_TOKEN']
}
executive_producer_auth_header = {
"Authorization": "Bearer " + os.environ['EXECUTIVE_PRODUCER_TOKEN']
}
class AgencyTestCase(unittest.TestCase):
"""Agency test case"""
def setUp(self):
"""define test variables and init app"""
self.app = create_app()
self.client = self.app.test_client
self.database_path = "postgres://{}/{}".format(
'localhost:5432', "agency_test")
setup_db(self.app, self.database_path)
db_drop_and_create()
with self.app.app_context():
self.db = SQLAlchemy()
self.db.init_app(self.app)
# create all tables
self.db.create_all()
# --
# get movies
# --
def test_get_movies(self):
response = self.client().get("/movies", headers=casting_assistant_auth_header)
data = json.loads(response.data)
self.assertEqual(response.status_code, 200)
self.assertTrue(data["success"])
self.assertEqual(len(data["movies"]), 1)
def test_get_movies_not_authorized(self):
response = self.client().get("/movies")
data = json.loads(response.data)
self.assertEqual(response.status_code, 401)
# --
# create movies
# --
def test_create_movie(self):
new_movie = {
"title": "Titanic",
"release_date": date.today()
}
response = self.client().post("/movies", json=new_movie,
headers=executive_producer_auth_header)
data = json.loads(response.data)
self.assertEqual(response.status_code, 200)
self.assertTrue(data["success"])
def test_create_movie_not_authorized(self):
new_movie = {
"title": "Titanic",
"release_date": date.today()
}
response = self.client().post("/movies", json=new_movie,
headers=casting_assistant_auth_header)
data = json.loads(response.data)
self.assertEqual(response.status_code, 401)
self.assertFalse(data["success"])
# --
# update movies
# --
def test_update_movie(self):
update_movie = {"title": "Avatar"}
response = self.client().patch("/movies/1", json=update_movie,
headers=casting_director_auth_header)
data = json.loads(response.data)
self.assertEqual(response.status_code, 200)
self.assertTrue(data["success"])
def test_update_movie_unauthorized(self):
update_movie = {"title": "Avatar"}
response = self.client().patch("/movies/1", json=update_movie,
headers=casting_assistant_auth_header)
data = json.loads(response.data)
print(data)
self.assertEqual(response.status_code, 401)
self.assertFalse(data["success"])
# --
# delete movie
# --
def test_delete_movie(self):
response = self.client().delete("/movies/1", headers=executive_producer_auth_header)
data = json.loads(response.data)
self.assertEqual(response.status_code, 200)
self.assertTrue(data["success"])
def test_delete_movie_unauthorized(self):
response = self.client().delete("/movies/1", headers=casting_assistant_auth_header)
data = json.loads(response.data)
self.assertEqual(response.status_code, 401)
self.assertFalse(data["success"])
def test_delete_movie_not_found(self):
response = self.client().delete(
"/movies/5000", headers=executive_producer_auth_header)
data = json.loads(response.data)
self.assertEqual(response.status_code, 404)
# --
# get actors
# --
def test_get_actors(self):
response = self.client().get("/actors", headers=casting_assistant_auth_header)
data = json.loads(response.data)
self.assertEqual(response.status_code, 200)
self.assertTrue(data["success"])
self.assertEqual(len(data["actors"]), 1)
def test_get_actors_not_authorized(self):
response = self.client().get("/movies")
data = json.loads(response.data)
self.assertEqual(response.status_code, 401)
# --
# create actor
# --
def test_create_actor(self):
new_actor = {
"name": "Steve",
"age": 20,
"gender": "Male"
}
response = self.client().post("/actors", json=new_actor,
headers=casting_director_auth_header)
data = json.loads(response.data)
self.assertEqual(response.status_code, 200)
self.assertTrue(data["success"])
def test_create_actor_not_authorized(self):
new_actor = {
"name": "Steve",
"age": 20,
"gender": "Male"
}
response = self.client().post("/actors", json=new_actor)
data = json.loads(response.data)
self.assertEqual(response.status_code, 401)
self.assertEqual(data["message"], "Authorization header is expected.")
self.assertFalse(data["success"])
# --
# update actor
# --
def test_update_actor(self):
updated_actor = {"name": "Bill"}
response = self.client().patch("/actors/1", json=updated_actor,
headers=casting_director_auth_header)
data = json.loads(response.data)
self.assertEqual(response.status_code, 200)
self.assertTrue(data["success"])
def test_update_actor_unauthorized(self):
updated_actor = {"name": "Bill"}
response = self.client().patch(
"/actors/1", json=updated_actor, headers=casting_assistant_auth_header)
data = json.loads(response.data)
self.assertEqual(response.status_code, 401)
self.assertFalse(data["success"])
# --
# delete actor
# --
def test_delete_actor(self):
response = self.client().delete("/actors/1", headers=casting_director_auth_header)
data = json.loads(response.data)
self.assertEqual(response.status_code, 200)
self.assertTrue(data["success"])
def test_delete_actor_unauthorized(self):
response = self.client().delete("/actors/1", headers=casting_assistant_auth_header)
data = json.loads(response.data)
self.assertEqual(response.status_code, 401)
self.assertFalse(data["success"])
def test_delete_actor_not_found(self):
response = self.client().delete("/actors/5000", headers=casting_director_auth_header)
data = json.loads(response.data)
self.assertEqual(response.status_code, 404)
# Make the tests conveniently executable.
# From app directory, run 'python test_app.py' to start tests
if __name__ == "__main__":
unittest.main()