wechat-login.spec.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /**
  2. * SC-G-02 微信授权登录 · API 级 e2e(auth.guest.wechatLogin / S-cfc-9)
  3. * ================================================================
  4. * 覆盖:
  5. * - 正常流(TC-G-02):POST /api/auth/wechat-login → Result 含 token + 用户信息,
  6. * 角色默认 parent;新微信自动建 User + 家庭 + family_members 成员(自己能查到)。
  7. * - 权限流(TC-G-02-P):微信登录是公开路径(游客无 token 可触达);受保护接口
  8. * /api/family/member/list 无 token → 401;跨家庭 familyId → 403(越权被拒)。
  9. * - 边界(TC-G-02-E):登录响应不泄露内部错误堆栈(Result 统一 code/message/data 信封)。
  10. *
  11. * 目标端由 PW_BASE_URL 指定(默认 http://cfc.iwintrue.com,本地验证用
  12. * http://127.0.0.1:9082,后端开 wechat.test-mode=true)。
  13. *
  14. * 运行:npx playwright test wechat-login -c playwright.config.api.js --project=desktop
  15. *
  16. * ⚠️ TC-G-02 正常流②(member/list data 非数组):
  17. * 探针 _probe_tc-g02-member-list.spec.js 已取证:
  18. * /api/family/member/list 返回的 data 字段为对象 {familyId, familyName, inviteCode, members:[...]},
  19. * 非纯数组。原严格断言 expect(Array.isArray(members.data)) 属承袭·真实缺陷未修,本轮不造绿。
  20. */
  21. const { test, expect } = require('@playwright/test');
  22. const BASE = process.env.PW_BASE_URL || 'http://cfc.iwintrue.com';
  23. const API_TIMEOUT = 60000;
  24. /**
  25. * 统一 POST(全项目接口一律 POST),返回 { http, code, message, data }
  26. */
  27. async function api(request, path, body, token) {
  28. const headers = { 'Content-Type': 'application/json' };
  29. if (token) headers.Authorization = 'Bearer ' + token;
  30. const res = await request.post(BASE + path, { data: body || {}, headers, timeout: API_TIMEOUT });
  31. const raw = await res.text();
  32. let json = null;
  33. try {
  34. json = raw ? JSON.parse(raw) : null;
  35. } catch (e) {
  36. json = null;
  37. }
  38. return {
  39. http: res.status(),
  40. code: json ? json.code : null,
  41. message: json ? json.message : null,
  42. data: json ? json.data : null,
  43. };
  44. }
  45. test.describe('SC-G-02 微信授权登录(API 级)', () => {
  46. let ctx = {};
  47. test.beforeAll(async ({ request }) => {
  48. const login = await api(request, '/api/auth/wechat-login', {
  49. code: 'scg02-' + Date.now() + '-' + String(Math.random()).slice(2, 8),
  50. nickname: 'SC-G-02游客',
  51. });
  52. expect(login.http).toBe(200);
  53. expect(login.code).toBe(200);
  54. expect(login.data && login.data.token).toBeTruthy();
  55. ctx = login.data;
  56. });
  57. test('正常流:微信一键登录返回 token+用户信息,角色默认 parent', async () => {
  58. expect(ctx.role).toBe('parent');
  59. expect(ctx.userId).toBeTruthy();
  60. expect(ctx.openid).toBeTruthy();
  61. expect(typeof ctx.token).toBe('string');
  62. expect(ctx.token.length).toBeGreaterThan(20); // JWT 三段式
  63. });
  64. // TC-G-02 正常流②:member/list data 非数组(承袭·真实缺陷未修,禁造绿)。
  65. // 探针 _probe_tc-g02-member-list.spec.js 已取证:data 实际为对象 {familyId,familyName,inviteCode,members:[...]},
  66. // 故 Array.isArray(members.data) 必红。待实现侧将返回值改为平铺数组后再解除 fixme。
  67. test.fixme('正常流:首次登录自动建档,家庭列表能看到自己(family_members 成员)—— 承袭·真实缺陷未修', async ({ request }) => {
  68. const members = await api(request, '/api/family/member/list', { includeFamily: true }, ctx.token);
  69. expect(members.http).toBe(200);
  70. expect(members.code).toBe(200);
  71. // 真实契约:members.data 为对象而非数组,以下行按原始意图保留注释以备后续修复后取消 fixme
  72. // expect(Array.isArray(members.data)).toBeTruthy();
  73. // const self = (members.data || []).find((m) => String(m.userId) === String(ctx.userId));
  74. // expect(self).toBeTruthy();
  75. console.log('[TC-G-02-N2] 承袭·真实缺陷:data 实际类型=' + (members.data === null ? 'null' : typeof members.data) + ' keys=' + Object.keys(members.data || {}));
  76. });
  77. test('权限流:微信登录是公开路径,无 token 也可调用(游客可触达)', async ({ request }) => {
  78. const login = await api(request, '/api/auth/wechat-login', {
  79. code: 'scg02-anon-' + Date.now(),
  80. });
  81. expect(login.http).toBe(200);
  82. expect(login.code).toBe(200);
  83. expect(login.data && login.data.token).toBeTruthy();
  84. });
  85. test('权限流:受保护接口无 token → 统一 401,不泄露内部堆栈', async ({ request }) => {
  86. const r = await api(request, '/api/family/member/list', {});
  87. expect(r.http).toBe(401);
  88. expect(r.code).toBe(401);
  89. expect(String(r.message)).toContain('请先登录');
  90. expect(JSON.stringify(r)).not.toContain('Exception');
  91. expect(JSON.stringify(r)).not.toContain('at com.etotem');
  92. });
  93. test('权限流:登录后携带 token 可访问;跨家庭 familyId → 403 越权被拒', async ({ request }) => {
  94. const own = await api(request, '/api/family/member/list', { familyId: ctx.familyId }, ctx.token);
  95. expect(own.code).toBe(200);
  96. const cross = await api(request, '/api/family/member/list', { familyId: 999999999 }, ctx.token);
  97. expect(cross.http).toBe(403);
  98. expect(cross.code).toBe(403);
  99. expect(String(cross.message)).toContain('无权访问');
  100. });
  101. });