Newer
Older
from tests import app, client, db
from tests.test_helpers import (add_default_values, change_order_test, delete,
get, post, put)
add_default_values()
# Login in with default user
response, body = post(client, "/api/auth/login", {"email": "test@test.se", "password": "password"})
headers = {"Authorization": "Bearer " + body["access_token"]}
## Get misc
response, body = get(client, "/api/misc/roles", headers=headers)
assert body["count"] >= 2
response, body = get(client, "/api/misc/cities", headers=headers)
assert response.status_code == codes.OK
assert body["count"] == 2
assert body["items"][0]["name"] == "Linköping"
assert body["items"][1]["name"] == "Testköping"
response, body = get(client, "/api/misc/media_types", headers=headers)
assert body["count"] >= 2
response, body = get(client, "/api/misc/question_types", headers=headers)
assert body["count"] >= 3
## Cities
response, body = post(client, "/api/misc/cities", {"name": "Göteborg"}, headers=headers)
assert body["items"][2]["name"] == "Göteborg"
# Rename city
response, body = put(client, "/api/misc/cities/3", {"name": "Gbg"}, headers=headers)
assert response.status_code == codes.OK
assert body["items"][2]["name"] == "Gbg"
# Delete city
# First checks current cities
response, body = get(client, "/api/misc/cities", headers=headers)
assert response.status_code == codes.OK
assert body["count"] == 3
assert body["items"][0]["name"] == "Linköping"
assert body["items"][1]["name"] == "Testköping"
assert body["items"][2]["name"] == "Gbg"
# Deletes city
response, body = delete(client, "/api/misc/cities/3", headers=headers)
assert response.status_code == codes.OK
assert body["count"] == 2
assert body["items"][0]["name"] == "Linköping"
assert body["items"][1]["name"] == "Testköping"
add_default_values()
# Login in with default user
response, body = post(client, "/api/auth/login", {"email": "test@test.se", "password": "password"})
headers = {"Authorization": "Bearer " + body["access_token"]}
# Create competition
data = {"name": "c1", "year": 2020, "city_id": 1}
response, body = post(client, "/api/competitions", data, headers=headers)
assert body["name"] == "c1"
competition_id = body["id"]
# Save number of slides
num_slides = len(Slide.query.all())
# Get competition
response, body = get(client, f"/api/competitions/{competition_id}", headers=headers)
assert response.status_code == codes.OK
assert body["name"] == "c1"
response, body = post(client, f"/api/competitions/{competition_id}/slides", headers=headers)
assert response.status_code == codes.OK
response, body = get(client, f"/api/competitions/{competition_id}/slides", headers=headers)
assert response.status_code == codes.OK
response, body = put(
client, f"/api/competitions/{competition_id}/slides/{num_slides}/order", {"order": 1}, headers=headers
)
assert response.status_code == codes.OK
response, body = post(client, f"/api/competitions/{competition_id}/teams", {"name": "t1"}, headers=headers)
assert response.status_code == codes.OK
response, body = get(client, f"/api/competitions/{competition_id}/teams", headers=headers)
assert response.status_code == codes.OK
assert len(body["items"]) == 1
assert body["items"][0]["name"] == "t1"
response, body = delete(client, f"/api/competitions/{competition_id}", headers=headers)
assert response.status_code == codes.OK
response, body = post(client, "/api/auth/login", {"email": "test@test.se", "password": "password"})
headers = {"Authorization": "Bearer " + body["access_token"]}
register_data = {"email": "test1@test.se", "password": "abc123", "role_id": 2, "city_id": 1}
response, body = post(client, "/api/auth/signup", register_data, headers)
assert body["id"] == 2
assert "password" not in body
# Try to create user with same email
register_data = {"email": "test1@test.se", "password": "354213", "role_id": 1, "city_id": 1}
response, body = post(client, "/api/auth/signup", register_data, headers)
assert response.status_code == codes.BAD_REQUEST
response, body = post(client, "/api/auth/login", {"email": "test1@test.se", "password": "abc1234"})
assert response.status_code == codes.UNAUTHORIZED
response, body = post(client, "/api/auth/login", {"email": "testx@test.se", "password": "abc1234"})
assert response.status_code == codes.UNAUTHORIZED
response, body = post(client, "/api/auth/login", {"email": "test1@test.se", "password": "abc123"})
assert response.status_code == codes.OK
refresh_token = body["refresh_token"]
headers = {"Authorization": "Bearer " + body["access_token"]}
response, body = get(client, "/api/users", headers=headers)
assert body["email"] == "test1@test.se"
response, body = put(client, "/api/users", {"name": "carl carlsson", "city_id": 2, "role_id": 1}, headers=headers)
assert response.status_code == codes.OK
assert body["name"] == "Carl Carlsson"
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
297
298
299
300
301
302
303
304
assert body["city"]["id"] == 2
assert body["role"]["id"] == 1
# Find other user
response, body = get(
client,
"/api/users/search",
query_string={"name": "Olle Olsson", "email": "test@test.se", "role_id": 1, "city_id": 1},
headers=headers,
)
assert response.status_code == codes.OK
assert body["count"] == 1
# Get user from ID
searched_user = body["items"][0]
user_id = searched_user["id"]
response, body = get(client, f"/api/users/{user_id}", headers=headers)
assert response.status_code == codes.OK
assert searched_user["name"] == body["name"]
assert searched_user["email"] == body["email"]
assert searched_user["role_id"] == body["role"]["id"]
assert searched_user["city_id"] == body["city"]["id"]
assert searched_user["id"] == body["id"]
# Edit user from ID
response, body = put(client, f"/api/users/{user_id}", {"email": "carl@carlsson.test"}, headers=headers)
assert response.status_code == codes.OK
assert body["email"] == "carl@carlsson.test"
# Edit user from ID but add the same email as other user
response, body = put(client, f"/api/users/{user_id}", {"email": "test1@test.se"}, headers=headers)
assert response.status_code == codes.BAD_REQUEST
# Delete other user
response, body = delete(client, f"/api/auth/delete/{user_id}", headers=headers)
assert response.status_code == codes.OK
# Try to delete other user again
response, body = delete(client, f"/api/auth/delete/{user_id}", headers=headers)
assert response.status_code == codes.NOT_FOUND
# Logout and try to access current user
response, body = post(client, f"/api/auth/logout", headers=headers)
assert response.status_code == codes.OK
# TODO: Check if current users jwt (jti) is in blacklist after logging out
response, body = get(client, "/api/users", headers=headers)
assert response.status_code == codes.UNAUTHORIZED
# Login in again with default user
response, body = post(client, "/api/auth/login", {"email": "test1@test.se", "password": "abc123"})
assert response.status_code == codes.OK
headers = {"Authorization": "Bearer " + body["access_token"]}
# TODO: Add test for refresh api for current user
# response, body = post(client, "/api/auth/refresh", headers={**headers, "refresh_token": refresh_token})
# assert response.status_code == codes.OK
# Find current user
response, body = get(client, "/api/users", headers=headers)
assert response.status_code == codes.OK
assert body["email"] == "test1@test.se"
assert body["city"]["id"] == 2
assert body["role"]["id"] == 1
# Delete current user
user_id = body["id"]
response, body = delete(client, f"/api/auth/delete/{user_id}", headers=headers)
assert response.status_code == codes.OK
# TODO: Check that user was blacklisted
# Look for current users jwt in blacklist
# Blacklist.query.filter(Blacklist.jti == )
def test_slide_api(client):
add_default_values()
# Login in with default user
response, body = post(client, "/api/auth/login", {"email": "test@test.se", "password": "password"})
assert response.status_code == codes.OK
headers = {"Authorization": "Bearer " + body["access_token"]}
# Get slides from empty competition
CID = 1
response, body = get(client, f"/api/competitions/{CID}/slides", headers=headers)
assert response.status_code == codes.OK
assert body["count"] == 0
# Get slides
CID = 2
num_slides = 3
response, body = get(client, f"/api/competitions/{CID}/slides", headers=headers)
assert response.status_code == codes.OK
assert body["count"] == num_slides
# Add slide
response, body = post(client, f"/api/competitions/{CID}/slides", headers=headers)
num_slides += 1
assert response.status_code == codes.OK
assert body["count"] == num_slides
# Get slide
SID = 1
response, item_slide = get(client, f"/api/competitions/{CID}/slides/{SID}", headers=headers)
assert response.status_code == codes.OK
assert item_slide["id"] == SID
# Edit slide
order = 6
title = "Ny titel"
body = "Ny body"
timer = 43
assert item_slide["order"] != order
assert item_slide["title"] != title
# assert item_slide["body"] != body
assert item_slide["timer"] != timer
response, item_slide = put(
client,
f"/api/competitions/{CID}/slides/{SID}",
# TODO: Implement so these commented lines can be edited
# {"order": order, "title": title, "body": body, "timer": timer},
{"title": title, "timer": timer},
headers=headers,
)
assert response.status_code == codes.OK
# assert item_slide["order"] == order
assert item_slide["title"] == title
# assert item_slide["body"] == body
assert item_slide["timer"] == timer
# Delete slide
response, _ = delete(client, f"/api/competitions/{CID}/slides/{SID}", headers=headers)
num_slides -= 1
assert response.status_code == codes.NO_CONTENT
# Checks that there are fewer slides
response, body = get(client, f"/api/competitions/{CID}/slides", headers=headers)
assert response.status_code == codes.OK
assert body["count"] == num_slides
# Tries to delete slide again
response, _ = delete(client, f"/api/competitions/{CID}/slides/{SID}", headers=headers)
assert response.status_code == codes.NOT_FOUND
# Changes the order to the same order
i = 0
SID = body["items"][i]["id"]
order = body["items"][i]["order"]
response, _ = put(client, f"/api/competitions/{CID}/slides/{SID}/order", {"order": order}, headers=headers)
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# Changes the order
change_order_test(client, CID, SID, order + 1, headers)
# Changes order to 0
SID = 7
change_order_test(client, CID, SID, -1, headers)
def test_question_api(client):
add_default_values()
# Login in with default user
response, body = post(client, "/api/auth/login", {"email": "test@test.se", "password": "password"})
assert response.status_code == codes.OK
headers = {"Authorization": "Bearer " + body["access_token"]}
# Get questions from empty competition
CID = 1 # TODO: Fix api-calls so that the ones not using CID don't require one
response, body = get(client, f"/api/competitions/{CID}/questions", headers=headers)
assert response.status_code == codes.OK
assert body["count"] == 0
# Get questions from another competition that should have some questions
CID = 3
num_questions = 3
response, body = get(client, f"/api/competitions/{CID}/questions", headers=headers)
assert response.status_code == codes.OK
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
assert body["count"] == num_questions
# # Get specific question
# name = "Q2"
# # total_score = 2
# type_id = 5
# slide_id = 5
# response, body = get(
# client,
# f"/api/competitions/{CID}/questions/",
# headers=headers,
# )
# # print(f"357: {body['items']}")
# assert response.status_code == codes.OK
# assert body["count"] == 1
# item_question = body["items"][0]
# # print(f"338: {item_question}")
# assert item_question["name"] == name
# # assert item_question["total_score"] == total_score
# assert item_question["type_id"] == type_id
# assert item_question["slide_id"] == slide_id
# Add question
name = "Nytt namn"
# total_score = 2
type_id = 2
slide_id = 5
response, item_question = post(
client,
f"/api/competitions/{CID}/questions",
{"name": name, "type_id": type_id, "slide_id": slide_id},
headers=headers,
)
num_questions += 1
assert response.status_code == codes.OK
assert item_question["name"] == name
# # assert item_question["total_score"] == total_score
assert item_question["type"]["id"] == type_id
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
# Checks number of questions
response, body = get(client, f"/api/competitions/{CID}/questions", headers=headers)
assert response.status_code == codes.OK
assert body["count"] == num_questions
# Try to get question in another competition
QID = 1
response, item_question = get(client, f"/api/competitions/{CID}/questions/{QID}", headers=headers)
assert response.status_code == codes.NOT_FOUND
# Get question
QID = 4
response, item_question = get(client, f"/api/competitions/{CID}/questions/{QID}", headers=headers)
assert response.status_code == codes.OK
assert item_question["id"] == QID
# Try to edit question in another competition
name = "Nyare namn"
# total_score = 2
type_id = 3
slide_id = 1
QID = 1
response, _ = put(
client,
f"/api/competitions/{CID}/questions/{QID}",
# {"name": name, "total_score": total_score, "type_id": type_id, "slide_id": slide_id},
{"name": name, "type_id": type_id, "slide_id": slide_id},
headers=headers,
)
assert response.status_code == codes.NOT_FOUND
# Checks number of questions
response, body = get(client, f"/api/competitions/{CID}/questions", headers=headers)
assert response.status_code == codes.OK
assert body["count"] == num_questions
# Edit question
name = "Nyare namn"
# total_score = 2
type_id = 3
slide_id = 5
QID = 4
assert item_question["name"] != name
# assert item_question["total_score"] != total_score
assert item_question["type"]["id"] != type_id
response, item_question = put(
client,
f"/api/competitions/{CID}/questions/{QID}",
# {"name": name, "total_score": total_score, "type_id": type_id, "slide_id": slide_id},
{"name": name, "type_id": type_id, "slide_id": slide_id},
headers=headers,
)
assert response.status_code == codes.OK
assert item_question["name"] == name
# # assert item_question["total_score"] == total_score
assert item_question["type"]["id"] == type_id
# Checks number of questions
response, body = get(client, f"/api/competitions/{CID}/questions", headers=headers)
assert response.status_code == codes.OK
assert body["count"] == num_questions
# Delete question
response, _ = delete(client, f"/api/competitions/{CID}/questions/{QID}", headers=headers)
num_questions -= 1
assert response.status_code == codes.NO_CONTENT
# Checks that there are fewer questions
response, body = get(client, f"/api/competitions/{CID}/questions", headers=headers)
assert response.status_code == codes.OK
assert body["count"] == num_questions
# Tries to delete question again
response, _ = delete(client, f"/api/competitions/{CID}/questions/{QID}", headers=headers)
assert response.status_code == codes.NOT_FOUND