소스 검색

fix: 修复集成测试API响应格式断言

- 修改测试以匹配API返回格式 { code, message, data: { list, total } }
- 添加必要的测试数据(广告类型)以满足外键约束
- 修改实体字段名以匹配实际模型

🤖 Generated with [Claude Code](https://claude.com/claude-code)
via [Happy](https://happy.engineering)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
yourname 2 주 전
부모
커밋
3fdf8c13d2
1개의 변경된 파일87개의 추가작업 그리고 22개의 파일을 삭제
  1. 87 22
      packages/server/tests/integration/unified-advertisement-auth.integration.test.ts

+ 87 - 22
packages/server/tests/integration/unified-advertisement-auth.integration.test.ts

@@ -91,7 +91,10 @@ describe('统一广告管理员权限集成测试', () => {
       expect([200, 404]).toContain(response.status);
       if (response.status === 200) {
         const data = await response.json();
-        expect(Array.isArray(data)).toBeTruthy();
+        // API返回格式: { code, message, data: { list, total } }
+        expect(data).toHaveProperty('data');
+        expect(data.data).toHaveProperty('list');
+        expect(Array.isArray(data.data.list)).toBeTruthy();
       }
     });
 
@@ -142,7 +145,10 @@ describe('统一广告管理员权限集成测试', () => {
       expect([200, 404]).toContain(response.status);
       if (response.status === 200) {
         const data = await response.json();
-        expect(Array.isArray(data)).toBeTruthy();
+        // API返回格式: { code, message, data: { list, total } }
+        expect(data).toHaveProperty('data');
+        expect(data.data).toHaveProperty('list');
+        expect(Array.isArray(data.data.list)).toBeTruthy();
       }
     });
 
@@ -179,7 +185,9 @@ describe('统一广告管理员权限集成测试', () => {
       expect([200, 404]).toContain(response.status);
       if (response.status === 200) {
         const data = await response.json();
-        expect(Array.isArray(data)).toBeTruthy();
+        expect(data).toHaveProperty("data");
+        expect(data.data).toHaveProperty("list");
+        expect(Array.isArray(data.data.list)).toBeTruthy();
       }
     });
 
@@ -202,7 +210,10 @@ describe('统一广告管理员权限集成测试', () => {
       expect([200, 404]).toContain(response.status);
       if (response.status === 200) {
         const data = await response.json();
-        expect(Array.isArray(data)).toBeTruthy();
+        // API返回格式: { code, message, data: { list, total } }
+        expect(data).toHaveProperty('data');
+        expect(data.data).toHaveProperty('list');
+        expect(Array.isArray(data.data.list)).toBeTruthy();
       }
     });
   });
@@ -212,13 +223,22 @@ describe('统一广告管理员权限集成测试', () => {
       const dataSource = await IntegrationTestDatabase.getDataSource();
       if (!dataSource) throw new Error('Database not initialized');
 
+      // 先创建广告类型
+      const adTypeRepository = dataSource.getRepository(UnifiedAdvertisementType);
+      const adType = await adTypeRepository.save({
+        name: 'Test Type',
+        code: 'test_type_verify',
+        sort: 0,
+        status: 1
+      });
+
       // 创建测试广告数据
       const adRepository = dataSource.getRepository(UnifiedAdvertisement);
       await adRepository.save({
         title: 'Test Ad',
-        imageUrl: 'http://example.com/ad.jpg',
-        linkUrl: 'http://example.com',
-        position: 'home',
+        typeId: adType.id,
+        code: 'test_ad_verify',
+        url: 'http://example.com',
         status: 1
       });
 
@@ -232,10 +252,13 @@ describe('统一广告管理员权限集成测试', () => {
 
       if (response.status === 200) {
         const data = await response.json();
-        expect(Array.isArray(data)).toBeTruthy();
+        // API返回格式: { code, message, data: { list, total } }
+        expect(data).toHaveProperty('data');
+        expect(data.data).toHaveProperty('list');
+        expect(Array.isArray(data.data.list)).toBeTruthy();
         // 验证返回的是统一广告数据,不是按租户隔离的
-        if (data.length > 0) {
-          const ad = data[0];
+        if (data.data.list.length > 0) {
+          const ad = data.data.list[0];
           expect(ad).not.toHaveProperty('tenantId'); // 统一广告不应该有tenantId字段
         }
       }
@@ -260,11 +283,23 @@ describe('统一广告管理员权限集成测试', () => {
 
   describe('管理员操作权限验证', () => {
     it('超级管理员应该能创建统一广告', async () => {
+      const dataSource = await IntegrationTestDatabase.getDataSource();
+      if (!dataSource) throw new Error('Database not initialized');
+
+      // 先创建广告类型
+      const adTypeRepository = dataSource.getRepository(UnifiedAdvertisementType);
+      const adType = await adTypeRepository.save({
+        name: 'Test Type',
+        code: 'test_type',
+        sort: 0,
+        status: 1
+      });
+
       const newAd = {
         title: 'New Unified Ad',
-        imageUrl: 'http://example.com/new-ad.jpg',
-        linkUrl: 'http://example.com/new',
-        position: 'home',
+        typeId: adType.id,  // 使用创建的广告类型ID
+        code: 'test_ad',  // 必填
+        url: 'http://example.com/new',
         status: 1
       };
 
@@ -283,11 +318,23 @@ describe('统一广告管理员权限集成测试', () => {
     });
 
     it('普通管理员不应该能创建统一广告', async () => {
+      const dataSource = await IntegrationTestDatabase.getDataSource();
+      if (!dataSource) throw new Error('Database not initialized');
+
+      // 先创建广告类型
+      const adTypeRepository = dataSource.getRepository(UnifiedAdvertisementType);
+      const adType = await adTypeRepository.save({
+        name: 'Test Type 2',
+        code: 'test_type_2',
+        sort: 0,
+        status: 1
+      });
+
       const newAd = {
         title: 'New Unified Ad',
-        imageUrl: 'http://example.com/new-ad.jpg',
-        linkUrl: 'http://example.com/new',
-        position: 'home',
+        typeId: adType.id,
+        code: 'test_ad_2',
+        url: 'http://example.com/new',
         status: 1
       };
 
@@ -309,13 +356,22 @@ describe('统一广告管理员权限集成测试', () => {
       const dataSource = await IntegrationTestDatabase.getDataSource();
       if (!dataSource) throw new Error('Database not initialized');
 
+      // 先创建广告类型
+      const adTypeRepository = dataSource.getRepository(UnifiedAdvertisementType);
+      const adType = await adTypeRepository.save({
+        name: 'Test Type Update',
+        code: 'test_type_update',
+        sort: 0,
+        status: 1
+      });
+
       // 创建测试广告
       const adRepository = dataSource.getRepository(UnifiedAdvertisement);
       const testAd = await adRepository.save({
         title: 'Test Ad',
-        imageUrl: 'http://example.com/ad.jpg',
-        linkUrl: 'http://example.com',
-        position: 'home',
+        typeId: adType.id,
+        code: 'test_ad_update',
+        url: 'http://example.com',
         status: 1
       });
 
@@ -342,13 +398,22 @@ describe('统一广告管理员权限集成测试', () => {
       const dataSource = await IntegrationTestDatabase.getDataSource();
       if (!dataSource) throw new Error('Database not initialized');
 
+      // 先创建广告类型
+      const adTypeRepository = dataSource.getRepository(UnifiedAdvertisementType);
+      const adType = await adTypeRepository.save({
+        name: 'Test Type Delete',
+        code: 'test_type_delete',
+        sort: 0,
+        status: 1
+      });
+
       // 创建测试广告
       const adRepository = dataSource.getRepository(UnifiedAdvertisement);
       const testAd = await adRepository.save({
         title: 'Test Ad',
-        imageUrl: 'http://example.com/ad.jpg',
-        linkUrl: 'http://example.com',
-        position: 'home',
+        typeId: adType.id,
+        code: 'test_ad_delete',
+        url: 'http://example.com',
         status: 1
       });