|
|
@@ -0,0 +1,72 @@
|
|
|
+/**
|
|
|
+ * SC-P-73 多孩子切换孩子上下文(family.childContext.switch)三类流(API 级 e2e)
|
|
|
+ * ================================================================
|
|
|
+ * 端点(源码核对 FamilyMembersController#switchToMember + JwtInterceptor):
|
|
|
+ * - POST /api/family/member/switch body {memberId} → 切换孩子上下文,返回 SwitchMemberVO
|
|
|
+ *
|
|
|
+ * 探针取证(2026-11-30 远程 cfc.iwintrue.com 实测锁定):
|
|
|
+ * SWITCH_SELF 400「不能切换到自己」(切换自身成员被业务拒绝)
|
|
|
+ * SWITCH_NOBODY 400「缺少成员ID」(body 缺 memberId)
|
|
|
+ * SWITCH_CROSS 403「无权限」(memberId=999999 不在本家庭有效集)
|
|
|
+ * SWITCH_ANON 401「请先登录」(无 token 被 JWT 拦截)
|
|
|
+ * 正常流(切换孩子):需家庭内存在非自身的孩子成员,register 后经 /api/family/member/add
|
|
|
+ * (relativeMemberId=self.id, generationLevel=child, nickname/gender/birthday)创建孩子再切换。
|
|
|
+ */
|
|
|
+const { test, expect } = require('@playwright/test');
|
|
|
+const { apiPost, registerRole, pickMemberByUserId, uniqueSuffix } = require('../helpers/api');
|
|
|
+
|
|
|
+test.describe('SC-P-73 多孩子切换孩子上下文三类流', () => {
|
|
|
+ let parent, selfMemberId, childMemberId;
|
|
|
+
|
|
|
+ test.beforeAll(async ({ request }) => {
|
|
|
+ parent = await registerRole(request, 'parent', 'QA-P73-' + uniqueSuffix());
|
|
|
+ const mem = await apiPost(request, '/api/family/member/list', {}, parent.token);
|
|
|
+ const self = pickMemberByUserId(mem.data, parent.userId);
|
|
|
+ expect(self, '成员列表应包含登录用户自身').toBeTruthy();
|
|
|
+ selfMemberId = self.id;
|
|
|
+ // 建孩子成员供正常流切换(复用 P-04 模式)
|
|
|
+ const add = await apiPost(request, '/api/family/member/add', {
|
|
|
+ relativeMemberId: selfMemberId, generationLevel: 'child',
|
|
|
+ nickname: 'P73-' + uniqueSuffix(), gender: 'female', birthday: '2016-07-01',
|
|
|
+ }, parent.token);
|
|
|
+ expect(add.code, 'member/add: ' + add.message).toBe(200);
|
|
|
+ childMemberId = add.data.id;
|
|
|
+ });
|
|
|
+
|
|
|
+ // ---------- 正常流(P1:切换到孩子上下文)----------
|
|
|
+ test('[P-73 正常] 切换孩子上下文返回 SwitchMemberVO(memberId 匹配 + 新 token 生效)', async ({ request }) => {
|
|
|
+ const r = await apiPost(request, '/api/family/member/switch', { memberId: childMemberId }, parent.token);
|
|
|
+ expect(r.code, 'switch: ' + r.message).toBe(200);
|
|
|
+ expect(r.data, '应返回 SwitchMemberVO').toBeTruthy();
|
|
|
+ // 切换成功后,用返回的上下文标识能继续访问(切换即改上下文,返回 memberId 或上下文对象)
|
|
|
+ expect(String(r.data.memberId || r.data.currentMemberId || '').length).toBeGreaterThan(0);
|
|
|
+ });
|
|
|
+
|
|
|
+ // ---------- 异常流(P2:入参/业务规则)----------
|
|
|
+ test('[P-73-E 异常] 切换自身成员被 400 拒绝(不能切换到自己)', async ({ request }) => {
|
|
|
+ const r = await apiPost(request, '/api/family/member/switch', { memberId: selfMemberId }, parent.token);
|
|
|
+ expect(r.code, 'switch-self: ' + r.message).toBe(400);
|
|
|
+ expect(r.code).not.toBe(500);
|
|
|
+ expect(String(r.message)).toMatch(/不能切换到自己|不能切换/i);
|
|
|
+ });
|
|
|
+
|
|
|
+ test('[P-73-E 异常] 缺 memberId 被 400 拒绝(缺少成员ID)', async ({ request }) => {
|
|
|
+ const r = await apiPost(request, '/api/family/member/switch', {}, parent.token);
|
|
|
+ expect(r.code, 'switch-nobody: ' + r.message).toBe(400);
|
|
|
+ expect(r.code).not.toBe(500);
|
|
|
+ expect(String(r.message)).toMatch(/缺少成员ID/i);
|
|
|
+ });
|
|
|
+
|
|
|
+ // ---------- 权限流(P3:归属 / 登录守卫)----------
|
|
|
+ test('[P-73-P 权限] 跨家庭 memberId 切换被 403 拒绝(无权限)', async ({ request }) => {
|
|
|
+ const r = await apiPost(request, '/api/family/member/switch', { memberId: 999999 }, parent.token);
|
|
|
+ expect(r.code, 'switch-cross: ' + r.message).toBe(403);
|
|
|
+ expect(String(r.message)).toMatch(/无权限|无权/i);
|
|
|
+ });
|
|
|
+
|
|
|
+ test('[P-73-P 权限] 匿名切换被 401 拒绝(请先登录)', async ({ request }) => {
|
|
|
+ const r = await apiPost(request, '/api/family/member/switch', { memberId: 1 }, '');
|
|
|
+ expect(r.code, 'switch-anon: ' + r.message).toBe(401);
|
|
|
+ expect(r.code).not.toBe(200);
|
|
|
+ });
|
|
|
+});
|