|
|
@@ -1,8 +1,7 @@
|
|
|
-import { describe, it, expect, beforeEach } from 'vitest';
|
|
|
+import { describe, it, expect, beforeEach, beforeAll, afterAll } from 'vitest';
|
|
|
import { testClient } from 'hono/testing';
|
|
|
import {
|
|
|
IntegrationTestDatabase,
|
|
|
- setupIntegrationDatabaseHooks,
|
|
|
TestDataFactory
|
|
|
} from '../utils/integration-test-db';
|
|
|
import { UserEntityMt, UserServiceMt } from '@d8d/user-module-mt';
|
|
|
@@ -11,9 +10,6 @@ import { adminUnifiedAdvertisementApiRoutes, adminUnifiedAdvertisementTypeApiRou
|
|
|
import { AuthService } from '@d8d/auth-module-mt';
|
|
|
import { UnifiedAdvertisement, UnifiedAdvertisementType } from '@d8d/unified-advertisements-module';
|
|
|
|
|
|
-// 设置集成测试钩子
|
|
|
-setupIntegrationDatabaseHooks()
|
|
|
-
|
|
|
describe('统一广告管理员权限集成测试', () => {
|
|
|
let adminClient: ReturnType<typeof testClient<typeof adminUnifiedAdvertisementApiRoutes>>['api']['v1']['admin']['unified-advertisements'];
|
|
|
let adminTypeClient: ReturnType<typeof testClient<typeof adminUnifiedAdvertisementTypeApiRoutes>>['api']['v1']['admin']['unified-advertisement-types'];
|
|
|
@@ -25,6 +21,15 @@ describe('统一广告管理员权限集成测试', () => {
|
|
|
let regularUserToken: string;
|
|
|
let tenantUserToken: string;
|
|
|
|
|
|
+ // 使用 beforeAll 和 afterAll 而不是 beforeEach/afterEach,避免每次测试都销毁和重新创建数据源
|
|
|
+ beforeAll(async () => {
|
|
|
+ await IntegrationTestDatabase.getDataSource();
|
|
|
+ });
|
|
|
+
|
|
|
+ afterAll(async () => {
|
|
|
+ await IntegrationTestDatabase.cleanup();
|
|
|
+ });
|
|
|
+
|
|
|
beforeEach(async () => {
|
|
|
// 创建测试客户端 - 使用server包注册后的路由
|
|
|
adminClient = testClient(adminUnifiedAdvertisementApiRoutes).api.v1.admin['unified-advertisements'];
|
|
|
@@ -36,17 +41,36 @@ describe('统一广告管理员权限集成测试', () => {
|
|
|
const dataSource = await IntegrationTestDatabase.getDataSource();
|
|
|
if (!dataSource) throw new Error('Database not initialized');
|
|
|
|
|
|
- // 初始化服务
|
|
|
- userService = new UserServiceMt(dataSource);
|
|
|
- authService = new AuthService(userService);
|
|
|
-
|
|
|
- // 清理测试用户
|
|
|
+ // 清理测试数据(用户、广告、广告类型)
|
|
|
const userRepository = dataSource.getRepository(UserEntityMt);
|
|
|
await userRepository.delete({ username: 'superadmin' });
|
|
|
await userRepository.delete({ username: 'regularuser' });
|
|
|
await userRepository.delete({ username: 'tenantuser' });
|
|
|
|
|
|
+ // 清理广告和广告类型测试数据(使用repository delete方法)
|
|
|
+ const adRepository = dataSource.getRepository(UnifiedAdvertisement);
|
|
|
+ const adTypeRepository = dataSource.getRepository(UnifiedAdvertisementType);
|
|
|
+
|
|
|
+ // 先删除广告(因为它们引用广告类型)
|
|
|
+ const ads = await adRepository.find();
|
|
|
+ for (const ad of ads) {
|
|
|
+ await adRepository.remove(ad);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 再删除广告类型
|
|
|
+ const adTypes = await adTypeRepository.find();
|
|
|
+ for (const adType of adTypes) {
|
|
|
+ await adTypeRepository.remove(adType);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 初始化服务
|
|
|
+ userService = new UserServiceMt(dataSource);
|
|
|
+ authService = new AuthService(userService);
|
|
|
+
|
|
|
// 创建超级管理员 (ID=1, tenantId=1)
|
|
|
+ // 先删除可能存在的超级管理员
|
|
|
+ await userRepository.delete({ username: 'superadmin' });
|
|
|
+
|
|
|
const superAdmin = await TestDataFactory.createTestUser(dataSource, {
|
|
|
username: 'superadmin',
|
|
|
password: 'TestPassword123!',
|
|
|
@@ -55,8 +79,18 @@ describe('统一广告管理员权限集成测试', () => {
|
|
|
});
|
|
|
// 手动设置ID为1以确保是超级管理员
|
|
|
superAdmin.id = 1;
|
|
|
- await userRepository.save(superAdmin);
|
|
|
- superAdminToken = authService.generateToken(superAdmin);
|
|
|
+ // 使用query builder直接更新ID,避免save时的ID冲突
|
|
|
+ await dataSource.createQueryBuilder()
|
|
|
+ .update(UserEntityMt)
|
|
|
+ .set({ id: 1 })
|
|
|
+ .where('username = :username', { username: 'superadmin' })
|
|
|
+ .execute();
|
|
|
+ // 重新查询用户以确保ID正确
|
|
|
+ const superAdminWithId = await userRepository.findOne({ where: { username: 'superadmin' } });
|
|
|
+ if (!superAdminWithId || superAdminWithId.id !== 1) {
|
|
|
+ throw new Error('超级管理员ID设置失败');
|
|
|
+ }
|
|
|
+ superAdminToken = authService.generateToken(superAdminWithId);
|
|
|
|
|
|
// 创建普通管理员用户 (ID>1, tenantId=1)
|
|
|
const regularUser = await TestDataFactory.createTestUser(dataSource, {
|