-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_app.py
216 lines (158 loc) · 7.24 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
import json
import os
import unittest
from flask_sqlalchemy import SQLAlchemy
from app import APP
from db import setup_db
DATABASE_NAME = "capstone_test"
DATABASE_PATH = f"postgresql://postgres:135792468@localhost:5432/{DATABASE_NAME}"
ASSISTANT_TOKEN = os.getenv('ASSISTANT_TOKEN')
DIRECTOR_TOKEN = os.getenv('DIRECTOR_TOKEN')
PRODUCER_TOKEN = os.getenv('PRODUCER_TOKEN')
ASSISTANT_AUTH_HEADER = {'Authorization': f'Bearer {ASSISTANT_TOKEN}'}
DIRECTOR_AUTH_HEADER = {'Authorization': f'Bearer {DIRECTOR_TOKEN}'}
PRODUCER_AUTH_HEADER = {'Authorization': f'Bearer {PRODUCER_TOKEN}'}
class AppTestCase(unittest.TestCase):
def setUp(self):
self.app = APP
self.client = self.app.test_client
self.database_path = DATABASE_PATH
setup_db(self.app, self.database_path)
with self.app.app_context():
self.db = SQLAlchemy()
self.db.init_app(self.app)
self.db.create_all()
self.movie = {
'title': 'The Blacklist',
'release_date': 'Sun, 24 May 2020 13:04:03 GMT'
}
self.actor = {
'name': 'John Wick',
'age': 44,
'gender': 'Male'
}
def test_get_movies(self):
res = self.client().get('/movies', headers=ASSISTANT_AUTH_HEADER)
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
self.assertTrue(len(data['movies']))
def test_401_get_movies_unauthorized(self):
res = self.client().get('/movies', headers='')
data = json.loads(res.data)
self.assertEqual(res.status_code, 401)
self.assertEqual(data['success'], False)
self.assertFalse(len(data['movies']))
def test_create_movie(self):
res = self.client().post('/movies', json=self.movie, headers=DIRECTOR_AUTH_HEADER)
data = json.loads(res.data)
self.assertEqual(res.status_code, 201)
self.assertEqual(data['success'], True)
self.assertTrue(len(data['movie']))
def test_401_create_movie_unauthorized(self):
res = self.client().post('/movies', json=self.movie, headers='')
data = json.loads(res.data)
self.assertEqual(res.status_code, 401)
self.assertEqual(data['success'], False)
def test_400_create_movie_with_no_body(self):
res = self.client().post('/movies', json='', headers=DIRECTOR_AUTH_HEADER)
data = json.loads(res.data)
self.assertEqual(res.status_code, 400)
self.assertEqual(data['success'], False)
self.assertEqual(data['message'], 'bad request')
def test_update_movie(self):
res = self.client().patch('/movies/1', json=self.movie, headers=PRODUCER_AUTH_HEADER)
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
self.assertTrue(len(data['movie']))
def test_400_update_movie_with_no_body(self):
res = self.client().patch('/movies/1000', json='', headers=PRODUCER_AUTH_HEADER)
data = json.loads(res.data)
self.assertEqual(res.status_code, 400)
self.assertEqual(data['success'], False)
def test_401_update_movie_unauthorized(self):
res = self.client().patch('movies/1', json=self.movie, headers='')
data = json.loads(res.data)
self.assertEqual(res.status_code, 401)
self.assertEqual(data['success'], False)
def test_delete_movie(self):
res = self.client().delete('/movies/1', headers=PRODUCER_AUTH_HEADER)
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
self.assertEqual(data['deleted'], 1)
def test_404_delete_movie(self):
res = self.client().delete('/movies/1000', headers=PRODUCER_AUTH_HEADER)
data = json.loads(res.data)
self.assertEqual(res.status_code, 404)
self.assertEqual(data['success'], False)
def test_401_delete_movie_unauthorized(self):
res = self.client().delete('/movies/1', headers='')
data = json.loads(res.data)
self.assertEqual(res.status_code, 401)
self.assertEqual(data['success'], False)
# actors endpoint tests
def test_get_actors(self):
res = self.client().get('/actors', headers=ASSISTANT_AUTH_HEADER)
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
self.assertTrue(len(data['actors']))
def test_401_get_actors_unauthorized(self):
res = self.client().get('/actors', headers='')
data = json.loads(res.data)
self.assertEqual(res.status_code, 401)
self.assertEqual(data['success'], False)
self.assertFalse(len(data['movies']))
def test_create_actor(self):
res = self.client().post('/actors', json=self.actor, headers=DIRECTOR_AUTH_HEADER)
data = json.loads(res.data)
self.assertEqual(res.status_code, 201)
self.assertEqual(data['success'], True)
self.assertTrue(len(data['actor']))
def test_401_create_actor_unauthorized(self):
res = self.client().post('/actors', json=self.actor, headers='')
data = json.loads(res.data)
self.assertEqual(res.status_code, 401)
self.assertEqual(data['success'], False)
def test_400_create_actor_with_no_body(self):
res = self.client().post('/actors', json='', headers=DIRECTOR_AUTH_HEADER)
data = json.loads(res.data)
self.assertEqual(res.status_code, 400)
self.assertEqual(data['success'], False)
self.assertEqual(data['message'], 'bad request')
def test_update_actor(self):
res = self.client().patch('/actors/1', json=self.actor, headers=PRODUCER_AUTH_HEADER)
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
self.assertTrue(len(data['actor']))
def test_400_update_actor_with_no_body(self):
res = self.client().patch('/actors/1000', json='', headers=PRODUCER_AUTH_HEADER)
data = json.loads(res.data)
self.assertEqual(res.status_code, 400)
self.assertEqual(data['success'], False)
def test_401_update_actor_unauthorized(self):
res = self.client().patch('actors/1', json=self.actor, headers='')
data = json.loads(res.data)
self.assertEqual(res.status_code, 401)
self.assertEqual(data['success'], False)
def test_delete_actor(self):
res = self.client().delete('/actors/1', headers=PRODUCER_AUTH_HEADER)
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
self.assertEqual(data['deleted'], 1)
def test_404_delete_actor(self):
res = self.client().delete('/actors/1000', headers=PRODUCER_AUTH_HEADER)
data = json.loads(res.data)
self.assertEqual(res.status_code, 404)
self.assertEqual(data['success'], False)
def test_401_delete_actors_unauthorized(self):
res = self.client().delete('/actors/1', headers='')
data = json.loads(res.data)
self.assertEqual(res.status_code, 401)
self.assertEqual(data['success'], False)
if __name__ == '__main__':
unittest.main()