Refactoring: auth tests pass

This commit is contained in:
lvrossem
2023-04-16 07:15:03 -06:00
parent d38d4d5c16
commit 0bf764a0f4
14 changed files with 116 additions and 93 deletions

View File

@@ -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