返回 AiToEarn
manager.e2e-spec.ts
根目录 / project / aitoearn-electron / server / test / manager / manager.e2e-spec.ts
1 import { Agent } from '../agent';
2 import supertest from 'supertest';
3 import { Model } from 'mongoose';
4 import { getModelToken } from '@nestjs/mongoose';
5 import { Manager, ManagerStatus } from '../../src/db/schema/manager.schema';
6 import { encryptPassword } from '../../src/util/password.util';
7 import {
8 ErrHttpBack,
9 ErrHttpBackMap,
10 } from '../../src/filters/http-exception.back-code';
11
12 describe('ManagerController (e2e)', () => {
13 let agent: supertest.Agent;
14 let managerModel: Model<Manager>;
15 const testAccount = 'testadmin';
16 const testPassword = '123456';
17 const testName = '测试管理员';
18 let managerToken: string;
19
20 beforeAll(async () => {
21 agent = Agent.get();
22 managerModel = Agent.app().get(getModelToken(Manager.name));
23 });
24
25 beforeEach(async () => {
26 await managerModel.deleteMany({ account: testAccount });
27 });
28
29 describe('管理员认证', () => {
30 it('应该能创建管理员账号', async () => {
31 // 先创建一个管理员用于创建其他管理员
32 const { password, salt } = encryptPassword(
33 '123456',
34 '$2b$10$8n6z9rAh9rKfxoQkqxVqOe',
35 );
36 await managerModel.create({
37 account: 'admin',
38 password: password,
39 salt: salt,
40 name: '超级管理员',
41 status: ManagerStatus.OPEN,
42 });
43
44 // 登录获取token
45 const { body } = await agent.post('/manager/login').send({
46 account: 'admin',
47 password: '123456',
48 });
49
50 managerToken = body.data.token;
51
52 // 创建新管理员
53 const createRes = await agent
54 .post('/manager')
55 .set('Authorization', `Bearer ${managerToken}`)
56 .send({
57 account: testAccount,
58 password: testPassword,
59 name: testName,
60 });
61
62 expect(createRes.body.data).toMatchObject({
63 account: testAccount,
64 name: testName,
65 status: ManagerStatus.OPEN,
66 });
67
68 // 清理测试数据
69 await managerModel.deleteMany({ account: 'admin' });
70 });
71
72 it('应该能登录管理员账号', async () => {
73 // 先创建管理员
74 const { password, salt } = encryptPassword(
75 testPassword,
76 '$2b$10$8n6z9rAh9rKfxoQkqxVqOe',
77 );
78 await managerModel.create({
79 account: testAccount,
80 password: password,
81 salt: salt,
82 name: testName,
83 status: ManagerStatus.OPEN,
84 });
85
86 const { body } = await agent.post('/manager/login').send({
87 account: testAccount,
88 password: testPassword,
89 });
90
91 expect(body.data).toMatchObject({
92 token: expect.any(String),
93 managerInfo: expect.objectContaining({
94 account: testAccount,
95 name: testName,
96 status: ManagerStatus.OPEN,
97 }),
98 });
99
100 managerToken = body.data.token;
101 });
102
103 it('密码错误时不能登录', async () => {
104 const { password, salt } = encryptPassword(
105 testPassword,
106 '$2b$10$8n6z9rAh9rKfxoQkqxVqOe',
107 );
108 await managerModel.create({
109 account: testAccount,
110 password: password,
111 salt: salt,
112 name: testName,
113 status: ManagerStatus.OPEN,
114 });
115 const { body } = await agent
116 .post('/manager/login')
117 .send({
118 account: testAccount,
119 password: 'wrongpassword',
120 })
121 .expect(200);
122
123 expect(body.code).toBe(
124 ErrHttpBackMap.get(ErrHttpBack.err_no_power_login).errCode,
125 );
126 });
127 });
128
129 describe('管理员信息管理', () => {
130 beforeEach(async () => {
131 const { password, salt } = encryptPassword(
132 testPassword,
133 '$2b$10$8n6z9rAh9rKfxoQkqxVqOe',
134 );
135 // 创建测试管理员并登录
136 await managerModel.create({
137 account: testAccount,
138 password: password,
139 salt: salt,
140 name: testName,
141 status: ManagerStatus.OPEN,
142 });
143
144 const { body } = await agent.post('/manager/login').send({
145 account: testAccount,
146 password: testPassword,
147 });
148
149 managerToken = body.data.token;
150 });
151
152 it('应该能获取管理员信息', async () => {
153 const { body } = await agent
154 .get('/manager/info')
155 .set('Authorization', `Bearer ${managerToken}`)
156 .expect(200);
157
158 expect(body.data).toMatchObject({
159 account: testAccount,
160 name: testName,
161 status: ManagerStatus.OPEN,
162 });
163 });
164
165 it('应该能更新管理员信息', async () => {
166 const newName = '新名字';
167 const { body } = await agent
168 .put('/manager')
169 .set('Authorization', `Bearer ${managerToken}`)
170 .send({ name: newName })
171 .expect(200);
172
173 expect(body.data).toMatchObject({
174 account: testAccount,
175 name: newName,
176 status: ManagerStatus.OPEN,
177 });
178 });
179
180 it('应该能删除管理员', async () => {
181 await agent
182 .delete('/manager')
183 .set('Authorization', `Bearer ${managerToken}`)
184 .expect(200);
185
186 const manager = await managerModel.findOne({ account: testAccount });
187 expect(manager.status).toBe(ManagerStatus.DELETE);
188 });
189 });
190
191 afterEach(async () => {
192 await managerModel.deleteMany({ account: testAccount });
193 });
194 });
195
195 lines TYPESCRIPT