"""Pytest configuration.""" import pytest import sys from pathlib import Path from typing import Generator from fastapi.testclient import TestClient # Add parent directory to path sys.path.insert(0, str(Path(__file__).parent.parent)) @pytest.fixture def client(): """Create test client.""" from app.main import app from app.core.database import engine, Base, SessionLocal # Create tables Base.metadata.create_all(bind=engine) yield TestClient(app) # Drop tables Base.metadata.drop_all(bind=engine) @pytest.fixture def db(): """Create test database session.""" from app.core.database import SessionLocal db = SessionLocal() yield db db.close() @pytest.fixture def sample_user(db): """Create sample user for testing.""" from app.models.user import User from app.core.security import get_password_hash user = User( email="test@example.com", password_hash=get_password_hash("password123"), first_name="Test", last_name="User", is_active=True ) db.add(user) db.commit() db.refresh(user) return user @pytest.fixture def auth_headers(client, sample_user): """Get auth headers for testing.""" from app.core.security import create_access_token token = create_access_token(data={"sub": str(sample_user.id)}) return {"Authorization": f"Bearer {token}"} @pytest.fixture def sample_ticket(db, sample_user): """Create sample ticket for testing.""" from app.models.ticket import Ticket from app.models.priority import Priority from app.models.status import Status from app.models.category import Category # Create sample data category = Category(name="Hardware") priority = Priority(name="Medium", level=2) status = Status(name="Open", is_final=False) db.add_all([category, priority, status]) db.commit() db.refresh(category) db.refresh(priority) db.refresh(status) ticket = Ticket( subject="Test Ticket", description="This is a test ticket", category_id=category.id, priority_id=priority.id, status_id=status.id, source="web", opened_by=sample_user.id ) db.add(ticket) db.commit() db.refresh(ticket) return ticket @pytest.fixture def sample_admin_user(db): """Create sample admin user for testing.""" from app.models.user import User from app.models.role import Role from app.models.extra import UserRole from app.core.security import get_password_hash # Create admin role admin_role = Role(name="admin") db.add(admin_role) db.commit() db.refresh(admin_role) user = User( email="admin@example.com", password_hash=get_password_hash("admin123"), first_name="Admin", last_name="User", is_active=True ) db.add(user) db.commit() db.refresh(user) # Assign admin role user_role = UserRole(user_id=user.id, role_id=admin_role.id) db.add(user_role) db.commit() return user @pytest.fixture def sample_role(db): """Create sample role for testing.""" from app.models.role import Role role = Role(name="test_role", description="Test role description") db.add(role) db.commit() db.refresh(role) return role @pytest.fixture def sample_status(db): """Create sample status for testing.""" from app.models.status import Status status = Status(name="Open", is_final=False) db.add(status) db.commit() db.refresh(status) return status @pytest.fixture def sample_priority(db): """Create sample priority for testing.""" from app.models.priority import Priority priority = Priority(name="Medium", level=2, default_response_time_minutes=60) db.add(priority) db.commit() db.refresh(priority) return priority @pytest.fixture def sample_category(db): """Create sample category for testing.""" from app.models.category import Category category = Category(name="Hardware") db.add(category) db.commit() db.refresh(category) return category