galaxis-po/backend/tests/conftest.py

109 lines
2.8 KiB
Python
Raw Normal View History

"""
Pytest configuration and fixtures for E2E tests.
"""
import os
import pytest
from typing import Generator
from fastapi.testclient import TestClient
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, Session
from sqlalchemy.pool import StaticPool
from app.main import app
from app.core.database import Base, get_db
from app.models.user import User
from app.core.auth import get_password_hash, create_access_token
# Use in-memory SQLite for tests
SQLALCHEMY_DATABASE_URL = "sqlite:///:memory:"
engine = create_engine(
SQLALCHEMY_DATABASE_URL,
connect_args={"check_same_thread": False},
poolclass=StaticPool,
)
TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
def override_get_db():
"""Override database dependency for tests."""
try:
db = TestingSessionLocal()
yield db
finally:
db.close()
@pytest.fixture(scope="function")
def db() -> Generator[Session, None, None]:
"""Create a fresh database for each test."""
Base.metadata.create_all(bind=engine)
db = TestingSessionLocal()
try:
yield db
finally:
db.close()
Base.metadata.drop_all(bind=engine)
@pytest.fixture(scope="function")
def client(db: Session) -> Generator[TestClient, None, None]:
"""Create a test client with database override."""
app.dependency_overrides[get_db] = override_get_db
# Create tables
Base.metadata.create_all(bind=engine)
with TestClient(app) as test_client:
yield test_client
# Cleanup
Base.metadata.drop_all(bind=engine)
app.dependency_overrides.clear()
@pytest.fixture(scope="function")
def test_user(db: Session) -> User:
"""Create a test user."""
user = User(
username="testuser",
email="test@example.com",
hashed_password=get_password_hash("testpassword"),
is_admin=False,
)
db.add(user)
db.commit()
db.refresh(user)
return user
@pytest.fixture(scope="function")
def admin_user(db: Session) -> User:
"""Create an admin test user."""
user = User(
username="admin",
email="admin@example.com",
hashed_password=get_password_hash("adminpassword"),
is_admin=True,
)
db.add(user)
db.commit()
db.refresh(user)
return user
@pytest.fixture(scope="function")
def auth_headers(test_user: User) -> dict:
"""Create authorization headers for test user."""
token = create_access_token(data={"sub": test_user.username})
return {"Authorization": f"Bearer {token}"}
@pytest.fixture(scope="function")
def admin_auth_headers(admin_user: User) -> dict:
"""Create authorization headers for admin user."""
token = create_access_token(data={"sub": admin_user.username})
return {"Authorization": f"Bearer {token}"}