| """Test configuration and fixtures."""
|
|
|
| import pytest
|
| import pytest_asyncio
|
| from httpx import AsyncClient, ASGITransport
|
| from mac.main import app
|
| from mac.database import engine, Base, async_session
|
| from mac.services.auth_service import create_user
|
|
|
|
|
| import mac.models.user
|
| import mac.models.guardrail
|
| import mac.models.quota
|
| import mac.models.rag
|
| import mac.models.node
|
| import mac.models.model_submission
|
| import mac.models.agent
|
| import mac.models.notebook
|
| import mac.models.notification
|
| import mac.models.attendance
|
| import mac.models.doubt
|
| import mac.models.copy_check
|
|
|
| import mac.models.feature_flag
|
| import mac.models.academic
|
| import mac.models.cluster
|
| import mac.models.file_share
|
| import mac.models.video
|
| import mac.models.system_config
|
|
|
|
|
| @pytest_asyncio.fixture(autouse=True)
|
| async def setup_db():
|
| """Create fresh tables for each test."""
|
|
|
|
|
| await engine.dispose()
|
| async with engine.begin() as conn:
|
| await conn.run_sync(Base.metadata.create_all)
|
| yield
|
| async with engine.begin() as conn:
|
| await conn.run_sync(Base.metadata.drop_all)
|
| await engine.dispose()
|
|
|
|
|
| @pytest_asyncio.fixture
|
| async def client():
|
| """Async test client."""
|
| transport = ASGITransport(app=app)
|
| async with AsyncClient(transport=transport, base_url="http://test") as c:
|
| yield c
|
|
|
|
|
| @pytest_asyncio.fixture
|
| async def test_user():
|
| """Create a test user and return (user, password)."""
|
| async with async_session() as db:
|
| user = await create_user(db, "21CS045", "Test Student", "password123", "CSE", "student")
|
| await db.commit()
|
| return user, "password123"
|
|
|
|
|
| @pytest_asyncio.fixture
|
| async def admin_user():
|
| """Create an admin user."""
|
| async with async_session() as db:
|
| user = await create_user(db, "ADMIN001", "Admin", "admin12345", "CSE", "admin")
|
| await db.commit()
|
| return user, "admin12345"
|
|
|
|
|
| @pytest_asyncio.fixture
|
| async def auth_headers(client, test_user):
|
| """Login and return auth headers."""
|
| user, password = test_user
|
| resp = await client.post("/api/v1/auth/login", json={
|
| "roll_number": user.roll_number,
|
| "password": password,
|
| })
|
| token = resp.json()["access_token"]
|
| return {"Authorization": f"Bearer {token}"}
|
|
|
|
|
| @pytest_asyncio.fixture
|
| async def admin_headers(client, admin_user):
|
| """Login as admin and return auth headers."""
|
| user, password = admin_user
|
| resp = await client.post("/api/v1/auth/login", json={
|
| "roll_number": user.roll_number,
|
| "password": password,
|
| })
|
| token = resp.json()["access_token"]
|
| return {"Authorization": f"Bearer {token}"}
|
|
|