|
| 1 | +""" |
| 2 | +Assignment-server-specific test fixtures. |
| 3 | +
|
| 4 | +All clients use httpx.AsyncClient + ASGITransport so the server runs in the |
| 5 | +same asyncio event loop as the test session, avoiding asyncpg "attached to a |
| 6 | +different loop" errors. |
| 7 | +
|
| 8 | +``auth_assignment_client`` — authenticated as testuser1 (student). |
| 9 | +``instructor_user`` — a seeded instructor user with CourseInstructor row. |
| 10 | +``auth_instructor_client`` — auth_manager patched to return the instructor user. |
| 11 | +""" |
| 12 | + |
| 13 | +import datetime |
| 14 | +import pytest_asyncio |
| 15 | +import httpx |
| 16 | +from unittest.mock import AsyncMock, patch |
| 17 | + |
| 18 | + |
| 19 | +@pytest_asyncio.fixture(scope="session") |
| 20 | +async def auth_assignment_client(student_user): |
| 21 | + """Async HTTP client for the assignment server authenticated as testuser1.""" |
| 22 | + from rsptx.assignment_server_api.core import app |
| 23 | + from rsptx.auth.session import auth_manager |
| 24 | + |
| 25 | + app.dependency_overrides[auth_manager] = lambda: student_user |
| 26 | + transport = httpx.ASGITransport(app=app) |
| 27 | + async with httpx.AsyncClient(transport=transport, base_url="http://test") as client: |
| 28 | + yield client |
| 29 | + app.dependency_overrides.pop(auth_manager, None) |
| 30 | + |
| 31 | + |
| 32 | +@pytest_asyncio.fixture(scope="session") |
| 33 | +async def instructor_user(init_test_db): |
| 34 | + """ |
| 35 | + Create a test instructor user assigned to test_course_1, with a |
| 36 | + CourseInstructor row so that is_instructor() returns True for real. |
| 37 | + """ |
| 38 | + from rsptx.db.crud import ( |
| 39 | + create_user, |
| 40 | + fetch_user, |
| 41 | + fetch_course, |
| 42 | + create_course_instructor, |
| 43 | + ) |
| 44 | + from rsptx.db.models import AuthUserValidator |
| 45 | + |
| 46 | + course = await fetch_course("test_course_1") |
| 47 | + |
| 48 | + existing = await fetch_user("test_instructor") |
| 49 | + if existing: |
| 50 | + return existing |
| 51 | + |
| 52 | + user = await create_user( |
| 53 | + AuthUserValidator( |
| 54 | + username="test_instructor", |
| 55 | + first_name="Test", |
| 56 | + last_name="Instructor", |
| 57 | + password="xxx", |
| 58 | + email="test_instructor@example.com", |
| 59 | + course_name="test_course_1", |
| 60 | + course_id=course.id, |
| 61 | + donated=True, |
| 62 | + active=True, |
| 63 | + accept_tcp=True, |
| 64 | + created_on=datetime.datetime(2020, 1, 1), |
| 65 | + modified_on=datetime.datetime(2020, 1, 1), |
| 66 | + registration_key="", |
| 67 | + registration_id="", |
| 68 | + reset_password_key="", |
| 69 | + ) |
| 70 | + ) |
| 71 | + await create_course_instructor(course.id, user.id) |
| 72 | + return user |
| 73 | + |
| 74 | + |
| 75 | +@pytest_asyncio.fixture(scope="session") |
| 76 | +async def auth_instructor_client(instructor_user): |
| 77 | + """ |
| 78 | + Async HTTP client for the assignment server authenticated as test_instructor. |
| 79 | +
|
| 80 | + Some routes use @instructor_role_required() (which calls auth_manager() directly |
| 81 | + inside the decorator, bypassing FastAPI DI), while others use Depends(auth_manager). |
| 82 | + We cover both by: |
| 83 | + 1. Patching rsptx.endpoint_validators.core.auth_manager for the decorator path. |
| 84 | + 2. Setting app.dependency_overrides[auth_manager] for the Depends() path. |
| 85 | + """ |
| 86 | + from rsptx.assignment_server_api.core import app |
| 87 | + from rsptx.auth.session import auth_manager |
| 88 | + |
| 89 | + mock_auth = AsyncMock(return_value=instructor_user) |
| 90 | + app.dependency_overrides[auth_manager] = lambda: instructor_user |
| 91 | + transport = httpx.ASGITransport(app=app) |
| 92 | + with patch("rsptx.endpoint_validators.core.auth_manager", mock_auth): |
| 93 | + async with httpx.AsyncClient(transport=transport, base_url="http://test") as client: |
| 94 | + yield client |
| 95 | + app.dependency_overrides.pop(auth_manager, None) |
0 commit comments