Files
openrouter-watcher/tests/conftest.py
Luca Sacchi Ricciardi abf7e7a532 feat(api-keys): T24-T27 implement API keys CRUD endpoints
- T24: POST /api/keys with encryption and limit validation
- T25: GET /api/keys with pagination and sorting
- T26: PUT /api/keys/{id} for partial updates
- T27: DELETE /api/keys/{id} with cascade
- Add ownership verification (403 for unauthorized access)
- API key encryption with AES-256 before storage
- Never expose API key value in responses
- 100% coverage on api_keys router (25 tests)

Refs: T24 T25 T26 T27
2026-04-07 14:41:53 +02:00

115 lines
3.3 KiB
Python

"""Pytest configuration and fixtures.
This module contains shared fixtures and configuration for all tests.
"""
import sys
import os
import pytest
import pytest_asyncio
# Add src to path for importing in tests
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'src'))
# Markers for test organization
pytest_plugins = ['pytest_asyncio']
def pytest_configure(config):
"""Configure pytest with custom markers."""
config.addinivalue_line("markers", "unit: Unit tests (no external dependencies)")
config.addinivalue_line("markers", "integration: Integration tests (with mocked dependencies)")
config.addinivalue_line("markers", "e2e: End-to-end tests (full workflow)")
config.addinivalue_line("markers", "slow: Slow tests (skip in quick mode)")
@pytest.fixture(scope='session')
def project_root():
"""Return the project root directory."""
return os.path.dirname(os.path.dirname(__file__))
@pytest.fixture(scope='session')
def src_path(project_root):
"""Return the src directory path."""
return os.path.join(project_root, 'src')
@pytest.fixture
def temp_dir(tmp_path):
"""Provide a temporary directory for tests."""
return tmp_path
@pytest.fixture
def mock_env_vars(monkeypatch):
"""Set up mock environment variables for testing."""
monkeypatch.setenv('SECRET_KEY', 'test-secret-key-min-32-characters-long')
monkeypatch.setenv('ENCRYPTION_KEY', 'test-32-byte-encryption-key!!')
monkeypatch.setenv('DATABASE_URL', 'sqlite:///./test.db')
monkeypatch.setenv('DEBUG', 'true')
monkeypatch.setenv('LOG_LEVEL', 'DEBUG')
@pytest.fixture
def mock_db():
"""Create a mock database session for unit tests."""
from unittest.mock import MagicMock
return MagicMock()
@pytest.fixture
def mock_user():
"""Create a mock authenticated user for testing."""
from unittest.mock import MagicMock
user = MagicMock()
user.id = 1
user.email = "test@example.com"
user.is_active = True
return user
@pytest.fixture
def mock_encryption_service():
"""Create a mock encryption service for testing."""
from unittest.mock import MagicMock
mock = MagicMock()
mock.encrypt.return_value = "encrypted_key_value"
mock.decrypt.return_value = "sk-or-v1-decrypted"
return mock
@pytest.fixture
def client():
"""Create a test client with fresh database."""
from fastapi.testclient import TestClient
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.pool import StaticPool
from openrouter_monitor.database import Base, get_db
from openrouter_monitor.main import app
# Setup in-memory test database
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 get_db dependency for testing."""
try:
db = TestingSessionLocal()
yield db
finally:
db.close()
app.dependency_overrides[get_db] = override_get_db
Base.metadata.create_all(bind=engine)
with TestClient(app) as c:
yield c
Base.metadata.drop_all(bind=engine)