import re from dataclasses import replace import pytest from fastapi.testclient import TestClient from httpx import Response from ploop.api.app import create_app from tests.api.conftest import FakeReadPort def assert_safe_error(response: Response, status: int, code: str) -> None: assert response.status_code == status payload = response.json() assert set(payload) == {"code", "message", "request_id", "field_errors"} assert payload["code"] == code assert isinstance(payload["message"], str) and payload["message"] assert re.fullmatch(r"[A-Za-z0-9_-]{16,}", payload["request_id"]) assert isinstance(payload["field_errors"], list) assert "secret" not in response.text assert "/host/path" not in response.text assert "traceback" not in response.text.lower() @pytest.mark.parametrize("resource", ["datasets", "suts"]) def test_unknown_id_returns_safe_not_found(client: TestClient, resource: str) -> None: response = client.get(f"/api/v1/{resource}/unknown") assert_safe_error(response, 404, "not_found") assert response.json()["field_errors"] == [] @pytest.mark.parametrize("resource", ["datasets", "suts", "runs"]) @pytest.mark.parametrize("value", ["0", "101", "secret /host/path", "1.5"]) def test_invalid_limit_returns_sanitized_validation_error( client: TestClient, resource: str, value: str ) -> None: response = client.get(f"/api/v1/{resource}", params={"limit": value}) assert_safe_error(response, 422, "invalid_request") assert response.json()["field_errors"] == [{"field": "limit", "message": "Invalid value"}] @pytest.mark.parametrize("resource", ["datasets", "suts", "runs"]) @pytest.mark.parametrize( "cursor", ["", "!", "a", "ZGF0YXNldC0x=", "ZGF0YXNldC0x!", "_w", "dW5rbm93bg", "cnVuczpydW4tMR"] ) def test_malformed_or_unknown_cursor_is_rejected( client: TestClient, resource: str, cursor: str ) -> None: response = client.get(f"/api/v1/{resource}", params={"cursor": cursor}) assert_safe_error(response, 422, "invalid_request") assert response.json()["field_errors"] == [{"field": "cursor", "message": "Invalid cursor"}] @pytest.mark.parametrize("resource", ["datasets", "suts"]) def test_blank_path_id_returns_validation_error(client: TestClient, resource: str) -> None: response = client.get(f"/api/v1/{resource}/%20") assert_safe_error(response, 422, "invalid_request") @pytest.mark.parametrize( "url", [ "/api/v1/datasets", "/api/v1/datasets/dataset-1", "/api/v1/suts", "/api/v1/suts/sut-1", "/api/v1/runs", ], ) def test_unconfigured_read_store_returns_sanitized_503(url: str) -> None: assert_safe_error(TestClient(create_app()).get(url), 503, "service_unavailable") @pytest.mark.parametrize( "url", [ "/api/v1/datasets", "/api/v1/datasets/dataset-1", "/api/v1/suts", "/api/v1/suts/sut-1", "/api/v1/runs", ], ) def test_unexpected_error_is_sanitized_with_a_fresh_request_id(url: str) -> None: client = TestClient(create_app(FakeReadPort(fail=True)), raise_server_exceptions=False) first = client.get(url, headers={"X-Request-ID": "secret /host/path"}) second = client.get(url) assert_safe_error(first, 500, "internal_error") assert_safe_error(second, 500, "internal_error") assert first.json()["request_id"] != second.json()["request_id"] def test_unknown_route_uses_uniform_error_envelope(client: TestClient) -> None: assert_safe_error(client.get("/unknown"), 404, "not_found") def test_internal_error_does_not_escape_to_server_traceback_logs( caplog: pytest.LogCaptureFixture, ) -> None: # Default TestClient re-raises ASGI exceptions, just as a server can log their tracebacks. client = TestClient(create_app(FakeReadPort(fail=True))) response = client.get("/api/v1/runs") assert_safe_error(response, 500, "internal_error") assert response.json()["request_id"] in caplog.text assert "secret" not in caplog.text assert "/host/path" not in caplog.text assert "RuntimeError" not in caplog.text @pytest.mark.parametrize( ("source", "target"), [ ("datasets", "suts"), ("datasets", "runs"), ("suts", "datasets"), ("suts", "runs"), ("runs", "datasets"), ("runs", "suts"), ], ) def test_cursor_cannot_cross_resource_kinds_with_coinciding_ids(source: str, target: str) -> None: port = FakeReadPort() port.datasets = tuple(replace(item, id=f"shared-{i}") for i, item in enumerate(port.datasets)) port.suts = tuple(replace(item, id=f"shared-{i}") for i, item in enumerate(port.suts)) port.runs = tuple(replace(item, id=f"shared-{i}") for i, item in enumerate(port.runs)) client = TestClient(create_app(port)) first = client.get(f"/api/v1/{source}", params={"limit": 1}) assert first.status_code == 200 cursor = first.json()["meta"]["next_cursor"] assert cursor response = client.get(f"/api/v1/{target}", params={"cursor": cursor}) assert_safe_error(response, 422, "invalid_request") @pytest.mark.parametrize("resource", ["datasets", "suts", "runs"]) def test_cursor_rejects_a_position_no_longer_present_in_the_resource(resource: str) -> None: port = FakeReadPort() client = TestClient(create_app(port)) first = client.get(f"/api/v1/{resource}", params={"limit": 1}) assert first.status_code == 200 cursor = first.json()["meta"]["next_cursor"] setattr(port, resource, getattr(port, resource)[1:]) response = client.get(f"/api/v1/{resource}", params={"cursor": cursor}) assert_safe_error(response, 422, "invalid_request")