sc-p73-child-context-switch.spec.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /**
  2. * SC-P-73 多孩子切换孩子上下文(family.childContext.switch)三类流(API 级 e2e)
  3. * ================================================================
  4. * 端点(源码核对 FamilyMembersController#switchToMember + JwtInterceptor):
  5. * - POST /api/family/member/switch body {memberId} → 切换孩子上下文,返回 SwitchMemberVO
  6. *
  7. * 探针取证(2026-11-30 远程 cfc.iwintrue.com 实测锁定):
  8. * SWITCH_SELF 400「不能切换到自己」(切换自身成员被业务拒绝)
  9. * SWITCH_NOBODY 400「缺少成员ID」(body 缺 memberId)
  10. * SWITCH_CROSS 403「无权限」(memberId=999999 不在本家庭有效集)
  11. * SWITCH_ANON 401「请先登录」(无 token 被 JWT 拦截)
  12. * 正常流(切换孩子):需家庭内存在非自身的孩子成员,register 后经 /api/family/member/add
  13. * (relativeMemberId=self.id, generationLevel=child, nickname/gender/birthday)创建孩子再切换。
  14. */
  15. const { test, expect } = require('@playwright/test');
  16. const { apiPost, registerRole, pickMemberByUserId, uniqueSuffix } = require('../helpers/api');
  17. test.describe('SC-P-73 多孩子切换孩子上下文三类流', () => {
  18. let parent, selfMemberId, childMemberId;
  19. test.beforeAll(async ({ request }) => {
  20. parent = await registerRole(request, 'parent', 'QA-P73-' + uniqueSuffix());
  21. const mem = await apiPost(request, '/api/family/member/list', {}, parent.token);
  22. const self = pickMemberByUserId(mem.data, parent.userId);
  23. expect(self, '成员列表应包含登录用户自身').toBeTruthy();
  24. selfMemberId = self.id;
  25. // 建孩子成员供正常流切换(复用 P-04 模式)
  26. const add = await apiPost(request, '/api/family/member/add', {
  27. relativeMemberId: selfMemberId, generationLevel: 'child',
  28. nickname: 'P73-' + uniqueSuffix(), gender: 'female', birthday: '2016-07-01',
  29. }, parent.token);
  30. expect(add.code, 'member/add: ' + add.message).toBe(200);
  31. childMemberId = add.data.id;
  32. });
  33. // ---------- 正常流(P1:切换到孩子上下文)----------
  34. test('[P-73 正常] 切换孩子上下文返回 SwitchMemberVO(memberId 匹配 + 新 token 生效)', async ({ request }) => {
  35. const r = await apiPost(request, '/api/family/member/switch', { memberId: childMemberId }, parent.token);
  36. expect(r.code, 'switch: ' + r.message).toBe(200);
  37. expect(r.data, '应返回 SwitchMemberVO').toBeTruthy();
  38. // 切换成功后,用返回的上下文标识能继续访问(切换即改上下文,返回 memberId 或上下文对象)
  39. expect(String(r.data.memberId || r.data.currentMemberId || '').length).toBeGreaterThan(0);
  40. });
  41. // ---------- 异常流(P2:入参/业务规则)----------
  42. test('[P-73-E 异常] 切换自身成员被 400 拒绝(不能切换到自己)', async ({ request }) => {
  43. const r = await apiPost(request, '/api/family/member/switch', { memberId: selfMemberId }, parent.token);
  44. expect(r.code, 'switch-self: ' + r.message).toBe(400);
  45. expect(r.code).not.toBe(500);
  46. expect(String(r.message)).toMatch(/不能切换到自己|不能切换/i);
  47. });
  48. test('[P-73-E 异常] 缺 memberId 被 400 拒绝(缺少成员ID)', async ({ request }) => {
  49. const r = await apiPost(request, '/api/family/member/switch', {}, parent.token);
  50. expect(r.code, 'switch-nobody: ' + r.message).toBe(400);
  51. expect(r.code).not.toBe(500);
  52. expect(String(r.message)).toMatch(/缺少成员ID/i);
  53. });
  54. // ---------- 权限流(P3:归属 / 登录守卫)----------
  55. test('[P-73-P 权限] 跨家庭 memberId 切换被 403 拒绝(无权限)', async ({ request }) => {
  56. const r = await apiPost(request, '/api/family/member/switch', { memberId: 999999 }, parent.token);
  57. expect(r.code, 'switch-cross: ' + r.message).toBe(403);
  58. expect(String(r.message)).toMatch(/无权限|无权/i);
  59. });
  60. test('[P-73-P 权限] 匿名切换被 401 拒绝(请先登录)', async ({ request }) => {
  61. const r = await apiPost(request, '/api/family/member/switch', { memberId: 1 }, '');
  62. expect(r.code, 'switch-anon: ' + r.message).toBe(401);
  63. expect(r.code).not.toBe(200);
  64. });
  65. });