| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- /**
- * S-cfc-497 share.link.generate 三类流 e2e 契约用例
- * ================================================================
- * SC-P-84 分享报告/方案给家人或规划师(docs/tdd/orchestration/sc-key-map.md L201)。
- * 载体(ShareController,统一 @PostMapping):
- * POST /api/share/create 生成分享记录(越权分享被拒不落库不返回链接 · S-cfc-497 c71a518cf)
- * body: { targetType(必填), targetId(必填,正数), shareChannel?, title? }
- * POST /api/share/read 记录分享阅读(越权阅读被拒不落 share_reads · S-cfc-497 c71a518cf)
- * body: { shareEventId, duration? } → data: { rewarded }
- * POST /api/share/my 我的分享记录
- * POST /api/share/stats 分享统计
- * POST /api/share/qrcode 生成分享小程序码(登录强制 · JwtInterceptor.LOGIN_REQUIRED_PATHS "/api/share/qrcode")
- *
- * 探针取证(_probe_sc497*.spec.js,gitignore 不入库,本机 127.0.0.1:9082 + 测试库实测):
- * ── 本环境真实契约(不以源码/台账自述为准)──
- * ANON_CREATE http=200 code=500「无权分享该内容」(userId=-1 匿名放行到 controller,业务拒绝;**非 401**)
- * ANON_READ http=200 code=200 data={rewarded:false}(readerId=-1 业务拒绝不落库;**非 401**)
- * ANON_MY http=200 code=200 data=[](**非 401**)
- * 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}
- * NO_TARGET_TYPE http=200 code=500「服务器内部错误」(params.get("targetType").toString() NPE → GlobalExceptionHandler;非 400)
- * NO_TARGET_ID http=200 code=500「无权分享该内容」(targetId==null → canCreateShare=false)
- * NEG_TARGET_ID http=200 code=500「无权分享该内容」(targetId<=0 → 拒绝)
- * READ_NO_ID http=200 code=200 rewarded=false(shareEventId==null → event==null → false)
- * READ_BAD_ID http=200 code=200 rewarded=false(不存在 shareEventId → false)
- * READ_BAD_DURATION http=200 code=400「For input string: \"abc\"」(Integer.parseInt → GlobalExceptionHandler 400;duration 非数字)
- * A_READ_SHORT rewarded=false(duration < share_read_min_duration,默认 60)
- * A_READ_LONG rewarded=true(duration >= minDuration;已落 share_reads + readCount++)
- * readCount 每次落库阅读 +1(POST /api/share/my 回读核验)
- * ── 越权取证(G-304-1 同族,实测)──
- * B_CREATE_TARGET_A http=200 code=200 SUCCESS(B 跨家庭对 A 的 target 发起分享,**越权分享未被拒**)
- * B_READ_A_EVENT http=200 code=200 rewarded=true + readCount++(B 跨用户阅读 A 的 share event,**越权阅读未被拒** + 奖励发放)
- * 根因(源码级,本轮不改生产代码):
- * ShareService.canCreateShare → canViewMemberReports(userId, targetId, **caller自己familyId**),
- * ReportMemberPermissionService.isFamilyAdmin(userId, caller自己family)=true(direct-register 建家庭者即 admin)→ 无条件放行;
- * 从不校验 targetId 所属家庭 == caller 家庭;familyId==null 时直接放行。
- * ShareService.canReadSharedTarget → 属主本人放行;否则同上 canViewMemberReports(readerId, event.targetId, reader自己familyId);
- * /api/share/create、/api/share/read、/api/share/my **不在 JwtInterceptor.LOGIN_REQUIRED_PATHS**,
- * 匿名 setAnonymousUser(userId=-1) 放行到 controller;**仅 /api/share/qrcode 在 LOGIN_REQUIRED_PATHS**。
- *
- * ── 真实缺陷·承袭登记(不为缺陷造绿)──
- * [G-304-1 承袭·真实缺陷未修] 越权分享/越权阅读未被拒;匿名 /api/share/create 非 401;匿名 /api/share/read 非 401;
- * 匿名 /api/share/my 非 401。本 spec 对「拒绝/401」写严格断言,命中即如实判红登记「承袭·真实缺陷未修」。
- * 注:c71a518cf 提交信息自述「越权分享/越权阅读拒绝落库(6/6绿)」,但本环境运行时探针实测相反,以实测为准。
- * [承袭·真实缺陷未修] 缺 targetType → code=500「服务器内部错误」(NPE,非 400 参数校验);缺 duration 非数字 → code=400 透传异常文案。
- * (schema.sql 的 share_events 无 event_type;本环境共享测试库 share_events 存在 legacy NOT NULL event_type,
- * 已为测试环境补 DEFAULT '';干净 schema 下 INSERT 不含 event_type。本 spec 不改生产源码、不改 schema.sql。)
- *
- * 【硬边界】仅写测试文件;禁 `?.`;禁直接 `new Date(string)`;探针文件保持 gitignore 不入库;不改 src/main。
- */
- const { test, expect } = require('@playwright/test');
- const { registerRole, apiPost, uniqueSuffix } = require('../helpers/api');
- test.describe('S-cfc-497 share.link.generate 三类流', () => {
- let userA, userB;
- test.beforeAll(async ({ request }) => {
- userA = await registerRole(request, 'parent', 'QA-S497-A' + uniqueSuffix());
- userB = await registerRole(request, 'parent', 'QA-S497-B' + uniqueSuffix());
- });
- // -------------------------------------------------------------------------
- // 正常流(生成 + 阅读)
- // -------------------------------------------------------------------------
- test('[S497-N 正常·生成] 登录用户生成分享 → 200 + 分享链接/状态/过期', async ({ request }) => {
- const r = await apiPost(request, '/api/share/create', {
- targetType: 'article', targetId: 12345, shareChannel: 'wechat', title: '分享标题',
- }, userA.token);
- expect(r.code, 'create code=200, 实际=' + r.code + ' msg=' + r.message).toBe(200);
- const d = r.data || {};
- expect(typeof d.shareUrl, '返回分享链接 shareUrl').toBe('string');
- expect((d.shareUrl || '').length, 'shareUrl 非空').toBeGreaterThan(0);
- expect(d.status, 'status=active').toBe('active');
- expect(d.userId, 'userId=本人').toBe(userA.userId);
- expect(d.targetType, 'targetType 回显').toBe('article');
- expect(d.targetId, 'targetId 回显').toBe(12345);
- expect(d.shareChannel, 'shareChannel 回显').toBe('wechat');
- expect(d.clickCount, 'clickCount=0').toBe(0);
- expect(d.readCount, 'readCount=0').toBe(0);
- expect(typeof d.expiresAt, 'expiresAt 过期时间').toBe('string');
- });
- test('[S497-N 正常·生成] targetType=member + targetId=本人 → 200(目标属主本人可分享)', async ({ request }) => {
- const r = await apiPost(request, '/api/share/create', {
- targetType: 'member', targetId: userA.userId, shareChannel: 'timeline', title: 'member share',
- }, userA.token);
- expect(r.code, 'member self share 200, 实际=' + r.code + ' msg=' + r.message).toBe(200);
- expect(r.data && r.data.targetType).toBe('member');
- });
- test('[S497-N 正常·阅读] 本人阅读自己的分享 → 200 + rewarded 语义 + readCount 落库 +1', async ({ request }) => {
- const c = await apiPost(request, '/api/share/create', { targetType: 'article', targetId: 424242, title: 'read-me' }, userA.token);
- expect(c.code, 'create 200').toBe(200);
- const shareEventId = c.data && c.data.id;
- expect(shareEventId, '拿到 shareEventId').toBeTruthy();
- // 短阅读:未达 share_read_min_duration(默认 60)→ rewarded=false,但仍落库 readCount+1
- const short = await apiPost(request, '/api/share/read', { shareEventId, duration: 5 }, userA.token);
- expect(short.code, 'read short 200').toBe(200);
- expect(short.data && short.data.rewarded, '短阅读 rewarded=false').toBe(false);
- // 长阅读:达阈值 → rewarded=true
- const long = await apiPost(request, '/api/share/read', { shareEventId, duration: 120 }, userA.token);
- expect(long.code, 'read long 200').toBe(200);
- expect(long.data && long.data.rewarded, '长阅读 rewarded=true').toBe(true);
- // readCount 落库核验(POST /api/share/my 回读本人分享)
- const my = await apiPost(request, '/api/share/my', {}, userA.token);
- expect(my.code, 'my 200').toBe(200);
- const list = Array.isArray(my.data) ? my.data : [];
- const ev = list.find((x) => String(x.id) === String(shareEventId));
- expect(ev, 'my 中含该 shareEvent').toBeTruthy();
- expect(ev && ev.readCount, '两次阅读 readCount=2').toBe(2);
- });
- test('[S497-N 正常] /api/share/my 仅返回本人分享;/api/share/stats 统计本人', async ({ request }) => {
- const my = await apiPost(request, '/api/share/my', {}, userA.token);
- expect(my.code).toBe(200);
- const list = Array.isArray(my.data) ? my.data : [];
- for (const ev of list) {
- expect(String(ev.userId), 'my 仅本人').toBe(String(userA.userId));
- }
- const stats = await apiPost(request, '/api/share/stats', {}, userA.token);
- expect(stats.code, 'stats 200').toBe(200);
- expect(typeof (stats.data && stats.data.totalShares), 'totalShares 数值').toBe('number');
- expect(typeof (stats.data && stats.data.totalReads), 'totalReads 数值').toBe('number');
- });
- // -------------------------------------------------------------------------
- // 异常流
- // -------------------------------------------------------------------------
- test('[S497-E 异常·生成] 缺 targetType → 拒绝(实测 code=500「服务器内部错误」,非 400;不造绿)', async ({ request }) => {
- const r = await apiPost(request, '/api/share/create', { targetId: userA.userId }, userA.token);
- expect(r.code, '缺 targetType → 非成功, 实际=' + r.code).not.toBe(200);
- expect(r.data, '无分享链接返回').toBeNull();
- // 真实契约取证:NPE → GlobalExceptionHandler code=500
- expect(r.code, '实测 code=500 服务器内部错误(缺陷:非 400 参数校验)').toBe(500);
- expect(r.message, '服务器内部错误').toBe('服务器内部错误');
- });
- test('[S497-E 异常·生成] 缺 targetId / targetId<=0 → code=500「无权分享该内容」不落库', async ({ request }) => {
- // 缺 targetId
- let r = await apiPost(request, '/api/share/create', { targetType: 'member' }, userA.token);
- expect(r.code, '缺 targetId → 500, 实际=' + r.code).toBe(500);
- expect(r.message).toBe('无权分享该内容');
- expect(r.data).toBeNull();
- // targetId=0
- r = await apiPost(request, '/api/share/create', { targetType: 'member', targetId: 0 }, userA.token);
- expect(r.code, 'targetId=0 → 500').toBe(500);
- expect(r.message).toBe('无权分享该内容');
- // targetId<0
- r = await apiPost(request, '/api/share/create', { targetType: 'member', targetId: -5 }, userA.token);
- expect(r.code, 'targetId<0 → 500').toBe(500);
- expect(r.message).toBe('无权分享该内容');
- });
- test('[S497-E 异常·阅读] 缺 shareEventId / 不存在 shareEventId → 200 rewarded=false(不落库,不 500)', async ({ request }) => {
- // 缺 shareEventId
- let r = await apiPost(request, '/api/share/read', { duration: 1 }, userA.token);
- expect(r.code, '缺 id → 200').toBe(200);
- expect(r.data && r.data.rewarded, 'rewarded=false').toBe(false);
- // 不存在 shareEventId
- r = await apiPost(request, '/api/share/read', { shareEventId: 99999999, duration: 1 }, userA.token);
- expect(r.code, 'bad id → 200').toBe(200);
- expect(r.data && r.data.rewarded, 'rewarded=false').toBe(false);
- });
- test('[S497-E 异常·阅读] duration 非数字 → code=400 拒绝(不落库)', async ({ request }) => {
- const c = await apiPost(request, '/api/share/create', { targetType: 'article', targetId: 555, title: 'x' }, userA.token);
- expect(c.code).toBe(200);
- const shareEventId = c.data && c.data.id;
- const r = await apiPost(request, '/api/share/read', { shareEventId, duration: 'abc' }, userA.token);
- expect(r.code, 'duration 非数字 → 400, 实际=' + r.code).toBe(400);
- expect(r.data).toBeNull();
- });
- // -------------------------------------------------------------------------
- // 权限流(严格契约 + 承袭·真实缺陷未修登记)
- // -------------------------------------------------------------------------
- test('[S497-P 权限·匿名401] 匿名 create → 401「请先登录」(严格契约;实测非401 → G-304-1 承袭判红)', async ({ request }) => {
- const res = await request.post('/api/share/create', { data: { targetType: 'article', targetId: 1 }, timeout: 30000 });
- const body = await res.json().catch(() => ({}));
- // 任务要求匿名 401;实测 http=200 code=500「无权分享该内容」(userId=-1 放行到业务层)。
- // /api/share/create 不在 JwtInterceptor.LOGIN_REQUIRED_PATHS。不造绿:保持严格断言,判红登记。
- expect(res.status(), '【承袭·真实缺陷未修|G-304-1】anon create→401, 实际 http=' + res.status()).toBe(401);
- expect(body.code, 'body.code=401').toBe(401);
- expect(body.message, '请先登录').toBe('请先登录');
- });
- test('[S497-P 权限·匿名401] 匿名 read → 401「请先登录」(严格契约;实测非401 → G-304-1 承袭判红)', async ({ request }) => {
- const res = await request.post('/api/share/read', { data: { shareEventId: 1, duration: 1 }, timeout: 30000 });
- const body = await res.json().catch(() => ({}));
- // 任务要求匿名 401;实测 http=200 code=200 rewarded=false。/api/share/read 不在 LOGIN_REQUIRED_PATHS。
- expect(res.status(), '【承袭·真实缺陷未修|G-304-1】anon read→401, 实际 http=' + res.status()).toBe(401);
- expect(body.code, 'body.code=401').toBe(401);
- expect(body.message, '请先登录').toBe('请先登录');
- });
- test('[S497-P 权限·匿名401] 匿名 my → 401「请先登录」(严格契约;实测非401 → G-304-1 承袭判红)', async ({ request }) => {
- const res = await request.post('/api/share/my', { data: {}, timeout: 30000 });
- const body = await res.json().catch(() => ({}));
- expect(res.status(), '【承袭·真实缺陷未修|G-304-1】anon my→401, 实际 http=' + res.status()).toBe(401);
- expect(body.code, 'body.code=401').toBe(401);
- });
- test('[S497-P 权限·越权分享] 跨用户对他人 target 生成分享 → 被拒(严格契约;G-304-1 实测未拒 → 承袭判红)', async ({ request }) => {
- // B(跨家庭、非 A 授权) 对 A 的 member target 发起分享
- const r = await apiPost(request, '/api/share/create', {
- targetType: 'member', targetId: userA.userId, shareChannel: 'wechat', title: 'B->A 越权',
- }, userB.token);
- // 严格契约:越权分享被拒、不落库、不返回分享链接(S-cfc-497 自述 + TC-P-84-P)。
- // 实测:B 为自建家庭 admin → canViewMemberReports(B,...,B自己family)=true → code=200 SUCCESS。
- // 不造绿:保持严格断言,判红登记「承袭·真实缺陷未修|G-304-1 跨家庭越权」。
- expect(r.code, '【承袭·真实缺陷未修|G-304-1】越权分享应被拒, 实际 code=' + r.code + ' msg=' + r.message).not.toBe(200);
- expect(r.data, '越权不返回分享链接').toBeNull();
- });
- test('[S497-P 权限·越权阅读] 跨用户阅读他人分享 → 拒绝不落 share_reads 不发放奖励(严格契约;G-304-1 实测未拒 → 承袭判红)', async ({ request }) => {
- // A 创建分享
- const c = await apiPost(request, '/api/share/create', { targetType: 'article', targetId: 909090, title: 'A only' }, userA.token);
- expect(c.code).toBe(200);
- const shareEventId = c.data && c.data.id;
- expect(shareEventId).toBeTruthy();
- // A 回读基线 readCount
- const myBefore = await apiPost(request, '/api/share/my', {}, userA.token);
- const before = ((myBefore.data || []).find((x) => String(x.id) === String(shareEventId)) || {}).readCount || 0;
- // B 跨用户阅读 A 的 share event(长时长)
- const r = await apiPost(request, '/api/share/read', { shareEventId, duration: 120 }, userB.token);
- expect(r.code, 'read http code=200').toBe(200);
- // 严格契约:越权阅读被拒不落 share_reads、不发放奖励。
- // 实测:rewarded=true + readCount++。不造绿:保持严格断言,判红登记「承袭·真实缺陷未修|G-304-1 跨用户越权阅读」。
- expect(r.data && r.data.rewarded, '【承袭·真实缺陷未修|G-304-1】越权阅读不应发放奖励 rewarded=true').toBe(false);
- const myAfter = await apiPost(request, '/api/share/my', {}, userA.token);
- const after = ((myAfter.data || []).find((x) => String(x.id) === String(shareEventId)) || {}).readCount || 0;
- expect(after, '【承袭·真实缺陷未修|G-304-1】越权阅读不应 readCount++').toBe(before);
- });
- });
|