Finish off backend tests
This commit is contained in:
16
tests/base.py
Normal file
16
tests/base.py
Normal file
@@ -0,0 +1,16 @@
|
||||
import sys
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
sys.path.append("..")
|
||||
|
||||
from src.main import app, get_db
|
||||
from tests.config.database import override_get_db
|
||||
|
||||
app.dependency_overrides[get_db] = override_get_db
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
username = "user1"
|
||||
password = "password"
|
||||
avatar = "lion"
|
||||
@@ -1,21 +1,10 @@
|
||||
import sys
|
||||
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
sys.path.append("..")
|
||||
|
||||
from src.main import app, get_db
|
||||
from tests.base import avatar, client, password, username
|
||||
from tests.config.database import clear_db, override_get_db
|
||||
|
||||
app.dependency_overrides[get_db] = override_get_db
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
username = "user1"
|
||||
password = "password"
|
||||
avatar = "lion"
|
||||
|
||||
|
||||
async def register_user():
|
||||
response = client.post(
|
||||
|
||||
@@ -1,23 +1,13 @@
|
||||
import random
|
||||
import sys
|
||||
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
sys.path.append("..")
|
||||
|
||||
from src.enums import CourseEnum
|
||||
from src.main import app, get_db
|
||||
from tests.base import avatar, client, password, username
|
||||
from tests.config.database import clear_db, override_get_db
|
||||
|
||||
app.dependency_overrides[get_db] = override_get_db
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
username = "user1"
|
||||
password = "password"
|
||||
avatar = "lion"
|
||||
|
||||
|
||||
async def register_user():
|
||||
response = client.post(
|
||||
@@ -67,6 +57,19 @@ async def test_get_all_returns_all():
|
||||
assert {"progress_value": 0.0, "course": course} in response
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_course_progress_value_without_auth_should_fail():
|
||||
"""Test whether fetching a course progress value without authentication fails"""
|
||||
clear_db()
|
||||
|
||||
headers = {"Content-Type": "application/json"}
|
||||
|
||||
for course in CourseEnum:
|
||||
response = client.get(f"/courseprogress/{course}", headers=headers)
|
||||
|
||||
assert response.status_code == 403
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_nonexisting_course_should_fail():
|
||||
"""Test whether fetching the progress of a nonexisting course fails"""
|
||||
@@ -178,3 +181,20 @@ async def test_patch_course_with_invalid_value_should_fail():
|
||||
)
|
||||
|
||||
assert response.status_code == 400
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_patch_course_progress_value_without_auth_should_fail():
|
||||
"""Test whether updating a course progress value without authentication fails"""
|
||||
clear_db()
|
||||
|
||||
headers = {"Content-Type": "application/json"}
|
||||
|
||||
for course in CourseEnum:
|
||||
response = client.patch(
|
||||
f"/courseprogress/{course}",
|
||||
headers=headers,
|
||||
json={"progress_value": random.uniform(0, 1)},
|
||||
)
|
||||
|
||||
assert response.status_code == 403
|
||||
|
||||
@@ -1,21 +1,13 @@
|
||||
import sys
|
||||
import random
|
||||
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
sys.path.append("..")
|
||||
|
||||
from src.enums import MinigameEnum
|
||||
from src.main import app, get_db
|
||||
from tests.base import avatar, client, password, username
|
||||
from tests.config.database import clear_db, override_get_db
|
||||
|
||||
app.dependency_overrides[get_db] = override_get_db
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
username = "user1"
|
||||
password = "password"
|
||||
avatar = "lion"
|
||||
|
||||
|
||||
async def register_user():
|
||||
response = client.post(
|
||||
@@ -30,6 +22,218 @@ async def register_user():
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_post_highscore():
|
||||
"""Test whether posting a new high score succeeds"""
|
||||
async def test_put_highscore():
|
||||
"""Test whether putting a new high score succeeds"""
|
||||
clear_db()
|
||||
token = await register_user()
|
||||
|
||||
headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
|
||||
|
||||
for minigame in MinigameEnum:
|
||||
score_value = random.random()
|
||||
response = client.put(
|
||||
f"/highscores/{minigame}",
|
||||
headers=headers,
|
||||
json={"score_value": score_value},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
|
||||
response = response.json()
|
||||
|
||||
assert response["minigame"] == minigame
|
||||
assert response["score_value"] == score_value
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_put_lower_highscore_does_not_change_old_value():
|
||||
"""Test whether putting a new high score lower than the current one doesn't change the old one"""
|
||||
clear_db()
|
||||
token = await register_user()
|
||||
|
||||
headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
|
||||
|
||||
for minigame in MinigameEnum:
|
||||
score_value = random.random()
|
||||
response = client.put(
|
||||
f"/highscores/{minigame}",
|
||||
headers=headers,
|
||||
json={"score_value": score_value},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
|
||||
response = response.json()
|
||||
|
||||
assert response["minigame"] == minigame
|
||||
assert response["score_value"] == score_value
|
||||
|
||||
lower_score_value = score_value - 100
|
||||
response = client.put(
|
||||
f"/highscores/{minigame}",
|
||||
headers=headers,
|
||||
json={"score_value": lower_score_value},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
|
||||
response = response.json()
|
||||
|
||||
assert response["minigame"] == minigame
|
||||
assert response["score_value"] == score_value
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_put_highscore_for_nonexisting_minigame_should_fail():
|
||||
"""Test whether putting a new high score for a nonexisting minigame fails"""
|
||||
clear_db()
|
||||
token = await register_user()
|
||||
|
||||
fake_minigame = "FakeGame"
|
||||
|
||||
headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
|
||||
|
||||
response = client.put(
|
||||
f"/highscores/{fake_minigame}",
|
||||
headers=headers,
|
||||
json={"score_value": random.random()},
|
||||
)
|
||||
|
||||
assert response.status_code == 422
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_put_highscores_without_auth_should_fail():
|
||||
"""Test whether putting high scores without authentication fails"""
|
||||
clear_db()
|
||||
|
||||
headers = {"Content-Type": "application/json"}
|
||||
|
||||
for minigame in MinigameEnum:
|
||||
response = client.put(
|
||||
f"/highscores/{minigame}",
|
||||
headers=headers,
|
||||
json={"score_value": random.random()},
|
||||
)
|
||||
|
||||
assert response.status_code == 403
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_highscores_without_auth_should_fail():
|
||||
"""Test whether fetching high scores without authentication fails"""
|
||||
clear_db()
|
||||
|
||||
headers = {"Content-Type": "application/json"}
|
||||
|
||||
for minigame in MinigameEnum:
|
||||
response = client.get(
|
||||
f"/highscores/{minigame}",
|
||||
headers=headers,
|
||||
)
|
||||
|
||||
assert response.status_code == 403
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_highscore_for_nonexisting_minigame_should_fail():
|
||||
"""Test whether fetching a new high score for a nonexisting minigame fails"""
|
||||
clear_db()
|
||||
token = await register_user()
|
||||
|
||||
fake_minigame = "FakeGame"
|
||||
|
||||
headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
|
||||
|
||||
response = client.get(
|
||||
f"/highscores/{fake_minigame}",
|
||||
headers=headers,
|
||||
)
|
||||
|
||||
assert response.status_code == 422
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_invalid_number_of_highscores_should_fail():
|
||||
"""Test whether getting a numbe rof high scores lower than 1 fails"""
|
||||
clear_db()
|
||||
token = await register_user()
|
||||
|
||||
headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
|
||||
|
||||
for minigame in MinigameEnum:
|
||||
response = client.get(
|
||||
f"/highscores/{minigame}?nr_highest={random.randint(-100, 0)}",
|
||||
headers=headers,
|
||||
)
|
||||
|
||||
assert response.status_code == 400
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_highscores_should_work_with_default_value():
|
||||
"""Test whether fetching high scores without passing an explicit amount still succeeds"""
|
||||
clear_db()
|
||||
token = await register_user()
|
||||
|
||||
headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
|
||||
|
||||
for minigame in MinigameEnum:
|
||||
response = client.get(
|
||||
f"/highscores/{minigame}",
|
||||
headers=headers,
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_highscores_returns_sorted_list_with_correct_length():
|
||||
clear_db()
|
||||
token = await register_user()
|
||||
|
||||
headers = {"Content-Type": "application/json"}
|
||||
|
||||
for minigame in MinigameEnum:
|
||||
clear_db()
|
||||
nr_entries = random.randint(5, 50)
|
||||
|
||||
users_score_tuples = [
|
||||
(f"user{i + 1}", random.random()) for i in range(nr_entries)
|
||||
]
|
||||
|
||||
for user, score in users_score_tuples:
|
||||
response = client.post(
|
||||
"/register",
|
||||
headers=headers,
|
||||
json={"username": user, "password": password, "avatar": avatar},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
|
||||
token = response.json()["access_token"]
|
||||
|
||||
response = client.put(
|
||||
f"/highscores/{minigame}?nr_highest={random.randint(1, nr_entries)}",
|
||||
headers={
|
||||
"Authorization": f"Bearer {token}",
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
json={"score_value": score},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
|
||||
response = client.get(
|
||||
f"/highscores/{minigame}",
|
||||
headers={
|
||||
"Authorization": f"Bearer {token}",
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
response = response.json()
|
||||
|
||||
for i in range(1, len(response)):
|
||||
assert response[i]["score_value"] <= response[i - 1]["score_value"]
|
||||
|
||||
@@ -1,21 +1,10 @@
|
||||
import sys
|
||||
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
sys.path.append("..")
|
||||
|
||||
from src.main import app, get_db
|
||||
from tests.base import avatar, client, password, username
|
||||
from tests.config.database import clear_db, override_get_db
|
||||
|
||||
app.dependency_overrides[get_db] = override_get_db
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
username = "user1"
|
||||
password = "password"
|
||||
avatar = "lion"
|
||||
|
||||
patched_username = "New name"
|
||||
patched_password = "New password"
|
||||
patched_avatar = "New avatar"
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from main import app
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
|
||||
def test_add_user():
|
||||
response = client.post(
|
||||
"/users",
|
||||
headers={"Content-Type": "application/json"},
|
||||
json={"username": "Lukas", "password": "mettn"},
|
||||
)
|
||||
assert response.status_code == 200
|
||||
Reference in New Issue
Block a user