| """Pytest fixtures for Schemathesis contract testing. |
| |
| This module provides comprehensive fixtures for API contract testing using |
| Schemathesis with Hypothesis property-based testing. Validates OpenAPI schema |
| compliance across agent, canvas, and browser endpoints. |
| """ |
| import pytest |
| import schemathesis |
| from fastapi.testclient import TestClient |
| from main_api_app import app |
| from hypothesis import settings, HealthCheck |
| from typing import Dict, List, Set |
|
|
|
|
| |
| |
| schema = schemathesis.openapi.from_dict(app.openapi()) |
|
|
|
|
| @pytest.fixture |
| def app_client(): |
| """FastAPI TestClient for contract testing. |
| |
| Provides a test client that can make HTTP requests to the FastAPI app |
| without starting a server. Used by Schemathesis for endpoint validation. |
| """ |
| return TestClient(app) |
|
|
|
|
| @pytest.fixture |
| def auth_headers(): |
| """Mock authentication headers for protected endpoints. |
| |
| Provides Bearer token authentication for testing protected endpoints. |
| In production, this would be replaced with actual auth tokens. |
| """ |
| return {"Authorization": "Bearer test_token"} |
|
|
|
|
| @pytest.fixture |
| def admin_headers(): |
| """Admin authentication headers for admin-only endpoints. |
| |
| Simulates admin-level permissions for testing administrative endpoints. |
| """ |
| return {"Authorization": "Bearer admin_token", "X-User-Role": "admin"} |
|
|
|
|
| @pytest.fixture |
| def authenticated_client_for_contract(app_client, auth_headers): |
| """TestClient with pre-configured authentication headers. |
| |
| This client automatically includes auth headers in all requests, |
| simulating an authenticated user session for contract testing. |
| |
| Usage: |
| response = authenticated_client_for_contract.get("/api/agents/") |
| """ |
| |
| app_client.headers.update(auth_headers) |
| return app_client |
|
|
|
|
| @pytest.fixture |
| def admin_client_for_contract(app_client, admin_headers): |
| """TestClient with admin-level authentication headers.""" |
| app_client.headers.update(admin_headers) |
| return app_client |
|
|
|
|
| |
| |
| hypothesis_settings = settings( |
| max_examples=10, |
| deadline=1000, |
| derandomize=True, |
| suppress_health_check=list(HealthCheck), |
| ) |
|
|
|
|
| |
|
|
| @schemathesis.hook |
| def before_process_case(context, case, **kwargs): |
| """Hook called before each test case is processed. |
| |
| Injects auth headers for protected endpoints and resets database state. |
| """ |
| |
| |
| pass |
|
|
|
|
| @schemathesis.hook |
| def after_process_case(context, case, response, **kwargs): |
| """Hook called after each test case is processed. |
| |
| Can be used for custom validation or logging beyond schema compliance. |
| """ |
| |
| if response.status_code >= 400: |
| |
| pass |
|
|
|
|
| |
| |
| |
|
|
| EXCLUDED_ENDPOINTS: Set[str] = { |
| |
| "/ws/agent", |
| "/ws/browser", |
| "/api/v1/stream", |
|
|
| |
| |
| "/api/browser/screenshot", |
| "/api/browser/cdp", |
|
|
| |
| |
| } |
|
|
|
|
| @pytest.fixture |
| def endpoint_filter() -> Set[str]: |
| """Returns set of endpoints to exclude from contract testing. |
| |
| Excluded endpoints fall into these categories: |
| 1. WebSocket endpoints (Schemathesis limitation) |
| 2. External service dependencies (LLM calls, browser automation) |
| 3. Endpoints with irreversible side effects |
| |
| Returns: |
| Set of endpoint paths to exclude from testing |
| """ |
| return EXCLUDED_ENDPOINTS |
|
|
|
|
| @pytest.fixture |
| def schema_with_excluded_filters(endpoint_filter: Set[str]): |
| """Schema with excluded endpoints filtered out. |
| |
| Creates a Schemathesis schema that excludes endpoints requiring |
| external services or having special requirements. |
| |
| Args: |
| endpoint_filter: Set of endpoint paths to exclude |
| |
| Returns: |
| Filtered Schemathesis schema |
| """ |
| |
| |
| return schema |
|
|
|
|
| |
| |
|
|
| CUSTOM_VALIDATORS: Dict[str, callable] = { |
| |
| |
| } |
|
|
|
|
| @pytest.fixture |
| def custom_validators() -> Dict[str, callable]: |
| """Returns custom response validators for special endpoints. |
| |
| Some endpoints may have valid responses that deviate from the schema |
| due to streaming, binary data, or other special cases. |
| |
| Returns: |
| Dict mapping endpoint paths to validator functions |
| """ |
| return CUSTOM_VALIDATORS |
|
|