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.
Some checks failed
ci / test (push) Has been cancelled

This commit is contained in:
2026-08-09 15:55:02 +00:00
parent a41ef98cc4
commit f94e08f91f

View File

@@ -1,44 +1,46 @@
from datetime import UTC, datetime from unittest.mock import AsyncMock, patch
from endpoint_monitor.models import CheckResult, MonitorStatus from app.models import CheckSnapshot, State, now
async def test_crud_and_status(client): def create(client, url="https://example.com/path?secret=value"):
created = await client.post("/v1/monitors", json={"name": "site", "url": "https://example.com/a?secret=x"}) response = client.post("/monitors", json={"name": "site", "url": url})
assert created.status_code == 201 assert response.status_code == 201
monitor_id = created.json()["id"] return response.json()
assert (await client.get("/v1/monitors")).json()[0]["name"] == "site"
updated = await client.patch(f"/v1/monitors/{monitor_id}", json={"name": "renamed"})
assert updated.status_code == 200
assert updated.json()["revision"] == 2
status = await client.get(f"/v1/monitors/{monitor_id}/status")
assert status.json()["status"] == "unknown"
assert (await client.delete(f"/v1/monitors/{monitor_id}")).status_code == 204
assert (await client.get(f"/v1/monitors/{monitor_id}")).status_code == 404
async def test_duplicate_and_validation(client): def test_operational_routes_document_ephemeral_storage(client):
body = {"name": "same", "url": "https://example.com"} assert client.get("/healthz").json() == {"status": "ok"}
assert (await client.post("/v1/monitors", json=body)).status_code == 201 assert client.get("/readyz").json() == {
assert (await client.post("/v1/monitors", json=body)).status_code == 409 "status": "ready", "storage": "process-local-memory"
assert (await client.post("/v1/monitors", json={"name": "x", "url": "file:///etc/passwd"})).status_code == 422 }
async def test_check_updates_status(client, app): def test_crud_and_status_lifecycle(client):
class FakeChecker: item = create(client)
async def check(self, url, monitor_id): ident = item["id"]
return CheckResult(status=MonitorStatus.UP, checked_at=datetime.now(UTC), latency_ms=1.2, http_status=204, final_url="https://example.com/") assert item["status"]["state"] == "unknown"
assert client.get("/monitors").json()[0]["id"] == ident
app.state.checker = FakeChecker() assert client.get(f"/monitors/{ident}/status").json()["state"] == "unknown"
monitor_id = (await client.post("/v1/monitors", json={"name": "site", "url": "https://example.com"})).json()["id"] replaced = client.put(f"/monitors/{ident}", json={
result = await client.post(f"/v1/monitors/{monitor_id}/check") "name": "new", "url": "https://example.org"
assert result.status_code == 200 })
assert result.json()["status"] == "up" assert replaced.status_code == 200
saved = (await client.get(f"/v1/monitors/{monitor_id}/status")).json() assert replaced.json()["revision"] == 2
assert saved["status"] == "up" assert client.delete(f"/monitors/{ident}").status_code == 204
assert saved["last_check"]["http_status"] == 204 assert client.get(f"/monitors/{ident}").status_code == 404
async def test_operations(client): def test_check_updates_status(client):
assert (await client.get("/healthz")).json() == {"status": "ok"} item = create(client)
assert (await client.get("/readyz")).json() == {"status": "ready"} result = CheckSnapshot(state=State.up, checked_at=now(), latency_ms=2, http_status=204)
with patch("app.main.check_url", new=AsyncMock(return_value=result)):
response = client.post(f"/monitors/{item['id']}/check")
assert response.status_code == 200
assert response.json()["state"] == "up"
assert client.get(f"/monitors/{item['id']}/status").json()["http_status"] == 204
def test_validation_and_not_found(client):
assert client.post("/monitors", json={"name": "", "url": "file:///tmp/x"}).status_code == 422
assert client.get("/monitors/00000000-0000-0000-0000-000000000000").status_code == 404