Buckets:

download
raw
4.67 kB
"""API contract tests — verify response shapes match expectations."""
import pytest
import pytest_asyncio
from httpx import AsyncClient, ASGITransport
from faultray.api.server import app as fastapi_app
from tests.conftest import TEST_API_KEY, _setup_test_user_async
@pytest_asyncio.fixture
async def client():
await _setup_test_user_async()
transport = ASGITransport(app=fastapi_app)
headers = {"Authorization": f"Bearer {TEST_API_KEY}"}
async with AsyncClient(transport=transport, base_url="http://test", headers=headers) as c:
await c.get("/demo")
yield c
@pytest.mark.asyncio
async def test_simulate_response_shape(client):
"""POST /api/simulate should return specific fields."""
response = await client.post("/api/simulate")
assert response.status_code == 200
data = response.json()
assert "resilience_score" in data
assert "total_scenarios" in data
assert "critical_count" in data
assert "warning_count" in data
assert "passed_count" in data
assert isinstance(data["resilience_score"], (int, float))
@pytest.mark.asyncio
async def test_graph_data_response_shape(client):
"""GET /api/graph-data should return nodes and edges."""
response = await client.get("/api/graph-data")
assert response.status_code == 200
data = response.json()
assert "nodes" in data
assert "edges" in data
assert isinstance(data["nodes"], list)
if data["nodes"]:
node = data["nodes"][0]
assert "id" in node
assert "name" in node
assert "type" in node
@pytest.mark.asyncio
async def test_insurance_score_response_shape(client):
"""POST /api/insurance/score should return scoring fields."""
response = await client.post("/api/insurance/score",
json={"yaml_content": ""})
# Should work even with empty content (returns error or default)
assert response.status_code in (200, 400, 422)
@pytest.mark.asyncio
async def test_graphql_response_shape(client):
"""POST /graphql should return standard GraphQL response."""
response = await client.post("/graphql",
json={"query": "{ components { id } }"})
assert response.status_code == 200
data = response.json()
assert "data" in data or "errors" in data
@pytest.mark.asyncio
async def test_widget_scorecard_is_html(client):
"""GET /widget/scorecard should return HTML."""
response = await client.get("/widget/scorecard")
assert response.status_code == 200
assert "text/html" in response.headers.get("content-type", "")
@pytest.mark.asyncio
async def test_widget_badge_is_json(client):
"""GET /widget/badge should return shields.io badge JSON."""
response = await client.get("/widget/badge")
assert response.status_code == 200
data = response.json()
assert "schemaVersion" in data or "label" in data
@pytest.mark.asyncio
async def test_leaderboard_response_shape(client):
"""GET /api/leaderboard/ should return list."""
response = await client.get("/api/leaderboard/")
assert response.status_code == 200
@pytest.mark.asyncio
async def test_openapi_spec_available(client):
"""GET /openapi.json should return valid OpenAPI spec."""
response = await client.get("/openapi.json")
assert response.status_code == 200
data = response.json()
assert "openapi" in data
assert "paths" in data
@pytest.mark.asyncio
async def test_all_api_endpoints_respond(client):
"""Every registered GET /api/* endpoint should respond (not 500)."""
# Get all routes from OpenAPI
response = await client.get("/openapi.json")
spec = response.json()
failures = []
for path, methods in spec.get("paths", {}).items():
# Only test JSON API endpoints; skip HTML template routes and
# paths with parameters (e.g. /api/runs/{run_id}).
if not path.startswith("/api/"):
continue
if "{" in path:
continue
for method in methods:
if method == "get":
try:
r = await client.get(path)
if r.status_code == 500:
failures.append(f"GET {path} returned 500")
except Exception as exc:
# Server-side errors may propagate through ASGI transport
failures.append(f"GET {path} raised {type(exc).__name__}: {exc}")
# Allow a small number of broken endpoints (pre-existing bugs) but
# flag regressions if many new ones appear.
assert len(failures) <= 3, (
f"{len(failures)} endpoints returned 500:\n" + "\n".join(failures)
)

Xet Storage Details

Size:
4.67 kB
·
Xet hash:
88393446806fe269d96465838833ae4a237b8ab812dd2566c3a8ebdf64bb6018

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.