s-cfc-497-share-link-generate.spec.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /**
  2. * S-cfc-497 share.link.generate 三类流 e2e 契约用例
  3. * ================================================================
  4. * SC-P-84 分享报告/方案给家人或规划师(docs/tdd/orchestration/sc-key-map.md L201)。
  5. * 载体(ShareController,统一 @PostMapping):
  6. * POST /api/share/create 生成分享记录(越权分享被拒不落库不返回链接 · S-cfc-497 c71a518cf)
  7. * body: { targetType(必填), targetId(必填,正数), shareChannel?, title? }
  8. * POST /api/share/read 记录分享阅读(越权阅读被拒不落 share_reads · S-cfc-497 c71a518cf)
  9. * body: { shareEventId, duration? } → data: { rewarded }
  10. * POST /api/share/my 我的分享记录
  11. * POST /api/share/stats 分享统计
  12. * POST /api/share/qrcode 生成分享小程序码(登录强制 · JwtInterceptor.LOGIN_REQUIRED_PATHS "/api/share/qrcode")
  13. *
  14. * 探针取证(_probe_sc497*.spec.js,gitignore 不入库,本机 127.0.0.1:9082 + 测试库实测):
  15. * ── 本环境真实契约(不以源码/台账自述为准)──
  16. * ANON_CREATE http=200 code=500「无权分享该内容」(userId=-1 匿名放行到 controller,业务拒绝;**非 401**)
  17. * ANON_READ http=200 code=200 data={rewarded:false}(readerId=-1 业务拒绝不落库;**非 401**)
  18. * ANON_MY http=200 code=200 data=[](**非 401**)
  19. * A_CREATE_SELF http=200 code=200 data={id,userId,targetType,targetId,shareChannel,shareTitle,shareUrl(16hex),status:'active',rewardPoints,clickCount:0,readCount:0,expiresAt,createdAt,updatedAt}
  20. * NO_TARGET_TYPE http=200 code=500「服务器内部错误」(params.get("targetType").toString() NPE → GlobalExceptionHandler;非 400)
  21. * NO_TARGET_ID http=200 code=500「无权分享该内容」(targetId==null → canCreateShare=false)
  22. * NEG_TARGET_ID http=200 code=500「无权分享该内容」(targetId<=0 → 拒绝)
  23. * READ_NO_ID http=200 code=200 rewarded=false(shareEventId==null → event==null → false)
  24. * READ_BAD_ID http=200 code=200 rewarded=false(不存在 shareEventId → false)
  25. * READ_BAD_DURATION http=200 code=400「For input string: \"abc\"」(Integer.parseInt → GlobalExceptionHandler 400;duration 非数字)
  26. * A_READ_SHORT rewarded=false(duration < share_read_min_duration,默认 60)
  27. * A_READ_LONG rewarded=true(duration >= minDuration;已落 share_reads + readCount++)
  28. * readCount 每次落库阅读 +1(POST /api/share/my 回读核验)
  29. * ── 越权取证(G-304-1 同族,实测)──
  30. * B_CREATE_TARGET_A http=200 code=200 SUCCESS(B 跨家庭对 A 的 target 发起分享,**越权分享未被拒**)
  31. * B_READ_A_EVENT http=200 code=200 rewarded=true + readCount++(B 跨用户阅读 A 的 share event,**越权阅读未被拒** + 奖励发放)
  32. * 根因(源码级,本轮不改生产代码):
  33. * ShareService.canCreateShare → canViewMemberReports(userId, targetId, **caller自己familyId**),
  34. * ReportMemberPermissionService.isFamilyAdmin(userId, caller自己family)=true(direct-register 建家庭者即 admin)→ 无条件放行;
  35. * 从不校验 targetId 所属家庭 == caller 家庭;familyId==null 时直接放行。
  36. * ShareService.canReadSharedTarget → 属主本人放行;否则同上 canViewMemberReports(readerId, event.targetId, reader自己familyId);
  37. * /api/share/create、/api/share/read、/api/share/my **不在 JwtInterceptor.LOGIN_REQUIRED_PATHS**,
  38. * 匿名 setAnonymousUser(userId=-1) 放行到 controller;**仅 /api/share/qrcode 在 LOGIN_REQUIRED_PATHS**。
  39. *
  40. * ── 真实缺陷·承袭登记(不为缺陷造绿)──
  41. * [G-304-1 承袭·真实缺陷未修] 越权分享/越权阅读未被拒;匿名 /api/share/create 非 401;匿名 /api/share/read 非 401;
  42. * 匿名 /api/share/my 非 401。本 spec 对「拒绝/401」写严格断言,命中即如实判红登记「承袭·真实缺陷未修」。
  43. * 注:c71a518cf 提交信息自述「越权分享/越权阅读拒绝落库(6/6绿)」,但本环境运行时探针实测相反,以实测为准。
  44. * [承袭·真实缺陷未修] 缺 targetType → code=500「服务器内部错误」(NPE,非 400 参数校验);缺 duration 非数字 → code=400 透传异常文案。
  45. * (schema.sql 的 share_events 无 event_type;本环境共享测试库 share_events 存在 legacy NOT NULL event_type,
  46. * 已为测试环境补 DEFAULT '';干净 schema 下 INSERT 不含 event_type。本 spec 不改生产源码、不改 schema.sql。)
  47. *
  48. * 【硬边界】仅写测试文件;禁 `?.`;禁直接 `new Date(string)`;探针文件保持 gitignore 不入库;不改 src/main。
  49. */
  50. const { test, expect } = require('@playwright/test');
  51. const { registerRole, apiPost, uniqueSuffix } = require('../helpers/api');
  52. test.describe('S-cfc-497 share.link.generate 三类流', () => {
  53. let userA, userB;
  54. test.beforeAll(async ({ request }) => {
  55. userA = await registerRole(request, 'parent', 'QA-S497-A' + uniqueSuffix());
  56. userB = await registerRole(request, 'parent', 'QA-S497-B' + uniqueSuffix());
  57. });
  58. // -------------------------------------------------------------------------
  59. // 正常流(生成 + 阅读)
  60. // -------------------------------------------------------------------------
  61. test('[S497-N 正常·生成] 登录用户生成分享 → 200 + 分享链接/状态/过期', async ({ request }) => {
  62. const r = await apiPost(request, '/api/share/create', {
  63. targetType: 'article', targetId: 12345, shareChannel: 'wechat', title: '分享标题',
  64. }, userA.token);
  65. expect(r.code, 'create code=200, 实际=' + r.code + ' msg=' + r.message).toBe(200);
  66. const d = r.data || {};
  67. expect(typeof d.shareUrl, '返回分享链接 shareUrl').toBe('string');
  68. expect((d.shareUrl || '').length, 'shareUrl 非空').toBeGreaterThan(0);
  69. expect(d.status, 'status=active').toBe('active');
  70. expect(d.userId, 'userId=本人').toBe(userA.userId);
  71. expect(d.targetType, 'targetType 回显').toBe('article');
  72. expect(d.targetId, 'targetId 回显').toBe(12345);
  73. expect(d.shareChannel, 'shareChannel 回显').toBe('wechat');
  74. expect(d.clickCount, 'clickCount=0').toBe(0);
  75. expect(d.readCount, 'readCount=0').toBe(0);
  76. expect(typeof d.expiresAt, 'expiresAt 过期时间').toBe('string');
  77. });
  78. test('[S497-N 正常·生成] targetType=member + targetId=本人 → 200(目标属主本人可分享)', async ({ request }) => {
  79. const r = await apiPost(request, '/api/share/create', {
  80. targetType: 'member', targetId: userA.userId, shareChannel: 'timeline', title: 'member share',
  81. }, userA.token);
  82. expect(r.code, 'member self share 200, 实际=' + r.code + ' msg=' + r.message).toBe(200);
  83. expect(r.data && r.data.targetType).toBe('member');
  84. });
  85. test('[S497-N 正常·阅读] 本人阅读自己的分享 → 200 + rewarded 语义 + readCount 落库 +1', async ({ request }) => {
  86. const c = await apiPost(request, '/api/share/create', { targetType: 'article', targetId: 424242, title: 'read-me' }, userA.token);
  87. expect(c.code, 'create 200').toBe(200);
  88. const shareEventId = c.data && c.data.id;
  89. expect(shareEventId, '拿到 shareEventId').toBeTruthy();
  90. // 短阅读:未达 share_read_min_duration(默认 60)→ rewarded=false,但仍落库 readCount+1
  91. const short = await apiPost(request, '/api/share/read', { shareEventId, duration: 5 }, userA.token);
  92. expect(short.code, 'read short 200').toBe(200);
  93. expect(short.data && short.data.rewarded, '短阅读 rewarded=false').toBe(false);
  94. // 长阅读:达阈值 → rewarded=true
  95. const long = await apiPost(request, '/api/share/read', { shareEventId, duration: 120 }, userA.token);
  96. expect(long.code, 'read long 200').toBe(200);
  97. expect(long.data && long.data.rewarded, '长阅读 rewarded=true').toBe(true);
  98. // readCount 落库核验(POST /api/share/my 回读本人分享)
  99. const my = await apiPost(request, '/api/share/my', {}, userA.token);
  100. expect(my.code, 'my 200').toBe(200);
  101. const list = Array.isArray(my.data) ? my.data : [];
  102. const ev = list.find((x) => String(x.id) === String(shareEventId));
  103. expect(ev, 'my 中含该 shareEvent').toBeTruthy();
  104. expect(ev && ev.readCount, '两次阅读 readCount=2').toBe(2);
  105. });
  106. test('[S497-N 正常] /api/share/my 仅返回本人分享;/api/share/stats 统计本人', async ({ request }) => {
  107. const my = await apiPost(request, '/api/share/my', {}, userA.token);
  108. expect(my.code).toBe(200);
  109. const list = Array.isArray(my.data) ? my.data : [];
  110. for (const ev of list) {
  111. expect(String(ev.userId), 'my 仅本人').toBe(String(userA.userId));
  112. }
  113. const stats = await apiPost(request, '/api/share/stats', {}, userA.token);
  114. expect(stats.code, 'stats 200').toBe(200);
  115. expect(typeof (stats.data && stats.data.totalShares), 'totalShares 数值').toBe('number');
  116. expect(typeof (stats.data && stats.data.totalReads), 'totalReads 数值').toBe('number');
  117. });
  118. // -------------------------------------------------------------------------
  119. // 异常流
  120. // -------------------------------------------------------------------------
  121. test('[S497-E 异常·生成] 缺 targetType → 拒绝(实测 code=500「服务器内部错误」,非 400;不造绿)', async ({ request }) => {
  122. const r = await apiPost(request, '/api/share/create', { targetId: userA.userId }, userA.token);
  123. expect(r.code, '缺 targetType → 非成功, 实际=' + r.code).not.toBe(200);
  124. expect(r.data, '无分享链接返回').toBeNull();
  125. // 真实契约取证:NPE → GlobalExceptionHandler code=500
  126. expect(r.code, '实测 code=500 服务器内部错误(缺陷:非 400 参数校验)').toBe(500);
  127. expect(r.message, '服务器内部错误').toBe('服务器内部错误');
  128. });
  129. test('[S497-E 异常·生成] 缺 targetId / targetId<=0 → code=500「无权分享该内容」不落库', async ({ request }) => {
  130. // 缺 targetId
  131. let r = await apiPost(request, '/api/share/create', { targetType: 'member' }, userA.token);
  132. expect(r.code, '缺 targetId → 500, 实际=' + r.code).toBe(500);
  133. expect(r.message).toBe('无权分享该内容');
  134. expect(r.data).toBeNull();
  135. // targetId=0
  136. r = await apiPost(request, '/api/share/create', { targetType: 'member', targetId: 0 }, userA.token);
  137. expect(r.code, 'targetId=0 → 500').toBe(500);
  138. expect(r.message).toBe('无权分享该内容');
  139. // targetId<0
  140. r = await apiPost(request, '/api/share/create', { targetType: 'member', targetId: -5 }, userA.token);
  141. expect(r.code, 'targetId<0 → 500').toBe(500);
  142. expect(r.message).toBe('无权分享该内容');
  143. });
  144. test('[S497-E 异常·阅读] 缺 shareEventId / 不存在 shareEventId → 200 rewarded=false(不落库,不 500)', async ({ request }) => {
  145. // 缺 shareEventId
  146. let r = await apiPost(request, '/api/share/read', { duration: 1 }, userA.token);
  147. expect(r.code, '缺 id → 200').toBe(200);
  148. expect(r.data && r.data.rewarded, 'rewarded=false').toBe(false);
  149. // 不存在 shareEventId
  150. r = await apiPost(request, '/api/share/read', { shareEventId: 99999999, duration: 1 }, userA.token);
  151. expect(r.code, 'bad id → 200').toBe(200);
  152. expect(r.data && r.data.rewarded, 'rewarded=false').toBe(false);
  153. });
  154. test('[S497-E 异常·阅读] duration 非数字 → code=400 拒绝(不落库)', async ({ request }) => {
  155. const c = await apiPost(request, '/api/share/create', { targetType: 'article', targetId: 555, title: 'x' }, userA.token);
  156. expect(c.code).toBe(200);
  157. const shareEventId = c.data && c.data.id;
  158. const r = await apiPost(request, '/api/share/read', { shareEventId, duration: 'abc' }, userA.token);
  159. expect(r.code, 'duration 非数字 → 400, 实际=' + r.code).toBe(400);
  160. expect(r.data).toBeNull();
  161. });
  162. // -------------------------------------------------------------------------
  163. // 权限流(严格契约 + 承袭·真实缺陷未修登记)
  164. // -------------------------------------------------------------------------
  165. test('[S497-P 权限·匿名401] 匿名 create → 401「请先登录」(严格契约;实测非401 → G-304-1 承袭判红)', async ({ request }) => {
  166. const res = await request.post('/api/share/create', { data: { targetType: 'article', targetId: 1 }, timeout: 30000 });
  167. const body = await res.json().catch(() => ({}));
  168. // 任务要求匿名 401;实测 http=200 code=500「无权分享该内容」(userId=-1 放行到业务层)。
  169. // /api/share/create 不在 JwtInterceptor.LOGIN_REQUIRED_PATHS。不造绿:保持严格断言,判红登记。
  170. expect(res.status(), '【承袭·真实缺陷未修|G-304-1】anon create→401, 实际 http=' + res.status()).toBe(401);
  171. expect(body.code, 'body.code=401').toBe(401);
  172. expect(body.message, '请先登录').toBe('请先登录');
  173. });
  174. test('[S497-P 权限·匿名401] 匿名 read → 401「请先登录」(严格契约;实测非401 → G-304-1 承袭判红)', async ({ request }) => {
  175. const res = await request.post('/api/share/read', { data: { shareEventId: 1, duration: 1 }, timeout: 30000 });
  176. const body = await res.json().catch(() => ({}));
  177. // 任务要求匿名 401;实测 http=200 code=200 rewarded=false。/api/share/read 不在 LOGIN_REQUIRED_PATHS。
  178. expect(res.status(), '【承袭·真实缺陷未修|G-304-1】anon read→401, 实际 http=' + res.status()).toBe(401);
  179. expect(body.code, 'body.code=401').toBe(401);
  180. expect(body.message, '请先登录').toBe('请先登录');
  181. });
  182. test('[S497-P 权限·匿名401] 匿名 my → 401「请先登录」(严格契约;实测非401 → G-304-1 承袭判红)', async ({ request }) => {
  183. const res = await request.post('/api/share/my', { data: {}, timeout: 30000 });
  184. const body = await res.json().catch(() => ({}));
  185. expect(res.status(), '【承袭·真实缺陷未修|G-304-1】anon my→401, 实际 http=' + res.status()).toBe(401);
  186. expect(body.code, 'body.code=401').toBe(401);
  187. });
  188. test('[S497-P 权限·越权分享] 跨用户对他人 target 生成分享 → 被拒(严格契约;G-304-1 实测未拒 → 承袭判红)', async ({ request }) => {
  189. // B(跨家庭、非 A 授权) 对 A 的 member target 发起分享
  190. const r = await apiPost(request, '/api/share/create', {
  191. targetType: 'member', targetId: userA.userId, shareChannel: 'wechat', title: 'B->A 越权',
  192. }, userB.token);
  193. // 严格契约:越权分享被拒、不落库、不返回分享链接(S-cfc-497 自述 + TC-P-84-P)。
  194. // 实测:B 为自建家庭 admin → canViewMemberReports(B,...,B自己family)=true → code=200 SUCCESS。
  195. // 不造绿:保持严格断言,判红登记「承袭·真实缺陷未修|G-304-1 跨家庭越权」。
  196. expect(r.code, '【承袭·真实缺陷未修|G-304-1】越权分享应被拒, 实际 code=' + r.code + ' msg=' + r.message).not.toBe(200);
  197. expect(r.data, '越权不返回分享链接').toBeNull();
  198. });
  199. test('[S497-P 权限·越权阅读] 跨用户阅读他人分享 → 拒绝不落 share_reads 不发放奖励(严格契约;G-304-1 实测未拒 → 承袭判红)', async ({ request }) => {
  200. // A 创建分享
  201. const c = await apiPost(request, '/api/share/create', { targetType: 'article', targetId: 909090, title: 'A only' }, userA.token);
  202. expect(c.code).toBe(200);
  203. const shareEventId = c.data && c.data.id;
  204. expect(shareEventId).toBeTruthy();
  205. // A 回读基线 readCount
  206. const myBefore = await apiPost(request, '/api/share/my', {}, userA.token);
  207. const before = ((myBefore.data || []).find((x) => String(x.id) === String(shareEventId)) || {}).readCount || 0;
  208. // B 跨用户阅读 A 的 share event(长时长)
  209. const r = await apiPost(request, '/api/share/read', { shareEventId, duration: 120 }, userB.token);
  210. expect(r.code, 'read http code=200').toBe(200);
  211. // 严格契约:越权阅读被拒不落 share_reads、不发放奖励。
  212. // 实测:rewarded=true + readCount++。不造绿:保持严格断言,判红登记「承袭·真实缺陷未修|G-304-1 跨用户越权阅读」。
  213. expect(r.data && r.data.rewarded, '【承袭·真实缺陷未修|G-304-1】越权阅读不应发放奖励 rewarded=true').toBe(false);
  214. const myAfter = await apiPost(request, '/api/share/my', {}, userA.token);
  215. const after = ((myAfter.data || []).find((x) => String(x.id) === String(shareEventId)) || {}).readCount || 0;
  216. expect(after, '【承袭·真实缺陷未修|G-304-1】越权阅读不应 readCount++').toBe(before);
  217. });
  218. });