2021-11-19 18:32:30 +01:00
|
|
|
from starlette.testclient import TestClient
|
2022-03-20 17:20:56 +01:00
|
|
|
|
2022-08-24 19:19:58 +02:00
|
|
|
from app.main import app
|
2021-11-19 18:32:30 +01:00
|
|
|
from tests.routers.utils import call_route
|
2026-07-12 17:11:04 +02:00
|
|
|
import app.main as main
|
|
|
|
|
from app.api.models import StartupState
|
2021-11-19 18:32:30 +01:00
|
|
|
|
2022-08-24 19:19:58 +02:00
|
|
|
client = TestClient(app)
|
2021-11-19 18:32:30 +01:00
|
|
|
|
2022-08-24 19:19:58 +02:00
|
|
|
|
|
|
|
|
def test_route_authentications_latest():
|
2026-02-02 12:25:30 +01:00
|
|
|
prefixes = ["/system"]
|
2021-11-19 18:32:30 +01:00
|
|
|
|
|
|
|
|
for prefix in prefixes:
|
2022-08-24 19:19:58 +02:00
|
|
|
call_route(client, f"{prefix}/refresh-token", method="p")
|
2026-07-12 17:11:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
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"]
|