feat(admin): auth + four data panels (sessions/conversations/approvals/config)
- HTTP Basic auth from config http.admin (constant-time compare; absent config -> UI/api fail-closed 503, legacy JSON endpoints stay open) - GET /api/sessions: bindings + pinned model + idle - GET /api/conversations: hub-side text logs (tail 50/binding) - GET /api/approvals: pending permission requests incl. options/presented - GET /api/config: effective config with secret-looking values redacted - UI: tabbed layout (Overview/Sessions/Conversations/Approvals/Config), approvals tab shows pending count badge
This commit is contained in:
1 parent
e1afdce073
commit
c396ce2d5e
5 files changed
+423
-97
No files matched your search
+94
-30
@@ -1,15 +1,20 @@
|
||||
"""Tests for the admin UI endpoint (served on / and /ui)."""
|
||||
"""Tests for admin UI + auth + data endpoints (served on /, /ui, /api/*)."""
|
||||
|
||||
import base64
|
||||
import json
|
||||
import threading
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import pytest
|
||||
|
||||
from agenthub.admin_data import check_auth, sanitize_config
|
||||
from agenthub.http_admin import StatusHTTP
|
||||
|
||||
ADMIN = {"username": "ops", "password": "s3cret"}
|
||||
|
||||
|
||||
class FakeBinding:
|
||||
def __init__(self, channel, chat_id, agent, acp_session_id):
|
||||
@@ -17,58 +22,117 @@ class FakeBinding:
|
||||
self.chat_id = chat_id
|
||||
self.agent = agent
|
||||
self.acp_session_id = acp_session_id
|
||||
self.created_at = time.time()
|
||||
self.last_active = time.time()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def server():
|
||||
class FakeRecord:
|
||||
def __init__(self, key, agent, sid, log):
|
||||
self.binding_key = key
|
||||
self.agent = agent
|
||||
self.acp_session_id = sid
|
||||
self.text_log = log
|
||||
self.updated_at = time.time()
|
||||
|
||||
|
||||
def make_mgr():
|
||||
mgr = MagicMock()
|
||||
mgr.list_agents.return_value = [
|
||||
{"name": "opencode", "cmd": "opencode acp", "running": True},
|
||||
{"name": "codex", "cmd": "codex acp", "running": False},
|
||||
]
|
||||
mgr._bindings = {
|
||||
("wecom", "c1"): FakeBinding("wecom", "c1", "opencode", "s1"),
|
||||
b = FakeBinding("wecom", "c1", "opencode", "s1")
|
||||
mgr._bindings = {("wecom", "c1"): b}
|
||||
mgr._records = {
|
||||
"wecom:c1": FakeRecord(
|
||||
"wecom:c1", "opencode", "s1",
|
||||
[{"role": "user", "text": "hi"}, {"role": "assistant", "text": "hello"}],
|
||||
)
|
||||
}
|
||||
mgr._pending_permissions = {}
|
||||
mgr._lock = threading.Lock()
|
||||
http = StatusHTTP(mgr, host="127.0.0.1", port=0) # port 0 = ephemeral
|
||||
mgr._session_config.return_value = {"model": {"current": "opencode/mimo-v2.6-flash-free"}}
|
||||
return mgr
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def auth_server():
|
||||
http = StatusHTTP(make_mgr(), host="127.0.0.1", port=0, admin_cfg=ADMIN)
|
||||
http.start()
|
||||
port = http._server.server_address[1]
|
||||
yield f"http://127.0.0.1:{port}"
|
||||
http.stop()
|
||||
|
||||
|
||||
def _get(url):
|
||||
with urllib.request.urlopen(url, timeout=5) as r:
|
||||
return r.status, r.read()
|
||||
@pytest.fixture
|
||||
def open_server():
|
||||
http = StatusHTTP(make_mgr(), host="127.0.0.1", port=0, admin_cfg=None)
|
||||
http.start()
|
||||
port = http._server.server_address[1]
|
||||
yield f"http://127.0.0.1:{port}"
|
||||
http.stop()
|
||||
|
||||
|
||||
def test_root_serves_html(server):
|
||||
code, body = _get(server + "/")
|
||||
assert code == 200
|
||||
assert b"text/html" not in body # header not in body; content check below
|
||||
assert b"agenthub" in body
|
||||
assert b"<!DOCTYPE html>" in body
|
||||
def _get(url, auth=None):
|
||||
req = urllib.request.Request(url)
|
||||
if auth:
|
||||
req.add_header("Authorization", "Basic " + base64.b64encode(auth.encode()).decode())
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=5) as r:
|
||||
return r.status, r.read()
|
||||
except urllib.error.HTTPError as e:
|
||||
return e.code, e.read()
|
||||
|
||||
|
||||
def test_ui_alias(server):
|
||||
code, body = _get(server + "/ui")
|
||||
# --- auth unit ---
|
||||
|
||||
def test_check_auth_ok_and_bad():
|
||||
assert check_auth("Basic " + base64.b64encode(b"ops:s3cret").decode(), ADMIN)
|
||||
assert not check_auth("Basic " + base64.b64encode(b"ops:wrong").decode(), ADMIN)
|
||||
assert not check_auth(None, ADMIN)
|
||||
assert not check_auth("Basic abc", ADMIN) # malformed b64 -> False
|
||||
assert not check_auth("Basic " + base64.b64encode(b"ops:s3cret").decode(), None) # fail-closed
|
||||
|
||||
|
||||
def test_sanitize_config_redacts_secrets():
|
||||
cfg = {"channels": [{"name": "wecom", "secret": "abcdef123456"}], "http": {"port": 30990}}
|
||||
out = sanitize_config(cfg)
|
||||
assert out["channels"][0]["secret"].endswith("[REDACTED]")
|
||||
assert "abcdef123456" not in json.dumps(out)
|
||||
assert out["http"]["port"] == 30990
|
||||
|
||||
|
||||
# --- auth-gated server ---
|
||||
|
||||
def test_root_requires_auth(auth_server):
|
||||
code, _ = _get(auth_server + "/")
|
||||
assert code == 401
|
||||
|
||||
|
||||
def test_ui_ok_with_auth(auth_server):
|
||||
code, body = _get(auth_server + "/ui", auth="ops:s3cret")
|
||||
assert code == 200 and b"<!DOCTYPE html>" in body
|
||||
|
||||
|
||||
def test_json_endpoints_still_work(server):
|
||||
code, body = _get(server + "/status")
|
||||
def test_api_endpoints(auth_server):
|
||||
code, body = _get(auth_server + "/api/sessions", auth="ops:s3cret")
|
||||
d = json.loads(body)
|
||||
assert d["service"] == "agenthub"
|
||||
assert len(d["agents"]) == 2
|
||||
code, body = _get(server + "/sessions")
|
||||
assert d["sessions"][0]["model"] == "opencode/mimo-v2.6-flash-free"
|
||||
code, body = _get(auth_server + "/api/conversations", auth="ops:s3cret")
|
||||
d = json.loads(body)
|
||||
assert d["bindings"][0]["agent"] == "opencode"
|
||||
assert d["conversations"][0]["turns"] == 2
|
||||
assert d["conversations"][0]["log"][0]["text"] == "hi"
|
||||
code, body = _get(auth_server + "/api/approvals", auth="ops:s3cret")
|
||||
assert json.loads(body)["approvals"] == []
|
||||
|
||||
|
||||
def test_html_references_fetch_targets(server):
|
||||
_, body = _get(server + "/")
|
||||
# UI fetches status/sessions relatively — must not hardcode hosts
|
||||
assert b"fetch('status')" in body
|
||||
assert b"fetch('sessions')" in body
|
||||
assert b"botflow_truncated" not in body # sanity: unrelated content absent
|
||||
def test_open_server_ui_fail_closed(open_server):
|
||||
code, body = _get(open_server + "/ui")
|
||||
assert code == 503
|
||||
assert b"not configured" in body
|
||||
|
||||
|
||||
def test_open_server_legacy_json_survives(open_server):
|
||||
code, body = _get(open_server + "/status")
|
||||
assert code == 200
|
||||
code, body = _get(open_server + "/sessions")
|
||||
assert code == 200 and b"bindings" in body
|
||||
Reference in new issue
Block a user