| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- /**
- * 认证流程集成测试
- * 测试注册→登录→Token刷新完整流程
- */
- import { describe, it, expect, beforeAll } from 'vitest';
- import { apiPost, apiGet, setAuthToken, initHttpClient } from '../../integration/http-client';
- const BASE_URL = 'http://localhost:3000';
- describe('认证流程集成测试', () => {
- let testPhone: string;
- let authToken: string;
- beforeAll(() => {
- initHttpClient();
- testPhone = `138${Date.now()}`.slice(0, 11);
- });
- describe('发送验证码', () => {
- it('发送验证码成功', async () => {
- const res = await apiPost('/api/auth/send-code', { phone: testPhone });
- expect(res.code).toBe(0);
- });
- it('无效手机号格式被拒绝', async () => {
- const res = await apiPost('/api/auth/send-code', { phone: '123' });
- expect(res.code).not.toBe(0);
- });
- });
- describe('登录/注册', () => {
- it('新用户注册并登录', async () => {
- await apiPost('/api/auth/send-code', { phone: testPhone });
- const res = await apiPost('/api/auth/login', {
- phone: testPhone,
- code: '123456'
- });
- expect(res.code).toBe(0);
- expect(res.data?.token).toBeDefined();
- authToken = res.data?.token;
- setAuthToken(authToken);
- });
- it('获取用户信息', async () => {
- const res = await apiGet('/api/auth/user-info');
- expect(res.code).toBe(0);
- expect(res.data?.phone).toBe(testPhone);
- });
- it('Token 无效时返回认证错误', async () => {
- setAuthToken('invalid_token_12345');
- const res = await apiGet('/api/auth/user-info');
- expect(res.code).not.toBe(0);
- });
- });
- describe('Token 刷新', () => {
- it('有效 Token 获取用户信息成功', async () => {
- if (!authToken) {
- await apiPost('/api/auth/send-code', { phone: testPhone });
- const loginRes = await apiPost('/api/auth/login', {
- phone: testPhone,
- code: '123456'
- });
- authToken = loginRes.data?.token || '';
- setAuthToken(authToken);
- }
- const res = await apiGet('/api/auth/user-info');
- expect(res.code).toBe(0);
- expect(res.data).toBeDefined();
- });
- });
- describe('会员状态', () => {
- it('新用户默认免费会员', async () => {
- if (!authToken) return;
- const res = await apiGet('/api/auth/user-info');
- expect(res.data?.memberLevel).toBe(0);
- });
- });
- });
|