58 lines
1.6 KiB
Python
58 lines
1.6 KiB
Python
"""API test fixtures."""
|
|
|
|
from collections.abc import AsyncGenerator
|
|
from unittest.mock import AsyncMock, MagicMock, patch
|
|
|
|
import pytest
|
|
from httpx import ASGITransport, AsyncClient
|
|
|
|
from app.infrastructure.auth.models import TokenInfo
|
|
from app.main import app_factory
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_keycloak_client() -> MagicMock:
|
|
"""Create mock Keycloak client for testing."""
|
|
mock_client = AsyncMock()
|
|
mock_client.introspect_token.return_value = TokenInfo(
|
|
active=True,
|
|
user_id="test-user-id",
|
|
username="testuser",
|
|
email="test@example.com",
|
|
roles=["user"],
|
|
)
|
|
return mock_client
|
|
|
|
|
|
@pytest.fixture
|
|
async def client(mock_keycloak_client: MagicMock) -> AsyncGenerator[AsyncClient]:
|
|
"""Create async HTTP client for API testing."""
|
|
with patch(
|
|
"app.presentation.api.deps.KeycloakAuthClient",
|
|
return_value=mock_keycloak_client,
|
|
):
|
|
app = app_factory()
|
|
transport = ASGITransport(app=app)
|
|
async with AsyncClient(transport=transport, base_url="http://test") as ac:
|
|
yield ac
|
|
|
|
|
|
@pytest.fixture
|
|
def auth_headers() -> dict[str, str]:
|
|
"""Return mock authentication headers."""
|
|
return {"Authorization": "Bearer test_token"}
|
|
|
|
|
|
@pytest.fixture
|
|
def unauthorized_keycloak_client() -> MagicMock:
|
|
"""Create mock Keycloak client that returns invalid token."""
|
|
mock_client = AsyncMock()
|
|
mock_client.introspect_token.return_value = TokenInfo(
|
|
active=False,
|
|
user_id="",
|
|
username="",
|
|
email="",
|
|
roles=[],
|
|
)
|
|
return mock_client
|