create-dp-koa 1.0.1 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. package/package.json +1 -1
  2. package/template/.cursor/commands/cheatsheet-backend-controller.md +2 -2
  3. package/template/.cursor/commands/implement-backend-api-controller.md +6 -4
  4. package/template/.cursor/rules/11-backend-controller-recipes.skill.md +89 -10
  5. package/template/.trae/skills/11-backend-controller-recipes.skill.md +91 -10
  6. package/template/docs/FRAMEWORK_V2_UPGRADE_GUIDE.md +134 -0
  7. package/template/package.json +2 -0
  8. package/template/scripts/sync-template.mjs +39 -1
  9. package/template/src/app.ts +21 -16
  10. package/template/src/controllers/demo/AnnotationDemoController.ts +1 -3
  11. package/template/src/controllers/example/EnterpriseExampleController.ts +2 -9
  12. package/template/src/controllers/example/ExampleController.ts +13 -4
  13. package/template/src/entity/index.ts +1 -15
  14. package/template/src/framework/decorator/processor/AnnotationProcessor.ts +5 -1
  15. package/template/src/routers/index.ts +1 -37
  16. package/template/src/utils/testDataInitializer.ts +2 -269
  17. package/template/test/controllers/example/ExampleController.test.ts +29 -31
  18. package/template/test/framework/annotation/AnnotationDecorators.test.ts +15 -15
  19. package/template/test/framework/annotation/AnnotationExecutor.test.ts +27 -32
  20. package/template/test/framework/annotation/AnnotationProcessor.test.ts +25 -24
  21. package/template/test/framework/annotation/CustomProcessors.test.ts +15 -25
  22. package/template/test/framework/annotation/NewRouter.test.ts +9 -7
  23. package/template/test/framework/annotation/ProcessorManager.test.ts +14 -27
  24. package/template/test/framework/databaseConfig.test.ts +2 -2
  25. package/template/test/integration/integration.test.ts +15 -72
  26. package/template/webpack.config.js +2 -0
  27. package/template/package-lock.json +0 -13240
  28. package/template/src/controllers/cacheManagement.controller.ts +0 -131
  29. package/template/src/controllers/captcha.controller.ts +0 -57
  30. package/template/src/controllers/example/NewAnnotationExampleController.ts +0 -159
  31. package/template/src/controllers/example/SwaggerExampleController.ts +0 -205
  32. package/template/src/controllers/example/TransactionExample.controller.ts +0 -336
  33. package/template/src/controllers/health.controller.ts +0 -235
  34. package/template/src/controllers/home/register.controller.ts +0 -58
  35. package/template/src/controllers/home/ytGoods.controller.ts +0 -92
  36. package/template/src/controllers/home/ytShop.controller.ts +0 -135
  37. package/template/src/controllers/home/ytUser.controller.ts +0 -89
  38. package/template/src/controllers/logManagement.controller.ts +0 -396
  39. package/template/src/controllers/public/emailSend.controller.ts +0 -65
  40. package/template/src/controllers/public/ytUserAuth.controller.ts +0 -174
  41. package/template/src/controllers/testData.controller.ts +0 -253
  42. package/template/src/dto/controller/example/NewAnnotationExampleController.dto.ts +0 -73
  43. package/template/src/dto/controller/home/emailSend.controller.dto.ts +0 -40
  44. package/template/src/dto/controller/home/register.controller.dto.ts +0 -45
  45. package/template/src/dto/controller/home/ytGoods.controller.dto.ts +0 -55
  46. package/template/src/dto/controller/home/ytShop.controller.dto.ts +0 -69
  47. package/template/src/dto/controller/home/ytUser.controller.dto.ts +0 -44
  48. package/template/src/dto/controller/public/ytUserAuth.controller.dto.ts +0 -63
  49. package/template/src/dto/goods.dto.ts +0 -212
  50. package/template/src/dto/service/ytService.dto.ts +0 -13
  51. package/template/src/dto/user.dto.ts +0 -177
  52. package/template/src/entity/columnTypes.ts +0 -13
  53. package/template/src/entity/goodsImagesUnlockKey.entity.ts +0 -33
  54. package/template/src/entity/goodsUnlocker.entity.ts +0 -34
  55. package/template/src/entity/shop.entity.ts +0 -52
  56. package/template/src/entity/shopUser.entity.ts +0 -41
  57. package/template/src/entity/ytGoods.entity.ts +0 -94
  58. package/template/src/entity/ytUser.entity.ts +0 -96
  59. package/template/src/examples/SwaggerProcessorExample.ts +0 -169
  60. package/template/src/examples/TransactionManagerDemo.ts +0 -377
  61. package/template/src/framework/utils/dynamicSwagger.ts +0 -410
  62. package/template/src/repository/UserRepository.ts +0 -122
  63. package/template/src/service/paramValidateTest.service.ts +0 -139
  64. package/template/src/service/ytGoods.service.ts +0 -42
  65. package/template/src/service/ytShop.service.ts +0 -90
  66. package/template/src/service/ytUser.service.ts +0 -451
  67. package/template/src/test/swaggerParameterTest.ts +0 -90
  68. package/template/test/controllers/controllers.test.ts +0 -173
  69. package/template/test/controllers/example/NewAnnotationExampleController.test.ts +0 -200
  70. package/template/test/framework/TransactionManagerDemo.test.ts +0 -363
  71. package/template/test/service/business.test.ts +0 -87
  72. package/template/test/service/paramValidateTest.service.test.ts +0 -184
  73. package/template/test/service/ytUser.service.test.ts +0 -566
  74. package/template/yarn.lock +0 -7354
@@ -1,566 +0,0 @@
1
- import { TestDatabaseHelper, TestUtils } from '../utils/testHelpers';
2
- import { YtUserService } from '../../src/service/ytUser.service';
3
- import { Provider } from 'dp-ioc2';
4
- import { CreateUserDto, UpdateUserDto, QueryUserDto, LoginUserDto, ChangePasswordDto } from '../../src/dto/user.dto';
5
- import { YtUserTypeEnum, YtUserStatusEnum } from '../../src/entity/ytUser.entity';
6
- import { CommonServiceResultCode } from '../../src/framework/types/ServiceResult';
7
-
8
- describe('YtUserService 测试', () => {
9
- let userService: YtUserService;
10
-
11
- beforeAll(async () => {
12
- await TestDatabaseHelper.initTestDatabase();
13
- });
14
-
15
- afterAll(async () => {
16
- await TestDatabaseHelper.cleanupTestDatabase();
17
- });
18
-
19
- beforeEach(async () => {
20
- await TestDatabaseHelper.resetTestData();
21
- userService = Provider(YtUserService);
22
- });
23
-
24
- describe('createUser - 创建用户', () => {
25
- test('应该能够成功创建用户', async () => {
26
- const createUserDto: CreateUserDto = {
27
- nickName: '新测试用户',
28
- email: 'newuser@example.com',
29
- telnumber: '13900139000',
30
- password: 'Password123!',
31
- userType: YtUserTypeEnum.CUSTOMER,
32
- status: YtUserStatusEnum.NORMAL
33
- };
34
-
35
- const result = await userService.createUser(createUserDto);
36
-
37
- expect(result.code).toBe(CommonServiceResultCode.SUCCESS);
38
- expect(result.data).toBeDefined();
39
- expect(result.data?.email).toBe(createUserDto.email);
40
- expect(result.data?.telnumber).toBe(createUserDto.telnumber);
41
- expect(result.data?.nickName).toBe(createUserDto.nickName);
42
- expect(result.data?.password).not.toBe(createUserDto.password); // 密码应该被加密
43
- });
44
-
45
- test('应该拒绝重复的邮箱', async () => {
46
- const createUserDto: CreateUserDto = {
47
- nickName: '重复邮箱用户',
48
- email: 'test1@example.com', // 使用已有的邮箱
49
- telnumber: '13900139001',
50
- password: 'Password123!'
51
- };
52
-
53
- const result = await userService.createUser(createUserDto);
54
-
55
- expect(result.code).toBe(CommonServiceResultCode.CONFLICT);
56
- expect(result.message).toContain('邮箱已被使用');
57
- });
58
-
59
- test('应该拒绝重复的手机号', async () => {
60
- const createUserDto: CreateUserDto = {
61
- nickName: '重复手机用户',
62
- email: 'unique@example.com',
63
- telnumber: '13800138001', // 使用已有的手机号
64
- password: 'Password123!'
65
- };
66
-
67
- const result = await userService.createUser(createUserDto);
68
-
69
- expect(result.code).toBe(CommonServiceResultCode.CONFLICT);
70
- expect(result.message).toContain('手机号已被使用');
71
- });
72
-
73
- test('应该拒绝无效的邮箱格式', async () => {
74
- const createUserDto: CreateUserDto = {
75
- nickName: '无效邮箱用户',
76
- email: 'invalid-email', // 无效的邮箱格式
77
- telnumber: '13900139002',
78
- password: 'Password123!'
79
- };
80
-
81
- const result = await userService.createUser(createUserDto);
82
-
83
- expect(result.code).toBe(CommonServiceResultCode.VALIDATION_ERROR);
84
- expect(result.message).toContain('邮箱格式不正确');
85
- });
86
-
87
- test('应该拒绝过短的密码', async () => {
88
- const createUserDto: CreateUserDto = {
89
- nickName: '短密码用户',
90
- email: 'shortpass@example.com',
91
- telnumber: '13900139003',
92
- password: '12345!' // 少于8个字符,不符合强度要求
93
- };
94
-
95
- const result = await userService.createUser(createUserDto);
96
-
97
- expect(result.code).toBe(CommonServiceResultCode.VALIDATION_ERROR);
98
- expect(result.message).toContain('密码强度不足');
99
- });
100
-
101
- test('应该为密码使用默认的用户类型和状态', async () => {
102
- const createUserDto: CreateUserDto = {
103
- nickName: '默认类型用户',
104
- email: TestUtils.generateRandomEmail(),
105
- telnumber: TestUtils.generateRandomPhone(),
106
- password: 'Password123!'
107
- };
108
-
109
- const result = await userService.createUser(createUserDto);
110
-
111
- expect(result.code).toBe(CommonServiceResultCode.SUCCESS);
112
- expect(result.data?.userType).toBe(YtUserTypeEnum.CUSTOMER);
113
- expect(result.data?.status).toBe(YtUserStatusEnum.NORMAL);
114
- });
115
- });
116
-
117
- describe('updateUser - 更新用户', () => {
118
- test('应该能够成功更新用户信息', async () => {
119
- // 先获取一个测试用户
120
- const existingUser = await userService.findByEmail('test1@example.com');
121
- if (!existingUser) {
122
- throw new Error('测试用户不存在');
123
- }
124
- const userId = existingUser.id;
125
- const updateUserDto: UpdateUserDto = {
126
- nickName: '更新后的昵称',
127
- age: 30
128
- };
129
-
130
- const result = await userService.updateUser(userId, updateUserDto);
131
-
132
- expect(result.code).toBe(CommonServiceResultCode.SUCCESS);
133
- expect(result.data?.nickName).toBe(updateUserDto.nickName);
134
- expect(result.data?.age).toBe(updateUserDto.age);
135
- });
136
-
137
- test('应该拒绝更新不存在的用户', async () => {
138
- const updateUserDto: UpdateUserDto = {
139
- nickName: '不存在的用户'
140
- };
141
-
142
- const result = await userService.updateUser(999999, updateUserDto);
143
-
144
- expect(result.code).toBe(CommonServiceResultCode.NOT_FOUND);
145
- expect(result.message).toContain('用户不存在');
146
- });
147
-
148
- test('应该拒绝使用已存在的邮箱更新用户', async () => {
149
- // 获取第二个测试用户
150
- const existingUser = await userService.findByEmail('test2@example.com');
151
- if (!existingUser) {
152
- throw new Error('测试用户不存在');
153
- }
154
- const userId = existingUser.id;
155
- const updateUserDto: UpdateUserDto = {
156
- email: 'test1@example.com' // 使用第一个用户的邮箱
157
- };
158
-
159
- const result = await userService.updateUser(userId, updateUserDto);
160
-
161
- expect(result.code).toBe(CommonServiceResultCode.ERROR);
162
- expect(result.message).toContain('邮箱已被其他用户使用');
163
- });
164
-
165
- test('应该拒绝使用已存在的手机号更新用户', async () => {
166
- // 获取第二个测试用户
167
- const existingUser = await userService.findByEmail('test2@example.com');
168
- if (!existingUser) {
169
- throw new Error('测试用户不存在');
170
- }
171
- const userId = existingUser.id;
172
- const updateUserDto: UpdateUserDto = {
173
- telnumber: '13800138001' // 使用第一个用户的手机号
174
- };
175
-
176
- const result = await userService.updateUser(userId, updateUserDto);
177
-
178
- expect(result.code).toBe(CommonServiceResultCode.ERROR);
179
- expect(result.message).toContain('手机号已被其他用户使用');
180
- });
181
- });
182
-
183
- describe('getUserById - 获取用户', () => {
184
- test('应该能够成功获取用户', async () => {
185
- // 先获取一个测试用户
186
- const existingUser = await userService.findByEmail('test1@example.com');
187
- if (!existingUser) {
188
- throw new Error('测试用户不存在');
189
- }
190
- const userId = existingUser.id;
191
-
192
- const result = await userService.getUserById(userId);
193
-
194
- expect(result.code).toBe(CommonServiceResultCode.SUCCESS);
195
- expect(result.data).toBeDefined();
196
- expect(result.data?.id).toBe(userId);
197
- });
198
-
199
- test('应该返回NOT_FOUND当用户不存在', async () => {
200
- const result = await userService.getUserById(999999);
201
-
202
- expect(result.code).toBe(CommonServiceResultCode.NOT_FOUND);
203
- expect(result.message).toContain('用户不存在');
204
- });
205
- });
206
-
207
- describe('login - 用户登录', () => {
208
- test('应该能够使用邮箱成功登录', async () => {
209
- const loginDto: LoginUserDto = {
210
- credential: 'test1@example.com',
211
- password: 'Password123!'
212
- };
213
-
214
- const result = await userService.login(loginDto);
215
-
216
- expect(result.code).toBe(CommonServiceResultCode.SUCCESS);
217
- expect(result.data).toBeDefined();
218
- expect(result.message).toContain('登录成功');
219
- });
220
-
221
- test('应该能够使用手机号成功登录', async () => {
222
- const loginDto: LoginUserDto = {
223
- credential: '13800138001',
224
- password: 'Password123!'
225
- };
226
-
227
- const result = await userService.login(loginDto);
228
-
229
- expect(result.code).toBe(CommonServiceResultCode.SUCCESS);
230
- expect(result.data).toBeDefined();
231
- });
232
-
233
- test('应该拒绝不存在的用户登录', async () => {
234
- const loginDto: LoginUserDto = {
235
- credential: 'nonexistent@example.com',
236
- password: 'Password123!'
237
- };
238
-
239
- const result = await userService.login(loginDto);
240
-
241
- expect(result.code).toBe(CommonServiceResultCode.NOT_FOUND);
242
- expect(result.message).toContain('用户不存在');
243
- });
244
-
245
- test('应该拒绝错误密码的登录', async () => {
246
- const loginDto: LoginUserDto = {
247
- credential: 'test1@example.com',
248
- password: 'WrongPassword123!'
249
- };
250
-
251
- const result = await userService.login(loginDto);
252
-
253
- expect(result.code).toBe(CommonServiceResultCode.UNAUTHORIZED);
254
- expect(result.message).toContain('密码错误');
255
- });
256
-
257
- test('应该拒绝被禁用用户的登录', async () => {
258
- // 先获取一个用户并禁用
259
- const existingUser = await userService.findByEmail('test1@example.com');
260
- if (!existingUser) {
261
- throw new Error('测试用户不存在');
262
- }
263
- const userId = existingUser.id;
264
- await userService.toggleUserStatus(userId, false);
265
-
266
- const loginDto: LoginUserDto = {
267
- credential: 'test1@example.com',
268
- password: 'Password123!'
269
- };
270
-
271
- const result = await userService.login(loginDto);
272
-
273
- expect(result.code).toBe(CommonServiceResultCode.FORBIDDEN);
274
- expect(result.message).toContain('用户已被禁用');
275
-
276
- // 恢复用户状态
277
- await userService.toggleUserStatus(userId, true);
278
- });
279
- });
280
-
281
- describe('changePassword - 修改密码', () => {
282
- test('应该能够成功修改密码', async () => {
283
- // 先获取一个用户
284
- const existingUser = await userService.findByEmail('test2@example.com');
285
- if (!existingUser) {
286
- throw new Error('测试用户不存在');
287
- }
288
- const userId = existingUser.id;
289
- const changePasswordDto: ChangePasswordDto = {
290
- currentPassword: 'Password123!',
291
- newPassword: 'NewPassword123!',
292
- confirmPassword: 'NewPassword123!'
293
- };
294
-
295
- const result = await userService.changePassword(userId, changePasswordDto);
296
-
297
- expect(result.code).toBe(CommonServiceResultCode.SUCCESS);
298
- expect(result.data).toBe(true);
299
- expect(result.message).toContain('密码修改成功');
300
-
301
- // 验证新密码可以登录
302
- const loginDto: LoginUserDto = {
303
- credential: 'test2@example.com',
304
- password: 'NewPassword123!'
305
- };
306
- const loginResult = await userService.login(loginDto);
307
- expect(loginResult.code).toBe(CommonServiceResultCode.SUCCESS);
308
- });
309
-
310
- test('应该拒绝错误的当前密码', async () => {
311
- // 先获取一个用户
312
- const existingUser = await userService.findByEmail('test2@example.com');
313
- if (!existingUser) {
314
- throw new Error('测试用户不存在');
315
- }
316
- const userId = existingUser.id;
317
- const changePasswordDto: ChangePasswordDto = {
318
- currentPassword: 'WrongPassword123!',
319
- newPassword: 'newpassword123',
320
- confirmPassword: 'newpassword123'
321
- };
322
-
323
- const result = await userService.changePassword(userId, changePasswordDto);
324
-
325
- expect(result.code).toBe(CommonServiceResultCode.ERROR);
326
- expect(result.message).toContain('当前密码错误');
327
- });
328
-
329
- test('应该拒绝新密码和确认密码不一致', async () => {
330
- const userId = 1;
331
- const changePasswordDto: ChangePasswordDto = {
332
- currentPassword: 'Password123!',
333
- newPassword: 'NewPassword123!',
334
- confirmPassword: 'DifferentPassword123!'
335
- };
336
-
337
- const result = await userService.changePassword(userId, changePasswordDto);
338
-
339
- expect(result.code).toBe(CommonServiceResultCode.VALIDATION_ERROR);
340
- expect(result.message).toContain('新密码和确认密码不一致');
341
- });
342
-
343
- test('应该拒绝修改不存在的用户的密码', async () => {
344
- const changePasswordDto: ChangePasswordDto = {
345
- currentPassword: 'Password123!',
346
- newPassword: 'NewPassword123!',
347
- confirmPassword: 'NewPassword123!'
348
- };
349
-
350
- const result = await userService.changePassword(999999, changePasswordDto);
351
-
352
- expect(result.code).toBe(CommonServiceResultCode.ERROR);
353
- expect(result.message).toContain('用户不存在');
354
- });
355
- });
356
-
357
- describe('getUsers - 查询用户列表', () => {
358
- test('应该能够获取用户列表', async () => {
359
- const queryDto: QueryUserDto = {
360
- page: 1,
361
- pageSize: 10
362
- };
363
-
364
- const result = await userService.getUsers(queryDto);
365
-
366
- expect(result.code).toBe(CommonServiceResultCode.SUCCESS);
367
- expect(result.data).toBeDefined();
368
- if (result.data) {
369
- expect(Array.isArray(result.data[0])).toBe(true);
370
- expect(typeof result.data[1]).toBe('number');
371
- }
372
- });
373
-
374
- test('应该能够按用户类型筛选', async () => {
375
- const queryDto: QueryUserDto = {
376
- userType: YtUserTypeEnum.CUSTOMER,
377
- page: 1,
378
- pageSize: 10
379
- };
380
-
381
- const result = await userService.getUsers(queryDto);
382
-
383
- expect(result.code).toBe(CommonServiceResultCode.SUCCESS);
384
- if (result.data && result.data[0].length > 0) {
385
- expect(result.data[0][0].userType).toBe(YtUserTypeEnum.CUSTOMER);
386
- }
387
- });
388
-
389
- test('应该能够按状态筛选', async () => {
390
- const queryDto: QueryUserDto = {
391
- status: YtUserStatusEnum.NORMAL,
392
- page: 1,
393
- pageSize: 10
394
- };
395
-
396
- const result = await userService.getUsers(queryDto);
397
-
398
- expect(result.code).toBe(CommonServiceResultCode.SUCCESS);
399
- if (result.data && result.data[0].length > 0) {
400
- expect(result.data[0][0].status).toBe(YtUserStatusEnum.NORMAL);
401
- }
402
- });
403
-
404
- test('应该能够分页查询', async () => {
405
- const queryDto: QueryUserDto = {
406
- page: 1,
407
- pageSize: 2
408
- };
409
-
410
- const result = await userService.getUsers(queryDto);
411
-
412
- expect(result.code).toBe(CommonServiceResultCode.SUCCESS);
413
- if (result.data) {
414
- expect(result.data[0].length).toBeLessThanOrEqual(2);
415
- }
416
- });
417
- });
418
-
419
- describe('deleteUser - 删除用户', () => {
420
- test('应该能够成功删除用户', async () => {
421
- // 先创建一个用户用于删除
422
- const createUserDto: CreateUserDto = {
423
- nickName: '待删除用户',
424
- email: TestUtils.generateRandomEmail(),
425
- telnumber: TestUtils.generateRandomPhone(),
426
- password: 'Password123!'
427
- };
428
-
429
- const createResult = await userService.createUser(createUserDto);
430
- const userId = createResult.data?.id;
431
-
432
- if (!userId) {
433
- throw new Error('创建用户失败');
434
- }
435
-
436
- // 删除用户
437
- const deleteResult = await userService.deleteUser(userId);
438
-
439
- expect(deleteResult.code).toBe(CommonServiceResultCode.SUCCESS);
440
- expect(deleteResult.data).toBe(true);
441
-
442
- // 验证用户已被删除
443
- const getUserResult = await userService.getUserById(userId);
444
- expect(getUserResult.code).toBe(CommonServiceResultCode.NOT_FOUND);
445
- });
446
-
447
- test('应该拒绝删除不存在的用户', async () => {
448
- const result = await userService.deleteUser(999999);
449
-
450
- expect(result.code).toBe(CommonServiceResultCode.NOT_FOUND);
451
- expect(result.message).toContain('用户不存在');
452
- });
453
- });
454
-
455
- describe('toggleUserStatus - 切换用户状态', () => {
456
- test('应该能够禁用用户', async () => {
457
- // 先获取一个用户
458
- const existingUser = await userService.findByEmail('test2@example.com');
459
- if (!existingUser) {
460
- throw new Error('测试用户不存在');
461
- }
462
- const userId = existingUser.id;
463
-
464
- const result = await userService.toggleUserStatus(userId, false);
465
-
466
- expect(result.code).toBe(CommonServiceResultCode.SUCCESS);
467
- expect(result.data).toBe(true);
468
- expect(result.message).toContain('禁用');
469
-
470
- // 验证用户已被禁用
471
- const userResult = await userService.getUserById(userId);
472
- expect(userResult.data?.status).toBe(YtUserStatusEnum.FORBIDDEN);
473
-
474
- // 恢复用户状态
475
- await userService.toggleUserStatus(userId, true);
476
- });
477
-
478
- test('应该能够激活用户', async () => {
479
- // 先获取一个用户
480
- const existingUser = await userService.findByEmail('test2@example.com');
481
- if (!existingUser) {
482
- throw new Error('测试用户不存在');
483
- }
484
- const userId = existingUser.id;
485
-
486
- // 先禁用用户
487
- await userService.toggleUserStatus(userId, false);
488
-
489
- // 再激活用户
490
- const result = await userService.toggleUserStatus(userId, true);
491
-
492
- expect(result.code).toBe(CommonServiceResultCode.SUCCESS);
493
- expect(result.data).toBe(true);
494
- expect(result.message).toContain('激活');
495
-
496
- // 验证用户已被激活
497
- const userResult = await userService.getUserById(userId);
498
- expect(userResult.data?.status).toBe(YtUserStatusEnum.NORMAL);
499
- });
500
-
501
- test('应该拒绝切换不存在用户的状态', async () => {
502
- const result = await userService.toggleUserStatus(999999, false);
503
-
504
- expect(result.code).toBe(CommonServiceResultCode.NOT_FOUND);
505
- expect(result.message).toContain('用户不存在');
506
- });
507
- });
508
-
509
- describe('findByEmail - 根据邮箱查找用户', () => {
510
- test('应该能够找到存在的用户', async () => {
511
- const user = await userService.findByEmail('test1@example.com');
512
-
513
- expect(user).toBeDefined();
514
- expect(user?.email).toBe('test1@example.com');
515
- });
516
-
517
- test('应该返回null当用户不存在', async () => {
518
- const user = await userService.findByEmail('nonexistent@example.com');
519
-
520
- expect(user).toBeNull();
521
- });
522
- });
523
-
524
- describe('addUser - 添加用户(兼容接口)', () => {
525
- test('应该能够添加用户', async () => {
526
- const newUser: any = {
527
- nickName: '兼容接口用户',
528
- email: TestUtils.generateRandomEmail(),
529
- telnumber: TestUtils.generateRandomPhone(),
530
- password: 'Password123!',
531
- userType: YtUserTypeEnum.CUSTOMER,
532
- status: YtUserStatusEnum.NORMAL,
533
- gender: 0,
534
- age: 0,
535
- constellation: '',
536
- avatar: ''
537
- };
538
-
539
- const result = await userService.addUser(newUser);
540
-
541
- expect(result).toBeDefined();
542
- expect(result.email).toBe(newUser.email);
543
- });
544
- });
545
-
546
- describe('getUserByIdLegacy - 获取用户(兼容接口)', () => {
547
- test('应该能够获取用户', async () => {
548
- // 先获取一个用户
549
- const existingUser = await userService.findByEmail('test1@example.com');
550
- if (!existingUser) {
551
- throw new Error('测试用户不存在');
552
- }
553
- const userId = existingUser.id;
554
- const user = await userService.getUserByIdLegacy(null as any, userId);
555
-
556
- expect(user).toBeDefined();
557
- expect(user?.id).toBe(userId);
558
- });
559
-
560
- test('应该返回null当用户不存在', async () => {
561
- const user = await userService.getUserByIdLegacy(null as any, 999999);
562
-
563
- expect(user).toBeNull();
564
- });
565
- });
566
- });