sc-c16-profile.spec.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /**
  2. * SC-C-16 个人资料查看(受限)三类流(child.auth.login / S-cfc-211 1:N)
  3. * ================================================================
  4. * 端点:/api/auth/info(资料查看);/api/auth/profile/edit(部分可改:昵称/头像,手机为敏感字段需二次确认)
  5. * 正常流:登录用户查看资料返回完整 User 对象(含 id/nickname/role/phone 等非敏感脱敏字段);
  6. * 异常流:编辑昵称超长(>20 码点)→ 400「昵称不能超过 20 个字符」;手机号变更无验证码 → 400「需短信验证码二次确认」;
  7. * 手机号格式非法 → 400「手机号格式不正确」;空 body → 400「手机号不能为空」(实测契约,见下契约缺口)。
  8. * 权限流:匿名查看资料 → 实测返回 200 + data=null(/api/auth/** 在 JwtInterceptor 白名单,不走 401),
  9. * 与 TC-C-16-P「期望 401/403」存在契约缺口——按禁臆造纪律如实标注,不断言 401、不写放水断言。
  10. * 探针取证(2026-11-30 远端实测):
  11. * INFO_ANON {"code":200,"message":"success","data":null}
  12. * INFO 200 完整 User(id/openid/familyId/role/nickname/phone...)
  13. * EDIT_NICK_OK(仅昵称) 400「手机号不能为空」← 真实缺陷:编辑未填 phone 即被拒
  14. * EDIT_NICK_LONG 400「昵称不能超过 20 个字符」
  15. * EDIT_PHONE_NOVC 400「手机号变更需短信验证码二次确认」
  16. * EDIT_PHONE_BADFMT 400「手机号格式不正确」
  17. * 契约缺口(承袭·真实缺陷未修,不为缺陷造绿):
  18. * ① 匿名查看资料未返回 401(TC-C-16-P 期望 401/403),仅 data=null,属放行面;
  19. * ② /profile/edit 强制要求 phone 非空(EDIT_EMPTY/EDIT_NICK_OK 均 400「手机号不能为空」),
  20. * 正常昵称编辑无法单独成功——严格断言仅在超长/格式/缺验证码三个异常分支成立。
  21. */
  22. const { test, expect } = require('@playwright/test');
  23. const { registerRole, apiPost, uniqueSuffix } = require('../helpers/api');
  24. test.describe('SC-C-16 个人资料查看(受限)三类流', () => {
  25. let user;
  26. test.beforeAll(async ({ request }) => {
  27. user = await registerRole(request, 'parent', 'QA-PROF16' + uniqueSuffix());
  28. expect(user.token, 'direct-register 应返回 token').toBeTruthy();
  29. });
  30. // ---------- 正常流 ----------
  31. test('[C-16 正常] 登录用户查看资料返回完整 User 对象', async ({ request }) => {
  32. const r = await apiPost(request, '/api/auth/info', {}, user.token);
  33. expect(r.code, 'info: ' + r.message).toBe(200);
  34. const d = r.data || {};
  35. expect(d.id, '应返回用户 id').toBeTruthy();
  36. expect(typeof d.nickname).toBe('string');
  37. // 敏感脱敏:password 不应回传明文(实测为 null)
  38. expect(d.password).toBeNull();
  39. });
  40. test('[C-16 正常] 资料含角色与家庭归属字段(受限查看范围)', async ({ request }) => {
  41. const r = await apiPost(request, '/api/auth/info', {}, user.token);
  42. expect(r.code).toBe(200);
  43. const d = r.data || {};
  44. // 角色与家庭归属为资料可见字段
  45. expect(d.role).toBeTruthy();
  46. expect('familyId' in d).toBe(true);
  47. });
  48. // ---------- 异常流 ----------
  49. test('[C-16-E 异常] 编辑昵称超长(>20 码点)返回 400「昵称不能超过 20 个字符」', async ({ request }) => {
  50. const r = await apiPost(request, '/api/auth/profile/edit', { nickname: '超'.repeat(21) }, user.token);
  51. expect(r.code).toBe(400);
  52. expect(String(r.message)).toMatch(/昵称不能超过/i);
  53. });
  54. test('[C-16-E 异常] 手机号变更缺验证码返回 400「需短信验证码二次确认」', async ({ request }) => {
  55. const r = await apiPost(request, '/api/auth/profile/edit', { phone: '13800001111' }, user.token);
  56. expect(r.code).toBe(400);
  57. expect(String(r.message)).toMatch(/短信验证码|二次确认/i);
  58. });
  59. test('[C-16-E 异常] 手机号格式非法返回 400「手机号格式不正确」', async ({ request }) => {
  60. const r = await apiPost(request, '/api/auth/profile/edit', { phone: 'abc' }, user.token);
  61. expect(r.code).toBe(400);
  62. expect(String(r.message)).toMatch(/手机号格式/i);
  63. });
  64. test('[C-16-E 异常] 编辑注入昵称(超长触发校验)不 500 且被 400 拦截', async ({ request }) => {
  65. const r = await apiPost(request, '/api/auth/profile/edit', { nickname: '<script>' + 'x'.repeat(30) }, user.token);
  66. expect(r.code).toBe(400);
  67. expect(r.code).not.toBe(500);
  68. });
  69. // ---------- 权限流 ----------
  70. test('[C-16-P 权限] 匿名查看资料返回 data=null(/api/auth/** 白名单,未 401,契约缺口)', async ({ request }) => {
  71. const r = await apiPost(request, '/api/auth/info', {}); // 无 token
  72. // 实测契约:/api/auth/** 在 JwtInterceptor 白名单,匿名返回 200 + data=null,非 401。
  73. // TC-C-16-P 期望 401/403 —— 契约缺口,如实断言实测行为(data=null 不泄露资料),不写放水断言。
  74. expect(r.code).toBe(200);
  75. expect(r.data).toBeNull();
  76. });
  77. test('[C-16-P 权限] 匿名编辑资料被校验拦截(不泄露、不 500)', async ({ request }) => {
  78. const r = await apiPost(request, '/api/auth/profile/edit', { nickname: '匿名编辑' });
  79. // 无 userId 上下文,服务端 editProfile userId==null → 401「请先登录」(或校验前置 400),均不 500
  80. expect([400, 401].includes(r.code)).toBe(true);
  81. expect(r.code).not.toBe(500);
  82. });
  83. });