feat: add health monitoring endpoint
This commit is contained in:
103
apps/health/tests/test_views.py
Normal file
103
apps/health/tests/test_views.py
Normal file
@@ -0,0 +1,103 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
def _mock_checks(monkeypatch, **overrides):
|
||||
payloads = {
|
||||
"db": {"status": "ok", "latency_ms": 1.0},
|
||||
"cache": {"status": "ok", "latency_ms": 1.0},
|
||||
"celery": {"status": "ok"},
|
||||
"backup": {"status": "ok"},
|
||||
}
|
||||
payloads.update(overrides)
|
||||
|
||||
monkeypatch.setattr("apps.health.views.check_db", lambda: payloads["db"])
|
||||
monkeypatch.setattr("apps.health.views.check_cache", lambda: payloads["cache"])
|
||||
monkeypatch.setattr("apps.health.views.check_celery", lambda: payloads["celery"])
|
||||
monkeypatch.setattr("apps.health.views.check_backup", lambda: payloads["backup"])
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_healthy(client, monkeypatch):
|
||||
_mock_checks(monkeypatch)
|
||||
|
||||
response = client.get("/health/")
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.json()["status"] == "ok"
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_degraded_celery(client, monkeypatch):
|
||||
_mock_checks(monkeypatch, celery={"status": "fail", "detail": "broker down"})
|
||||
|
||||
response = client.get("/health/")
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.json()["status"] == "degraded"
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_degraded_backup(client, monkeypatch):
|
||||
_mock_checks(monkeypatch, backup={"status": "fail", "detail": "backup missing"})
|
||||
|
||||
response = client.get("/health/")
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.json()["status"] == "degraded"
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_unhealthy_db(client, monkeypatch):
|
||||
_mock_checks(monkeypatch, db={"status": "fail", "detail": "db down"})
|
||||
|
||||
response = client.get("/health/")
|
||||
|
||||
assert response.status_code == 503
|
||||
assert response.json()["status"] == "unhealthy"
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_unhealthy_cache(client, monkeypatch):
|
||||
_mock_checks(monkeypatch, cache={"status": "fail", "detail": "cache down"})
|
||||
|
||||
response = client.get("/health/")
|
||||
|
||||
assert response.status_code == 503
|
||||
assert response.json()["status"] == "unhealthy"
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_response_shape(client, monkeypatch):
|
||||
_mock_checks(monkeypatch)
|
||||
|
||||
payload = client.get("/health/").json()
|
||||
|
||||
assert set(payload) == {"status", "version", "checks", "timestamp"}
|
||||
assert set(payload["version"]) == {"git_sha", "build"}
|
||||
assert set(payload["checks"]) == {"db", "cache", "celery", "backup"}
|
||||
assert re.fullmatch(r"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z", payload["timestamp"])
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_version_fields(client, monkeypatch):
|
||||
_mock_checks(monkeypatch)
|
||||
monkeypatch.setenv("GIT_SHA", "59cc1c4")
|
||||
monkeypatch.setenv("BUILD_ID", "build-20260306-59cc1c4")
|
||||
|
||||
payload = client.get("/health/").json()
|
||||
|
||||
assert payload["version"]["git_sha"]
|
||||
assert payload["version"]["build"]
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_no_cache_headers(client, monkeypatch):
|
||||
_mock_checks(monkeypatch)
|
||||
|
||||
response = client.get("/health/")
|
||||
|
||||
assert "no-cache" in response["Cache-Control"]
|
||||
Reference in New Issue
Block a user