“实现帖子与评论上传图片,删除评论,评论计数,管理员界面”
Change-Id: I33d5331e41de0411f2d6f1913f3a939db61f665d
diff --git a/src/api/administer.test.js b/src/api/administer.test.js
new file mode 100644
index 0000000..beb1e11
--- /dev/null
+++ b/src/api/administer.test.js
@@ -0,0 +1,215 @@
+import axios from 'axios';
+import MockAdapter from 'axios-mock-adapter';
+import {
+ getAllUsers,
+ searchUsers,
+ updateUserAuthority,
+ getAllDiscounts,
+ getCurrentDiscount,
+ addDiscount,
+ deleteDiscount
+} from './administer';
+
+describe('Administer API', () => {
+ let mock;
+
+ beforeAll(() => {
+ mock = new MockAdapter(axios);
+ localStorage.setItem('token', 'test-token');
+ });
+
+ afterEach(() => {
+ mock.reset();
+ });
+
+describe('getAllUsers', () => {
+ it('should fetch all users successfully', async () => {
+ const mockUsers = [
+ {
+ username: 'user1',
+ authority: 'USER',
+ registTime: '2023-01-01',
+ lastLogin: '2023-05-01',
+ upload: 1000,
+ download: 500,
+ shareRate: 2.0,
+ magicPoints: 100
+ }
+ ];
+
+ mock.onGet('http://localhost:8088/user/allUser').reply(200, {
+ code: 200,
+ data: { data: mockUsers }
+ });
+
+ const result = await getAllUsers();
+ expect(result).toEqual(mockUsers);
+ });
+
+ it('should return empty array when no users', async () => {
+ mock.onGet('http://localhost:8088/user/allUser').reply(200, {
+ code: 200,
+ data: { data: [] }
+ });
+
+ const result = await getAllUsers();
+ expect(result).toEqual([]);
+ });
+
+ it('should handle error when fetching users', async () => {
+ mock.onGet('http://localhost:8088/user/allUser').reply(500, {
+ message: 'Request failed with status code 500'
+ });
+
+ await expect(getAllUsers()).rejects.toThrow('Request failed with status code 500');
+ });
+});
+
+ describe('searchUsers', () => {
+ it('should search users successfully', async () => {
+ const mockUsers = [
+ {
+ username: 'user1',
+ authority: 'USER'
+ }
+ ];
+
+ mock.onGet('http://localhost:8088/user/searchUser', { params: { key: 'user' } })
+ .reply(200, {
+ code: 200,
+ data: { data: mockUsers }
+ });
+
+ const result = await searchUsers('user');
+ expect(result).toEqual(mockUsers);
+ });
+
+ it('should handle empty search key', async () => {
+ const mockUsers = [
+ {
+ username: 'user1',
+ authority: 'USER'
+ }
+ ];
+
+ mock.onGet('http://localhost:8088/user/searchUser', { params: { key: '' } })
+ .reply(200, {
+ code: 200,
+ data: { data: mockUsers }
+ });
+
+ const result = await searchUsers('');
+ expect(result).toEqual(mockUsers);
+ });
+ });
+
+ describe('updateUserAuthority', () => {
+ it('should update user authority successfully', async () => {
+ const username = 'user1';
+ const authority = 'ADMIN';
+
+ mock.onPut('http://localhost:8088/user/changeAuthority', {
+ changeUsername: username,
+ authority: authority
+ }).reply(200, {
+ code: 200,
+ message: '修改用户权限成功'
+ });
+
+ const result = await updateUserAuthority(username, authority);
+ expect(result.code).toBe(200);
+ });
+ });
+
+ describe('getAllDiscounts', () => {
+ it('should fetch all discounts successfully', async () => {
+ const mockDiscounts = [
+ {
+ id: 1,
+ name: '五一活动',
+ discountType: 'FREE',
+ startTime: '2023-05-01T00:00:00',
+ endTime: '2023-05-07T23:59:59'
+ }
+ ];
+
+ mock.onGet('http://localhost:8088/discount/all').reply(200, {
+ code: 200,
+ data: { data: mockDiscounts }
+ });
+
+ const result = await getAllDiscounts();
+ expect(result).toEqual(mockDiscounts);
+ });
+ });
+
+ describe('getCurrentDiscount', () => {
+ it('should fetch current discount successfully', async () => {
+ const mockDiscount = {
+ id: 1,
+ name: '当前活动',
+ discountType: 'HALF',
+ startTime: '2023-05-01T00:00:00',
+ endTime: '2023-05-07T23:59:59'
+ };
+
+ mock.onGet('http://localhost:8088/discount/current').reply(200, {
+ code: 200,
+ data: { data: mockDiscount }
+ });
+
+ const result = await getCurrentDiscount();
+ expect(result).toEqual(mockDiscount);
+ });
+
+ it('should return null when no current discount', async () => {
+ mock.onGet('http://localhost:8088/discount/current').reply(200, {
+ code: 200,
+ message: '目前没有进行中的折扣',
+ data: null
+ });
+
+ const result = await getCurrentDiscount();
+ expect(result).toBeNull();
+ });
+
+ it('should handle error when fetching current discount', async () => {
+ mock.onGet('http://localhost:8088/discount/current').reply(500);
+
+ await expect(getCurrentDiscount()).rejects.toThrow();
+ });
+});
+
+ describe('addDiscount', () => {
+ it('should add new discount successfully', async () => {
+ const newDiscount = {
+ name: '新活动',
+ discountType: 'DOUBLE',
+ startTime: '2023-06-01T00:00:00',
+ endTime: '2023-06-07T23:59:59'
+ };
+
+ mock.onPost('http://localhost:8088/discount/add', newDiscount).reply(200, {
+ code: 200,
+ data: { data: { id: 2, ...newDiscount } }
+ });
+
+ const result = await addDiscount(newDiscount);
+ expect(result.id).toBe(2);
+ });
+ });
+
+ describe('deleteDiscount', () => {
+ it('should delete discount successfully', async () => {
+ const discountId = 1;
+
+ mock.onDelete(`http://localhost:8088/discount/delete/${discountId}`).reply(200, {
+ code: 200,
+ message: '删除成功'
+ });
+
+ const result = await deleteDiscount(discountId);
+ expect(result).toBe(true);
+ });
+ });
+});
\ No newline at end of file