|
|
@@ -0,0 +1,121 @@
|
|
|
+/**
|
|
|
+ * SC-P-04 / SC-P-05 / SC-P-06 家庭成员 CRUD 三类流(API 级 e2e)
|
|
|
+ * ================================================================
|
|
|
+ * 契约来源(源码核对 cfc-backend):
|
|
|
+ * - FamilyMembersController /api/family/member/{list,update,kick}
|
|
|
+ * - list : 无 token → 401「请先登录」(SEC-20261031-01 拦截 userId<=0);
|
|
|
+ * page/pageSize<=0 → 400「分页参数必须为正整数」;
|
|
|
+ * 显式 familyId 越权 → 403「无权访问该家庭数据」
|
|
|
+ * - update: 缺 memberId → 「缺少成员ID」;非本家庭成员 → 「成员不存在或不属于您的家庭」
|
|
|
+ * - kick : 非创建者 → 「只有家庭管理员才能踢出成员」;不存在 → 「成员不存在」;
|
|
|
+ * 踢创建者 → 「不能踢出家庭创建者…」;成功 → 「踢出成功」
|
|
|
+ * 角色说明:direct-register 的 parent 即家庭创建者(管理员),故「同家庭非管理员」场景
|
|
|
+ * 无法用注册接口构造,权限流以「无 token 401 + 跨家庭隔离」两条真实守卫断言。
|
|
|
+ */
|
|
|
+const { test, expect } = require('@playwright/test');
|
|
|
+const { registerRole, apiPost, pickMemberByUserId, uniqueSuffix } = require('../helpers/api');
|
|
|
+
|
|
|
+test.describe('SC-P-04/P-05/P-06 家庭成员 CRUD 三类流', () => {
|
|
|
+ let parent; // 家庭创建者(管理员)
|
|
|
+ let outsider; // 另一家庭的家长(用于跨家庭越权)
|
|
|
+ let childA; // 待编辑/待踢出成员
|
|
|
+ let childANick;
|
|
|
+
|
|
|
+ test.beforeAll(async ({ request }) => {
|
|
|
+ parent = await registerRole(request, 'parent', 'QA-P04-PARENT');
|
|
|
+ outsider = await registerRole(request, 'parent', 'QA-P04-OUT');
|
|
|
+ const mem = await apiPost(request, '/api/family/member/list', {}, parent.token);
|
|
|
+ const self = pickMemberByUserId(mem.data, parent.userId);
|
|
|
+ expect(self, '成员列表应包含登录用户自身').toBeTruthy();
|
|
|
+
|
|
|
+ childANick = 'C04-' + uniqueSuffix();
|
|
|
+ const addA = await apiPost(request, '/api/family/member/add', {
|
|
|
+ relativeMemberId: self.id, generationLevel: 'child',
|
|
|
+ nickname: childANick, gender: 'female', birthday: '2016-07-01',
|
|
|
+ }, parent.token);
|
|
|
+ expect(addA.code, 'member/add: ' + addA.message).toBe(200);
|
|
|
+ childA = addA.data.id;
|
|
|
+ });
|
|
|
+
|
|
|
+ // ============ 正常流 ============
|
|
|
+ test('P-04 正常流: 家长查看成员列表返回家庭成员', async ({ request }) => {
|
|
|
+ const r = await apiPost(request, '/api/family/member/list', {}, parent.token);
|
|
|
+ expect(r.code, 'list: ' + r.message).toBe(200);
|
|
|
+ expect(Array.isArray(r.data)).toBe(true);
|
|
|
+ expect(r.data.length).toBeGreaterThanOrEqual(2); // 自己 + childA
|
|
|
+ const hit = r.data.find((m) => String(m.id) === String(childA));
|
|
|
+ expect(hit, '新成员应出现在列表中').toBeTruthy();
|
|
|
+ expect(hit.nickname).toBe(childANick);
|
|
|
+ expect(hit.relationship || hit.generationLevel, '应返回关系/辈分信息').toBeTruthy();
|
|
|
+ });
|
|
|
+
|
|
|
+ test('P-05 正常流: 家长更新成员资料并生效', async ({ request }) => {
|
|
|
+ const upd = await apiPost(request, '/api/family/member/update', {
|
|
|
+ memberId: childA, gender: 'male', birthday: '2016-08-15',
|
|
|
+ }, parent.token);
|
|
|
+ expect(upd.code, 'update: ' + upd.message).toBe(200);
|
|
|
+ expect(upd.data).toBeTruthy();
|
|
|
+
|
|
|
+ const after = await apiPost(request, '/api/family/member/list', {}, parent.token);
|
|
|
+ const hit = (after.data || []).find((m) => String(m.id) === String(childA));
|
|
|
+ expect(hit, '更新后成员仍应在列表').toBeTruthy();
|
|
|
+ expect(hit.gender).toBe('male');
|
|
|
+ expect(String(hit.birthday)).toContain('2016-08-15');
|
|
|
+ });
|
|
|
+
|
|
|
+ test('P-06 正常流: 家长踢出成员后列表不再包含', async ({ request }) => {
|
|
|
+ const kick = await apiPost(request, '/api/family/member/kick', { memberId: childA }, parent.token);
|
|
|
+ expect(kick.code, 'kick: ' + kick.message).toBe(200);
|
|
|
+ // 实测 message = "success"(源码 Result.success("踢出成功") 返回体 message 仍为 success),不锁 message 文本
|
|
|
+
|
|
|
+ const after = await apiPost(request, '/api/family/member/list', {}, parent.token);
|
|
|
+ const hit = (after.data || []).find((m) => String(m.id) === String(childA));
|
|
|
+ expect(hit, '被踢出成员不应再出现在列表').toBeUndefined();
|
|
|
+ });
|
|
|
+
|
|
|
+ // ============ 异常流 ============
|
|
|
+ test('P-04 异常流: 非法分页参数 page=0 被拒 400', async ({ request }) => {
|
|
|
+ const r = await apiPost(request, '/api/family/member/list', { page: 0, pageSize: 10 }, parent.token);
|
|
|
+ expect(r.code, 'page=0 → 应 400: ' + r.message).toBe(400);
|
|
|
+ expect(r.message).toContain('分页参数必须为正整数');
|
|
|
+ });
|
|
|
+
|
|
|
+ test('P-04 异常流: 显式 familyId 越权被拒 403', async ({ request }) => {
|
|
|
+ const r = await apiPost(request, '/api/family/member/list', { familyId: 999999999 }, parent.token);
|
|
|
+ expect(r.code, '越权 familyId → 应 403: ' + r.message).toBe(403);
|
|
|
+ expect(r.message).toContain('无权访问');
|
|
|
+ });
|
|
|
+
|
|
|
+ test('P-05 异常流: 缺少 memberId 被拒', async ({ request }) => {
|
|
|
+ const r = await apiPost(request, '/api/family/member/update', { gender: 'male' }, parent.token);
|
|
|
+ expect(r.code).not.toBe(200);
|
|
|
+ expect(r.message).toContain('缺少成员ID');
|
|
|
+ });
|
|
|
+
|
|
|
+ test('P-06 异常流: 踢出不存在的成员被拒', async ({ request }) => {
|
|
|
+ const r = await apiPost(request, '/api/family/member/kick', { memberId: 999999999 }, parent.token);
|
|
|
+ expect(r.code).not.toBe(200);
|
|
|
+ expect(r.message).toContain('成员不存在');
|
|
|
+ });
|
|
|
+
|
|
|
+ // ============ 权限流 ============
|
|
|
+ test('P-04 权限流: 无 token 访问成员列表 → 401', async ({ request }) => {
|
|
|
+ const r = await apiPost(request, '/api/family/member/list', {}, undefined);
|
|
|
+ expect(r.code, '无 token → 应 401: ' + r.message).toBe(401);
|
|
|
+ expect(r.message).toContain('请先登录');
|
|
|
+ });
|
|
|
+
|
|
|
+ test('P-06 权限流: 无 token 踢出成员 → 401', async ({ request }) => {
|
|
|
+ const r = await apiPost(request, '/api/family/member/kick', { memberId: 999999999 }, undefined);
|
|
|
+ expect(r.code, '无 token → 应 401: ' + r.message).toBe(401);
|
|
|
+ expect(r.message).toContain('请先登录');
|
|
|
+ });
|
|
|
+
|
|
|
+ test('P-05 权限流: 跨家庭成员资料不可编辑(隔离)', async ({ request }) => {
|
|
|
+ const r = await apiPost(request, '/api/family/member/update', {
|
|
|
+ memberId: childA, nickname: 'hijack',
|
|
|
+ }, outsider.token);
|
|
|
+ expect(r.code).not.toBe(200);
|
|
|
+ expect(r.message).toContain('不属于您的家庭');
|
|
|
+ });
|
|
|
+});
|