个人中心全部,模糊乱序搜索,类型筛选

Change-Id: Id635654fccccaea80bfbf4d1480abd55f7d12046
diff --git a/src/api/auth.test.js b/src/api/auth.test.js
index c9ace4d..9de5691 100644
--- a/src/api/auth.test.js
+++ b/src/api/auth.test.js
@@ -1,11 +1,10 @@
 import MockAdapter from 'axios-mock-adapter';

-import { api, login, register, getUserInfo } from './auth';

+import { api, login, register, getUserInfo, isAdmin } from './auth';

 

 describe('auth API', () => {

   let mockAxios;

 

   beforeEach(() => {

-    // 确保使用我们导出的 api 实例

     mockAxios = new MockAdapter(api);

     localStorage.clear();

   });

@@ -16,28 +15,30 @@
 

   describe('login', () => {

     it('should send login request with username and password', async () => {

-        const mockResponse = {

-          code: 200,

-          data: { 

-            token: 'mock-token',

-            // 确保响应结构与实际API一致

-            userInfo: { username: 'testuser' }

-          },

-          message: '登录成功'

-        };

-        

-        mockAxios.onPost('/user/login').reply(200, mockResponse);

+      const mockResponse = {

+        code: 200,

+        data: { 

+          token: 'mock-token',

+          userInfo: { username: 'testuser' }

+        },

+        message: '登录成功'

+      };

+      

+      mockAxios.onPost('/user/login', undefined, {

+        params: { username: 'testuser', password: 'testpass' }

+      }).reply(200, mockResponse);

     

-        const response = await login('testuser', 'testpass');

-        

-        expect(response.data).toEqual(mockResponse);

-        // 检查token是否存入localStorage

-        expect(localStorage.getItem('token')).toBe('mock-token');

-      });

-    

+      const response = await login('testuser', 'testpass');

+      

+      expect(response.data).toEqual(mockResponse);

+      expect(localStorage.getItem('token')).toBe('mock-token');

+    });

 

     it('should handle login failure', async () => {

-      mockAxios.onPost('/user/login').reply(401);

+      mockAxios.onPost('/user/login').reply(401, {

+        code: 401,

+        message: '登录失败'

+      });

       

       await expect(login('wronguser', 'wrongpass')).rejects.toThrow();

     });

@@ -50,7 +51,9 @@
         message: '注册成功'

       };

       

-      mockAxios.onPost('/user/regist').reply(200, mockResponse);

+      mockAxios.onPost('/user/regist', undefined, {

+        params: { username: 'newuser', password: 'newpass', code: 'invite123' }

+      }).reply(200, mockResponse);

 

       const response = await register('newuser', 'newpass', 'invite123');

       

@@ -58,30 +61,66 @@
     });

 

     it('should handle registration failure', async () => {

-      mockAxios.onPost('/user/regist').reply(400);

+      mockAxios.onPost('/user/regist').reply(400, {

+        code: 400,

+        message: '注册失败'

+      });

       

       await expect(register('newuser', 'newpass', 'wrongcode')).rejects.toThrow();

     });

   });

 

   describe('getUserInfo', () => {

-    it('should send request with token to get user info', async () => {

+    it('should send request to get user info', async () => {

       const mockResponse = {

         code: 200,

-        data: { username: 'testuser', role: 'user' }

+        data: { username: 'testuser', authority: 'USER' }

       };

       

-      mockAxios.onGet('/user/info').reply(200, mockResponse);

-

-      const response = await getUserInfo('test-token');

+      mockAxios.onGet('/user/userInfo').reply(200, mockResponse);

+  

+      const response = await getUserInfo();

       

-      expect(response.data).toEqual(mockResponse);

+      expect(response).toEqual(mockResponse.data);

+    });

+  

+    it('should handle unauthorized request', async () => {

+      mockAxios.onGet('/user/userInfo').reply(401);

+      

+      await expect(getUserInfo()).rejects.toThrow('Request failed with status code 401');

+    });

+  });

+  

+  describe('isAdmin', () => {

+    it('should return true when user is admin', async () => {

+      const mockResponse = {

+        code: 200,

+        data: { username: 'admin', authority: 'ADMIN' }

+      };

+      

+      mockAxios.onGet('/user/userInfo').reply(200, mockResponse);

+

+      const result = await isAdmin();

+      expect(result).toBe(true);

     });

 

-    it('should handle unauthorized request', async () => {

-      mockAxios.onGet('/user/info').reply(401);

+    it('should return false when user is not admin', async () => {

+      const mockResponse = {

+        code: 200,

+        data: { username: 'user', authority: 'USER' }

+      };

       

-      await expect(getUserInfo('invalid-token')).rejects.toThrow();

+      mockAxios.onGet('/user/userInfo').reply(200, mockResponse);

+

+      const result = await isAdmin();

+      expect(result).toBe(false);

+    });

+

+    it('should return false when request fails', async () => {

+      mockAxios.onGet('/user/userInfo').reply(401);

+      

+      const result = await isAdmin();

+      expect(result).toBe(false);

     });

   });