Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 1 | import MockAdapter from 'axios-mock-adapter';
|
| 2 | import { api, login, register, getUserInfo } from './auth';
|
| 3 |
|
| 4 | describe('auth API', () => {
|
| 5 | let mockAxios;
|
| 6 |
|
| 7 | beforeEach(() => {
|
| 8 | // 确保使用我们导出的 api 实例
|
| 9 | mockAxios = new MockAdapter(api);
|
| 10 | localStorage.clear();
|
| 11 | });
|
| 12 |
|
| 13 | afterEach(() => {
|
| 14 | mockAxios.restore();
|
| 15 | });
|
| 16 |
|
| 17 | describe('login', () => {
|
| 18 | it('should send login request with username and password', async () => {
|
| 19 | const mockResponse = {
|
| 20 | code: 200,
|
| 21 | data: {
|
| 22 | token: 'mock-token',
|
| 23 | // 确保响应结构与实际API一致
|
| 24 | userInfo: { username: 'testuser' }
|
| 25 | },
|
| 26 | message: '登录成功'
|
| 27 | };
|
| 28 |
|
| 29 | mockAxios.onPost('/user/login').reply(200, mockResponse);
|
| 30 |
|
| 31 | const response = await login('testuser', 'testpass');
|
| 32 |
|
| 33 | expect(response.data).toEqual(mockResponse);
|
| 34 | // 检查token是否存入localStorage
|
| 35 | expect(localStorage.getItem('token')).toBe('mock-token');
|
| 36 | });
|
| 37 |
|
| 38 |
|
| 39 | it('should handle login failure', async () => {
|
| 40 | mockAxios.onPost('/user/login').reply(401);
|
| 41 |
|
| 42 | await expect(login('wronguser', 'wrongpass')).rejects.toThrow();
|
| 43 | });
|
| 44 | });
|
| 45 |
|
| 46 | describe('register', () => {
|
| 47 | it('should send register request with username, password and code', async () => {
|
| 48 | const mockResponse = {
|
| 49 | code: 200,
|
| 50 | message: '注册成功'
|
| 51 | };
|
| 52 |
|
| 53 | mockAxios.onPost('/user/regist').reply(200, mockResponse);
|
| 54 |
|
| 55 | const response = await register('newuser', 'newpass', 'invite123');
|
| 56 |
|
| 57 | expect(response.data).toEqual(mockResponse);
|
| 58 | });
|
| 59 |
|
| 60 | it('should handle registration failure', async () => {
|
| 61 | mockAxios.onPost('/user/regist').reply(400);
|
| 62 |
|
| 63 | await expect(register('newuser', 'newpass', 'wrongcode')).rejects.toThrow();
|
| 64 | });
|
| 65 | });
|
| 66 |
|
| 67 | describe('getUserInfo', () => {
|
| 68 | it('should send request with token to get user info', async () => {
|
| 69 | const mockResponse = {
|
| 70 | code: 200,
|
| 71 | data: { username: 'testuser', role: 'user' }
|
| 72 | };
|
| 73 |
|
| 74 | mockAxios.onGet('/user/info').reply(200, mockResponse);
|
| 75 |
|
| 76 | const response = await getUserInfo('test-token');
|
| 77 |
|
| 78 | expect(response.data).toEqual(mockResponse);
|
| 79 | });
|
| 80 |
|
| 81 | it('should handle unauthorized request', async () => {
|
| 82 | mockAxios.onGet('/user/info').reply(401);
|
| 83 |
|
| 84 | await expect(getUserInfo('invalid-token')).rejects.toThrow();
|
| 85 | });
|
| 86 | });
|
| 87 |
|
| 88 | describe('request interceptor', () => {
|
| 89 | it('should add Authorization header when token exists', async () => {
|
| 90 | localStorage.setItem('token', 'test-token');
|
| 91 | const mockResponse = { data: 'success' };
|
| 92 |
|
| 93 | mockAxios.onGet('/test').reply((config) => {
|
| 94 | expect(config.headers.Authorization).toBe('Bearer test-token');
|
| 95 | return [200, mockResponse];
|
| 96 | });
|
| 97 |
|
| 98 | const response = await api.get('/test');
|
| 99 | expect(response.data).toEqual(mockResponse);
|
| 100 | });
|
| 101 |
|
| 102 | it('should not add Authorization header when token does not exist', async () => {
|
| 103 | const mockResponse = { data: 'success' };
|
| 104 |
|
| 105 | mockAxios.onGet('/test').reply((config) => {
|
| 106 | expect(config.headers.Authorization).toBeUndefined();
|
| 107 | return [200, mockResponse];
|
| 108 | });
|
| 109 |
|
| 110 | const response = await api.get('/test');
|
| 111 | expect(response.data).toEqual(mockResponse);
|
| 112 | });
|
| 113 | });
|
| 114 | }); |