diff --git a/tests/test_api.py b/tests/test_api.py index ff3a531..4104f7e 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -1,46 +1,32 @@ -from unittest.mock import AsyncMock, patch +import httpx +import pytest -from app.models import CheckSnapshot, State, now +pytestmark = pytest.mark.anyio -def create(client, url="https://example.com/path?secret=value"): - response = client.post("/monitors", json={"name": "site", "url": url}) - assert response.status_code == 201 - return response.json() +async def test_crud_status_and_operations(client: httpx.AsyncClient) -> None: + created = await client.post("/monitors", json={"name": "site", "url": "https://example.com/a"}) + assert created.status_code == 201 + monitor = created.json() + monitor_id = monitor["id"] + assert monitor["status"]["state"] == "unknown" + assert (await client.get("/monitors")).json()[0]["id"] == monitor_id + assert (await client.get(f"/monitors/{monitor_id}")).status_code == 200 + + updated = await client.put( + f"/monitors/{monitor_id}", + json={"name": "new", "url": "https://example.org/"}, + ) + assert updated.status_code == 200 + assert updated.json()["status"]["state"] == "unknown" + assert (await client.get(f"/monitors/{monitor_id}/status")).status_code == 200 + assert (await client.delete(f"/monitors/{monitor_id}")).status_code == 204 + assert (await client.get(f"/monitors/{monitor_id}")).status_code == 404 + + assert (await client.get("/health/live")).json() == {"status": "ok"} + assert (await client.get("/health/ready")).json() == {"status": "ready"} -def test_operational_routes_document_ephemeral_storage(client): - assert client.get("/healthz").json() == {"status": "ok"} - assert client.get("/readyz").json() == { - "status": "ready", "storage": "process-local-memory" - } - - -def test_crud_and_status_lifecycle(client): - item = create(client) - ident = item["id"] - assert item["status"]["state"] == "unknown" - assert client.get("/monitors").json()[0]["id"] == ident - assert client.get(f"/monitors/{ident}/status").json()["state"] == "unknown" - replaced = client.put(f"/monitors/{ident}", json={ - "name": "new", "url": "https://example.org" - }) - assert replaced.status_code == 200 - assert replaced.json()["revision"] == 2 - assert client.delete(f"/monitors/{ident}").status_code == 204 - assert client.get(f"/monitors/{ident}").status_code == 404 - - -def test_check_updates_status(client): - item = create(client) - 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 +async def test_validation_and_missing(client: httpx.AsyncClient) -> None: + assert (await client.post("/monitors", json={"name": "", "url": "file:///x"})).status_code == 422 + assert (await client.get("/monitors/00000000-0000-0000-0000-000000000000")).status_code == 404