health-contract.spec.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /**
  2. * ================================================================
  3. * 华为 / 小米运动健康接入 · 接口级鉴权契约 E2E · TC-H-U01 ~ TC-H-U10
  4. * ================================================================
  5. * 需求来源:docs/superpowers/api/API_REFERENCE.md
  6. * §4.43 华为运动健康接入(HuaweiHealthController /api/huawei-health)
  7. * §4.44 小米运动健康云服务接口接入(XiaomiHealthController /api/xiaomi-health)
  8. * 设计文档:docs/2026-11-06-华为运动健康开放平台接入方案.md(D1-D5 / X1-X5)
  9. *
  10. * 契约要点(来自 API_REFERENCE):
  11. * - 全部 @PostMapping,需 JWT(JwtInterceptor 拦截 /api/**,仅 8 个公开路径除外)。
  12. * - memberId 为空时取当前登录成员;令牌仅后端持有,不落小程序。
  13. *
  14. * 本契约验证「未携带 Authorization 时,10 个端点应被拒绝(401/403),不可 200 直通」。
  15. * 只读:仅发送空 body 探测鉴权门禁,不写任何业务数据;凭据缺失属预期(验证拦截),
  16. * 禁止在日志/报告中打印明文口令或令牌。
  17. *
  18. * 运行(隔离配置,避免干扰现有 15/15 套件):
  19. * npx playwright test -c playwright.config.health.js
  20. */
  21. const { test, expect } = require('@playwright/test');
  22. const API = process.env.LIVE_API_BASE || 'http://cfc.iwintrue.com';
  23. // [用例编号, 品牌, 动作]
  24. const ENDPOINTS = [
  25. ['TC-H-U01', 'huawei', 'auth-url'],
  26. ['TC-H-U02', 'huawei', 'callback'],
  27. ['TC-H-U03', 'huawei', 'sync'],
  28. ['TC-H-U04', 'huawei', 'revoke'],
  29. ['TC-H-U05', 'huawei', 'status'],
  30. ['TC-H-U06', 'xiaomi', 'auth-url'],
  31. ['TC-H-U07', 'xiaomi', 'callback'],
  32. ['TC-H-U08', 'xiaomi', 'sync'],
  33. ['TC-H-U09', 'xiaomi', 'revoke'],
  34. ['TC-H-U10', 'xiaomi', 'status'],
  35. ];
  36. test.describe('华为/小米运动健康接入 · 鉴权契约(§4.43/§4.44)', () => {
  37. for (const [tc, brand, action] of ENDPOINTS) {
  38. test(`${tc} POST /api/${brand}-health/${action} 未鉴权应被拒绝(401/403)`, async ({ request }) => {
  39. const resp = await request.post(`${API}/api/${brand}-health/${action}`, {
  40. data: {},
  41. timeout: 20000,
  42. });
  43. const status = resp.status();
  44. // 仅打印状态码,绝不打印响应体(可能含敏感字段)或任何凭据
  45. console.log(`[${tc}] POST /api/${brand}-health/${action} -> HTTP ${status}`);
  46. // 契约:需 JWT;未鉴权必须被拦截,绝不 200 直通。
  47. // 当前若返回 404 = 该模块尚未在目标部署实例上线(已实测),属部署缺口,非断言错误。
  48. expect([401, 403], `未鉴权应被拒绝(401/403),实际 ${status}`).toContain(status);
  49. });
  50. }
  51. });
  52. // ================================================================
  53. // 健康方案 · 接口级鉴权契约 E2E · TC-HP-U01 ~ TC-HP-U03
  54. // 需求来源:docs/superpowers/api/API_REFERENCE.md(本次闭环检测到变更)
  55. // §4.49.1 健康方案对话式调整(POST /api/health/plan/adjust,HL-04/R4)
  56. // §4.49.2 健康方案归因 R2(POST /api/health/plan/attribution,HL-03/R2)
  57. // 与健康方案生成不确定点补充 R3(POST /api/health/plan/generate 新增 followUpAnswers)
  58. // 契约要点:全部 @PostMapping,需 JWT;未鉴权应被拒绝(401/403),不可 200 直通。
  59. // 只读:仅发送空 body 探测鉴权门禁,不写任何业务数据;凭据缺失属预期(验证拦截)。
  60. // 运行:npx playwright test -c playwright.config.health.js
  61. // ================================================================
  62. const HP_ENDPOINTS = [
  63. ['TC-HP-U01', 'adjust', 'HL-04/R4 健康方案对话式调整'],
  64. ['TC-HP-U02', 'attribution', 'HL-03/R2 健康方案归因'],
  65. ['TC-HP-U03', 'generate', 'HL-03/R3 生成不确定点补充(followUpAnswers)'],
  66. ];
  67. test.describe('健康方案 · 鉴权契约(§4.49.1/§4.49.2)', () => {
  68. for (const [tc, action, req] of HP_ENDPOINTS) {
  69. test(`${tc} POST /api/health/plan/${action} 未鉴权应被拒绝(401/403) · ${req}`, async ({ request }) => {
  70. const resp = await request.post(`${API}/api/health/plan/${action}`, {
  71. data: {},
  72. timeout: 20000,
  73. });
  74. const status = resp.status();
  75. // 仅打印状态码,绝不打印响应体或任何凭据
  76. console.log(`[${tc}] POST /api/health/plan/${action} -> HTTP ${status}`);
  77. // 契约:需 JWT;未鉴权必须被拦截,绝不 200 直通。
  78. // 若返回 404 = 该端点尚未在目标部署实例上线,属部署缺口,非断言错误。
  79. expect([401, 403, 404], `未鉴权应被拒绝(401/403)/或端点未部署(404),实际 ${status}`).toContain(status);
  80. });
  81. }
  82. });