-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_app.py
255 lines (192 loc) · 7.51 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
import os
import unittest
import json
from flask_sqlalchemy import SQLAlchemy
from models import setup_db
from app import create_app
from flask import request, _request_ctx_stack ,abort
from functools import wraps
from jose import jwt
from urllib.request import urlopen
assistance_token = os.environ.get("assistance_token")
director_token = os.environ.get('director_token')
producer_token = os.environ.get('producer_token')
class CapstoneTest(unittest.TestCase):
def setUp(self):
self.app = create_app()
self.client = self.app.test_client
self.database_name = "capstone"
self.database_path = "postgres://{}:{}@{}/{}".format(
'postgres', '4952', 'localhost:5432', self.database_name)
setup_db(self.app, self.database_path)
self.new_movie = {
'title': 'who i am ?',
'release_date': '1/10/2010'
}
self.new_actor = {
"name": "alwaleed",
"age": "22",
"gender": "male"
}
with self.app.app_context():
self.db = SQLAlchemy()
self.db.init_app(self.app)
self.db.create_all()
def tearDown(self):
"""Executed after reach test"""
pass
'''
Test POST actor and movie
'''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# create actor with authorization token
def test_create_actor_with_valid_token(self):
res = self.client().post(
'/actors',
json=self.new_actor,
headers={
"Authorization": 'bearer '+producer_token})
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertTrue(data['success'])
# create actor with unauthorized token
def test_create_actor_with_invalid_token(self):
res = self.client().post(
'/actors',
json=self.new_actor,
headers=
{"Authorization": 'bearer '+assistance_token})
data = json.loads(res.data)
self.assertEqual(res.status_code, 403)
self.assertEqual(data['code'], "unauthorized")
self.assertEqual(data['description'], "Permission not found.")
# create movie with authorization token
def test_create_movie_with_valid_token(self):
response = self.client().post(
'/movies',
json=self.new_movie,
headers={
"Authorization": 'bearer '+producer_token})
res = json.loads(response.data)
self.assertEqual(response.status_code, 200)
self.assertTrue(res['success'])
# create movie with unauthorized token
def test_create_movie_with_invalid_token(self):
response = self.client().post(
'/movies',
json=self.new_movie,
headers={
"Authorization": 'bearer '+director_token})
res = json.loads(response.data)
self.assertEqual(response.status_code, 403)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''
Test GET actors and moves
'''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
def test_get_all_movies_with_valid_token(self):
response = self.client().get(
'/movies',
headers={"Authorization": 'bearer '+assistance_token})
res = json.loads(response.data)
self.assertEqual(response.status_code, 200)
self.assertTrue(res['success'])
def test_get_all_movies_without_token(self):
response = self.client().get('/movies')
res = json.loads(response.data)
self.assertEqual(response.status_code, 401)
def test_get_all_actors_with_valid_token(self):
response = self.client().get(
'/actors',
headers={"Authorization": 'bearer '+assistance_token})
res = json.loads(response.data)
self.assertEqual(response.status_code, 200)
self.assertTrue(res['success'])
def test_get_all_actors_without_token(self):
response = self.client().get('/actors')
res = json.loads(response.data)
self.assertEqual(response.status_code, 401)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''
Test PATCH actors and movies
'''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
def test_update_actor_with_valid_token(self):
request = {
"name": "saleh", "age": 34
}
res = self.client().patch(
'/actors/8',
json=request,
headers={
"Authorization": 'bearer '+producer_token})
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertTrue(data["success"])
def test_update_actor_with_invalid_token(self):
request = {
"name": "saleh", "age": 34
}
res = self.client().patch(
'/actors/8',
json=request,
headers={
"Authorization": 'bearer '+assistance_token})
data = json.loads(res.data)
self.assertEqual(res.status_code, 403)
def test_update_movies_with_valid_token(self):
request = {
"title": "inseption", "release_date": "11/2/2010"
}
res = self.client().patch(
'/movies/8',
json=request,
headers={
"Authorization": 'bearer '+producer_token})
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertTrue(data["success"])
def test_update_movies_with_invalid_token(self):
request = {
"title": "inseption", "release_date": "11/2/2010"
}
res = self.client().patch(
'/movies/8',
json=request,
headers={
"Authorization": 'bearer '+assistance_token})
data = json.loads(res.data)
self.assertEqual(res.status_code, 403)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''
Test DELETE actors and movies
'''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
def test_delete_actor_with_valid_token(self):
res = self.client().delete('/actors/6',
headers={"Authorization": 'bearer '+producer_token})
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertTrue(data["success"])
def test_delete_actor_with_invalid_token(self):
res = self.client().delete(
'/actors/5',
headers={
"Authorization": 'bearer '+assistance_token})
data = json.loads(res.data)
self.assertEqual(res.status_code, 403)
def test_delete_movie_with_valid_token(self):
res = self.client().delete('/movies/9',
headers={"Authorization": 'bearer '+producer_token})
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertTrue(data["success"])
def test_delete_movie_with_invalid_token(self):
res = self.client().delete(
'/movies/6',
headers={
"Authorization": 'bearer '+assistance_token})
data = json.loads(res.data)
self.assertEqual(res.status_code, 403)
if __name__ == "__main__":
unittest.main()