Refactoring: auth tests pass
This commit is contained in:
@@ -13,4 +13,15 @@ client = TestClient(app)
|
||||
|
||||
username = "user1"
|
||||
password = "password"
|
||||
avatar = "lion"
|
||||
avatar_index = 1
|
||||
|
||||
async def register_user():
|
||||
response = client.post(
|
||||
"/register",
|
||||
headers={"Content-Type": "application/json"},
|
||||
json={"username": username, "password": password, "avatar_index": avatar_index},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
|
||||
return response.json()["access_token"]
|
||||
|
||||
@@ -2,22 +2,10 @@ import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from src.main import app, get_db
|
||||
from tests.base import avatar, client, password, username
|
||||
from tests.base import avatar_index, client, password, username, register_user
|
||||
from tests.config.database import clear_db, override_get_db
|
||||
|
||||
|
||||
async def register_user():
|
||||
response = client.post(
|
||||
"/register",
|
||||
headers={"Content-Type": "application/json"},
|
||||
json={"username": username, "password": password, "avatar": avatar},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
|
||||
return response.json()["access_token"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_register():
|
||||
"""Test the register endpoint"""
|
||||
@@ -26,7 +14,7 @@ async def test_register():
|
||||
response = client.post(
|
||||
"/register",
|
||||
headers={"Content-Type": "application/json"},
|
||||
json={"username": username, "password": password, "avatar": avatar},
|
||||
json={"username": username, "password": password, "avatar_index": avatar_index},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -42,7 +30,7 @@ async def test_register_duplicate_name_should_fail():
|
||||
response = client.post(
|
||||
"/register",
|
||||
headers={"Content-Type": "application/json"},
|
||||
json={"username": username, "password": password, "avatar": avatar},
|
||||
json={"username": username, "password": password, "avatar_index": avatar_index},
|
||||
)
|
||||
|
||||
assert response.status_code == 400
|
||||
@@ -57,7 +45,7 @@ async def test_register_without_username_should_fail():
|
||||
response = client.post(
|
||||
"/register",
|
||||
headers={"Content-Type": "application/json"},
|
||||
json={"password": password, "avatar": avatar},
|
||||
json={"password": password, "avatar_index": avatar_index},
|
||||
)
|
||||
|
||||
assert response.status_code == 422
|
||||
@@ -72,7 +60,7 @@ async def test_register_without_password_should_fail():
|
||||
response = client.post(
|
||||
"/register",
|
||||
headers={"Content-Type": "application/json"},
|
||||
json={"username": username, "avatar": avatar},
|
||||
json={"username": username, "avatar_index": avatar_index},
|
||||
)
|
||||
|
||||
assert response.status_code == 422
|
||||
|
||||
@@ -5,22 +5,10 @@ from fastapi.testclient import TestClient
|
||||
|
||||
from src.enums import CourseEnum
|
||||
from src.main import app, get_db
|
||||
from tests.base import avatar, client, password, username
|
||||
from tests.base import client, register_user
|
||||
from tests.config.database import clear_db, override_get_db
|
||||
|
||||
|
||||
async def register_user():
|
||||
response = client.post(
|
||||
"/register",
|
||||
headers={"Content-Type": "application/json"},
|
||||
json={"username": username, "password": password, "avatar": avatar},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
|
||||
return response.json()["access_token"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_register_creates_progress_of_zero():
|
||||
"""Test whether registering a new user initializes all progress values to 0.0"""
|
||||
@@ -36,7 +24,7 @@ async def test_register_creates_progress_of_zero():
|
||||
|
||||
response = response.json()[0]
|
||||
|
||||
assert response["progress_value"] == 0.0
|
||||
assert response["progress"] == 0.0
|
||||
assert response["course"] == course
|
||||
|
||||
|
||||
@@ -54,11 +42,11 @@ async def test_get_all_returns_all():
|
||||
|
||||
for course in CourseEnum:
|
||||
if course != CourseEnum.All:
|
||||
assert {"progress_value": 0.0, "course": course} in response
|
||||
assert {"progress": 0.0, "course": course} in response
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_course_progress_value_without_auth_should_fail():
|
||||
async def test_get_course_progress_without_auth_should_fail():
|
||||
"""Test whether fetching a course progress value without authentication fails"""
|
||||
clear_db()
|
||||
|
||||
@@ -94,16 +82,16 @@ async def test_patch_course_progress():
|
||||
|
||||
for course in CourseEnum:
|
||||
if course != CourseEnum.All:
|
||||
progress_value = random.uniform(0, 1)
|
||||
progress = random.uniform(0, 1)
|
||||
|
||||
response = client.patch(
|
||||
f"/courseprogress/{course}",
|
||||
headers=headers,
|
||||
json={"progress_value": progress_value},
|
||||
json={"progress": progress},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.json()[0]["progress_value"] == progress_value
|
||||
assert response.json()[0]["progress"] == progress
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -114,12 +102,12 @@ async def test_patch_all_should_patch_all_courses():
|
||||
|
||||
headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
|
||||
|
||||
progress_value = random.uniform(0, 1)
|
||||
progress = random.uniform(0, 1)
|
||||
|
||||
response = client.patch(
|
||||
"/courseprogress/All",
|
||||
headers=headers,
|
||||
json={"progress_value": progress_value},
|
||||
json={"progress": progress},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -131,7 +119,7 @@ async def test_patch_all_should_patch_all_courses():
|
||||
|
||||
for course in CourseEnum:
|
||||
if course != CourseEnum.All:
|
||||
assert {"progress_value": progress_value, "course": course} in response
|
||||
assert {"progress": progress, "course": course} in response
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -144,12 +132,12 @@ async def test_patch_nonexisting_course_should_fail():
|
||||
|
||||
headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
|
||||
|
||||
progress_value = random.uniform(0, 1)
|
||||
progress = random.uniform(0, 1)
|
||||
|
||||
response = client.patch(
|
||||
f"/courseprogress/{fake_course}",
|
||||
headers=headers,
|
||||
json={"progress_value": progress_value},
|
||||
json={"progress": progress},
|
||||
)
|
||||
|
||||
assert response.status_code == 422
|
||||
@@ -163,13 +151,13 @@ async def test_patch_course_with_invalid_value_should_fail():
|
||||
|
||||
headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
|
||||
|
||||
too_high_progress_value = random.uniform(0, 1) + 2
|
||||
too_low_progress_value = random.uniform(0, 1) - 2
|
||||
too_high_progress = random.uniform(0, 1) + 2
|
||||
too_low_progress = random.uniform(0, 1) - 2
|
||||
|
||||
response = client.patch(
|
||||
"/courseprogress/All",
|
||||
headers=headers,
|
||||
json={"progress_value": too_high_progress_value},
|
||||
json={"progress": too_high_progress},
|
||||
)
|
||||
|
||||
assert response.status_code == 400
|
||||
@@ -177,14 +165,14 @@ async def test_patch_course_with_invalid_value_should_fail():
|
||||
response = client.patch(
|
||||
"/courseprogress/All",
|
||||
headers=headers,
|
||||
json={"progress_value": too_low_progress_value},
|
||||
json={"progress": too_low_progress},
|
||||
)
|
||||
|
||||
assert response.status_code == 400
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_patch_course_progress_value_without_auth_should_fail():
|
||||
async def test_patch_course_progress_without_auth_should_fail():
|
||||
"""Test whether updating a course progress value without authentication fails"""
|
||||
clear_db()
|
||||
|
||||
@@ -194,7 +182,7 @@ async def test_patch_course_progress_value_without_auth_should_fail():
|
||||
response = client.patch(
|
||||
f"/courseprogress/{course}",
|
||||
headers=headers,
|
||||
json={"progress_value": random.uniform(0, 1)},
|
||||
json={"progress": random.uniform(0, 1)},
|
||||
)
|
||||
|
||||
assert response.status_code == 403
|
||||
|
||||
@@ -5,22 +5,10 @@ from fastapi.testclient import TestClient
|
||||
|
||||
from src.enums import MinigameEnum
|
||||
from src.main import app, get_db
|
||||
from tests.base import avatar, client, password, username
|
||||
from tests.base import avatar_index, client, password, username, register_user
|
||||
from tests.config.database import clear_db, override_get_db
|
||||
|
||||
|
||||
async def register_user():
|
||||
response = client.post(
|
||||
"/register",
|
||||
headers={"Content-Type": "application/json"},
|
||||
json={"username": username, "password": password, "avatar": avatar},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
|
||||
return response.json()["access_token"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_put_highscore():
|
||||
"""Test whether putting a new high score succeeds"""
|
||||
|
||||
@@ -2,7 +2,7 @@ import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from src.main import app, get_db
|
||||
from tests.base import avatar, client, password, username
|
||||
from tests.base import avatar_index, client, password, username, register_user
|
||||
from tests.config.database import clear_db, override_get_db
|
||||
|
||||
patched_username = "New name"
|
||||
@@ -15,15 +15,7 @@ async def test_get_current_user():
|
||||
"""Test the GET /users endpoint to get info about the current user"""
|
||||
clear_db()
|
||||
|
||||
response = client.post(
|
||||
"/register",
|
||||
headers={"Content-Type": "application/json"},
|
||||
json={"username": username, "password": password, "avatar": avatar},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
|
||||
token = response.json()["access_token"]
|
||||
token = await register_user()
|
||||
headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
|
||||
|
||||
response = client.get("/users", headers=headers)
|
||||
|
||||
Reference in New Issue
Block a user