import os import tempfile from collections.abc import AsyncIterator from pathlib import Path import pytest # Environnement de test AVANT tout import de l'app _tmp = Path(tempfile.mkdtemp(prefix="ohm-test-")) os.environ["OHM_DATA_DIR"] = str(_tmp) os.environ["OHM_DOWNLOAD_DIR"] = str(_tmp / "downloads") os.environ["OHM_DATABASE_PATH"] = str(_tmp / "test.db") os.environ["OHM_SECRET_KEY"] = "test-secret-key-with-32-bytes-minimum!" from app.config import get_settings from app.db import db @pytest.fixture(scope="session") def anyio_backend() -> str: return "asyncio" @pytest.fixture(autouse=True) async def database() -> AsyncIterator[None]: """DB fraîche par test.""" get_settings.cache_clear() await db.connect() for table in ( "users", "refresh_tokens", "downloads", "metadata_cache", "settings", "favorites", "watch_progress", ): await db.execute(f"DELETE FROM {table}") yield await db.close() # ---------------------------------------------------------------- client HTTP partagé import pytest from httpx import ASGITransport, AsyncClient from app.main import app @pytest.fixture async def client() -> AsyncClient: transport = ASGITransport(app=app) async with AsyncClient(transport=transport, base_url="http://test") as c: yield c @pytest.fixture async def admin_cookies(client: AsyncClient): """Premier compte créé → administrateur, cookies de session.""" r = await client.post("/auth/register", data={"username": "admin", "password": "secret123"}) assert r.status_code == 303 return r.cookies