import pytest from app.config import ConfigError, Settings from tests.conftest import HEADERS, body pytestmark = pytest.mark.anyio @pytest.mark.parametrize( ("method", "path", "headers"), [ ("POST", "/health", {}), ("GET", "/v1/voices", {"X-Service-Key": "errada"}), ("GET", "/v1/voices/pt-br-test", {"X-Service-Key": "errada"}), ("POST", "/v1/synthesize", {}), ("GET", "/rota-que-nao-existe", {}), ("GET", "/docs", {}), ], ) async def test_without_valid_key_is_401(make_client, method, path, headers): async with make_client() as client: response = await client.request(method, path, headers=headers, json=body()) assert response.status_code == 401 assert response.json()["error"]["code"] == "unauthorized" async def test_malformed_json_without_key_is_401_not_422(make_client): async with make_client() as client: response = await client.post("/v1/synthesize", content=b"{nao-e-json", headers={"Content-Type": "application/json"}) assert response.status_code == 401 async def test_key_is_not_accepted_as_bearer(make_client): async with make_client() as client: response = await client.get("/v1/voices", headers={"Authorization": f"Bearer {HEADERS['X-Service-Key']}"}) assert response.status_code == 401 async def test_health_is_liveness_without_key_and_without_data(make_client): async with make_client() as client: anonymous = await client.get("/health") wrong = await client.get("/health", headers={"X-Service-Key": "errada"}) for response in (anonymous, wrong): assert response.status_code == 200 assert response.json() == {"status": "ok"} async def test_valid_key_opens_status_and_voices(make_client): async with make_client() as client: status = await client.get("/v1/status", headers=HEADERS) voices = await client.get("/v1/voices", headers=HEADERS) assert status.status_code == 200 and status.json()["status"] == "ok" assert [v["id"] for v in voices.json()["items"]] == ["pt-br-test"] async def test_openapi_is_disabled(make_client): async with make_client() as client: assert (await client.get("/openapi.json", headers=HEADERS)).status_code == 404 @pytest.mark.parametrize("key", ["", "curta"]) def test_service_refuses_to_start_without_strong_key(monkeypatch, key): monkeypatch.setenv("VOICE_SERVICE_KEY", key) with pytest.raises(ConfigError): Settings.from_env()