| 1 | import { Agent } from '../agent'; |
| 2 | import supertest from 'supertest'; |
| 3 | import { UserStatus } from '../../src/db/schema/user.schema'; |
| 4 | import { Model } from 'mongoose'; |
| 5 | import { getModelToken } from '@nestjs/mongoose'; |
| 6 | import { User } from '../../src/db/schema/user.schema'; |
| 7 | import { Manager, ManagerStatus } from '../../src/db/schema/manager.schema'; |
| 8 | import { encryptPassword } from '../../src/util/password.util'; |
| 9 | |
| 10 | describe('UserAdminController (e2e)', () => { |
| 11 | let agent: supertest.Agent; |
| 12 | let userModel: Model<User>; |
| 13 | let managerModel: Model<Manager>; |
| 14 | let managerToken: string; |
| 15 | |
| 16 | const testManager = { |
| 17 | account: 'testadmin', |
| 18 | password: '123456', |
| 19 | name: '测试管理员', |
| 20 | }; |
| 21 | |
| 22 | const testUsers = [ |
| 23 | { phone: '13800138001', name: '测试用户1', status: UserStatus.OPEN }, |
| 24 | { phone: '13800138002', name: '测试用户2', status: UserStatus.OPEN }, |
| 25 | { phone: '13800138003', name: '测试用户3', status: UserStatus.STOP }, |
| 26 | ]; |
| 27 | |
| 28 | beforeAll(async () => { |
| 29 | agent = Agent.get(); |
| 30 | userModel = Agent.app().get(getModelToken(User.name)); |
| 31 | managerModel = Agent.app().get(getModelToken(Manager.name)); |
| 32 | }); |
| 33 | |
| 34 | beforeEach(async () => { |
| 35 | // 清理测试数据 |
| 36 | await userModel.deleteMany({ |
| 37 | phone: { $in: testUsers.map((u) => u.phone) }, |
| 38 | }); |
| 39 | await managerModel.deleteMany({ account: testManager.account }); |
| 40 | |
| 41 | // 创建测试用户 |
| 42 | await userModel.create(testUsers); |
| 43 | |
| 44 | // 创建并登录测试管理员 |
| 45 | const { password, salt } = encryptPassword( |
| 46 | testManager.password, |
| 47 | '$2b$10$8n6z9rAh9rKfxoQkqxVqOe', |
| 48 | ); |
| 49 | await managerModel.create({ |
| 50 | account: testManager.account, |
| 51 | password, |
| 52 | salt, |
| 53 | name: testManager.name, |
| 54 | status: ManagerStatus.OPEN, |
| 55 | }); |
| 56 | |
| 57 | const { body } = await agent.post('/manager/login').send({ |
| 58 | account: testManager.account, |
| 59 | password: testManager.password, |
| 60 | }); |
| 61 | |
| 62 | managerToken = body.data.token; |
| 63 | }); |
| 64 | |
| 65 | describe('用户管理接口', () => { |
| 66 | it('应该能获取用户列表', async () => { |
| 67 | const { body } = await agent |
| 68 | .get('/admin/user/list') |
| 69 | .set('Authorization', `Bearer ${managerToken}`) |
| 70 | .query({ |
| 71 | page: 1, |
| 72 | pageSize: 10, |
| 73 | }) |
| 74 | .expect(200); |
| 75 | |
| 76 | expect(body.data).toMatchObject({ |
| 77 | items: expect.arrayContaining([ |
| 78 | expect.objectContaining({ |
| 79 | phone: testUsers[0].phone, |
| 80 | name: testUsers[0].name, |
| 81 | }), |
| 82 | ]), |
| 83 | meta: { |
| 84 | currentPage: 1, |
| 85 | itemsPerPage: 10, |
| 86 | totalItems: expect.any(Number), |
| 87 | totalPages: expect.any(Number), |
| 88 | itemCount: expect.any(Number), |
| 89 | }, |
| 90 | }); |
| 91 | }); |
| 92 | |
| 93 | it('应该能按条件搜索用户', async () => { |
| 94 | const { body } = await agent |
| 95 | .get('/admin/user/list') |
| 96 | .set('Authorization', `Bearer ${managerToken}`) |
| 97 | .query({ |
| 98 | phone: testUsers[0].phone, |
| 99 | status: 1, |
| 100 | }) |
| 101 | .expect(200); |
| 102 | |
| 103 | expect(body.data.items).toEqual( |
| 104 | expect.arrayContaining([ |
| 105 | expect.objectContaining({ |
| 106 | phone: testUsers[0].phone, |
| 107 | status: UserStatus.OPEN, |
| 108 | }), |
| 109 | ]), |
| 110 | ); |
| 111 | }); |
| 112 | |
| 113 | it('应该能获取用户详情', async () => { |
| 114 | const user = await userModel.findOne({ phone: testUsers[0].phone }); |
| 115 | |
| 116 | const { body } = await agent |
| 117 | .get(`/admin/user/${user.id}`) |
| 118 | .set('Authorization', `Bearer ${managerToken}`) |
| 119 | .expect(200); |
| 120 | |
| 121 | expect(body.data).toMatchObject({ |
| 122 | phone: testUsers[0].phone, |
| 123 | name: testUsers[0].name, |
| 124 | status: UserStatus.OPEN, |
| 125 | }); |
| 126 | }); |
| 127 | |
| 128 | it('应该能更新用户状态', async () => { |
| 129 | const user = await userModel.findOne({ phone: testUsers[0].phone }); |
| 130 | |
| 131 | const { body } = await agent |
| 132 | .put(`/admin/user/${user.id}/status`) |
| 133 | .set('Authorization', `Bearer ${managerToken}`) |
| 134 | .send({ status: 0 }) |
| 135 | .expect(200); |
| 136 | |
| 137 | const updatedUser = await userModel.findById(user.id); |
| 138 | expect(updatedUser.status).toBe(UserStatus.STOP); |
| 139 | }); |
| 140 | |
| 141 | it('应该能删除用户', async () => { |
| 142 | const user = await userModel.findOne({ phone: testUsers[0].phone }); |
| 143 | |
| 144 | await agent |
| 145 | .delete(`/admin/user/${user.id}`) |
| 146 | .set('Authorization', `Bearer ${managerToken}`) |
| 147 | .expect(200); |
| 148 | |
| 149 | const deletedUser = await userModel.findById(user.id); |
| 150 | expect(deletedUser.status).toBe(UserStatus.DELETE); |
| 151 | }); |
| 152 | |
| 153 | it('未登录时不能访问管理接口', async () => { |
| 154 | const { body } = await agent.get('/admin/user/list').expect(401); |
| 155 | |
| 156 | expect(body.code).not.toBe(0); |
| 157 | }); |
| 158 | |
| 159 | it('应该能正确处理不存在的用户ID', async () => { |
| 160 | const fakeId = '507f1f77bcf86cd799439011'; |
| 161 | |
| 162 | const { body: getBody } = await agent |
| 163 | .get(`/admin/user/${fakeId}`) |
| 164 | .set('Authorization', `Bearer ${managerToken}`) |
| 165 | .expect(200); |
| 166 | |
| 167 | if (getBody.data === null) { |
| 168 | expect(getBody.data).toBeNull(); |
| 169 | } else { |
| 170 | expect(getBody.code).not.toBe(0); |
| 171 | } |
| 172 | |
| 173 | const { body: updateBody } = await agent |
| 174 | .put(`/admin/user/${fakeId}/status`) |
| 175 | .set('Authorization', `Bearer ${managerToken}`) |
| 176 | .send({ status: 0 }) |
| 177 | .expect(200); |
| 178 | |
| 179 | expect(updateBody.code).not.toBe(0); |
| 180 | |
| 181 | const { body: deleteBody } = await agent |
| 182 | .delete(`/admin/user/${fakeId}`) |
| 183 | .set('Authorization', `Bearer ${managerToken}`) |
| 184 | .expect(200); |
| 185 | |
| 186 | expect(deleteBody.code).not.toBe(0); |
| 187 | }); |
| 188 | }); |
| 189 | |
| 190 | afterAll(async () => { |
| 191 | // 清理测试数据 |
| 192 | await userModel.deleteMany({ |
| 193 | phone: { $in: testUsers.map((u) => u.phone) }, |
| 194 | }); |
| 195 | await managerModel.deleteMany({ account: testManager.account }); |
| 196 | }); |
| 197 | }); |
| 198 |