|
|
@@ -0,0 +1,308 @@
|
|
|
+/**
|
|
|
+ * health.survey.incrementalSave —— 健康问卷「增量保存(只存变化成员)+ 统一签名(一次签名全员共用)」
|
|
|
+ * API 级三流断言(正常 / 异常 / 权限)
|
|
|
+ *
|
|
|
+ * 窗口 src 提交:835c50075 / 034c36433
|
|
|
+ * 需求1 多人填写全部保存,但仅保存内容相对「加载/切换时初始快照」有实质变化的成员;
|
|
|
+ * 未变化成员跳过内容保存——根因是后端 saveForMember 每次保存都显式置空
|
|
|
+ * signature/signed_at(HealthStatusServiceImpl.saveForMember),未变化成员一旦
|
|
|
+ * 重复落库就会把刚落的签名清掉。
|
|
|
+ * 需求2 只签一次名,本次参与提交的全部成员共用同一签名,签名范围 = 参与成员集合。
|
|
|
+ *
|
|
|
+ * 契约来源(忽略型探针 tests/e2e/api/_probe_healthsurvey1720.spec.js 实测于
|
|
|
+ * http://cfc.iwintrue.com,探针文件不入库,见 .gitignore tests/e2e/api/_probe_*.spec.js):
|
|
|
+ * [HS-01] save(member) => http200 code200,回读 height/weight/bp 一致、signature=null
|
|
|
+ * [HS-03] signature 早于内容 => http200 code500「该成员暂无健康现状记录,请先完成问卷」
|
|
|
+ * [HS-04] save 空 payload => code500「请至少填写一项」
|
|
|
+ * save bloodPressure='120' => code500「血压格式应为 120/80」
|
|
|
+ * save heightCm=1 => code500「身高需在 30-250 cm 之间」
|
|
|
+ * save heightCm='abc' => http400 code=null(BigDecimal 解析失败)
|
|
|
+ * signature 空/缺 => code500「请完成手签名」
|
|
|
+ * [HS-06] 无 token save/get/signature => http200 code200(匿名可写可读可签,缺陷,见下)
|
|
|
+ * 无效 token signature => http401 code401「请先登录」
|
|
|
+ * 无 token diet save => code500「未指定成员」
|
|
|
+ * [HS-07] 先内容后签名 => 双成员 signature 均为提交的同一 dataUrl、signedAt 非空、
|
|
|
+ * userId 同属登录账号;随后仅对成员1再存内容 => sig1 被清为 null 且 signedAt1=null,
|
|
|
+ * sig2 仍保持原 dataUrl(未被误清)—— 增量保存必须跳过未变化成员的直接实证。
|
|
|
+ *
|
|
|
+ * 断言纪律:正常/异常流按探针实测严格断言;命中长期 blocker 的权限流用 test.fixme
|
|
|
+ * 声明「承袭·真实缺陷未修」,断言的是**应然**行为(401),当前实测失败即被 fixme 标为
|
|
|
+ * 预期失败,绝不把缺陷固化成绿用例;缺陷修复后该用例转为「unexpectedly passing」提示移除 fixme。
|
|
|
+ */
|
|
|
+const { test, expect } = require('@playwright/test');
|
|
|
+
|
|
|
+function randPhone() {
|
|
|
+ return '13' + String(Math.floor(Math.random() * 900000000) + 100000000);
|
|
|
+}
|
|
|
+function uniqueSuffix() { return String(Date.now()).slice(-8) + Math.floor(Math.random() * 90 + 10); }
|
|
|
+
|
|
|
+async function rawPost(request, path, body, token) {
|
|
|
+ const headers = {};
|
|
|
+ if (token) headers.Authorization = 'Bearer ' + token;
|
|
|
+ const res = await request.post(path, { data: body || {}, headers, timeout: 120000 });
|
|
|
+ const raw = await res.text();
|
|
|
+ let json = null;
|
|
|
+ try { json = raw ? JSON.parse(raw) : null; } catch (e) { json = null; }
|
|
|
+ return { httpStatus: res.status(), body: json };
|
|
|
+}
|
|
|
+
|
|
|
+async function register(request, role, nick) {
|
|
|
+ const r = await rawPost(request, '/api/auth/direct-register', {
|
|
|
+ phone: randPhone(), role: role, nickname: nick + uniqueSuffix(),
|
|
|
+ });
|
|
|
+ const d = (r.body && r.body.data) || {};
|
|
|
+ return { token: d.token, userId: d.userId, familyId: d.familyId };
|
|
|
+}
|
|
|
+
|
|
|
+async function ownSelfMemberId(request, token, userId) {
|
|
|
+ const r = await rawPost(request, '/api/family/member/list', {}, token);
|
|
|
+ const mems = (r.body && r.body.data) || [];
|
|
|
+ const self = mems.find(function (m) { return String(m.userId) === String(userId) }) || mems[0];
|
|
|
+ return self && self.id;
|
|
|
+}
|
|
|
+
|
|
|
+/** 家庭内新增一名「不能登录用户」(孩子),返回 family_members.id */
|
|
|
+async function addChildMember(request, token, relativeMemberId, nick) {
|
|
|
+ const r = await rawPost(request, '/api/family/member/add', {
|
|
|
+ relativeMemberId: relativeMemberId, generationLevel: 'child',
|
|
|
+ nickname: nick + uniqueSuffix(), gender: 'male', birthday: '2016-03-01',
|
|
|
+ }, token);
|
|
|
+ const id = r.body && r.body.data && r.body.data.id;
|
|
|
+ expect(id, '新增成员应返回 family_members.id').toBeTruthy();
|
|
|
+ return id;
|
|
|
+}
|
|
|
+
|
|
|
+/** 建立一条成员级健康现状记录(增量保存/统一签名的前置:签名依赖内容已落库) */
|
|
|
+async function seedHealthStatus(request, token, memberId, extra) {
|
|
|
+ return rawPost(request, '/api/health-status/save', Object.assign({
|
|
|
+ memberId: memberId, heightCm: 170, weightKg: 60,
|
|
|
+ }, extra || {}), token);
|
|
|
+}
|
|
|
+
|
|
|
+const SHARED_SIGNATURE = 'data:image/png;base64,UNIFIED-1720';
|
|
|
+
|
|
|
+// ============================================================
|
|
|
+// 正常流:内容保存 + 统一签名(一次签名全员共用)
|
|
|
+// ============================================================
|
|
|
+test.describe('health.survey.incrementalSave 正常流', () => {
|
|
|
+ test('N1 成员级内容保存成功且回读一致,签名初始为空', async ({ request }) => {
|
|
|
+ const p = await register(request, 'parent', 'HS1720N1');
|
|
|
+ const selfId = await ownSelfMemberId(request, p.token, p.userId);
|
|
|
+ const childId = await addChildMember(request, p.token, selfId, 'HS1720N1C');
|
|
|
+
|
|
|
+ const save = await seedHealthStatus(request, p.token, childId, { bloodPressure: '120/80' });
|
|
|
+ expect(save.body.code).toBe(200);
|
|
|
+ expect(save.body.message).toBe('success');
|
|
|
+
|
|
|
+ const get = await rawPost(request, '/api/health-status/get', { memberId: childId }, p.token);
|
|
|
+ const d = get.body.data;
|
|
|
+ expect(get.body.code).toBe(200);
|
|
|
+ expect(Number(d.heightCm)).toBe(170);
|
|
|
+ expect(Number(d.weightKg)).toBe(60);
|
|
|
+ expect(d.bloodPressure).toBe('120/80');
|
|
|
+ expect(d.memberId).toBe(childId);
|
|
|
+ expect(d.userId).toBe(p.userId);
|
|
|
+ expect(d.signature).toBeNull();
|
|
|
+ });
|
|
|
+
|
|
|
+ test('N2 统一签名:一次签名的同一 dataUrl 落全体参与成员,回读一致且 signedAt 非空', async ({ request }) => {
|
|
|
+ const p = await register(request, 'parent', 'HS1720N2');
|
|
|
+ const selfId = await ownSelfMemberId(request, p.token, p.userId);
|
|
|
+ const c1 = await addChildMember(request, p.token, selfId, 'HS1720N2C1');
|
|
|
+ const c2 = await addChildMember(request, p.token, selfId, 'HS1720N2C2');
|
|
|
+
|
|
|
+ // 顺序契约:内容先落库(签名依赖内容已存在,见 A6)
|
|
|
+ const s1 = await seedHealthStatus(request, p.token, c1, { heightCm: 170, weightKg: 60 });
|
|
|
+ const s2 = await seedHealthStatus(request, p.token, c2, { heightCm: 165, weightKg: 55 });
|
|
|
+ expect(s1.body.code).toBe(200);
|
|
|
+ expect(s2.body.code).toBe(200);
|
|
|
+
|
|
|
+ // 需求2:只签一次,全部参与成员共用同一 signature dataUrl
|
|
|
+ const g1 = await rawPost(request, '/api/health-status/signature', { memberId: c1, signature: SHARED_SIGNATURE }, p.token);
|
|
|
+ const g2 = await rawPost(request, '/api/health-status/signature', { memberId: c2, signature: SHARED_SIGNATURE }, p.token);
|
|
|
+ expect(g1.body.code).toBe(200);
|
|
|
+ expect(g2.body.code).toBe(200);
|
|
|
+
|
|
|
+ const r1 = await rawPost(request, '/api/health-status/get', { memberId: c1 }, p.token);
|
|
|
+ const r2 = await rawPost(request, '/api/health-status/get', { memberId: c2 }, p.token);
|
|
|
+ expect(r1.body.data.signature).toBe(SHARED_SIGNATURE);
|
|
|
+ expect(r2.body.data.signature).toBe(SHARED_SIGNATURE);
|
|
|
+ expect(r1.body.data.signature).toBe(r2.body.data.signature);
|
|
|
+ expect(typeof r1.body.data.signedAt).toBe('string');
|
|
|
+ expect(r1.body.data.signedAt.length).toBeGreaterThan(0);
|
|
|
+ // 归属同属登录账号(不跨账号)
|
|
|
+ expect(r1.body.data.userId).toBe(p.userId);
|
|
|
+ expect(r2.body.data.userId).toBe(p.userId);
|
|
|
+ });
|
|
|
+
|
|
|
+ test('N3 增量保存的服务端根因:仅被再保存的成员签名被清,未触碰成员签名不动', async ({ request }) => {
|
|
|
+ const p = await register(request, 'parent', 'HS1720N3');
|
|
|
+ const selfId = await ownSelfMemberId(request, p.token, p.userId);
|
|
|
+ const c1 = await addChildMember(request, p.token, selfId, 'HS1720N3C1');
|
|
|
+ const c2 = await addChildMember(request, p.token, selfId, 'HS1720N3C2');
|
|
|
+ await seedHealthStatus(request, p.token, c1, { heightCm: 170, weightKg: 60 });
|
|
|
+ await seedHealthStatus(request, p.token, c2, { heightCm: 165, weightKg: 55 });
|
|
|
+ await rawPost(request, '/api/health-status/signature', { memberId: c1, signature: SHARED_SIGNATURE }, p.token);
|
|
|
+ await rawPost(request, '/api/health-status/signature', { memberId: c2, signature: SHARED_SIGNATURE }, p.token);
|
|
|
+
|
|
|
+ // 内容确有变化 → 必须再保存(该成员签名会被后端置空,属预期)
|
|
|
+ const again = await rawPost(request, '/api/health-status/save', {
|
|
|
+ memberId: c1, heightCm: 172, notes: '1720-changed',
|
|
|
+ }, p.token);
|
|
|
+ expect(again.body.code).toBe(200);
|
|
|
+
|
|
|
+ const r1 = await rawPost(request, '/api/health-status/get', { memberId: c1 }, p.token);
|
|
|
+ const r2 = await rawPost(request, '/api/health-status/get', { memberId: c2 }, p.token);
|
|
|
+ expect(r1.body.data.signature).toBeNull(); // 被再保存的成员:签名被清
|
|
|
+ expect(r1.body.data.signedAt).toBeNull();
|
|
|
+ expect(r2.body.data.signature).toBe(SHARED_SIGNATURE); // 未触碰成员:签名保持
|
|
|
+ });
|
|
|
+});
|
|
|
+
|
|
|
+// ============================================================
|
|
|
+// 异常流:参数校验 / 类型解析 / 签名前置依赖
|
|
|
+// ============================================================
|
|
|
+test.describe('health.survey.incrementalSave 异常流', () => {
|
|
|
+ test('A1 空内容:save 返回 code 500「请至少填写一项」', async ({ request }) => {
|
|
|
+ const p = await register(request, 'parent', 'HS1720A1');
|
|
|
+ const r = await rawPost(request, '/api/health-status/save', {}, p.token);
|
|
|
+ expect(r.body.code).toBe(500);
|
|
|
+ expect(r.body.message).toBe('请至少填写一项');
|
|
|
+ });
|
|
|
+
|
|
|
+ test('A2 血压格式非法:code 500「血压格式应为 120/80」', async ({ request }) => {
|
|
|
+ const p = await register(request, 'parent', 'HS1720A2');
|
|
|
+ const selfId = await ownSelfMemberId(request, p.token, p.userId);
|
|
|
+ const childId = await addChildMember(request, p.token, selfId, 'HS1720A2C');
|
|
|
+ const r = await rawPost(request, '/api/health-status/save', { memberId: childId, bloodPressure: '120' }, p.token);
|
|
|
+ expect(r.body.code).toBe(500);
|
|
|
+ expect(r.body.message).toBe('血压格式应为 120/80');
|
|
|
+ });
|
|
|
+
|
|
|
+ test('A3 身高越界:code 500「身高需在 30-250 cm 之间」', async ({ request }) => {
|
|
|
+ const p = await register(request, 'parent', 'HS1720A3');
|
|
|
+ const selfId = await ownSelfMemberId(request, p.token, p.userId);
|
|
|
+ const childId = await addChildMember(request, p.token, selfId, 'HS1720A3C');
|
|
|
+ const r = await rawPost(request, '/api/health-status/save', { memberId: childId, heightCm: 1 }, p.token);
|
|
|
+ expect(r.body.code).toBe(500);
|
|
|
+ expect(r.body.message).toBe('身高需在 30-250 cm 之间');
|
|
|
+ });
|
|
|
+
|
|
|
+ test('A4 数值字段传非数值:HTTP 400(BigDecimal 解析失败,非业务 Result)', async ({ request }) => {
|
|
|
+ const p = await register(request, 'parent', 'HS1720A4');
|
|
|
+ const selfId = await ownSelfMemberId(request, p.token, p.userId);
|
|
|
+ const childId = await addChildMember(request, p.token, selfId, 'HS1720A4C');
|
|
|
+ const r = await rawPost(request, '/api/health-status/save', { memberId: childId, heightCm: 'abc' }, p.token);
|
|
|
+ expect(r.httpStatus).toBe(400);
|
|
|
+ });
|
|
|
+
|
|
|
+ test('A5 签名为空或缺失:code 500「请完成手签名」', async ({ request }) => {
|
|
|
+ const p = await register(request, 'parent', 'HS1720A5');
|
|
|
+ const selfId = await ownSelfMemberId(request, p.token, p.userId);
|
|
|
+ const childId = await addChildMember(request, p.token, selfId, 'HS1720A5C');
|
|
|
+ const empty = await rawPost(request, '/api/health-status/signature', { memberId: childId, signature: '' }, p.token);
|
|
|
+ expect(empty.body.code).toBe(500);
|
|
|
+ expect(empty.body.message).toBe('请完成手签名');
|
|
|
+ const missing = await rawPost(request, '/api/health-status/signature', { memberId: childId }, p.token);
|
|
|
+ expect(missing.body.code).toBe(500);
|
|
|
+ expect(missing.body.message).toBe('请完成手签名');
|
|
|
+ });
|
|
|
+
|
|
|
+ test('A6 签名早于内容:code 500「该成员暂无健康现状记录,请先完成问卷」(统一签名的前置依赖)', async ({ request }) => {
|
|
|
+ const p = await register(request, 'parent', 'HS1720A6');
|
|
|
+ const selfId = await ownSelfMemberId(request, p.token, p.userId);
|
|
|
+ const childId = await addChildMember(request, p.token, selfId, 'HS1720A6C');
|
|
|
+ const r = await rawPost(request, '/api/health-status/signature', {
|
|
|
+ memberId: childId, signature: SHARED_SIGNATURE,
|
|
|
+ }, p.token);
|
|
|
+ expect(r.body.code).toBe(500);
|
|
|
+ expect(r.body.message).toBe('该成员暂无健康现状记录,请先完成问卷');
|
|
|
+ });
|
|
|
+
|
|
|
+ test('A7 饮食偏好无法定位成员:code 500「未指定成员」(匿名兜底路径优雅失败)', async ({ request }) => {
|
|
|
+ const p = await register(request, 'parent', 'HS1720A7');
|
|
|
+ const selfId = await ownSelfMemberId(request, p.token, p.userId);
|
|
|
+ const childId = await addChildMember(request, p.token, selfId, 'HS1720A7C');
|
|
|
+ // 跨家庭 familyMemberId:resolveMemberId 校验 isMemberInFamily 失败 → 回退 currentMemberId
|
|
|
+ const q = await register(request, 'parent', 'HS1720A7Q');
|
|
|
+ const r = await rawPost(request, '/api/diet/preferences/save', {
|
|
|
+ familyMemberId: childId, allergies: ['花生'],
|
|
|
+ }, q.token);
|
|
|
+ expect(r.body.code).toBe(500);
|
|
|
+ expect(r.body.message).toBe('未指定成员');
|
|
|
+ });
|
|
|
+});
|
|
|
+
|
|
|
+// ============================================================
|
|
|
+// 权限流:隔离保证 / 无效凭证 / 匿名访问(承袭·真实缺陷未修)
|
|
|
+// ============================================================
|
|
|
+test.describe('health.survey.incrementalSave 权限流', () => {
|
|
|
+ test('P1 越权写入不污染被越权成员的数据(隔离保证成立)', async ({ request }) => {
|
|
|
+ const A = await register(request, 'parent', 'HS1720P1A');
|
|
|
+ const B = await register(request, 'parent', 'HS1720P1B');
|
|
|
+ const selfIdB = await ownSelfMemberId(request, B.token, B.userId);
|
|
|
+ const childB = await addChildMember(request, B.token, selfIdB, 'HS1720P1CB');
|
|
|
+
|
|
|
+ // A 拿 B 家孩子的 memberId 直接保存:memberId 无归属校验,写入落在 A 账号名下
|
|
|
+ const cross = await rawPost(request, '/api/health-status/save', {
|
|
|
+ memberId: childB, heightCm: 175, weightKg: 65, notes: '1720-cross-family',
|
|
|
+ }, A.token);
|
|
|
+ expect(cross.body.code).toBe(200);
|
|
|
+
|
|
|
+ // 隔离保证:B 视角回读仍为空 —— B 家族的成员数据未被越权写入污染
|
|
|
+ const bGet = await rawPost(request, '/api/health-status/get', { memberId: childB }, B.token);
|
|
|
+ expect(bGet.body.code).toBe(200);
|
|
|
+ expect(bGet.body.data).toBeNull();
|
|
|
+ });
|
|
|
+
|
|
|
+ test.fixme('P2 跨家庭 memberId 应拒绝(承袭·真实缺陷未修:memberId 无归属校验)', async ({ request }) => {
|
|
|
+ const A = await register(request, 'parent', 'HS1720P2A');
|
|
|
+ const B = await register(request, 'parent', 'HS1720P2B');
|
|
|
+ const selfIdB = await ownSelfMemberId(request, B.token, B.userId);
|
|
|
+ const childB = await addChildMember(request, B.token, selfIdB, 'HS1720P2CB');
|
|
|
+
|
|
|
+ // 应然:非本家庭的 family_members.id 应被拒绝,而非静默接受并在 A 名下生成孤儿记录
|
|
|
+ const cross = await rawPost(request, '/api/health-status/save', {
|
|
|
+ memberId: childB, heightCm: 175,
|
|
|
+ }, A.token);
|
|
|
+ expect(cross.body.code).not.toBe(200);
|
|
|
+ });
|
|
|
+
|
|
|
+ test('P3 无效凭证:HTTP 401 code 401「请先登录」', async ({ request }) => {
|
|
|
+ const p = await register(request, 'parent', 'HS1720P3');
|
|
|
+ const r = await rawPost(request, '/api/health-status/signature', {
|
|
|
+ memberId: 1, signature: SHARED_SIGNATURE,
|
|
|
+ }, 'not-a-real-jwt-token');
|
|
|
+ expect(r.httpStatus).toBe(401);
|
|
|
+ expect(r.body.code).toBe(401);
|
|
|
+ expect(r.body.message).toBe('请先登录');
|
|
|
+ // 无效 token 下账号无关的资源也应 401(不静默降级为匿名写)
|
|
|
+ const g = await rawPost(request, '/api/health-status/get', { memberId: 1 }, 'not-a-real-jwt-token');
|
|
|
+ expect(g.httpStatus).toBe(401);
|
|
|
+ expect(g.body.code).toBe(401);
|
|
|
+ });
|
|
|
+
|
|
|
+ // 承袭·真实缺陷未修:/api/health-status/** 既不在 JwtInterceptor.PUBLIC_PATHS
|
|
|
+ // 也不在 LOGIN_REQUIRED_PATHS,缺省路径为「匿名放行(userId=-1)」→ 健康档案与手签名
|
|
|
+ // (base64 PNG)匿名可写、可读、可签。探针实测(HS-06):无 token 的 save/get/signature
|
|
|
+ // 均返回 http200 code200。此处断言应然行为(401),当前实测失败即由 fixme 标为预期失败,
|
|
|
+ // 不为缺陷造绿;修复后本用例转 unexpectedly passing,届时移除 fixme。
|
|
|
+ test.fixme('P4 匿名访问健康档案应 401(承袭·真实缺陷未修:匿名可写可读可签)', async ({ request }) => {
|
|
|
+ const p = await register(request, 'parent', 'HS1720P4');
|
|
|
+ const selfId = await ownSelfMemberId(request, p.token, p.userId);
|
|
|
+ const childId = await addChildMember(request, p.token, selfId, 'HS1720P4C');
|
|
|
+
|
|
|
+ const anonSave = await rawPost(request, '/api/health-status/save', {
|
|
|
+ memberId: childId, notes: '1720-anon',
|
|
|
+ });
|
|
|
+ expect(anonSave.httpStatus).toBe(401);
|
|
|
+
|
|
|
+ const anonGet = await rawPost(request, '/api/health-status/get', { memberId: childId });
|
|
|
+ expect(anonGet.httpStatus).toBe(401);
|
|
|
+
|
|
|
+ const anonSig = await rawPost(request, '/api/health-status/signature', {
|
|
|
+ memberId: childId, signature: SHARED_SIGNATURE,
|
|
|
+ });
|
|
|
+ expect(anonSig.httpStatus).toBe(401);
|
|
|
+ });
|
|
|
+});
|