Explorar o código

✅ test(auth): 增强认证测试的稳定性

- 导入UserEntity以操作用户数据
- 在创建测试用户前添加重复用户清理步骤,防止测试数据干扰
- 在禁用用户测试用例中添加重复用户清理
- 在刷新令牌测试用例中添加重复用户清理
yourname hai 2 meses
pai
achega
cca32a9622
Modificáronse 1 ficheiros con 13 adicións e 1 borrados
  1. 13 1
      src/server/api/auth/__tests__/auth.integration.test.ts

+ 13 - 1
src/server/api/auth/__tests__/auth.integration.test.ts

@@ -4,6 +4,7 @@ import {
   IntegrationTestDatabase,
   TestDataFactory
 } from '../../../__test_utils__/integration-test-db';
+import { UserEntity } from '../../../modules/users/user.entity';
 import { authRoutes } from '../../../api';
 import { AuthService } from '../../../modules/auth/auth.service';
 import { UserService } from '../../../modules/users/user.service';
@@ -40,7 +41,10 @@ describe('认证API集成测试 (使用hono/testing)', () => {
     userService = new UserService(dataSource);
     authService = new AuthService(userService);
 
-    // 创建测试用户
+    // 创建测试用户前先删除可能存在的重复用户
+    const userRepository = dataSource.getRepository(UserEntity);
+    await userRepository.delete({ username: 'testuser' });
+
     testUser = await TestDataFactory.createTestUser(dataSource, {
       username: 'testuser',
       password: 'TestPassword123!',
@@ -113,6 +117,10 @@ describe('认证API集成测试 (使用hono/testing)', () => {
       const dataSource = IntegrationTestDatabase.getDataSource();
       if (!dataSource) throw new Error('Database not initialized');
 
+      // 先删除可能存在的重复用户
+      const userRepository = dataSource.getRepository(UserEntity);
+      await userRepository.delete({ username: 'disabled_user' });
+
       const disabledUser = await TestDataFactory.createTestUser(dataSource, {
         username: 'disabled_user',
         password: 'TestPassword123!',
@@ -292,6 +300,10 @@ describe('认证API集成测试 (使用hono/testing)', () => {
       const dataSource = IntegrationTestDatabase.getDataSource();
       if (!dataSource) throw new Error('Database not initialized');
 
+      // 先删除可能存在的重复用户
+      const userRepository = dataSource.getRepository(UserEntity);
+      await userRepository.delete({ username: 'regular_user' });
+
       const regularUser = await TestDataFactory.createTestUser(dataSource, {
         username: 'regular_user',
         password: 'TestPassword123!',