blob: 9de5691e7a8d8c754a13475d075f34c6b30b83ea [file] [log] [blame]
Akane121765b61a72025-05-17 13:52:25 +08001import MockAdapter from 'axios-mock-adapter';
Akane12173a7bb972025-06-01 01:05:27 +08002import { api, login, register, getUserInfo, isAdmin } from './auth';
Akane121765b61a72025-05-17 13:52:25 +08003
4describe('auth API', () => {
5 let mockAxios;
6
7 beforeEach(() => {
Akane121765b61a72025-05-17 13:52:25 +08008 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 () => {
Akane12173a7bb972025-06-01 01:05:27 +080018 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);
Akane121765b61a72025-05-17 13:52:25 +080030
Akane12173a7bb972025-06-01 01:05:27 +080031 const response = await login('testuser', 'testpass');
32
33 expect(response.data).toEqual(mockResponse);
34 expect(localStorage.getItem('token')).toBe('mock-token');
35 });
Akane121765b61a72025-05-17 13:52:25 +080036
37 it('should handle login failure', async () => {
Akane12173a7bb972025-06-01 01:05:27 +080038 mockAxios.onPost('/user/login').reply(401, {
39 code: 401,
40 message: '登录失败'
41 });
Akane121765b61a72025-05-17 13:52:25 +080042
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
Akane12173a7bb972025-06-01 01:05:27 +080054 mockAxios.onPost('/user/regist', undefined, {
55 params: { username: 'newuser', password: 'newpass', code: 'invite123' }
56 }).reply(200, mockResponse);
Akane121765b61a72025-05-17 13:52:25 +080057
58 const response = await register('newuser', 'newpass', 'invite123');
59
60 expect(response.data).toEqual(mockResponse);
61 });
62
63 it('should handle registration failure', async () => {
Akane12173a7bb972025-06-01 01:05:27 +080064 mockAxios.onPost('/user/regist').reply(400, {
65 code: 400,
66 message: '注册失败'
67 });
Akane121765b61a72025-05-17 13:52:25 +080068
69 await expect(register('newuser', 'newpass', 'wrongcode')).rejects.toThrow();
70 });
71 });
72
73 describe('getUserInfo', () => {
Akane12173a7bb972025-06-01 01:05:27 +080074 it('should send request to get user info', async () => {
Akane121765b61a72025-05-17 13:52:25 +080075 const mockResponse = {
76 code: 200,
Akane12173a7bb972025-06-01 01:05:27 +080077 data: { username: 'testuser', authority: 'USER' }
Akane121765b61a72025-05-17 13:52:25 +080078 };
79
Akane12173a7bb972025-06-01 01:05:27 +080080 mockAxios.onGet('/user/userInfo').reply(200, mockResponse);
81
82 const response = await getUserInfo();
Akane121765b61a72025-05-17 13:52:25 +080083
Akane12173a7bb972025-06-01 01:05:27 +080084 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);
Akane121765b61a72025-05-17 13:52:25 +0800105 });
106
Akane12173a7bb972025-06-01 01:05:27 +0800107 it('should return false when user is not admin', async () => {
108 const mockResponse = {
109 code: 200,
110 data: { username: 'user', authority: 'USER' }
111 };
Akane121765b61a72025-05-17 13:52:25 +0800112
Akane12173a7bb972025-06-01 01:05:27 +0800113 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);
Akane121765b61a72025-05-17 13:52:25 +0800124 });
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});