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 types
response, body = get(client, "/api/misc/types", headers=headers)
assert response.status_code == codes.OK
assert len(body["media_types"]) >= 2
assert len(body["question_types"]) >= 3
assert len(body["component_types"]) >= 2
assert len(body["view_types"]) >= 2
## 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 body["items"][0]["name"] == "Linköping" and body["items"][1]["name"] == "Testköping"
## Cities
response, body = post(client, "/api/misc/cities", {"name": "Göteborg"}, headers=headers)
assert body["count"] >= 2 and 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["count"] >= 2 and 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["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" and 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"
# 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/{2}/order", {"order": 1}, headers=headers)
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"
assert body["city"]["id"] == 2 and body["role"]["id"] == 1
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
# 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
# Get slides
CID = 2
response, body = get(client, f"/api/competitions/{CID}/slides", headers=headers)
assert response.status_code == codes.OK
# Add slide
response, body = post(client, f"/api/competitions/{CID}/slides", headers=headers)
assert response.status_code == codes.OK
assert body["count"] == 4
# Add another slide
response, body = post(client, f"/api/competitions/{CID}/slides", headers=headers)
slide_order = 1
response, item_slide = get(client, f"/api/competitions/{CID}/slides/{slide_order}", headers=headers)
# 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/{slide_order}",
# 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/{slide_order}", headers=headers)
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
# Tries to delete slide again, should work since the order is now changed
response, _ = delete(client, f"/api/competitions/{CID}/slides/{slide_order}", headers=headers)
assert response.status_code == codes.NO_CONTENT
slide_order = body["items"][0]["order"]
response, _ = put(
client, f"/api/competitions/{CID}/slides/{slide_order}/order", {"order": slide_order}, headers=headers
)
change_order_test(client, CID, slide_order, slide_order + 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
assert body["count"] == num_questions
# Add question
name = "Nytt namn"
type_id = 2
f"/api/competitions/{CID}/slides/{slide_order}/questions",
{"name": name, "type_id": type_id},
assert response.status_code == codes.OK
assert item_question["name"] == name
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
response, _ = delete(client, f"/api/competitions/{CID}/slides/{slide_order}/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
response, _ = delete(client, f"/api/competitions/{CID}/slides/{NEW_slide_order}/questions/{QID}", headers=headers)
assert response.status_code == codes.NOT_FOUND