"""Pytest configuration and fixtures.""" from collections.abc import Generator import pytest from fastapi.testclient import TestClient from sqlalchemy import create_engine from sqlalchemy.orm import Session, sessionmaker from app.core.security import get_password_hash from app.db.session import Base, get_db from app.main import app from app.models.booking import Booking from app.models.space import Space from app.models.user import User # Test database SQLALCHEMY_DATABASE_URL = "sqlite:///./test.db" engine = create_engine( SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False} ) TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) @pytest.fixture def db() -> Generator[Session, None, None]: """Create test database.""" Base.metadata.create_all(bind=engine) db = TestingSessionLocal() try: yield db finally: db.close() Base.metadata.drop_all(bind=engine) @pytest.fixture def db_session(db: Session) -> Session: """Alias for db fixture.""" return db @pytest.fixture def client(db: Session) -> Generator[TestClient, None, None]: """Create test client.""" def override_get_db() -> Generator[Session, None, None]: try: yield db finally: pass app.dependency_overrides[get_db] = override_get_db with TestClient(app) as test_client: yield test_client app.dependency_overrides.clear() @pytest.fixture def test_user(db: Session) -> User: """Create test user.""" user = User( email="test@example.com", full_name="Test User", hashed_password=get_password_hash("testpassword"), role="user", is_active=True, ) db.add(user) db.commit() db.refresh(user) return user @pytest.fixture def test_admin(db: Session) -> User: """Create test admin user.""" admin = User( email="admin@example.com", full_name="Admin User", hashed_password=get_password_hash("adminpassword"), role="admin", is_active=True, ) db.add(admin) db.commit() db.refresh(admin) return admin def get_user_token() -> str: """Get JWT token for test user.""" from app.core.security import create_access_token return create_access_token(subject=1) def get_admin_token() -> str: """Get JWT token for test admin.""" from app.core.security import create_access_token return create_access_token(subject=2) @pytest.fixture def user_token(test_user: User) -> str: """Get token for test user.""" from app.core.security import create_access_token return create_access_token(subject=int(test_user.id)) @pytest.fixture def admin_token(test_admin: User) -> str: """Get token for test admin.""" from app.core.security import create_access_token return create_access_token(subject=int(test_admin.id)) @pytest.fixture def auth_headers(user_token: str) -> dict[str, str]: """Get authorization headers for test user.""" return {"Authorization": f"Bearer {user_token}"} @pytest.fixture def admin_headers(admin_token: str) -> dict[str, str]: """Get authorization headers for test admin.""" return {"Authorization": f"Bearer {admin_token}"} @pytest.fixture def test_space(db: Session) -> Space: """Create test space.""" space = Space( name="Test Conference Room", type="sala", capacity=10, description="Test room for bookings", is_active=True, ) db.add(space) db.commit() db.refresh(space) return space @pytest.fixture def test_booking(db: Session, test_user: User, test_space: Space) -> Booking: """Create test booking.""" from datetime import datetime booking = Booking( user_id=test_user.id, space_id=test_space.id, title="Test Meeting", description="Confidential meeting details", start_datetime=datetime(2024, 3, 15, 10, 0, 0), end_datetime=datetime(2024, 3, 15, 12, 0, 0), status="approved", ) db.add(booking) db.commit() db.refresh(booking) return booking