Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 1 | import MockAdapter from 'axios-mock-adapter';
|
Akane1217 | 3a7bb97 | 2025-06-01 01:05:27 +0800 | [diff] [blame] | 2 | import { api, login, register, getUserInfo, isAdmin } from './auth';
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 3 |
|
| 4 | describe('auth API', () => {
|
| 5 | let mockAxios;
|
| 6 |
|
| 7 | beforeEach(() => {
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 8 | mockAxios = new MockAdapter(api);
|
| 9 | localStorage.clear();
|
| 10 | });
|
| 11 |
|
| 12 | afterEach(() => {
|
| 13 | mockAxios.restore();
|
| 14 | });
|
| 15 |
|
| 16 | describe('login', () => {
|
| 17 | it('should send login request with username and password', async () => {
|
Akane1217 | 3a7bb97 | 2025-06-01 01:05:27 +0800 | [diff] [blame] | 18 | const mockResponse = {
|
| 19 | code: 200,
|
| 20 | data: {
|
| 21 | token: 'mock-token',
|
| 22 | userInfo: { username: 'testuser' }
|
| 23 | },
|
| 24 | message: '登录成功'
|
| 25 | };
|
| 26 |
|
| 27 | mockAxios.onPost('/user/login', undefined, {
|
| 28 | params: { username: 'testuser', password: 'testpass' }
|
| 29 | }).reply(200, mockResponse);
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 30 |
|
Akane1217 | 3a7bb97 | 2025-06-01 01:05:27 +0800 | [diff] [blame] | 31 | const response = await login('testuser', 'testpass');
|
| 32 |
|
| 33 | expect(response.data).toEqual(mockResponse);
|
| 34 | expect(localStorage.getItem('token')).toBe('mock-token');
|
| 35 | });
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 36 |
|
| 37 | it('should handle login failure', async () => {
|
Akane1217 | 3a7bb97 | 2025-06-01 01:05:27 +0800 | [diff] [blame] | 38 | mockAxios.onPost('/user/login').reply(401, {
|
| 39 | code: 401,
|
| 40 | message: '登录失败'
|
| 41 | });
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 42 |
|
| 43 | await expect(login('wronguser', 'wrongpass')).rejects.toThrow();
|
| 44 | });
|
| 45 | });
|
| 46 |
|
| 47 | describe('register', () => {
|
| 48 | it('should send register request with username, password and code', async () => {
|
| 49 | const mockResponse = {
|
| 50 | code: 200,
|
| 51 | message: '注册成功'
|
| 52 | };
|
| 53 |
|
Akane1217 | 3a7bb97 | 2025-06-01 01:05:27 +0800 | [diff] [blame] | 54 | mockAxios.onPost('/user/regist', undefined, {
|
| 55 | params: { username: 'newuser', password: 'newpass', code: 'invite123' }
|
| 56 | }).reply(200, mockResponse);
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 57 |
|
| 58 | const response = await register('newuser', 'newpass', 'invite123');
|
| 59 |
|
| 60 | expect(response.data).toEqual(mockResponse);
|
| 61 | });
|
| 62 |
|
| 63 | it('should handle registration failure', async () => {
|
Akane1217 | 3a7bb97 | 2025-06-01 01:05:27 +0800 | [diff] [blame] | 64 | mockAxios.onPost('/user/regist').reply(400, {
|
| 65 | code: 400,
|
| 66 | message: '注册失败'
|
| 67 | });
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 68 |
|
| 69 | await expect(register('newuser', 'newpass', 'wrongcode')).rejects.toThrow();
|
| 70 | });
|
| 71 | });
|
| 72 |
|
| 73 | describe('getUserInfo', () => {
|
Akane1217 | 3a7bb97 | 2025-06-01 01:05:27 +0800 | [diff] [blame] | 74 | it('should send request to get user info', async () => {
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 75 | const mockResponse = {
|
| 76 | code: 200,
|
Akane1217 | 3a7bb97 | 2025-06-01 01:05:27 +0800 | [diff] [blame] | 77 | data: { username: 'testuser', authority: 'USER' }
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 78 | };
|
| 79 |
|
Akane1217 | 3a7bb97 | 2025-06-01 01:05:27 +0800 | [diff] [blame] | 80 | mockAxios.onGet('/user/userInfo').reply(200, mockResponse);
|
| 81 |
|
| 82 | const response = await getUserInfo();
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 83 |
|
Akane1217 | 3a7bb97 | 2025-06-01 01:05:27 +0800 | [diff] [blame] | 84 | expect(response).toEqual(mockResponse.data);
|
| 85 | });
|
| 86 |
|
| 87 | it('should handle unauthorized request', async () => {
|
| 88 | mockAxios.onGet('/user/userInfo').reply(401);
|
| 89 |
|
| 90 | await expect(getUserInfo()).rejects.toThrow('Request failed with status code 401');
|
| 91 | });
|
| 92 | });
|
| 93 |
|
| 94 | describe('isAdmin', () => {
|
| 95 | it('should return true when user is admin', async () => {
|
| 96 | const mockResponse = {
|
| 97 | code: 200,
|
| 98 | data: { username: 'admin', authority: 'ADMIN' }
|
| 99 | };
|
| 100 |
|
| 101 | mockAxios.onGet('/user/userInfo').reply(200, mockResponse);
|
| 102 |
|
| 103 | const result = await isAdmin();
|
| 104 | expect(result).toBe(true);
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 105 | });
|
| 106 |
|
Akane1217 | 3a7bb97 | 2025-06-01 01:05:27 +0800 | [diff] [blame] | 107 | it('should return false when user is not admin', async () => {
|
| 108 | const mockResponse = {
|
| 109 | code: 200,
|
| 110 | data: { username: 'user', authority: 'USER' }
|
| 111 | };
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 112 |
|
Akane1217 | 3a7bb97 | 2025-06-01 01:05:27 +0800 | [diff] [blame] | 113 | mockAxios.onGet('/user/userInfo').reply(200, mockResponse);
|
| 114 |
|
| 115 | const result = await isAdmin();
|
| 116 | expect(result).toBe(false);
|
| 117 | });
|
| 118 |
|
| 119 | it('should return false when request fails', async () => {
|
| 120 | mockAxios.onGet('/user/userInfo').reply(401);
|
| 121 |
|
| 122 | const result = await isAdmin();
|
| 123 | expect(result).toBe(false);
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 124 | });
|
| 125 | });
|
| 126 |
|
| 127 | describe('request interceptor', () => {
|
| 128 | it('should add Authorization header when token exists', async () => {
|
| 129 | localStorage.setItem('token', 'test-token');
|
| 130 | const mockResponse = { data: 'success' };
|
| 131 |
|
| 132 | mockAxios.onGet('/test').reply((config) => {
|
| 133 | expect(config.headers.Authorization).toBe('Bearer test-token');
|
| 134 | return [200, mockResponse];
|
| 135 | });
|
| 136 |
|
| 137 | const response = await api.get('/test');
|
| 138 | expect(response.data).toEqual(mockResponse);
|
| 139 | });
|
| 140 |
|
| 141 | it('should not add Authorization header when token does not exist', async () => {
|
| 142 | const mockResponse = { data: 'success' };
|
| 143 |
|
| 144 | mockAxios.onGet('/test').reply((config) => {
|
| 145 | expect(config.headers.Authorization).toBeUndefined();
|
| 146 | return [200, mockResponse];
|
| 147 | });
|
| 148 |
|
| 149 | const response = await api.get('/test');
|
| 150 | expect(response.data).toEqual(mockResponse);
|
| 151 | });
|
| 152 | });
|
| 153 | }); |