返回 AiToEarn
user.e2e-spec.ts
根目录 / project / aitoearn-electron / server / test / user / user.e2e-spec.ts
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 { mockSmsService } from '../mocks/sms.mock';
8
9 describe('UserController (e2e)', () => {
10 let agent: supertest.Agent;
11 let userModel: Model<User>;
12 const testPhone = '13800138001';
13 const testPassword = '123456';
14 let smsServiceMock;
15
16 beforeAll(async () => {
17 agent = Agent.get();
18 userModel = Agent.app().get(getModelToken(User.name));
19 smsServiceMock = mockSmsService();
20 });
21
22 beforeEach(async () => {
23 await userModel.deleteMany({ phone: testPhone });
24 });
25
26 describe('用户注册登录', () => {
27 it('应该能发送验证码', async () => {
28 const { body } = await agent
29 .post('/user/code')
30 .send({ phone: testPhone })
31 .expect(201);
32
33 expect(body.data).toBe(true);
34 });
35
36 it('应该能注册新用户', async () => {
37 const { body } = await agent.post('/user/add').send({
38 phone: testPhone,
39 password: testPassword,
40 code: '123456',
41 });
42 expect(body.data).toMatchObject({
43 token: expect.any(String),
44 userInfo: expect.objectContaining({
45 phone: testPhone,
46 status: UserStatus.OPEN,
47 }),
48 });
49 });
50
51 it('应该能通过密码登录', async () => {
52 // 先创建用户
53 await agent.post('/user/add').send({
54 phone: testPhone,
55 password: testPassword,
56 code: '123456',
57 });
58
59 const { body } = await agent.post('/user/login/password').send({
60 phone: testPhone,
61 password: testPassword,
62 });
63
64 expect(body.data).toMatchObject({
65 token: expect.any(String),
66 userInfo: expect.objectContaining({
67 phone: testPhone,
68 status: UserStatus.OPEN,
69 }),
70 });
71 });
72
73 it('应该能通过验证码登录', async () => {
74 // 先发送验证码
75 await agent.post('/user/code').send({ phone: testPhone });
76
77 const { body } = await agent
78 .post('/user/login/code/phone')
79 .send({
80 phone: testPhone,
81 code: '123456', // 测试环境验证码固定为123456
82 })
83 .expect(201);
84
85 expect(body.data).toMatchObject({
86 token: expect.any(String),
87 exp: expect.any(Number),
88 userInfo: expect.objectContaining({
89 phone: testPhone,
90 status: UserStatus.OPEN,
91 }),
92 });
93 });
94 });
95
96 describe('用户信息管理', () => {
97 let token: string;
98
99 beforeEach(async () => {
100 // 创建测试用户并登录
101 const { body } = await agent.post('/user/add').send({
102 phone: testPhone,
103 password: testPassword,
104 code: '123456',
105 });
106 token = body.data.token;
107 agent.set('Authorization', `Bearer ${token}`);
108 });
109
110 it('应该能获取用户信息', async () => {
111 const { body } = await agent.get('/user/mine').expect(200);
112
113 expect(body.data).toMatchObject({
114 phone: testPhone,
115 status: UserStatus.OPEN,
116 });
117 });
118
119 it('应该能更新用户信息', async () => {
120 const newName = '测试用户';
121 const { body } = await agent
122 .put('/user/info/update')
123 .send({ name: newName })
124 .expect(200);
125
126 expect(body.data).toMatchObject({
127 name: newName,
128 phone: testPhone,
129 });
130 });
131
132 it('应该能修改密码', async () => {
133 const newPassword = '654321';
134 await agent
135 .post('/user/password/change')
136 .send({ password: newPassword })
137 .expect(201);
138
139 // 验证新密码是否生效
140 const { body } = await agent
141 .post('/user/login/password')
142 .send({
143 phone: testPhone,
144 password: newPassword,
145 })
146 .expect(201);
147
148 expect(body.data.userInfo).toMatchObject({
149 phone: testPhone,
150 status: UserStatus.OPEN,
151 });
152 });
153
154 it('应该能刷新token', async () => {
155 const { body } = await agent.post('/user/token/refresh').expect(201);
156
157 expect(body.data).toMatchObject({
158 token: expect.any(String),
159 });
160 });
161
162 it('应该能注销账号', async () => {
163 await agent.delete('/user/del');
164
165 // 验证用户状态是否更新
166 const userInfo = await userModel.findOne({ phone: testPhone });
167
168 expect(userInfo.status).toBe(UserStatus.DELETE);
169 });
170 });
171
172 afterAll(async () => {
173 await userModel.deleteMany({ phone: testPhone });
174 smsServiceMock.restore();
175 });
176 });
177
177 lines TYPESCRIPT