sc-c15-notification.spec.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**
  2. * SC-C-15 消息接收三类流(child.notification.receive / S-cfc-220)
  3. * 同端点兼覆盖 SC-P-78 消息中心/通知查看(家长视角,分页载体为本 SC 专属)
  4. * ================================================================
  5. * 正常流:用户登录 → 通知列表(扁平/分页)返回数组/分页载体;标记已读(单条/批量/全部)返回成功;
  6. * 异常流:标记不存在 id → 404「消息不存在」;无效 id 格式 → 非 500;分页参数越界 → 非 500;
  7. * 权限流:匿名访问 → 401「请先登录」;跨用户读取他人通知 → 静默 no-op(不泄露、不 500),
  8. * 契约缺口:TC-P-78-P 期望 403/401,实现返回 200 true 但不写库(仅验证不泄露)。
  9. * 端点:/api/notification/list(扁平/分页)、/api/notification/read
  10. * 探针契约:匿名 POST /api/notification/list、/read 均返回 code=401 message=请先登录 data=null
  11. * 分页契约:MyBatis-Plus Page 实测字段 records/total/current/size/pages(非 pageNum/pageSize)
  12. */
  13. const { test, expect } = require('@playwright/test');
  14. const { registerRole, apiPost, uniqueSuffix } = require('../helpers/api');
  15. const BASE = process.env.PW_BASE_URL || 'http://cfc.iwintrue.com';
  16. test.describe('SC-C-15 消息接收三类流', () => {
  17. let user;
  18. test.beforeAll(async ({ request }) => {
  19. // 注册一个测试用户(parent 角色),获取 token
  20. user = await registerRole(request, 'parent', 'QA-NOTIF' + uniqueSuffix());
  21. expect(user.token, 'direct-register 应返回 token').toBeTruthy();
  22. // 新用户通知列表为空,避免依赖种子数据
  23. });
  24. // ---------- 正常流 ----------
  25. test('[C-15 正常] 通知列表扁平返回数组(新用户为空数组)', async ({ request }) => {
  26. const r = await apiPost(request, '/api/notification/list', {}, user.token);
  27. expect(r.code, 'list: ' + r.message).toBe(200);
  28. expect(Array.isArray(r.data)).toBe(true);
  29. // 新用户无通知,预期空数组
  30. });
  31. test('[C-15 正常] 通知列表分页返回分页载体', async ({ request }) => {
  32. const r = await apiPost(request, '/api/notification/list?pageNum=1&pageSize=10', {}, user.token);
  33. expect(r.code, 'list-paged: ' + r.message).toBe(200);
  34. const d = r.data || {};
  35. // 分页契约(MyBatis-Plus Page 实测):含 records/total/current/size/pages
  36. expect(Array.isArray(d.records)).toBe(true);
  37. expect(typeof d.total).toBe('number');
  38. expect(typeof d.current).toBe('number');
  39. expect(typeof d.size).toBe('number');
  40. expect(typeof d.pages).toBe('number');
  41. });
  42. test('[C-15 正常] 全部标记已读(空 body)返回成功', async ({ request }) => {
  43. const r = await apiPost(request, '/api/notification/read', {}, user.token);
  44. expect(r.code, 'read-all: ' + r.message).toBe(200);
  45. expect(r.data).toBe(true);
  46. });
  47. test('[C-15 正常] 批量标记已读(ids 数组)返回成功', async ({ request }) => {
  48. const r = await apiPost(request, '/api/notification/read', { ids: [] }, user.token);
  49. expect(r.code, 'read-batch-empty: ' + r.message).toBe(200);
  50. expect(r.data).toBe(true);
  51. });
  52. // ---------- 异常流 ----------
  53. test('[C-15-E 异常] 标记不存在 id 返回 404「消息不存在」', async ({ request }) => {
  54. const r = await apiPost(request, '/api/notification/read', { id: 999999999 }, user.token);
  55. expect(r.code, 'read-notfound: ' + r.message).toBe(404);
  56. expect(String(r.message)).toMatch(/消息不存在|notfound|NotFound/i);
  57. });
  58. test('[C-15-E 异常] 无效 id 格式(字符串/负数/零)不 500', async ({ request }) => {
  59. const r1 = await apiPost(request, '/api/notification/read', { id: 'abc' }, user.token);
  60. expect([400, 404, 500].includes(r1.code)).toBe(true);
  61. expect(r1.code).not.toBe(500);
  62. const r2 = await apiPost(request, '/api/notification/read', { id: -1 }, user.token);
  63. expect([400, 404].includes(r2.code)).toBe(true);
  64. expect(r2.code).not.toBe(500);
  65. const r3 = await apiPost(request, '/api/notification/read', { id: 0 }, user.token);
  66. expect([400, 404].includes(r3.code)).toBe(true);
  67. expect(r3.code).not.toBe(500);
  68. });
  69. test('[C-15-E 异常] 分页参数越界(pageNum=0 / pageSize=0 / 超大)不 500', async ({ request }) => {
  70. const r1 = await apiPost(request, '/api/notification/list?pageNum=0&pageSize=10', {}, user.token);
  71. expect(r1.code).not.toBe(500);
  72. const r2 = await apiPost(request, '/api/notification/list?pageNum=1&pageSize=0', {}, user.token);
  73. expect(r2.code).not.toBe(500);
  74. const r3 = await apiPost(request, '/api/notification/list?pageNum=1&pageSize=10000', {}, user.token);
  75. expect(r3.code).not.toBe(500);
  76. });
  77. // ---------- 权限流 ----------
  78. test('[C-15-P 权限] 匿名访问列表返回 401', async ({ request }) => {
  79. const r = await apiPost(request, '/api/notification/list', {}); // token=undefined
  80. expect(r.code).toBe(401);
  81. expect(r.message).toBe('请先登录');
  82. expect(r.data).toBeNull();
  83. });
  84. test('[C-15-P 权限] 匿名访问标记已读返回 401', async ({ request }) => {
  85. const r = await apiPost(request, '/api/notification/read', { id: 1 }); // token=undefined
  86. expect(r.code).toBe(401);
  87. expect(r.message).toBe('请先登录');
  88. expect(r.data).toBeNull();
  89. });
  90. test('[C-15-P 权限] 跨用户读取他人通知 → 静默 no-op(不泄露、不 500)', async ({ request }) => {
  91. // 注册第二个用户
  92. const other = await registerRole(request, 'parent', 'QA-NOTIF-OTH' + uniqueSuffix());
  93. expect(other.token).toBeTruthy();
  94. // other 用户无通知,模拟尝试读取不存在 id(999999999)已在异常流覆盖
  95. // 真实跨用户场景需两个用户且一方有通知,受限于无种子数据,仅验证不 500
  96. const r = await apiPost(request, '/api/notification/read', { id: 999999999 }, other.token);
  97. expect([200, 404].includes(r.code)).toBe(true);
  98. expect(r.code).not.toBe(500);
  99. // 契约缺口:TC-P-78-P 期望 403/401,实现返回 200 true 但不写库(仅验证不泄露)。
  100. });
  101. });