From 2ee5703a63839e43e562bf52d410baae2f4f7c64 Mon Sep 17 00:00:00 2001 From: demo-bot Date: Sun, 9 Aug 2026 15:35:43 +0000 Subject: [PATCH] decomposer: generate deliverable files for Define the service contract and project architecture for the FastAPI endpoint monitoring service.; Implement the typed monitor CRUD API and concurrency-safe in-memory state according to the service design.; Implement secure on-demand endpoint checks with status updates, latency measurement, robust error handling, and redacted structured logs.; Add operational API endpoints and environment-driven runtime configuration to the monitoring service.; Create automated tests for the monitoring service.; Package the service with Docker and developer documentation.; Validate the complete project. --- tests/test_api.py | 55 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 tests/test_api.py diff --git a/tests/test_api.py b/tests/test_api.py new file mode 100644 index 0000000..dfcf505 --- /dev/null +++ b/tests/test_api.py @@ -0,0 +1,55 @@ +from unittest.mock import AsyncMock + +from fastapi.testclient import TestClient + +from app.models import CurrentStatus, State, utcnow + + +def create(client: TestClient, url: str = "https://example.com/health") -> dict: + response = client.post("/v1/monitors", json={"name": "web", "url": url}) + assert response.status_code == 201 + return response.json() + + +def test_crud_and_status(client: TestClient) -> None: + item = create(client) + assert item["current_status"]["state"] == "unknown" + assert client.get("/v1/monitors").json()[0]["id"] == item["id"] + changed = client.patch(f"/v1/monitors/{item['id']}", json={"name": "api"}) + assert changed.status_code == 200 + assert changed.json()["name"] == "api" + assert client.get(f"/v1/monitors/{item['id']}/status").json()["state"] == "unknown" + assert client.delete(f"/v1/monitors/{item['id']}").status_code == 204 + missing = client.get(f"/v1/monitors/{item['id']}") + assert missing.status_code == 404 + assert missing.json()["error"]["code"] == "monitor_not_found" + + +def test_validation_and_operations(client: TestClient) -> None: + assert client.post("/v1/monitors", json={"name": "x", "url": "file:///tmp/x"}).status_code == 422 + assert client.post("/v1/monitors", json={"name": "x", "url": "https://u:p@example.com"}).status_code == 422 + assert client.get("/healthz").json() == {"status": "ok"} + ready = client.get("/readyz").json() + assert ready == {"status": "ready", "storage": "process-local-memory"} + + +def test_check_updates_current_status(client: TestClient) -> None: + item = create(client) + result = CurrentStatus(state=State.UP, checked_at=utcnow(), latency_ms=2, + http_status=204, final_url="https://example.com/health") + client.app.state.checker.check = AsyncMock(return_value=result) + checked = client.post(f"/v1/monitors/{item['id']}/check") + assert checked.status_code == 200 + assert checked.json()["state"] == "up" + stored = client.get(f"/v1/monitors/{item['id']}/status").json() + assert stored["http_status"] == 204 + + +def test_outbound_error_is_recorded_not_raised(client: TestClient) -> None: + item = create(client) + result = CurrentStatus(state=State.ERROR, checked_at=utcnow(), latency_ms=1, + error="timeout") + client.app.state.checker.check = AsyncMock(return_value=result) + response = client.post(f"/v1/monitors/{item['id']}/check") + assert response.status_code == 200 + assert response.json()["error"] == "timeout"