import pytest import asyncio from httpx import AsyncClient, ASGITransport from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker, AsyncSession from app.main import app from app.core.database import Base, get_db from app.core.security import get_password_hash from app.models.user import User, UserRole TEST_DB_URL = "postgresql+asyncpg://postgres:postgres@localhost:5432/conai_test" test_engine = create_async_engine(TEST_DB_URL, echo=False) TestSessionLocal = async_sessionmaker(test_engine, class_=AsyncSession, expire_on_commit=False) @pytest.fixture(scope="session") def event_loop(): loop = asyncio.get_event_loop_policy().new_event_loop() yield loop loop.close() @pytest.fixture(scope="session") async def db_setup(): async with test_engine.begin() as conn: await conn.run_sync(Base.metadata.create_all) yield async with test_engine.begin() as conn: await conn.run_sync(Base.metadata.drop_all) @pytest.fixture async def db(db_setup): async with TestSessionLocal() as session: yield session await session.rollback() @pytest.fixture async def client(db): async def override_get_db(): yield db app.dependency_overrides[get_db] = override_get_db async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as ac: yield ac app.dependency_overrides.clear() @pytest.fixture async def test_user(db): user = User( email="test@conai.app", hashed_password=get_password_hash("testpass123"), name="테스트 현장소장", role=UserRole.SITE_MANAGER, ) db.add(user) await db.commit() await db.refresh(user) return user @pytest.fixture async def auth_headers(client, test_user): resp = await client.post("/api/v1/auth/login", data={"username": "test@conai.app", "password": "testpass123"}) token = resp.json()["access_token"] return {"Authorization": f"Bearer {token}"}