openrag/tests/fixtures/app_fixtures.py
Edwin Jose 3881c50ad5 Add comprehensive test suite and Makefile targets
Introduces a full test suite under the tests/ directory, including API, service, connector, and utility tests, along with fixtures and documentation. Expands Makefile with granular test commands for unit, integration, API, service, connector, coverage, and quick tests. Adds configuration files for pytest and coverage reporting, and provides a quickstart guide for testing workflow.
2025-10-07 04:41:52 -04:00

53 lines
1.5 KiB
Python

"""
Application-level fixtures for testing FastAPI/Starlette endpoints.
"""
import pytest
from starlette.testclient import TestClient
from typing import Generator
import sys
from pathlib import Path
# Add src to path
src_path = Path(__file__).parent.parent.parent / "src"
sys.path.insert(0, str(src_path))
@pytest.fixture
def test_client() -> Generator[TestClient, None, None]:
"""
Provide a test client for the Starlette application.
This allows testing HTTP endpoints without running the server.
"""
from main import app
with TestClient(app) as client:
yield client
@pytest.fixture
def authenticated_client(test_client: TestClient, sample_jwt_token: str) -> TestClient:
"""
Provide an authenticated test client with JWT token set.
"""
test_client.headers = {
**test_client.headers,
"Authorization": f"Bearer {sample_jwt_token}",
}
return test_client
@pytest.fixture
def admin_jwt_token() -> str:
"""Provide a sample admin JWT token for testing."""
return "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJhZG1pbl91c2VyIiwiZW1haWwiOiJhZG1pbkBleGFtcGxlLmNvbSIsIm5hbWUiOiJBZG1pbiBVc2VyIiwicm9sZXMiOlsiYWRtaW4iXX0.admin_signature"
@pytest.fixture
def admin_client(test_client: TestClient, admin_jwt_token: str) -> TestClient:
"""Provide an authenticated admin test client."""
test_client.headers = {
**test_client.headers,
"Authorization": f"Bearer {admin_jwt_token}",
}
return test_client