blitz_api/tests/routers/test_system.py

53 lines
1.6 KiB
Python
Raw Permalink Normal View History

from starlette.testclient import TestClient
2022-08-24 19:19:58 +02:00
from app.main import app
from tests.routers.utils import call_route
import app.main as main
from app.api.models import StartupState
2022-08-24 19:19:58 +02:00
client = TestClient(app)
2022-08-24 19:19:58 +02:00
def test_route_authentications_latest():
2026-02-02 12:25:30 +01:00
prefixes = ["/system"]
for prefix in prefixes:
2022-08-24 19:19:58 +02:00
call_route(client, f"{prefix}/refresh-token", method="p")
def _set_status(bitcoin, lightning):
main.api_startup_status.bitcoin = bitcoin
main.api_startup_status.lightning = lightning
def test_health_is_unauthenticated_and_200_when_ready():
_set_status(StartupState.DONE, StartupState.DONE)
# no `with`: don't start lifespan/background tasks
c = TestClient(main.app)
resp = c.get("/system/health") # no Authorization header
assert resp.status_code == 200
body = resp.json()
assert body["healthy"] is True
assert body["subsystems"] == []
def test_health_503_when_not_ready_body_is_health_info():
_set_status(StartupState.BOOTSTRAPPING, StartupState.DONE)
c = TestClient(main.app)
resp = c.get("/system/health")
assert resp.status_code == 503
body = resp.json()
# body is a SystemHealthInfo, NOT an ErrorMessage
assert body["healthy"] is False
assert "error_code" not in body
assert body["message"] == "bitcoind not ready: bootstrapping"
def test_health_verbose_lists_subsystems():
_set_status(StartupState.DONE, StartupState.DONE)
c = TestClient(main.app)
resp = c.get("/system/health?verbose=true")
assert resp.status_code == 200
names = [s["name"] for s in resp.json()["subsystems"]]
assert names == ["api", "bitcoind", "lightning"]