-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_app.py
296 lines (241 loc) · 11.8 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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import datetime
import os
import unittest
from flask_sqlalchemy import SQLAlchemy
from app import create_app
from models import setup_db, Author, Book
TOKEN_PUBLISHER = 'Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IkZlTU50Q1VNZ0lFN2FTTENYcEhQdiJ9.eyJpc3MiOiJodHRwczovL2RlZXBhbmthci5hdXRoMC5jb20vIiwic3ViIjoiYXV0aDB8NWVhZWVjMWIxY2MxYWMwYzE0ODBiNDNhIiwiYXVkIjoiYmlibGlvIiwiaWF0IjoxNTg4NTI4MTExLCJleHAiOjE1ODg2MTQ1MTEsImF6cCI6IkVYektLR2JxallxMXFVTk5RSkxCc25oaFNqWmwwWEVZIiwic2NvcGUiOiIiL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edkF9eM-39QUMAyquH3iWL0QP1nKzqnNb7Hk71R7Dn41cFk921dPda_DOoq6CYmfp43wnPXnfk-ogAx-NwFWpFoDo2iO_BVbKtVmjCi0fOA3UbHwQ-RgnydULjxnRfyNArSCNSqjpIDALKpUEEFS9XgdGi_La-UQqmEtkznYINB5B4os2jp_Sbvwl-nZ_oYToX2h7OybNF0fRAP_unkIg7nWQ-1c6tJm-zfRARxxT76sPweCA'
TOKEN_BIBLIOPHILE = 'Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IkZlTU50Q1VNZ0lFN2FTTENYcEhQdiJ9.eyJpc3MiOiJodHRwczovL2RlZXBhbmthci5hdXRoMC5jb20vIiwic3ViIjoiYXV0aDB8NWVhZWViOTQ2YjY5YmMwYzEyZjAzZDc2IiwiYXVkIjoiYmlibGlvIiwiaWF0IjoxNTg4NTIyODk5LCJleHAiOjE1ODg2MDkyOTksImF6cCI6IkVYektLR2JxallxMXFVTk5RSkxCc25oaFNqWmwwWEVZIiwic2NvcGUiOiIiLCJwZXJtaXNzaW9ucyI6WyJnZXQ6YXV0aG9ycyIsImdldDpib29rcyJdfQ.P3vtEeXcY_XXajt4FukzEag-EI0PLEXq3XXwHDwxRiZQTGj6gH080-C1aBR3-WTTsrExat7YL6IjQexJQPKHM3ET9-serzWzRShuM0ytSNEUH1EcTvdSxM9b_P5qbMziukPC9JjCsO9VCJRnzHF37oDA5UuQnE0zrpnIIzieUg_6ce8iX-MpR9tpCEeTl-XDtu2qbQ0HQYz4yzt_LbS7qWsQjiokcIMg-RFmFoeILCWMpdjxNOisFUWN9U287x4V1BE1G3BBlOuynsUZceOvNYS6-PI0fiJENPHBhMp9YlhaCrQbs7C6CdvSsN3U55DD_18KM0Ou3hqpUnTmdCjtEQ'
class CapstoneTestCase(unittest.TestCase):
def setUp(self):
self.app = create_app()
self.client = self.app.test_client
self.database_name = "capstone_test"
self.database_path = os.environ['TEST_DATABASE_URL']
setup_db(self.app, self.database_path)
self.new_author = {
'name': 'Stephen Hawking',
'age': 42,
'gender': 'male',
}
self.new_book = {
'author_id': '11',
'publishDate': datetime.datetime(2022, 2, 22),
'title': 'Gladiator',
}
# binds the app to the current context
with self.app.app_context():
self.db = SQLAlchemy()
self.db.init_app(self.app)
# create all tables
self.db.create_all()
def tearDown(self):
"""Executed after reach test"""
selection = Book.query.filter(Book.title == 'Gladiator').all()
for book in selection:
book.delete()
selection = Author.query.filter(Author.name == 'Stephen Hawking').all()
for author in selection:
author.delete()
pass
def test_get_all_authors(self):
res = self.client().get('/authors', headers={
'Authorization': TOKEN_PUBLISHER}
)
data = res.get_json()
selection = Author.query.all()
self.assertEqual(data['status'], True)
self.assertEqual(data['authors'], [author.format() for author in selection])
def test_get_all_authors_error(self):
res = self.client().get('/authors', headers={})
data = res.get_json()
self.assertEqual(data['success'], False)
self.assertEqual(data['error'], 401)
def test_post_author(self):
table_length = Author.query.all()
res = self.client().post('/authors/add', headers={
'Authorization': TOKEN_PUBLISHER},
json=self.new_author
)
data = res.get_json()
self.assertEqual(data['status'], True)
self.assertGreater(len(Author.query.all()), len(table_length))
def test_post_author_error(self):
new_author = self.new_author.copy()
del new_author['name']
res = self.client().post('/authors/add', headers={
'Authorization': TOKEN_PUBLISHER},
json=new_author
)
data = res.get_json()
self.assertEqual(data['success'], False)
self.assertEqual(data['error'], 400)
def test_patch_author(self):
author = Author(
name='Hello',
age=22,
gender='male'
)
author.id = 371
author.insert()
res = self.client().patch('/authors/371', headers={
'Authorization': TOKEN_PUBLISHER},
json={
"name": "Plato",
"age": "67"
}
)
data = res.get_json()
author = Author.query.filter(Author.id == 371).one_or_none()
self.assertEqual(data['status'], True)
self.assertEqual(author.name, 'Plato')
author.delete()
def test_patch_author_error(self):
author = Author(
name='Hello',
age=22,
gender='male'
)
author.id = 451
author.insert()
res = self.client().patch('/authors/451', headers={
'Authorization': TOKEN_PUBLISHER}, json={"name": None})
data = res.get_json()
self.assertEqual(data['success'], False)
self.assertEqual(data['error'], 400)
author.delete()
def test_delete_author(self):
author = Author(
name='Brian Greene',
age=55,
gender='male'
)
author.id = 333
author.insert()
res = self.client().delete('/authors/333', headers={
'Authorization': TOKEN_PUBLISHER},
)
data = res.get_json()
selection = Author.query.filter(Author.id == 333).one_or_none()
self.assertEqual(data['status'], True)
self.assertEqual(selection, None)
def test_delete_author_error(self):
res = self.client().delete('/authors/452', headers={
'Authorization': TOKEN_PUBLISHER},
)
data = res.get_json()
self.assertEqual(data['success'], False)
self.assertEqual(data['error'], 404)
def test_get_all_books(self):
res = self.client().get('/books', headers={
'Authorization': TOKEN_PUBLISHER})
data = res.get_json()
selection = Book.query.all()
self.maxDiff = None
self.assertEqual(data['status'], True)
self.assertEqual(data['books'], [book.format() for book in selection])
def test_get_all_books_error(self):
res = self.client().get('/books', headers={})
data = res.get_json()
self.assertEqual(data['success'], False)
self.assertEqual(data['error'], 401)
def test_post_book(self):
table_length = Book.query.all()
res = self.client().post('/books/add', headers={
'Authorization': TOKEN_PUBLISHER},
json=self.new_book)
data = res.get_json()
self.assertEqual(data['status'], True)
self.assertGreater(len(Book.query.all()), len(table_length))
def test_post_book_error(self):
new_book = self.new_book.copy()
del new_book['author_id']
res = self.client().post('/books/add', headers={
'Authorization': TOKEN_PUBLISHER},
json=new_book
)
data = res.get_json()
self.assertEqual(data['success'], False)
self.assertEqual(data['error'], 400)
def test_patch_book(self):
book = Book(
title='Sapiens',
publishDate=datetime.datetime(2022, 2, 22),
author_id='11'
)
book.id = 249
book.insert()
res = self.client().patch('/books/249', headers={
'Authorization': TOKEN_PUBLISHER},
json={
"title": "Gene",
"publishDate": "2021-03-25 11:55:11.271041"
}
)
data = res.get_json()
selection = Book.query.filter(Book.id == 249).one_or_none()
self.assertEqual(data['status'], True)
self.assertEqual(selection.title, 'Gene')
selection.delete()
def test_patch_book_error(self):
# Book ID doesn't exist
res = self.client().patch('/books/256', headers={
'Authorization': TOKEN_PUBLISHER}, json={"title": "Gene",
"publishDate": "2021-03-25 11:55:11.271041"
})
data = res.get_json()
self.assertEqual(data['success'], False)
self.assertEqual(data['error'], 404)
def test_delete_book(self):
book = Book(
title='new_book',
publishDate=datetime.datetime(2022, 2, 22),
author_id='11'
)
book.id = 144
book.insert()
res = self.client().delete('/books/144', headers={
'Authorization': TOKEN_PUBLISHER},
)
data = res.get_json()
selection = Book.query.filter(Book.id == 144).one_or_none()
self.assertEqual(data['status'], True)
self.assertEqual(selection, None)
def test_delete_book_error(self):
res = self.client().delete('/books/460', headers={
'Authorization': TOKEN_PUBLISHER},
)
data = res.get_json()
self.assertEqual(data['success'], False)
self.assertEqual(data['error'], 404)
# RBAC tests start from here.
# RBAC tests of Publisher Role are already included in the above tests.
def test_bibliophile_get_success(self):
res = self.client().get('/books', headers={
'Authorization': TOKEN_BIBLIOPHILE})
data = res.get_json()
selection = Book.query.all()
self.maxDiff = None
self.assertEqual(data['status'], True)
self.assertEqual(data['books'], [book.format() for book in selection])
def test_bibliophile_post_failure(self):
res = self.client().post('/books/add', headers={
'Authorization': TOKEN_BIBLIOPHILE},
json=self.new_book)
self.assertEqual(res.status_code, 401)
def test_author_post_author_success(self):
res = self.client().post('/authors/add', headers={
'Authorization': TOKEN_AUTHOR},
json=self.new_author
)
data = res.get_json()
self.assertEqual(data['status'], True)
def test_author_post_book_failure(self):
res = self.client().post('/books/add', headers={
'Authorization': TOKEN_AUTHOR},
json=self.new_book)
self.assertEqual(res.status_code, 401)
# Execute tests!
if __name__ == "__main__":
unittest.main()