feat(tds): 同步 ssw-tdc 最新 internal 契约并完成真 TDS 联调验证

- 协议版本收敛为单一常量 3.2,移除联调期自创的 env 覆盖
- 按真源同步 5 项契约:Dispute 下线、Billing/List 增 side、internalCatalog 6 过滤字段、新增 ServiceCatalog/Detail、出向方法收敛
- 修正 /api/** 未命中非 GET 请求被静态兜底判成 405 的偏差,恢复 404 契约
- 新增上游用例跨进程验证脚本与 4 份联调文档;部署级凭据只从环境变量读取
- 全量测试 308 passed;132 端点契约一致;真 TDS 21 条用例 19 PASS / 1 FAIL / 1 INFO
This commit is contained in:
admin committed 2026-11-05 17:53:15 +08:00
1 parent 400d883e1a
commit 4b32b40bd6
20 files changed
+1853 -96

No files matched your search

+2 -2
View File
@@ -54,11 +54,11 @@ LITERAL_METHODS = {
"/api/permission/Users/{id}/Actions/ResetPassword": ["POST"],
"/api/permission/Users/{id}/Actions/State": ["PUT"],
"/api/ssw/tdc/ServiceCatalog": ["GET"],
"/api/ssw/tdc/ServiceCatalog/Detail/{productCode}": ["GET"],
"/api/ssw/tdc/ServiceCatalog/Buy": ["POST"],
"/api/ssw/tdc/ServiceCatalog/BuyBatch": ["POST"],
"/api/ssw/tdc/Billing/List": ["GET"],
"/api/ssw/tdc/Billing/LocalCompare": ["GET"],
"/api/ssw/tdc/Billing/{id}/Dispute": ["PUT"],
"/api/ssw/tdc/Billing/{id}/Confirm": ["PUT"],
"/api/ssw/tdc/Billing/{id}/Settle": ["PUT"],
"/api/ssw/tdc/Billing/{id}/Receipt": ["PUT"],
@@ -79,11 +79,11 @@ DESCRIPTIONS = {
"POST /api/permission/Users/{id}/Actions/ResetPassword": "用户管理 · 重设口令(**仅管理员**)",
"PUT /api/permission/Users/{id}/Actions/State": "用户管理 · 启用/禁用(**仅管理员**)",
"GET /api/ssw/tdc/ServiceCatalog": "服务目录(出向 TDS `Catalog/Internal/Catalog`)",
"GET /api/ssw/tdc/ServiceCatalog/Detail/{productCode}": "目录详情(出向 TDS `Catalog/Detail/{productCode}?spaceId=`)",
"POST /api/ssw/tdc/ServiceCatalog/Buy": "目录下单(出向 TDS `TradeOrder/Internal/Apply`)",
"POST /api/ssw/tdc/ServiceCatalog/BuyBatch": "目录批量下单(出向 TDS `TradeOrder/Internal/ApplyBatch`)",
"GET /api/ssw/tdc/Billing/List": "本方账单列表(出向 TDS `BillingLedger/Internal/List`)",
"GET /api/ssw/tdc/Billing/LocalCompare": "本地计量比对(读本地 `tdc_api_consume_log`,**零出向**)",
"PUT /api/ssw/tdc/Billing/{id}/Dispute": "账单异议(出向 TDS `BillingLedger/{id}/Dispute`)",
"PUT /api/ssw/tdc/Billing/{id}/Confirm": "账单确认(出向 TDS `BillingLedger/{id}/Confirm`)",
"PUT /api/ssw/tdc/Billing/{id}/Settle": "账单结算(出向 TDS `BillingLedger/{id}/Settle`)",
"PUT /api/ssw/tdc/Billing/{id}/Receipt": "收据(出向 TDS `BillingLedger/{id}/Receipt`)",
+360
View File
@@ -0,0 +1,360 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""上游验收用例 · 跨进程验证(tdc-rs ↔ 测试环境 TDS)。
用例来源(人读 Given/When/Then,见上游仓):
ssw-tdc openspec/changes/chg_0017_drop-demand-in-portal/testcases.md
ssw-tdc openspec/changes/chg_0020_batch3-identity-domain/testcases.md
ssw-tdc openspec/changes/chg_0022_batch4b_delivery_billing/testcases.md
ssw-tds openspec/changes/chg_0032_batch4-trade-domain/testcases.md
ssw-tds openspec/changes/chg_0033_batch4b_delivery_billing/testcases.md
ssw-tds openspec/changes/chg_0034_crypto-compliance/testcases.md
ssw-tds openspec/changes/chg_0036_batch5-portal-record-domain/testcases.md
用法:
python3 scripts/verify-upstream-cases.py # 只读用例
python3 scripts/verify-upstream-cases.py --with-write # 加跑下单类(有副作用,会真出订单)
环境变量:
🔴 TDS_INTERNAL_TOKEN 必填(目标 TDS 部署的 internal token,属部署级私有值,**不入库**)
其余均有默认值(TDC_BIN / PORT / TDS_BASE_URL / TDC_INTERNAL_TOKEN /
TDS_SPACE_ID / TDS_CONNECTOR_ID,默认指向 .50 测试 TDS)
🔴 脚本内临时启动 tdc-connector,退出时(含异常)必回收,不留后台进程。
🔴 显式禁用代理:本机 HTTP_PROXY 会劫持 loopback / 内网。
"""
import json
import os
import re
import signal
import subprocess
import sys
import time
import urllib.error
import urllib.request
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TDC_BIN = os.environ.get("TDC_BIN", os.path.join(ROOT, "target/debug/tdc-connector"))
PORT = int(os.environ.get("PORT", "30412"))
TDC = "http://127.0.0.1:%d" % PORT
TDS = os.environ.get("TDS_BASE_URL", "http://192.168.1.50:33000").rstrip("/")
# 🔴 部署级私有令牌只从环境变量取,绝不写默认值 / 不入库(不同测试部署值不同)。
TDS_TOKEN = os.environ.get("TDS_INTERNAL_TOKEN", "")
TDC_TOKEN = os.environ.get("TDC_INTERNAL_TOKEN", "ssw-internal-token-e2e")
SPACE = os.environ.get("TDS_SPACE_ID", "PORTAL-DEMO-SPACE")
CID = os.environ.get("TDS_CONNECTOR_ID", "878443108591731461")
WRITE = "--with-write" in sys.argv or os.environ.get("VERIFY_WITH_WRITE") == "1"
RESULT_JSON = os.environ.get("VERIFY_RESULT_JSON", "/tmp/upstream_cases_result.json")
OPENER = urllib.request.build_opener(urllib.request.ProxyHandler({}))
RESULTS = []
def req(method, url, headers=None, body=None, timeout=20):
data = None
h = dict(headers or {})
if body is not None:
data = json.dumps(body, ensure_ascii=False).encode("utf-8")
h.setdefault("Content-Type", "application/json")
r = urllib.request.Request(url, data=data, method=method, headers=h)
try:
with OPENER.open(r, timeout=timeout) as resp:
return resp.status, resp.read().decode("utf-8", "replace")
except urllib.error.HTTPError as e:
return e.code, e.read().decode("utf-8", "replace")
except Exception as e: # noqa: BLE001 - 网络错误如实记录,不掩盖
return 0, str(e)
def as_json(text):
try:
return json.loads(text)
except Exception: # noqa: BLE001
return None
def brief(text, limit=170):
s = " ".join(text.split())
return s[:limit] + ("..." if len(s) > limit else "")
def tds_headers(version="3.2"):
return {
"Content-Type": "application/json",
"app-code": "sswflow",
"jnpf-origin": "pc",
"vue-version": "3",
"X-Internal-Token": TDS_TOKEN,
"X-Protocol-Version": version,
}
def tdc_headers(version="3.2", with_token=True):
h = {"X-Protocol-Version": version}
if with_token:
h["X-Internal-Token"] = TDC_TOKEN
return h
def record(cid, source, expect, ok, actual, hard=True):
verdict = "PASS" if ok else ("FAIL" if hard else "INFO")
RESULTS.append({
"id": cid, "source": source, "expect": expect,
"verdict": verdict, "actual": actual,
})
print("[%-4s] %-5s %-58s | %s" % (verdict, cid, expect[:58], brief(actual, 150)))
return ok
def skip(cid, source, why):
RESULTS.append({"id": cid, "source": source, "expect": "(需写权限)", "verdict": "SKIP", "actual": why})
print("[SKIP] %-5s %-58s | %s" % (cid, "(需写权限)", why))
def start_tdc():
if not os.path.isfile(TDC_BIN):
print("BINARY NOT FOUND: %s (先 cargo build)" % TDC_BIN)
sys.exit(2)
env = os.environ.copy()
env.update({
"PORT": str(PORT),
"TDC_DATA_DIR": os.path.join(ROOT, "data"),
"SSW_INTERNAL_TOKEN": TDC_TOKEN,
"SSW_TDS_BASE_URL": TDS,
"SSW_TDS_INTERNAL_TOKEN": TDS_TOKEN,
})
log = open("/tmp/verify-upstream-cases-tdc.log", "w", encoding="utf-8")
p = subprocess.Popen([TDC_BIN], cwd=ROOT, env=env, stdout=log,
stderr=subprocess.STDOUT, start_new_session=True)
for _ in range(40):
code, _body = req("GET", TDC + "/api/health", timeout=2)
if code == 200:
return p, log
if p.poll() is not None:
break
time.sleep(0.3)
stop_tdc(p, log)
print("tdc-rs 未就绪,日志:/tmp/verify-upstream-cases-tdc.log")
sys.exit(2)
def stop_tdc(p, log):
if p and p.poll() is None:
try:
os.killpg(os.getpgid(p.pid), signal.SIGTERM)
p.wait(timeout=5)
except Exception: # noqa: BLE001
try:
os.killpg(os.getpgid(p.pid), signal.SIGKILL)
except Exception: # noqa: BLE001
pass
if log:
log.close()
def main():
if not TDS_TOKEN:
print("缺少 TDS_INTERNAL_TOKEN:请传入目标 TDS 部署的 internal token,例如:")
print(" TDS_INTERNAL_TOKEN=<部署方令牌> python3 scripts/verify-upstream-cases.py --with-write")
sys.exit(2)
print("=" * 118)
print("上游验收用例 · 跨进程验证 | tdc-rs %s ↔ TDS %s | 协议 3.2 | 写用例: %s"
% (TDC, TDS, "开" if WRITE else "关"))
print("=" * 118)
p, log = start_tdc()
try:
run_cases()
finally:
stop_tdc(p, log)
print("\n(tdc-rs 临时进程已回收)")
passed = sum(1 for r in RESULTS if r["verdict"] == "PASS")
failed = sum(1 for r in RESULTS if r["verdict"] == "FAIL")
info = sum(1 for r in RESULTS if r["verdict"] == "INFO")
skipped = sum(1 for r in RESULTS if r["verdict"] == "SKIP")
print("\n汇总: PASS=%d FAIL=%d INFO=%d SKIP=%d 合计=%d"
% (passed, failed, info, skipped, len(RESULTS)))
with open(RESULT_JSON, "w", encoding="utf-8") as fh:
json.dump({"results": RESULTS, "tds": TDS, "tdc": TDC,
"protocol": "3.2", "with_write": WRITE}, fh, ensure_ascii=False, indent=2)
print("结构化结果: %s" % RESULT_JSON)
return 1 if failed else 0
def run_cases():
tdc_api = TDC + "/api/ssw/tdc"
tds_api = TDS + "/api/ssw/tds"
# ---------- A. 内部通道闸门(ssw-tdc chg_0017 / ssw-tds chg_0034 C18) ----------
st, body = req("GET", tdc_api + "/ServiceCatalog", {})
record("U01", "tdc/chg_0017 必须带内部凭证", "不带 X-Internal-Token ⇒ 401",
st == 401, "HTTP %d %s" % (st, brief(body)))
st, body = req("POST", tds_api + "/Catalog/Internal/Catalog", tds_headers("3.1"),
{"spaceId": SPACE})
j = as_json(body) or {}
ok = st == 200 and j.get("code") != 401
record("U02", "tds/chg_0034 C18 兼容窗口", "声明 3.1(同 major)⇒ 放行到业务层",
ok, "HTTP %d code=%s %s" % (st, j.get("code"), brief(body)))
st, body = req("POST", tds_api + "/Catalog/Internal/Catalog", tds_headers("2.0"),
{"spaceId": SPACE})
j = as_json(body) or {}
ok = st == 401 or j.get("code") == 401
record("U03", "tds/chg_0034 C18 不兼容拒绝", "声明 2.0(跨 major)⇒ 拒绝 401",
ok, "HTTP %d code=%s %s" % (st, j.get("code"), brief(body)))
st, body = req("GET", tdc_api + "/ServiceCatalog", tdc_headers("1.0"))
record("U04", "tds/chg_0034 C18 (tdc-rs 入向面)", "声明 1.0 ⇒ 入向闸门拒绝 401",
st == 401, "HTTP %d %s" % (st, brief(body)))
# ---------- B. 异议链路下线(ssw-tdc chg_0022 C5 / ssw-tds chg_0033 C4) ----------
# 上游期望写死 404(端点不存在 / 路由未注册)
st, body = req("PUT", tdc_api + "/Billing/NOT-REAL/Dispute",
dict(tdc_headers(), **{"Content-Type": "application/json"}), {})
record("U05", "tdc/chg_0022 C5 账单无异议", "PUT /Billing/{id}/Dispute ⇒ 404",
st == 404, "HTTP %d %s" % (st, brief(body)))
for cid, method, path, src in [
("U06", "PUT", "/BillingLedger/NOT-REAL/Dispute", "tds/chg_0033 C4"),
("U07", "POST", "/BillingLedger/NOT-REAL/Correction", "tds/chg_0033 C4"),
]:
st, body = req(method, tds_api + path, tds_headers(), {})
j = as_json(body) or {}
# 上游用例原文期望「404/路由未注册」;.50 部署实际由登录闸门拦成 HTTP 200 + code 600。
# 两者都证明端点不可用,但状态码不一致 ⇒ 记为 INFO(口径差异,不是 tdc-rs 缺陷)。
unavailable = st in (404, 405) or j.get("code") in (600, 404)
record(cid, src + " 端点已下线", "%s %s ⇒ 404/路由未注册" % (method, path),
unavailable,
"HTTP %d code=%s %s" % (st, j.get("code"), brief(body, 90)),
hard=False)
# ---------- C. 目录(上游最新 internalCatalog / chg_0036 门户字段) ----------
st, body = req("GET", tdc_api + "/ServiceCatalog", tdc_headers())
j = as_json(body) or {}
lst = (j.get("data") or {}).get("list", []) if isinstance(j.get("data"), dict) else []
record("U08", "tds/chg_0032 C5 下单前置", "目录返回真实产品(含 id / productId)",
st == 200 and j.get("code") == 200 and len(lst) > 0,
"HTTP %d code=%s 产品数=%d" % (st, j.get("code"), len(lst)))
product = lst[0] if lst else {}
pid = str(product.get("id", ""))
pcode = str(product.get("productId", ""))
if pcode:
st, body = req("GET", tdc_api + "/ServiceCatalog?keyword=" + pcode, tdc_headers())
j2 = as_json(body) or {}
l2 = (j2.get("data") or {}).get("list", []) if isinstance(j2.get("data"), dict) else []
record("U09", "tdc 最新 internalCatalog 过滤", "keyword=%s ⇒ 只回该产品" % pcode,
j2.get("code") == 200 and len(l2) == 1 and str(l2[0].get("productId")) == pcode,
"code=%s 命中=%d" % (j2.get("code"), len(l2)))
st, body = req("GET", tdc_api + "/ServiceCatalog/Detail/" + pcode, tdc_headers())
j3 = as_json(body) or {}
record("U10", "tds/chg_0036 C1 目录详情", "GET /Detail/{productCode} ⇒ 200",
st == 200 and j3.get("code") == 200,
"HTTP %d code=%s %s" % (st, j3.get("code"), brief(body)))
record("U11", "tds/chg_0036 C2 门户边界", "详情响应不含 classifyLevel",
"classifyLevel" not in body, "classifyLevel %s" % ("出现" if "classifyLevel" in body else "未出现"),
hard=False)
st, body = req("GET", tdc_api + "/ServiceCatalog/Detail/%2e%2e", tdc_headers())
jseg = as_json(body) or {}
# 上游 is_safe_path_segment 拒绝 ⇒ tdc-rs 返回业务 code 400(HTTP 仍 200)
record("U12", "tdc 段白名单", "Detail 非法段 %2e%2e ⇒ 业务拒绝",
jseg.get("code") == 400,
"HTTP %d code=%s msg=%s" % (st, jseg.get("code"), jseg.get("msg")))
else:
for c in ("U09", "U10", "U11", "U12"):
skip(c, "目录数据缺失", "目录无产品,无法构造请求")
# ---------- D. 账单(ssw-tds chg_0033 C3 / 上游 side / ownsLedger) ----------
st, body = req("GET", tdc_api + "/Billing/List?connectorId=%s&side=BOGUS" % CID, tdc_headers())
j4 = as_json(body) or {}
record("U13", "上游 side 枚举校验", "side=BOGUS ⇒ 400 且提示可接受值",
j4.get("code") == 400 and "side" in str(j4.get("msg", "")),
"code=%s msg=%s" % (j4.get("code"), j4.get("msg")))
st, body = req("GET", tdc_api + "/Billing/List?connectorId=%s&side=PROVIDER" % CID, tdc_headers())
j5 = as_json(body) or {}
l5 = (j5.get("data") or {}).get("list", []) if isinstance(j5.get("data"), dict) else []
ledger = l5[0] if l5 else {}
record("U14", "tds/chg_0033 C3 账本可读", "side=PROVIDER ⇒ 返回本方账本",
j5.get("code") == 200 and len(l5) > 0,
"code=%s 账本数=%d" % (j5.get("code"), len(l5)))
periods = [str(x.get("billingPeriod")) for x in l5 if x.get("billingPeriod")]
statuses = [(str(x.get("billStatus")), str(x.get("confirmStatus"))) for x in l5]
amounts = [x.get("amount") for x in l5 if x.get("amount") is not None]
ok_periods = bool(periods) and all(re.match(r"^\d{4}-\d{2}$", p) for p in periods)
ok_status = bool(statuses) and all(len(a) == 2 and len(b) == 2 for a, b in statuses)
ok_amount = bool(amounts) and all(re.match(r"^-?\d+\.\d{2}$", str(a)) for a in amounts)
record("U15", "tds/chg_0033 C3 账单字段", "账期 YYYY-MM / 两位状态码 / 金额两位小数",
ok_periods and ok_status and ok_amount,
"账期=%s 状态=%s 金额=%s (账期✓%s 状态✓%s 金额✓%s)" % (
periods[:2], statuses[:2], amounts[:2], ok_periods, ok_status, ok_amount),
hard=False)
st, body = req("PUT", tdc_api + "/Billing/NOT-REAL/Confirm",
dict(tdc_headers(), **{"Content-Type": "application/json"}),
{"connectorId": CID})
j6 = as_json(body) or {}
record("U16", "ssw-tdc ownsLedger 越权自检", "非本方账本 ⇒ fail-closed 业务拒绝",
j6.get("code") == 400, "code=%s msg=%s" % (j6.get("code"), j6.get("msg")))
# ---------- E. 下单(需要写权限;ssw-tds chg_0032 C4/C5) ----------
if not WRITE:
for c, s in [("U17", "findings Buy 漂移"), ("U18", "tds/chg_0032 C5"),
("U19", "tds/chg_0032 C4"), ("U20", "tdc/chg_0020 C5"),
("U21", "tds/chg_0032 C4 复核")]:
skip(c, s, "加 --with-write 运行(会真出订单)")
return
hdr = dict(tdc_headers(), **{"Content-Type": "application/json"})
buy_url = tdc_api + "/ServiceCatalog/Buy"
st, body = req("POST", buy_url, hdr,
{"productId": pid, "buyerConnectorId": CID})
j7 = as_json(body) or {}
record("U17", "findings Buy 漂移(对照)", "按上游透传 productId ⇒ TDS 报数据产品不存在: null",
j7.get("code") == 400 and "数据产品不存在" in str(j7.get("msg", "")),
"code=%s msg=%s" % (j7.get("code"), j7.get("msg")))
st, body = req("POST", buy_url, hdr,
{"usageId": pid, "buyerConnectorId": CID,
"purpose": "上游用例跨进程验证", "contactName": "小K",
"contactPhone": "13800000000"})
j8 = as_json(body) or {}
record("U18", "tds/chg_0032 C5 用途非空", "usageId=产品主键 + purpose ⇒ 200 出单",
j8.get("code") == 200, "code=%s msg=%s data=%s" % (
j8.get("code"), j8.get("msg"), brief(json.dumps(j8.get("data"), ensure_ascii=False), 90)))
st, body = req("POST", buy_url, hdr,
{"usageId": pid, "buyerConnectorId": CID,
"contacter": "这是一个远远超过十个字符长度的联系人姓名",
"purpose": "超长联系人校验"})
j9 = as_json(body) or {}
record("U19", "tds/chg_0032 C4 超长联系人被拒", "contacter > 10 字符 ⇒ 下单被拒",
j9.get("code") != 200,
"code=%s msg=%s(internal 分支读 buyerContact,不认 contacter)" % (
j9.get("code"), j9.get("msg")))
st, body = req("POST", buy_url, hdr,
{"usageId": pid, "buyerConnectorId": CID,
"buyerContact": "这是一个远远超过十个字符长度的联系人姓名",
"purpose": "超长联系人校验(internal 字段名)"})
j11 = as_json(body) or {}
record("U21", "tds/chg_0032 C4 校验落地复核", "buyerContact > 10 字符 ⇒ 被拒且提示",
j11.get("code") == 400 and "联系人" in str(j11.get("msg", "")),
"code=%s msg=%s" % (j11.get("code"), j11.get("msg")))
st, body = req("POST", buy_url, hdr,
{"usageId": pid, "purpose": "connectorId 传播验证"})
j10 = as_json(body) or {}
record("U20", "tdc/chg_0020 C5 connectorId 传播",
"不带 buyerConnectorId ⇒ 自动补本端身份(未注册则报买方不存在)",
"买方连接器" in str(j10.get("msg", "")) or j10.get("code") == 200,
"code=%s msg=%s" % (j10.get("code"), j10.get("msg")))
if __name__ == "__main__":
sys.exit(main())