Files
crucible-agent-build-fastap…/tests/test_api.py

42 lines
1.7 KiB
Python

from fastapi.testclient import TestClient
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()
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"] == "renamed"
checked = client.post(f"/monitors/{monitor_id}/check")
assert checked.status_code == 200
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
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
def test_health(client: TestClient) -> None:
assert client.get("/health/live").json() == {"status": "ok"}
assert client.get("/health/ready").json() == {"status": "ready"}