from fastapi.testclient import TestClient def test_main_defaults_to_hub(): from app.main import app with TestClient(app) as client: r = client.get("/healthz") assert r.status_code == 200 and r.json()["role"] == "hub" def test_index_assets_and_spa_fallback(hub): page = hub.get("/") assert page.status_code == 200 and "network monitor" in page.text.lower() css = hub.get("/static/css/app.css") assert css.status_code == 200 and css.headers["cache-control"] == "no-cache" assert hub.get("/clients/aa:bb:cc:dd:ee:ff").status_code == 200 # client-side route assert hub.get("/api/nope").status_code == 404 def test_info(hub): j = hub.get("/api/info").json() assert j["role"] == "hub" and j["adguard"] is False def test_password(hub, monkeypatch): monkeypatch.setenv("UI_PASSWORD", "s3cret") assert hub.get("/api/info").status_code == 401 assert hub.get("/api/info", auth=("admin", "wrong")).status_code == 401 assert hub.get("/api/info", auth=("admin", "s3cret")).status_code == 200 assert hub.get("/healthz").status_code == 200 assert hub.post("/api/probe/report", json={}).status_code in (401, 422) # token auth, not the password def test_hub_serves_installers_without_password(hub, monkeypatch): monkeypatch.setenv("UI_PASSWORD", "s3cret") r = hub.get("/install-probe.sh") assert r.status_code == 200 and r.text.startswith("#!/usr/bin/env bash") and "\nNETMON_ROLE=probe\n" in r.text assert "\nNETMON_ROLE=hub\n" in hub.get("/install.sh").text assert hub.get("/api/probes").status_code == 401 # the rest stays protected monkeypatch.delenv("UI_PASSWORD") assert hub.get("/api/probes").json()["install_url"] == "" # UI builds /install-probe.sh