主站 - 新增 m0006 invite_codes 迁移;注册强制邀请码(首个管理员除外),消码与建号 同一事务,并发提交只有一个能成功 - 新增 /api/hub-admin/* 服务端点(共享 HUB_ADMIN_TOKEN,先于鉴权校验),供数据 中枢桥接读写会话/密码/模型池/会员/邀请码,并提供供应商模型列表拉取 - 前端:注册表单加邀请码(桌面 login、index.html、移动端);「系统管理」改为 「数据中枢」入口指向 8766,原模型池与会员管理分区移除,仅留「行情管理」; 随之清理陈旧 CSS 数据中枢 - 取消独立账号:删除 hub_admin/hub_sessions 与登录、改密、锁定逻辑,改为校验 主站 xiaobai_session,仅管理员可进,CSRF 由会话派生,危险操作二次确认走主站 - 控制台新增数据源凭证可编辑区(原有内容一项不删)、供应商制模型池(自动拉取 /models,失败退回卡内手动录入)、会员管理与邀请码页 - 日夜双主题:颜色收敛为同名 token 换值,SVG 改用 inline style 以吃到变量 自测 - 主站 verify_baseline 通过(498 项);数据中枢 235 项通过 - tools/verify_datahub_console.py 端到端跑通两服务真实对话; tools/verify_datahub_console_ui.py 浏览器跑通门禁/凭证/模型池/会员/主题/1030 窄屏 Co-authored-by: multica-agent <github@multica.ai>
168 lines
7.2 KiB
Python
168 lines
7.2 KiB
Python
from __future__ import annotations
|
|
|
|
import json
|
|
import tempfile
|
|
import threading
|
|
import unittest
|
|
from http.server import ThreadingHTTPServer
|
|
from pathlib import Path
|
|
from urllib.request import Request, urlopen
|
|
|
|
from datahub.adapters.tushare import TushareAdapter
|
|
from datahub.crypto import SecretVault
|
|
from datahub.httpapp import make_handler
|
|
from datahub.hub import Hub
|
|
from datahub.settings import Settings
|
|
from tests.fixtures import TRADE_DATE, StubSiteAuth, fake_transport
|
|
|
|
|
|
class AdminObservabilityApiTests(unittest.TestCase):
|
|
"""HEL-543: new read-only admin endpoints for provider status, source
|
|
catalog and lineage. These must never require write access and must
|
|
never touch the existing routing/publish logic."""
|
|
|
|
def setUp(self) -> None:
|
|
self.tmp = tempfile.TemporaryDirectory()
|
|
settings = Settings(
|
|
encryption_key=SecretVault.generate_key(),
|
|
api_token="z" * 32,
|
|
tushare_token="real-tushare-token-abcdef",
|
|
db_path=Path(self.tmp.name) / "hub.db",
|
|
scheduler_enabled=False,
|
|
)
|
|
self.site_auth = StubSiteAuth()
|
|
self.hub = Hub(
|
|
settings,
|
|
adapter=TushareAdapter("real-tushare-token-abcdef", transport=fake_transport),
|
|
site_auth=self.site_auth,
|
|
)
|
|
handler = make_handler(self.hub)
|
|
self.server = ThreadingHTTPServer(("127.0.0.1", 0), handler)
|
|
threading.Thread(target=self.server.serve_forever, daemon=True).start()
|
|
self.base = f"http://127.0.0.1:{self.server.server_address[1]}"
|
|
self.cookie = f"xiaobai_session={StubSiteAuth.SESSION}"
|
|
self.csrf = self.site_auth.csrf_token(StubSiteAuth.SESSION)
|
|
|
|
def tearDown(self) -> None:
|
|
self.server.shutdown()
|
|
self.server.server_close()
|
|
self.tmp.cleanup()
|
|
|
|
def _json(self, path, method="GET", body=None, cookie="", csrf=""):
|
|
data = None if body is None else json.dumps(body).encode()
|
|
headers = {"Content-Type": "application/json"}
|
|
if cookie:
|
|
headers["Cookie"] = cookie
|
|
if csrf:
|
|
headers["X-CSRF-Token"] = csrf
|
|
req = Request(self.base + path, data=data, headers=headers, method=method)
|
|
with urlopen(req, timeout=5) as resp:
|
|
set_cookie = resp.headers.get("Set-Cookie", "")
|
|
return resp.status, json.loads(resp.read().decode()), set_cookie
|
|
|
|
def _get(self, path):
|
|
return self._json(path, cookie=self.cookie, csrf=self.csrf)
|
|
|
|
def test_providers_status_reflects_real_pipeline_activity(self) -> None:
|
|
pipeline = self.hub.pipeline
|
|
pipeline.ingest_reference(TRADE_DATE)
|
|
pipeline.run_dataset("daily", TRADE_DATE)
|
|
|
|
status, body, _ = self._get("/admin/api/providers/status")
|
|
self.assertEqual(status, 200)
|
|
health = body["health"]
|
|
self.assertTrue(any(item["provider"] == "tushare" and item["interface"] == "daily" for item in health))
|
|
row = next(item for item in health if item["provider"] == "tushare" and item["interface"] == "daily")
|
|
self.assertEqual(row["state"], "ok")
|
|
recent = body["recent_calls"]
|
|
self.assertTrue(any(item["provider"] == "tushare" and item["interface"] == "daily" for item in recent))
|
|
|
|
def test_providers_status_filters_by_provider(self) -> None:
|
|
pipeline = self.hub.pipeline
|
|
pipeline.ingest_reference(TRADE_DATE)
|
|
pipeline.run_dataset("daily", TRADE_DATE)
|
|
|
|
status, body, _ = self._get("/admin/api/providers/status?provider=tushare")
|
|
self.assertEqual(status, 200)
|
|
self.assertTrue(all(item["provider"] == "tushare" for item in body["health"]))
|
|
self.assertTrue(all(item["provider"] == "tushare" for item in body["recent_calls"]))
|
|
|
|
status, body, _ = self._get("/admin/api/providers/status?provider=eastmoney")
|
|
self.assertEqual(status, 200)
|
|
self.assertEqual(body["health"], [])
|
|
self.assertEqual(body["recent_calls"], [])
|
|
|
|
def test_source_catalog_lists_known_providers_without_leaking_secrets(self) -> None:
|
|
status, body, _ = self._get("/admin/api/source-catalog")
|
|
self.assertEqual(status, 200)
|
|
blob = json.dumps(body)
|
|
self.assertNotIn("real-tushare-token-abcdef", blob)
|
|
providers = {item["provider"] for item in body["items"]}
|
|
self.assertIn("tushare", providers)
|
|
self.assertIn("eastmoney", providers)
|
|
self.assertIn("tencent", providers)
|
|
self.assertIn("ifind", providers)
|
|
|
|
def test_lineage_snapshot_and_affected_query(self) -> None:
|
|
status, body, _ = self._get("/admin/api/lineage")
|
|
self.assertEqual(status, 200)
|
|
self.assertTrue(len(body["items"]) > 0)
|
|
datasets = {item["dataset"] for item in body["items"]}
|
|
self.assertIn("stocks", datasets)
|
|
|
|
status, body, _ = self._get("/admin/api/lineage/affected?provider=tushare&interface=daily")
|
|
self.assertEqual(status, 200)
|
|
self.assertEqual(body["provider"], "tushare")
|
|
self.assertEqual(body["interface"], "daily")
|
|
|
|
def test_disabled_kill_switch_reports_enabled_false_with_empty_structure(self) -> None:
|
|
# HEL-543 total-review 🔴: flip the runtime kill switch the same way
|
|
# Hub.__init__ wires Settings.observability_enabled onto the db
|
|
# handle, then confirm every new endpoint reports disabled with an
|
|
# explicit empty structure rather than silently going quiet.
|
|
self.hub.db.observability_enabled = False
|
|
try:
|
|
pipeline = self.hub.pipeline
|
|
pipeline.ingest_reference(TRADE_DATE)
|
|
pipeline.run_dataset("daily", TRADE_DATE) # must still fully succeed
|
|
|
|
status, body, _ = self._get("/admin/api/providers/status")
|
|
self.assertEqual(status, 200)
|
|
self.assertEqual(body, {"enabled": False, "health": [], "recent_calls": []})
|
|
|
|
status, body, _ = self._get("/admin/api/source-catalog")
|
|
self.assertEqual(status, 200)
|
|
self.assertEqual(body, {"enabled": False, "items": []})
|
|
|
|
status, body, _ = self._get("/admin/api/lineage")
|
|
self.assertEqual(status, 200)
|
|
self.assertFalse(body["enabled"])
|
|
self.assertEqual(body["items"], [])
|
|
|
|
status, body, _ = self._get("/admin/api/lineage/affected?provider=tushare")
|
|
self.assertEqual(status, 200)
|
|
self.assertEqual(
|
|
body, {"enabled": False, "provider": "tushare", "interface": "", "items": []}
|
|
)
|
|
|
|
# Nothing was ever written while disabled.
|
|
self.assertEqual(self.hub.db.fetchall("SELECT * FROM provider_call_log"), [])
|
|
finally:
|
|
self.hub.db.observability_enabled = True
|
|
|
|
def test_non_admin_site_accounts_cannot_read_the_new_endpoints(self) -> None:
|
|
from urllib.error import HTTPError
|
|
|
|
self.site_auth.add_session("member-session", StubSiteAuth.MEMBER)
|
|
with self.assertRaises(HTTPError) as ctx:
|
|
self._json("/admin/api/source-catalog", cookie="xiaobai_session=member-session")
|
|
self.assertEqual(ctx.exception.code, 403)
|
|
|
|
with self.assertRaises(HTTPError) as ctx:
|
|
self._json("/admin/api/source-catalog")
|
|
self.assertEqual(ctx.exception.code, 401)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|