Fix tests for users and highscores

This commit is contained in:
lvrossem
2023-04-17 14:52:36 -06:00
parent 3596394f3f
commit 81e9eb154b
11 changed files with 156 additions and 201 deletions

View File

@@ -46,7 +46,7 @@ async def test_register_without_username_should_fail():
json={"password": password, "avatar_index": avatar_index},
)
assert response.status_code == 422
assert response.status_code == 400
assert "access_token" not in response.json()
@@ -61,7 +61,7 @@ async def test_register_without_password_should_fail():
json={"username": username, "avatar_index": avatar_index},
)
assert response.status_code == 422
assert response.status_code == 400
assert "access_token" not in response.json()
@@ -76,7 +76,6 @@ async def test_register_without_avatar_should_fail():
json={"username": username, "password": password},
)
# Not ideal that this is 400 instead of 422, but had no other choice than to give this field a default value
assert response.status_code == 400
assert "access_token" not in response.json()
@@ -115,7 +114,7 @@ async def test_login_wrong_password_should_fail():
@pytest.mark.asyncio
async def test_login_without_username_should_fail():
"""Test whether logging in without passing a username fails"""
"""Test whether logging in without passing a username fails, since the default is an empty string"""
clear_db()
await register_user()
@@ -125,13 +124,13 @@ async def test_login_without_username_should_fail():
json={"password": password},
)
assert response.status_code == 422
assert response.status_code == 401
assert "access_token" not in response.json()
@pytest.mark.asyncio
async def test_login_without_password_should_fail():
"""Test whether logging in without passing a password fails"""
"""Test whether logging in without passing a password fails, since the default is an empty string"""
clear_db()
await register_user()
@@ -141,5 +140,5 @@ async def test_login_without_password_should_fail():
json={"username": username},
)
assert response.status_code == 422
assert response.status_code == 401
assert "access_token" not in response.json()

View File

@@ -3,7 +3,7 @@ import random
import pytest
from src.enums import MinigameEnum
from tests.base import client, password, register_user
from tests.base import client, password, avatar_index, register_user
from tests.config.database import clear_db
@@ -27,7 +27,6 @@ async def test_put_highscore():
response = response.json()
assert response["minigame"] == minigame
assert response["score_value"] == score_value
@@ -51,7 +50,6 @@ async def test_put_lower_highscore_does_not_change_old_value():
response = response.json()
assert response["minigame"] == minigame
assert response["score_value"] == score_value
lower_score_value = score_value - 100
@@ -65,7 +63,6 @@ async def test_put_lower_highscore_does_not_change_old_value():
response = response.json()
assert response["minigame"] == minigame
assert response["score_value"] == score_value
@@ -121,7 +118,7 @@ async def test_get_highscores_without_auth_should_fail():
assert response.status_code == 403
response = client.get(
f"/highscores/{minigame}?mine_only=false&nr_highest={random.randint(1, 50)}",
f"/highscores/{minigame}?mine_only=false&amount={random.randint(1, 50)}",
headers=headers,
)
@@ -146,7 +143,7 @@ async def test_get_highscore_for_nonexisting_minigame_should_fail():
assert response.status_code == 422
response = client.get(
f"/highscores/{fake_minigame}?mine_only=false&nr_highest={random.randint(1, 50)}",
f"/highscores/{fake_minigame}?mine_only=false&amount={random.randint(1, 50)}",
headers=headers,
)
@@ -163,7 +160,7 @@ async def test_get_invalid_number_of_highscores_should_fail():
for minigame in MinigameEnum:
response = client.get(
f"/highscores/{minigame}?nr_highest={random.randint(-100, 0)}",
f"/highscores/{minigame}?amount={random.randint(-100, 0)}",
headers=headers,
)
@@ -231,7 +228,7 @@ async def test_get_highscores_returns_sorted_list_with_correct_length():
assert response.status_code == 200
response = client.get(
f"/highscores/{minigame}?mine_only=false&nr_highest={int(nr_entries)}",
f"/highscores/{minigame}?mine_only=false&amount={int(nr_entries)}",
headers={
"Authorization": f"Bearer {token}",
"Content-Type": "application/json",
@@ -301,7 +298,7 @@ async def test_get_multiple_own_high_scores_of_same_game_should_fail():
for minigame in MinigameEnum:
response = client.get(
f"/highscores/{minigame}?nr_highest={random.randint(2, 20)}",
f"/highscores/{minigame}?amount={random.randint(2, 20)}",
headers=headers,
)

View File

@@ -8,30 +8,6 @@ patched_password = "New password"
patched_avatar_index = 2
@pytest.mark.asyncio
async def test_get_current_user():
"""Test the GET /users endpoint to get info about the current user"""
clear_db()
token = await register_user()
headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
response = client.get("/users", headers=headers)
assert response.status_code == 200
response = response.json()
assert response["username"] == username
assert response["avatar_index"] == avatar_index
@pytest.mark.asyncio
async def test_get_current_user_without_auth():
"""Getting the current user without a token should fail"""
clear_db()
response = client.get("/users", headers={"Content-Type": "application/json"})
assert response.status_code == 403
@pytest.mark.asyncio
async def test_patch_user():
@@ -64,7 +40,7 @@ async def test_patch_user():
headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
response = client.get("/users", headers=headers)
response = client.get("/saveddata", headers=headers)
assert response.status_code == 200
# Correctness of password and username is already asserted by the login
@@ -80,35 +56,38 @@ async def test_patch_user_with_empty_fields():
headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
response = client.patch(
"/users",
json={
"username": patched_username,
"password": patched_password,
"avatar_index": "",
},
headers=headers,
)
assert response.status_code == 422
response = client.patch(
"/users",
json={
"username": patched_username,
"password": "",
"avatar_index": patched_avatar_index,
},
headers=headers,
)
assert response.status_code == 400
response = client.patch(
"/users",
json={
"username": "",
"password": patched_password,
"avatar_index": patched_avatar_index,
"playtime": 0.0
},
headers=headers,
)
assert response.status_code == 400
assert response.status_code == 200
response = client.patch(
"/users",
json={
"username": username,
"password": patched_password,
"avatar_index": -1,
"playtime": 0.0
},
headers=headers,
)
assert response.status_code == 200
response = client.patch(
"/users",
json={
"username": username,
"password": "",
"avatar_index": patched_avatar_index,
"playtime": 0.0
},
headers=headers,
)
assert response.status_code == 200