notification-setting-contract.spec.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // ============================================================================
  2. // [F-1111-09 · SC-P-65/79 · 能力键 notification.setting.config · TC-P-65/79]
  3. // 三流(功能 / 异常 / 权限)e2e 契约用例 —— 通知开关/提醒时间设置
  4. // 缺口来源:docs/tdd/orchestration/discovery-2026-11-30-1111.md F-1111-09
  5. // ----------------------------------------------------------------------------
  6. // 接口(API_REFERENCE.md L3593;NotificationSettingController L29/L35):
  7. // POST /api/notification/setting/get 读取本人通知设置(未配置回显默认值)
  8. // POST /api/notification/setting/save 保存本人通知设置(HH:mm 校验 + 二值开关 + 关键安全通知不可关闭)
  9. //
  10. // 契约来源(已逐条核对后端实现,只读,不改生产源码):
  11. // cfc-backend/.../service/NotificationSettingService.java(S-cfc-286 落地):
  12. // getConfig: 未配置回显默认 {enabled:1, reminderEnabled:1, reminderTime:'08:00', safetyEnabled:1};
  13. // 脏数据 time 非法按 '08:00' 回显;safety 恒按 1 回显(分发侧 isNotificationEnabled('safety') 恒 true)
  14. // saveConfig: reminderTime 非法(正则 ^([01]\d|2[0-3]):[0-5]\d$)→IllegalArgumentException→400「提醒时间格式不合法」;
  15. // safetyEnabled=0→400「关键安全通知不可关闭」;enabled/reminderEnabled 非 0/1→400「仅允许 0/1」;
  16. // 成功 200 data=true,落库 safety_enabled 恒 1
  17. // 隔离设计:userId 一律取自 JWT(@RequestAttribute),DTO 内 userId 不得改写他人配置
  18. // JwtInterceptor:/api/notification/setting/** 列匿名 401 强制族 → 匿名 get/save → http=401「请先登录」
  19. // Result<T>:业务码在 body,HTTP 恒 200(401 为拦截器 HTTP 状态)
  20. //
  21. // 真实契约探针(_probe_notification_setting.spec.js,gitignore,本地 127.0.0.1:9082 + 测试库 192.168.16.251/zxyj 实测):
  22. // 正常/异常流全部符合上述契约(25:00→400、safetyEnabled=0→400、二值→400、匿名→401 均已实测确认)。
  23. // ✦✦ 权限流【按 userId 隔离】实测为**真实缺陷**(G-304-1 同族:跨用户隔离缺失):
  24. // NotificationSettingService.selectByUserId 的「兜底」selectOne(null)(无 WHERE 全表取行)在
  25. // 当前用户**未配置**时会取到**其他用户**的已落库行,导致:
  26. // ① 跨用户读泄漏:B 未配置时 get(B) 回显 A 的已保存配置(开关/提醒时间串户);
  27. // ② 跨用户写覆盖:B 未配置时 save(B) 命中 A 的行并 updateById 改写(user_id 被改为 B,
  28. // A 的配置丢失,之后 get(A) 回显 B 的值)——DTO.userId 隔离设计落空。
  29. // 文档契约(API_REF L3593「按登录 userId 隔离」)与实现不符。按任务纪律【不造绿】:
  30. // 本 spec 的权限流按文档真实契约写**严格断言**(期望隔离生效),实测不隔离则该测试判红,
  31. // 记「承袭·真实缺陷未修」,不改断言放水(见 test 4 断言消息与汇报登记)。
  32. //
  33. // DB 依赖:notification_settings 表为 QA 独占表(自然状态为空;仅 saveConfig 建/改行)。
  34. // 为使「默认回显/隔离」行为确定可复现,测试前经 mysql CLI 清空该表(测试库 192.168.16.251/zxyj 约定,
  35. // 与 reset-db.sh / flow-l1-tests.js 直连一致)。mysql CLI 不可用 → 本 spec 明确失败(不造绿)。
  36. //
  37. // 运行:
  38. // CFC_API_BASE=http://127.0.0.1:9082 MYSQL_HOST=192.168.16.251 MYSQL_USER=zxyj MYSQL_PASS=zxyj@123 MYSQL_DB=zxyj \
  39. // npx playwright test api/notification-setting-contract.spec.js -c playwright.config.api.js --project=desktop
  40. // 安全约定:不打印 token / 手机号;清空表仅作用于测试库 QA 独占表。
  41. // ============================================================================
  42. const { test, expect } = require('@playwright/test');
  43. const { execFileSync } = require('child_process');
  44. const { apiPost } = require('../helpers/api');
  45. async function register(request, role, tag) {
  46. const phone = '13' + String(Math.floor(Math.random() * 900000000) + 100000000);
  47. let lastErr;
  48. for (let i = 0; i < 3; i++) {
  49. try {
  50. const r = await request.post('/api/auth/direct-register', {
  51. data: { phone, role, nickname: 'QA-' + tag },
  52. timeout: 60000,
  53. });
  54. const body = await r.json().catch(() => ({}));
  55. if (body.code === 200 && body.data && body.data.token) {
  56. return { token: body.data.token, userId: body.data.userId, familyId: body.data.familyId, role: body.data.role };
  57. }
  58. lastErr = body;
  59. } catch (e) {
  60. lastErr = e && e.message ? e.message : String(e);
  61. }
  62. }
  63. throw new Error('register(' + role + ') 失败: ' + JSON.stringify(lastErr));
  64. }
  65. // ---------- mysql CLI(测试库约定;不可用即失败,不造绿) ----------
  66. const MYSQL_CLI = process.env.MYSQL_CLI || 'mysql';
  67. const MYSQL_HOST = process.env.MYSQL_HOST || '192.168.16.251';
  68. const MYSQL_PORT = process.env.MYSQL_PORT || '3306';
  69. const MYSQL_USER = process.env.MYSQL_USER || 'zxyj';
  70. const MYSQL_PASS = process.env.MYSQL_PASS != null ? process.env.MYSQL_PASS : 'zxyj@123';
  71. const MYSQL_DB = process.env.MYSQL_DB || 'zxyj';
  72. function mysqlExec(sql) {
  73. const args = ['-h', MYSQL_HOST, '-P', MYSQL_PORT, '-u', MYSQL_USER];
  74. if (MYSQL_PASS !== '') args.push('-p' + MYSQL_PASS);
  75. // 实测该 mysql 客户端 --default-character-set 不生效(连接恒 latin1),每会话显式 SET NAMES utf8mb4
  76. args.push('-N', '-B', MYSQL_DB);
  77. return execFileSync(MYSQL_CLI, args, { input: 'SET NAMES utf8mb4;\n' + sql + ';\n', encoding: 'utf8', timeout: 30000 });
  78. }
  79. /** 清空 QA 独占表,保证「未配置回显默认值 / 按 userId 隔离」行为确定可复现 */
  80. function resetSettings() {
  81. mysqlExec('DELETE FROM notification_settings');
  82. }
  83. const DEFAULTS = { enabled: 1, reminderEnabled: 1, reminderTime: '08:00', safetyEnabled: 1 };
  84. function assertDtoDefaults(label, dto) {
  85. expect(dto.userId, label + ' userId 回显调用者').toBeTruthy();
  86. expect(dto.enabled, label + ' 默认全开 enabled=1').toBe(1);
  87. expect(dto.reminderEnabled, label + ' 默认 reminderEnabled=1').toBe(1);
  88. expect(dto.reminderTime, label + ' 默认提醒时间 08:00').toBe(DEFAULTS.reminderTime);
  89. expect(dto.safetyEnabled, label + ' 关键安全通知恒开=1').toBe(1);
  90. }
  91. test.describe.serial('F-1111-09 · /api/notification/setting/{get,save}(三流)', () => {
  92. test.setTimeout(180000);
  93. // -------------------------------------------------------------------------
  94. // 正常流 TC-P-65(默认回显 / 保存 / 全关后安全通知仍放行)
  95. // -------------------------------------------------------------------------
  96. test('[功能] 未配置回显默认值;合法保存生效;普通开关全关时关键安全通知恒开', async ({ request }) => {
  97. const A = await register(request, 'parent', 'ns-fn-A');
  98. resetSettings();
  99. // 未配置 → 默认值回显(全开 + 08:00 + 安全恒开)
  100. const fresh = await apiPost(request, '/api/notification/setting/get', {}, A.token);
  101. expect(fresh.status, 'get 不500').toBe(200);
  102. expect(fresh.code, 'get code=200, 实际=' + fresh.code + ' msg=' + fresh.message).toBe(200);
  103. expect(fresh.data.userId, 'userId 回显登录者本人').toBe(A.userId);
  104. assertDtoDefaults('未配置默认回显', fresh.data);
  105. // 合法保存(HH:mm + 二值开关)→ 200 true,再读回显已保存值
  106. const save = await apiPost(request, '/api/notification/setting/save', {
  107. enabled: 0, reminderEnabled: 1, safetyEnabled: 1, reminderTime: '09:30',
  108. }, A.token);
  109. expect(save.code, 'save code=200, 实际=' + save.code + ' msg=' + save.message).toBe(200);
  110. expect(save.data, 'save 返回 true').toBe(true);
  111. let back = await apiPost(request, '/api/notification/setting/get', {}, A.token);
  112. expect(back.code, '读回 code=200').toBe(200);
  113. expect(back.data.userId).toBe(A.userId);
  114. expect(back.data.enabled, '总开关关闭持久化').toBe(0);
  115. expect(back.data.reminderEnabled, '提醒开关开启持久化').toBe(1);
  116. expect(back.data.reminderTime, '提醒时间 HH:mm 持久化').toBe('09:30');
  117. expect(back.data.safetyEnabled, '安全通知恒 1').toBe(1);
  118. // 全部普通开关关闭(enabled=0 + reminderEnabled=0)仍保存成功,且安全通知不可关(恒 1)
  119. const allOff = await apiPost(request, '/api/notification/setting/save', {
  120. enabled: 0, reminderEnabled: 0, safetyEnabled: 1, reminderTime: '00:00',
  121. }, A.token);
  122. expect(allOff.code, '全关保存 code=200, 实际=' + allOff.code).toBe(200);
  123. back = await apiPost(request, '/api/notification/setting/get', {}, A.token);
  124. expect(back.data.enabled, '普通总开关可关').toBe(0);
  125. expect(back.data.reminderEnabled, '普通提醒开关可关').toBe(0);
  126. expect(back.data.safetyEnabled, '关键安全通知关闭全部后仍放行(恒 1)').toBe(1);
  127. });
  128. // -------------------------------------------------------------------------
  129. // 异常流 TC-P-65-E(HH:mm 校验 / 非二值开关 / 安全通知不可关)
  130. // -------------------------------------------------------------------------
  131. test('[异常] 非法提醒时间 25:00/8:5/缺失 → 400;非二值开关 → 400;安全通知关闭 → 400 拒绝', async ({ request }) => {
  132. const A = await register(request, 'parent', 'ns-exc-A');
  133. resetSettings();
  134. // 先建一行合法配置,保证异常流在「已配置」态验证(校验先于写库,与是否已配置无关)
  135. await apiPost(request, '/api/notification/setting/save', {
  136. enabled: 1, reminderEnabled: 1, safetyEnabled: 1, reminderTime: '08:00',
  137. }, A.token);
  138. const badTime = [
  139. ['25:00', '小时越界'],
  140. ['24:00', '小时越界(23:59 为上限)'],
  141. ['8:5', '缺前导零'],
  142. ['9:30:00', '含秒'],
  143. ['abcd', '非时间'],
  144. ['23:60', '分钟越界'],
  145. ['', '空串'],
  146. ];
  147. for (const [t, why] of badTime) {
  148. const r = await apiPost(request, '/api/notification/setting/save', {
  149. enabled: 1, reminderEnabled: 1, safetyEnabled: 1, reminderTime: t,
  150. }, A.token);
  151. expect(r.code, '非法时间 ' + JSON.stringify(t) + '(' + why + ')→400, 实际=' + r.code + ' msg=' + r.message).toBe(400);
  152. expect(r.message).toBe('提醒时间格式不合法');
  153. }
  154. // 缺失提醒时间 → 400
  155. const missing = await apiPost(request, '/api/notification/setting/save', { enabled: 1, reminderEnabled: 1, safetyEnabled: 1 }, A.token);
  156. expect(missing.code, '缺失 reminderTime→400, 实际=' + missing.code).toBe(400);
  157. expect(missing.message).toBe('提醒时间格式不合法');
  158. // 关键安全通知关不掉 → 400 拒绝(非法 25:00 与 safety=0 同时出现时,时间校验先行)
  159. const safetyOff = await apiPost(request, '/api/notification/setting/save', {
  160. enabled: 1, reminderEnabled: 1, safetyEnabled: 0, reminderTime: '08:00',
  161. }, A.token);
  162. expect(safetyOff.code, 'safetyEnabled=0→400, 实际=' + safetyOff.code + ' msg=' + safetyOff.message).toBe(400);
  163. expect(safetyOff.message).toBe('关键安全通知不可关闭');
  164. // 非二值开关 → 400
  165. const badEnabled = await apiPost(request, '/api/notification/setting/save', {
  166. enabled: 2, reminderEnabled: 1, safetyEnabled: 1, reminderTime: '08:00',
  167. }, A.token);
  168. expect(badEnabled.code, 'enabled=2→400, 实际=' + badEnabled.code).toBe(400);
  169. expect(badEnabled.message).toBe('enabled 仅允许 0/1');
  170. const badReminder = await apiPost(request, '/api/notification/setting/save', {
  171. enabled: 1, reminderEnabled: 9, safetyEnabled: 1, reminderTime: '08:00',
  172. }, A.token);
  173. expect(badReminder.code, 'reminderEnabled=9→400, 实际=' + badReminder.code).toBe(400);
  174. expect(badReminder.message).toBe('reminderEnabled 仅允许 0/1');
  175. // 异常不写库:配置未被异常请求篡改
  176. const back = await apiPost(request, '/api/notification/setting/get', {}, A.token);
  177. expect(back.data.reminderTime, '异常请求未破坏已存配置').toBe('08:00');
  178. expect(back.data.safetyEnabled).toBe(1);
  179. });
  180. // -------------------------------------------------------------------------
  181. // 权限流 TC-P-65-P / TC-P-79-P(匿名 401 / 按 userId 隔离)
  182. // -------------------------------------------------------------------------
  183. test('[权限] 匿名 get/save → http=401「请先登录」(拦截器强制登录族)', async ({ request }) => {
  184. const g = await request.post('/api/notification/setting/get', { data: {}, timeout: 30000 });
  185. expect(g.status(), '匿名 get→401, 实际=' + g.status()).toBe(401);
  186. const gBody = await g.json().catch(() => ({}));
  187. expect(gBody.code).toBe(401);
  188. expect(gBody.message).toBe('请先登录');
  189. const s = await request.post('/api/notification/setting/save', {
  190. data: { enabled: 1, reminderEnabled: 1, safetyEnabled: 1, reminderTime: '08:00' },
  191. timeout: 30000,
  192. });
  193. expect(s.status(), '匿名 save→401, 实际=' + s.status()).toBe(401);
  194. const sBody = await s.json().catch(() => ({}));
  195. expect(sBody.code).toBe(401);
  196. expect(sBody.message).toBe('请先登录');
  197. });
  198. test('[权限·按 userId 隔离] 未配置用户不得读/写他人配置(文档契约;实测缺陷 → 判红·承袭·真实缺陷未修)', async ({ request }) => {
  199. const A = await register(request, 'parent', 'ns-iso-A');
  200. const B = await register(request, 'parent', 'ns-iso-B');
  201. resetSettings();
  202. // 表清空后 A 建唯一行(insert 路径确定)
  203. const saveA = await apiPost(request, '/api/notification/setting/save', {
  204. enabled: 0, reminderEnabled: 0, safetyEnabled: 1, reminderTime: '09:30',
  205. }, A.token);
  206. expect(saveA.code, 'A 保存 code=200').toBe(200);
  207. // ---- ① 跨用户读(严格断言按文档契约:B 未配置 → 默认值回显,不泄漏 A 的配置)----
  208. const getB = await apiPost(request, '/api/notification/setting/get', {}, B.token);
  209. expect(getB.code, 'B get code=200').toBe(200);
  210. console.log('[DEFECT-PROBE] B(未配置) get 实际回显 =', JSON.stringify(getB.data));
  211. expect(
  212. getB.data.reminderTime,
  213. '【承袭·真实缺陷未修|G-304-1 同族】B 未配置应回显默认 08:00,实际泄漏 A 已保存的 09:30:' +
  214. 'NotificationSettingService.selectByUserId 兜底 selectOne(null) 无 WHERE 全表取行,跨用户读泄漏。' +
  215. '文档契约=按登录 userId 隔离(API_REF L3593)。不造绿:保持严格断言,判红登记。',
  216. ).toBe(DEFAULTS.reminderTime);
  217. // ---- ② 跨用户写(DTO.userId 伪造 A 亦不得改写 A 的配置)----
  218. const saveB = await apiPost(request, '/api/notification/setting/save', {
  219. userId: A.userId, // DTO 字段伪造他人 userId(隔离设计应忽略,仅按 JWT userId 落库)
  220. enabled: 1, reminderEnabled: 1, safetyEnabled: 1, reminderTime: '21:15',
  221. }, B.token);
  222. expect(saveB.code, 'B 保存 code=200').toBe(200);
  223. const getA = await apiPost(request, '/api/notification/setting/get', {}, A.token);
  224. console.log('[DEFECT-PROBE] A get 实际回显 =', JSON.stringify(getA.data));
  225. expect(
  226. getA.data.reminderTime,
  227. '【承袭·真实缺陷未修|G-304-1 同族】B 保存/伪造 userId 后 A 的配置应仍为 09:30,实际被 B 的行改写/串户:' +
  228. 'saveConfig 在 B 未配置时经兜底 selectOne(null) 命中 A 的行并 updateById 改写(user_id 被改为 B),' +
  229. '跨用户写覆盖。保持严格断言,判红登记,不造绿。',
  230. ).toBe('09:30');
  231. });
  232. });
  233. // 安全约定:本文件不打印 token / 手机号。