import random import pytest from src.enums import CourseEnum from tests.base import client, get_headers, register_user from tests.config.database import clear_db @pytest.mark.asyncio async def test_register_should_create_progress_of_zero(): """Test whether registering a new user initializes all progress values to 0.0""" clear_db() token = await register_user() headers = get_headers(token) for course in CourseEnum: if course != CourseEnum.All: response = client.get(f"/courseprogress/{course}", headers=headers) assert response.status_code == 200 response = response.json()[0] assert response["progress"] == 0.0 assert response["course_index"] == course @pytest.mark.asyncio async def test_get_all_sould_return_all(): """Test whether the 'All'-course fetches all course progress values""" clear_db() token = await register_user() headers = get_headers(token) response = client.get("/courseprogress/All", headers=headers) assert response.status_code == 200 response = response.json() for course in CourseEnum: if course != CourseEnum.All: assert {"progress": 0.0, "course_index": course, "completed_learnables": 0, "in_use_learnables": 0, "total_learnables": 0, "learnables": []} in response @pytest.mark.asyncio async def test_get_course_progress_without_auth_should_fail(): """Test whether fetching a course progress value without authentication fails""" clear_db() headers = get_headers() 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""" clear_db() token = await register_user() fake_course = "FakeCourse" headers = get_headers(token) response = client.get(f"/courseprogress/{fake_course}", headers=headers) assert response.status_code == 422 @pytest.mark.asyncio async def test_patch_course_progress_should_succeed(): """Test whether patching the progress value of a course works properly""" clear_db() token = await register_user() headers = get_headers(token) for course in CourseEnum: if course != CourseEnum.All: progress = random.uniform(0, 1) response = client.patch( f"/courseprogress/{course}", headers=headers, json={"progress": progress}, ) assert response.status_code == 200 assert response.json()[0]["progress"] == progress @pytest.mark.asyncio async def test_patch_all_should_patch_all_courses(): """Test whether patching the 'All'-course updates all progress values""" clear_db() token = await register_user() headers = get_headers(token) progress = random.uniform(0, 1) response = client.patch( "/courseprogress/All", headers=headers, json={"progress": progress}, ) assert response.status_code == 200 response = client.get("/courseprogress/All", headers=headers) assert response.status_code == 200 response = response.json() for course in CourseEnum: if course != CourseEnum.All: assert {"progress": 0.0, "course_index": course, "completed_learnables": 0, "in_use_learnables": 0, "total_learnables": 0, "learnables": []} in response @pytest.mark.asyncio async def test_patch_nonexisting_course_should_fail(): """Test whether patching a nonexisting course fails""" clear_db() token = await register_user() fake_course = "FakeCourse" headers = get_headers(token) progress = random.uniform(0, 1) response = client.patch( f"/courseprogress/{fake_course}", headers=headers, json={"progress": progress}, ) assert response.status_code == 422 @pytest.mark.asyncio async def test_patch_course_with_invalid_value_should_fail(): """Test whether patching a course progress value with an invalid value fails""" clear_db() token = await register_user() headers = get_headers(token) 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": too_high_progress}, ) assert response.status_code == 400 response = client.patch( "/courseprogress/All", headers=headers, json={"progress": too_low_progress}, ) assert response.status_code == 400 @pytest.mark.asyncio async def test_patch_course_progress_without_auth_should_fail(): """Test whether updating a course progress value without authentication fails""" clear_db() headers = get_headers() for course in CourseEnum: response = client.patch( f"/courseprogress/{course}", headers=headers, json={"progress": random.uniform(0, 1)}, ) assert response.status_code == 403