| """Contract tests for core API endpoints using Schemathesis schema validation.""" |
| import pytest |
| from fastapi.testclient import TestClient |
| from main_api_app import app |
| from tests.contract.conftest import schema |
|
|
|
|
| class TestHealthEndpoints: |
| """Contract tests for health check endpoints.""" |
|
|
| def test_health_endpoint_contracts(self): |
| """Test /health endpoint conforms to OpenAPI spec.""" |
| |
| operation = schema["/health"]["GET"] |
| with TestClient(app) as client: |
| response = client.get("/health") |
| |
| operation.validate_response(response) |
| |
| |
| assert response.status_code in [200, 503] |
|
|
| def test_api_v1_health_contracts(self): |
| """Test /api/v1/health endpoint conforms to OpenAPI spec.""" |
| |
| operation = schema["/api/v1/health"]["GET"] |
| with TestClient(app) as client: |
| response = client.get("/api/v1/health") |
| |
| operation.validate_response(response) |
| |
| assert response.status_code in [200, 404] |
|
|
| def test_root_endpoint_contracts(self): |
| """Test root endpoint conforms to OpenAPI spec.""" |
| |
| operation = schema["/"]["GET"] |
| with TestClient(app) as client: |
| response = client.get("/") |
| |
| operation.validate_response(response) |
| |
| assert response.status_code == 200 |
|
|
|
|
| class TestAgentEndpoints: |
| """Contract tests for agent endpoints.""" |
|
|
| def test_list_agents_contracts(self): |
| """Test GET /api/agents/ conforms to OpenAPI spec.""" |
| |
| operation = schema["/api/agents/"]["GET"] |
| with TestClient(app) as client: |
| response = client.get("/api/agents/") |
| |
| operation.validate_response(response) |
| |
| assert response.status_code in [200, 401, 403, 404] |
|
|
| def test_get_agent_contracts(self): |
| """Test GET /api/agents/{id} conforms to OpenAPI spec.""" |
| |
| operation = schema["/api/agents/{agent_id}"]["GET"] |
| with TestClient(app) as client: |
| response = client.get("/api/agents/test-agent-id") |
| |
| operation.validate_response(response) |
| |
| assert response.status_code in [200, 401, 403, 404] |
|
|
| def test_create_agent_contracts(self): |
| """Test POST /api/agents/spawn conforms to OpenAPI spec.""" |
| |
| operation = schema["/api/agents/spawn"]["POST"] |
| with TestClient(app) as client: |
| response = client.post( |
| "/api/agents/spawn", |
| json={ |
| "agent_id": "test-spawn-agent", |
| "config": {} |
| } |
| ) |
| |
| operation.validate_response(response) |
| |
| assert response.status_code in [200, 400, 401, 403, 404] |
|
|