Files
pokedexter/backend/tests/conftest.py
T

117 lines
3.1 KiB
Python
Raw Normal View History

2026-07-11 02:10:27 +02:00
"""Test fixtures — async database and HTTP client."""
import pytest
import pytest_asyncio
from sqlalchemy.ext.asyncio import (
create_async_engine,
async_sessionmaker,
)
2026-07-11 02:10:27 +02:00
from httpx import ASGITransport, AsyncClient
from app.database import Base, get_db
from app.main import app
from app.auth import hash_password
from app.models import User
import os
DATABASE_URL = os.getenv(
"DATABASE_URL",
"postgresql+asyncpg://pokedexter:pokedexter_secret@localhost/pokedexter",
)
@pytest_asyncio.fixture(scope="session")
async def engine():
"""Create a fresh database for testing."""
e = create_async_engine(DATABASE_URL, echo=False)
async with e.begin() as conn:
await conn.run_sync(Base.metadata.drop_all)
await conn.run_sync(Base.metadata.create_all)
yield e
async with e.begin() as conn:
await conn.run_sync(Base.metadata.drop_all)
await e.dispose()
@pytest_asyncio.fixture
async def session(engine):
"""Per-test database session med transaction rollback.
Hver test får sin egen connection med en ydre transaction.
commit() inde i testen opretter en savepoint (via create_savepoint),
så al data rulles tilbage efter testen — ingen krydskontaminering.
"""
conn = await engine.connect()
transaction = await conn.begin()
maker = async_sessionmaker(
bind=conn,
expire_on_commit=False,
join_transaction_mode="create_savepoint",
)
2026-07-11 02:10:27 +02:00
async with maker() as s:
yield s
await transaction.rollback()
await conn.close()
2026-07-11 02:10:27 +02:00
@pytest_asyncio.fixture
async def client(session):
"""HTTP client with injected test DB session."""
async def override_get_db():
yield session
app.dependency_overrides[get_db] = override_get_db
transport = ASGITransport(app=app)
async with AsyncClient(transport=transport, base_url="http://test") as ac:
yield ac
app.dependency_overrides.clear()
@pytest_asyncio.fixture
async def admin_user(session):
"""Insert admin user into test DB (rulles tilbage af session-fixture)."""
2026-07-11 02:10:27 +02:00
u = User(
username="admin",
password_hash=hash_password("admin123"),
role="admin",
)
session.add(u)
await session.commit()
return u
@pytest_asyncio.fixture
async def normal_user(session):
"""Insert normal user into test DB (rulles tilbage af session-fixture)."""
2026-07-11 02:10:27 +02:00
u = User(
username="testuser",
password_hash=hash_password("test123"),
role="user",
)
session.add(u)
await session.commit()
return u
@pytest_asyncio.fixture
async def admin_token(client, admin_user):
"""Log in as admin and return the JWT token."""
resp = await client.post("/api/login", json={
"username": "admin",
"password": "admin123",
})
assert resp.status_code == 200
return resp.cookies.get("token")
@pytest_asyncio.fixture
async def user_token(client, normal_user):
"""Log in as normal user and return the JWT token."""
resp = await client.post("/api/login", json={
"username": "testuser",
"password": "test123",
})
assert resp.status_code == 200
return resp.cookies.get("token")