diff --git a/tests/test_api.py b/tests/test_api.py index 558f767..a43b182 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -1,58 +1,41 @@ -import httpx -import pytest - -from app.main import app -from tests.conftest import install_checker - -pytestmark = pytest.mark.anyio +from fastapi.testclient import TestClient -async def test_crud_and_status_lifecycle(api_client): - created = await api_client.post("/api/v1/monitors", json={"name": "Example", "url": "https://example.com/path?secret=yes"}) - assert created.status_code == 201 - monitor_id = created.json()["id"] - assert created.json()["status"] == "unknown" - assert (await api_client.get("/api/v1/monitors")).json()[0]["id"] == monitor_id +def create(client: TestClient, name: str = "site", url: str = "https://example.com/health") -> dict: + response = client.post("/monitors", json={"name": name, "target_url": url}) + assert response.status_code == 201 + return response.json() - updated = await api_client.put(f"/api/v1/monitors/{monitor_id}", json={"name": "New", "url": "https://example.org"}) + +def test_crud_and_status(client: TestClient) -> None: + monitor = create(client) + monitor_id = monitor["id"] + assert monitor["current_status"]["state"] == "unknown" + assert client.get("/monitors").json()[0]["id"] == monitor_id + assert client.get(f"/monitors/{monitor_id}").status_code == 200 + + updated = client.put( + f"/monitors/{monitor_id}", + json={"name": "renamed", "target_url": "https://example.org/ready"}, + ) assert updated.status_code == 200 - assert updated.json()["name"] == "New" - assert (await api_client.get(f"/api/v1/monitors/{monitor_id}/status")).json()["status"] == "unknown" - assert (await api_client.delete(f"/api/v1/monitors/{monitor_id}")).status_code == 204 - assert (await api_client.get(f"/api/v1/monitors/{monitor_id}")).status_code == 404 + assert updated.json()["name"] == "renamed" - -async def test_validation_and_probes(api_client): - assert (await api_client.post("/api/v1/monitors", json={"name": "", "url": "file:///tmp/x"})).status_code == 422 - assert (await api_client.get("/healthz")).json() == {"status": "ok"} - assert (await api_client.get("/readyz")).json() == {"status": "ready"} - - -async def test_check_updates_status(api_client): - install_checker(lambda request: httpx.Response(204, request=request)) - item = (await api_client.post("/api/v1/monitors", json={"name": "ok", "url": "https://example.com"})).json() - checked = await api_client.post(f"/api/v1/monitors/{item['id']}/check") + checked = client.post(f"/monitors/{monitor_id}/check") assert checked.status_code == 200 - assert checked.json()["status"] == "up" - assert checked.json()["http_status"] == 204 - assert checked.json()["latency_ms"] >= 0 - assert (await api_client.get(f"/api/v1/monitors/{item['id']}/status")).json()["status"] == "up" + assert checked.json()["state"] == "up" + assert client.get(f"/monitors/{monitor_id}/status").json()["http_status"] == 204 + + assert client.delete(f"/monitors/{monitor_id}").status_code == 204 + assert client.get(f"/monitors/{monitor_id}").status_code == 404 -async def test_http_failure_is_down(api_client): - install_checker(lambda request: httpx.Response(503, request=request)) - item = (await api_client.post("/api/v1/monitors", json={"name": "bad", "url": "https://example.com"})).json() - checked = await api_client.post(f"/api/v1/monitors/{item['id']}/check") - assert checked.json()["status"] == "down" - assert checked.json()["http_status"] == 503 +def test_validation_and_not_found(client: TestClient) -> None: + assert client.post("/monitors", json={"name": "", "target_url": "file:///tmp/x"}).status_code == 422 + assert client.get("/monitors/not-a-uuid").status_code == 422 + assert client.get("/monitors/00000000-0000-0000-0000-000000000000").status_code == 404 -async def test_timeout_is_sanitized_error(api_client): - def timeout(request): - raise httpx.ReadTimeout("contains-a-secret", request=request) - install_checker(timeout) - item = (await api_client.post("/api/v1/monitors", json={"name": "slow", "url": "https://example.com"})).json() - checked = await api_client.post(f"/api/v1/monitors/{item['id']}/check") - assert checked.json()["status"] == "error" - assert checked.json()["error"] == "timeout" - assert "contains-a-secret" not in checked.text +def test_health(client: TestClient) -> None: + assert client.get("/health/live").json() == {"status": "ok"} + assert client.get("/health/ready").json() == {"status": "ready"}