01-auth.spec.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /**
  2. * L2 回归测试:认证模块
  3. * 覆盖:发送验证码、登录、获取用户信息、更新用户信息
  4. */
  5. import { test, expect } from '@playwright/test';
  6. import { apiGet, apiPost, apiPut, initApiClient, disposeApiClient, setAuthToken } from '../helpers/api-client';
  7. let token: string;
  8. let phone: string;
  9. test.describe('认证模块 (Auth)', () => {
  10. test.beforeAll(async () => {
  11. await initApiClient();
  12. phone = `138${String(Date.now()).slice(-8)}`;
  13. });
  14. test.afterAll(async () => {
  15. await disposeApiClient();
  16. });
  17. test('A01: 发送验证码 POST /api/auth/send-code', async () => {
  18. const res = await apiPost('/api/auth/send-code', { phone });
  19. expect(res.code).toBe(0);
  20. expect(res.data?.code).toBeDefined();
  21. });
  22. test('A02: 手机号登录 POST /api/auth/login', async () => {
  23. const codeRes = await apiPost('/api/auth/send-code', { phone });
  24. const code = codeRes.data?.code || '123456';
  25. const res = await apiPost('/api/auth/login', { phone, code });
  26. expect(res.code).toBe(0);
  27. expect(res.data?.token).toBeDefined();
  28. token = res.data.token;
  29. setAuthToken(token);
  30. });
  31. test('A03: 获取用户信息 GET /api/auth/user-info', async () => {
  32. const res = await apiGet('/api/auth/user-info');
  33. // 需要 token,可能 401 如果没有中间件豁免
  34. expect([0, 401]).toContain(res.code);
  35. });
  36. test('A04: 无效手机号被拒绝', async () => {
  37. const res = await apiPost('/api/auth/send-code', { phone: '12345' });
  38. expect(res.code).not.toBe(0);
  39. });
  40. });