| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- /**
- * SC-P-66 / S-cfc-505 privacy.setting.config 三类流 e2e 契约用例
- * ================================================================
- * 接口(PrivacySettingController L23):
- * POST /api/privacy/setting/get 读取本人隐私设置(未配置回显最小权限默认值)
- * POST /api/privacy/setting/save 保存本人数据共享范围(dataSharingScope ∈ {0,1,2},敏感字段不外露)
- *
- * 探针取证(_probe_sc_p66_p70.spec.js,gitignore 不入库,远程 cfc.iwintrue.com 实测):
- * ANON_GET http=200 code=200 userId=-1 data_scope=2 ← B-66-1 实际行为
- * ANON_SAVE http=200 code=200 data=true ← B-66-1 实际行为
- * NORMAL_GET 200 data={userId, dataSharingScope, sensitiveFieldsExposed=0, ...}
- * SAVE_CYCLE 200 true for scope∈{0,1,2};敏感字段恒 0
- * BAD_SCOPE 400 「数据共享范围不合法」 (-1/3/null/undefined 均拒)
- * EMPTY_BODY 400 「数据共享范围不合法」(saveConfig dto==null 分支不触发——body={} 时
- * dataSharingScope 为 undefined,走 else 分支抛出 IllegalArgumentException)
- * SENS_REJ 400 「敏感字段不可外露」;read-back sensitiveFieldsExposed=0
- * SPOOF_UID 200(DTO.userId 被忽略,仅 JWT userId 写入);A.get 不受影响
- * CROSS_B_INIT 200 userId=新B.userId dataSharingScope=已有某用户的已存值(泄漏!)
- * CROSS_A_OVERWRITTEN B.save 改写 A 的 scope;A.get 读回非 baseline(G-304-1 同族缺陷)
- *
- * ── 真实缺陷·承袭登记(不为缺陷造绿)──
- * [B-66-1] 匿名访问 /api/privacy/setting/{get,save} 返回 http=200 + code=200(userId=-1);
- * 端点未列入 JwtInterceptor.LOGIN_REQUIRED_PATHS,违反 TC-P-66-P「匿名 401」契约。
- * [B-66-2] 跨用户隔离失效(G-304-1 同族):
- * PrivacySettingService.selectByUserId 在 selectOne(userId) 异常时 fallback 至
- * selectOne(null) 全表取行,导致新注册用户未配置时泄漏其他用户已存记录;
- * 进一步 B 的 save 可覆盖 A 的行(A.get 回显被改写)。
- * 本 spec 对隔离断言为严格契约,若命中则如实判红并标注「承袭·真实缺陷未修」。
- *
- * 【硬边界】仅写测试文件;禁 `?.`;禁直接 `new Date(string)`;探针文件保持 gitignore 不入库。
- */
- const { test, expect } = require('@playwright/test');
- const { registerRole, apiPost, uniqueSuffix } = require('../helpers/api');
- test.describe('SC-P-66 隐私设置三类流', () => {
- let userA, userB;
- test.beforeAll(async ({ request }) => {
- userA = await registerRole(request, 'parent', 'QA-P66-A' + uniqueSuffix());
- userB = await registerRole(request, 'parent', 'QA-P66-B' + uniqueSuffix());
- });
- // -------------------------------------------------------------------------
- // 正常流(TC-P-66-N)
- // -------------------------------------------------------------------------
- test('[P-66-N 正常] get 返回当前用户隐私配置(userId 匹配登录者、敏感字段恒不外露)', async ({ request }) => {
- const r = await apiPost(request, '/api/privacy/setting/get', {}, userA.token);
- expect(r.code, 'get code=200, 实际=' + r.code).toBe(200);
- expect(r.data && r.data.userId, 'userId 回显调用者').toBe(userA.userId);
- // 敏感字段在外露兜底恒为 0(最小权限保底)
- expect(r.data && r.data.sensitiveFieldsExposed, 'sensitiveFieldsExposed 恒 0').toBe(0);
- });
- test('[P-66-N 正常] 合法 scope 0/1/2 save → 200 true;get 回显已保存 scope', async ({ request }) => {
- const validScopes = [0, 1, 2];
- for (const scope of validScopes) {
- const s = await apiPost(request, '/api/privacy/setting/save', { dataSharingScope: scope }, userA.token);
- expect(s.code, 'save scope=' + scope + ' code=200, 实际=' + s.code).toBe(200);
- expect(s.data, 'save scope=' + scope + ' data=true').toBe(true);
- const g = await apiPost(request, '/api/privacy/setting/get', {}, userA.token);
- expect(g.code, 'get after save scope=' + scope + ' code=200').toBe(200);
- expect(g.data.dataSharingScope, 'get 回显 scope=' + scope).toBe(scope);
- }
- });
- test('[P-66-N 正常] sensitiveFieldsExposed=1 一律拒绝且不落库', async ({ request }) => {
- const s = await apiPost(request, '/api/privacy/setting/save', { dataSharingScope: 1, sensitiveFieldsExposed: 1 }, userA.token);
- expect(s.code, 'save sensitiveFieldsExposed=1 → 400, 实际=' + s.code).toBe(400);
- expect(s.message, '拒绝原因应为最小权限保底').toBe('敏感字段不可外露');
- // 确认后续 get 不泄漏敏感字段(sensitiveFieldsExposed 恒 0)
- const g = await apiPost(request, '/api/privacy/setting/get', {}, userA.token);
- expect(g.data.sensitiveFieldsExposed, 'read-back sensitiveFieldsExposed=0').toBe(0);
- });
- // -------------------------------------------------------------------------
- // 异常流(TC-P-66-E)
- // -------------------------------------------------------------------------
- test('[P-66-E 异常] 非法 scope(-1/3/null/undefined) 全部被 400 拒绝,文案含「数据共享范围不合法」', async ({ request }) => {
- // null 显式传值 + undefined(body={})覆盖 service 中 scope == null 分支;两者均应触发 IllegalArgumentException
- const badValues = [-1, 3, null];
- for (const v of badValues) {
- const r = await apiPost(request, '/api/privacy/setting/save', { dataSharingScope: v }, userA.token);
- expect(r.code, 'scope=' + JSON.stringify(v) + ' → 400, 实际=' + r.code).toBe(400);
- expect(r.message, 'scope=' + JSON.stringify(v) + ' 文案').toBe('数据共享范围不合法');
- }
- // undefined:完整缺省 body {} → dataSharingScope 为 undefined;service 中 scope == null 判定同样触发
- const rUndefined = await apiPost(request, '/api/privacy/setting/save', {}, userA.token);
- expect(rUndefined.code, 'undefined scope → 400, 实际=' + rUndefined.code).toBe(400);
- expect(rUndefined.message, 'undefined scope 文案').toBe('数据共享范围不合法');
- });
- test('[P-66-E 异常] 空体 / 缺 dataSharingScope → 400 拒绝(saveConfig 校验 DTO 不能为空)', async ({ request }) => {
- // 完整缺省 body {}:dataSharingScope 为 undefined → service 中 scope == null 分支触发
- const r = await apiPost(request, '/api/privacy/setting/save', {}, userA.token);
- expect(r.code, '空 body → 400, 实际=' + r.code + ' msg=' + r.message).toBe(400);
- expect(r.message).toBe('数据共享范围不合法');
- });
- test('[P-66-E 异常] DTO.userId 伪造他人不得改写他人配置(controller 层以 JWT userId 为准)', async ({ request }) => {
- // 先拿到 A 的真实配置
- const ga = await apiPost(request, '/api/privacy/setting/get', {}, userA.token);
- expect(ga.code).toBe(200);
- const beforeScope = ga.data.dataSharingScope;
- // 伪造 body.userId=B 发起 save
- const s = await apiPost(request, '/api/privacy/setting/save', { userId: userB.userId, dataSharingScope: 2 }, userA.token);
- expect(s.code, 'spoof userId save → 200(JWT userId 仍为主键)').toBe(200);
- // A 的配置必须仍以 JWT userId 为准;body.userId 被忽略
- const gb = await apiPost(request, '/api/privacy/setting/get', {}, userA.token);
- expect(gb.code).toBe(200);
- expect(gb.data.userId, 'userId 始终来自 JWT,不会被 body 改写').toBe(userA.userId);
- expect(gb.data.dataSharingScope, 'A scope 不因 spoof save 改变').toBe(beforeScope);
- });
- // -------------------------------------------------------------------------
- // 权限流(TC-P-66-P)
- // -------------------------------------------------------------------------
- // ★ 实测(_probe_sc_p66_p70.spec.js):anon get/save 返回 http=200 code=200 userId=-1
- // 端点未在 JwtInterceptor.LOGIN_REQUIRED_PATHS 中,违反 TC-P-66-P。
- // 本用例按文档契约写严格断言,命中即如实标记「承袭·真实缺陷未修」。
- test('[P-66-P 权限] 匿名 get → 401「请先登录」(TC-P-66-P 要求;端点应列入 LOGIN_REQUIRED_PATHS)', async ({ request }) => {
- const r = await request.post('/api/privacy/setting/get', { data: {}, timeout: 30000 });
- const body = await r.json().catch(() => ({}));
- expect(r.status(), 'anon get→401, 实际 http=' + r.status()).toBe(401);
- expect(body.code, 'anon get body.code=401').toBe(401);
- expect(body.message, 'anon get message').toBe('请先登录');
- });
- test('[P-66-P 权限] 匿名 save → 401「请先登录」(TC-P-66-P 要求)', async ({ request }) => {
- const r = await request.post('/api/privacy/setting/save', {
- data: { dataSharingScope: 1 }, timeout: 30000,
- });
- const body = await r.json().catch(() => ({}));
- expect(r.status(), 'anon save→401, 实际 http=' + r.status()).toBe(401);
- expect(body.code, 'anon save body.code=401').toBe(401);
- expect(body.message, 'anon save message').toBe('请先登录');
- });
- test('[P-66-P 权限·跨用户隔离] B 的 get 应独立于 A(严格契约;B-66-2 跨用户隔离缺陷 → 承袭判红)', async ({ request }) => {
- // 约定:B 首次 get 应回显最小权限默认值 {dataSharingScope:0, ...}
- // 若 B-66-2 未修复,B 会读到 A 或其他用户的已有行(selectOne(null) 兜底泄漏)
- const getB = await apiPost(request, '/api/privacy/setting/get', {}, userB.token);
- expect(getB.code, 'B get code=200').toBe(200);
- console.log(
- '[DEFECT-PROBE] B(未主动配置) get 实际回显=' + JSON.stringify(getB.data)
- + ';若 dataSharingScope≠0 说明跨用户隔离未生效(G-304-1 同族缺陷)。'
- );
- // 严格按文档契约:新用户应看到默认最小权限
- expect(
- getB.data.dataSharingScope,
- '【承袭·真实缺陷未修|B-66-2】B 未配置时应回显默认最小权限 0,'
- + '而非 A 等其他用户的已保存 scope。PrivacySettingService.selectByUserId fallback selectOne(null) 全表取行。'
- + '不造绿:保持断言,判红登记。',
- ).toBe(0);
- });
- test('[P-66-P 权限·跨用户写覆盖] B 的 save 不得改写 A 已存的 scope(严格契约;B-66-2 缺陷 → 承袭判红)', async ({ request }) => {
- // 前置:确保 A 有一次明确的 save(scope=1),作为基准
- const saveA1 = await apiPost(request, '/api/privacy/setting/save', { dataSharingScope: 1 }, userA.token);
- expect(saveA1.code).toBe(200);
- const getABefore = await apiPost(request, '/api/privacy/setting/get', {}, userA.token);
- const baseline = getABefore.data.dataSharingScope;
- // B 保存另一值
- const saveB = await apiPost(request, '/api/privacy/setting/save', { dataSharingScope: 2 }, userB.token);
- expect(saveB.code).toBe(200);
- // A 的配置不应因 B 的 save 而改变
- const getAAfter = await apiPost(request, '/api/privacy/setting/get', {}, userA.token);
- console.log(
- '[DEFECT-PROBE] A get after B save → dataSharingScope=' + getAAfter.data.dataSharingScope
- + '(baseline=' + baseline + ');不等说明跨用户写覆盖泄漏。'
- );
- expect(
- getAAfter.data.dataSharingScope,
- '【承袭·真实缺陷未修|B-66-2】A 的配置不应受 B save 影响。'
- + 'B 未配置时的 fallback selectOne(null) 可能命中 A 的行并 updateById 覆盖。'
- + '不造绿:保持严格断言,判红登记。',
- ).toBe(baseline);
- });
- });
|