from uuid import uuid4 import pytest from fastapi import HTTPException from fastapi.testclient import TestClient def test_validate_json_allows_valid_payload(api_client: TestClient) -> None: payload = { "name": f"ValidJSON-{uuid4()}", "description": "Middleware should allow valid JSON.", } response = api_client.post("/api/scenarios/", json=payload) assert response.status_code == 200 data = response.json() assert data["name"] == payload["name"] def test_validate_json_rejects_invalid_payload(api_client: TestClient) -> None: with pytest.raises(HTTPException) as exc_info: api_client.post( "/api/scenarios/", content=b"{not valid json", headers={"Content-Type": "application/json"}, ) assert exc_info.value.status_code == 400 assert exc_info.value.detail == "Invalid JSON payload"