Просмотр исходного кода

✅ feat(system-config): 完成故事010.003系统配置与认证支付模块集成测试

- 修复认证模块系统配置集成测试数据库连接问题
- 修复支付模块系统配置集成测试依赖和配置问题
- 优化PaymentMtService初始化逻辑,延迟微信支付SDK初始化
- 添加支付模块对@d8d/core-module-mt的依赖
- 所有系统配置集成测试通过验证

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

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
yourname 2 месяцев назад
Родитель
Сommit
cb19fbbec5

+ 14 - 7
packages/core-module-mt/auth-module-mt/tests/integration/system-config-integration.test.ts

@@ -1,8 +1,16 @@
 import { describe, it, expect, beforeEach, afterEach } from 'vitest';
-import { AppDataSource } from '@d8d/shared-utils';
+import {
+  IntegrationTestDatabase,
+  setupIntegrationDatabaseHooksWithEntities
+} from '@d8d/shared-test-util';
 import { MiniAuthService } from '../../src/services/index.mt';
 import { SystemConfigServiceMt } from '@d8d/core-module-mt/system-config-module-mt';
 import { SystemConfigMt } from '@d8d/core-module-mt/system-config-module-mt';
+import { UserEntityMt, RoleMt } from '@d8d/core-module-mt/user-module-mt';
+import { FileMt } from '@d8d/core-module-mt/file-module-mt';
+
+// 设置集成测试钩子
+setupIntegrationDatabaseHooksWithEntities([SystemConfigMt, UserEntityMt, RoleMt, FileMt])
 
 /**
  * 认证模块系统配置集成测试
@@ -14,13 +22,12 @@ describe('认证模块系统配置集成测试', () => {
   const testTenantId = 1;
 
   beforeEach(async () => {
-    // 确保数据库连接
-    if (!AppDataSource.isInitialized) {
-      await AppDataSource.initialize();
-    }
+    // 获取数据源
+    const dataSource = await IntegrationTestDatabase.getDataSource();
+    if (!dataSource) throw new Error('Database not initialized');
 
-    miniAuthService = new MiniAuthService(AppDataSource);
-    systemConfigService = new SystemConfigServiceMt(AppDataSource);
+    miniAuthService = new MiniAuthService(dataSource);
+    systemConfigService = new SystemConfigServiceMt(dataSource);
 
     // 清理测试配置
     await systemConfigService.deleteConfig('wx.mini.app.id', testTenantId);

+ 1 - 0
packages/mini-payment-mt/package.json

@@ -51,6 +51,7 @@
     "@d8d/user-module-mt": "workspace:*",
     "@d8d/auth-module-mt": "workspace:*",
     "@d8d/file-module-mt": "workspace:*",
+    "@d8d/core-module-mt": "workspace:*",
     "@hono/zod-openapi": "^1.0.2",
     "typeorm": "^0.3.20",
     "wechatpay-node-v3": "2.1.8",

+ 12 - 15
packages/mini-payment-mt/src/services/payment.mt.service.ts

@@ -24,25 +24,18 @@ export class PaymentMtService extends GenericCrudService<PaymentMtEntity> {
 
     this.systemConfigService = new SystemConfigServiceMt(dataSource);
 
-    // 初始化微信支付SDK,但配置将在使用时动态获取
-    this.wxPay = new WxPay({
-      appid: '',
-      mchid: '',
-      publicKey: Buffer.from(''),
-      privateKey: Buffer.from(''),
-      key: '',
-      serial_no: ''
-    });
+    // 微信支付SDK将在需要时动态初始化
+    this.wxPay = null as any;
   }
 
   /**
-   * 重新初始化微信支付SDK实例
+   * 初始化微信支付SDK实例
    */
   private async initializeWxPay(tenantId: number): Promise<void> {
     const config = await this.getPaymentConfig(tenantId);
 
-    // 重新初始化微信支付SDK
-    (this.wxPay as any) = new WxPay({
+    // 初始化微信支付SDK
+    this.wxPay = new WxPay({
       appid: config.appId,
       mchid: config.merchantId,
       publicKey: Buffer.from(config.publicKey),
@@ -313,11 +306,15 @@ export class PaymentMtService extends GenericCrudService<PaymentMtEntity> {
   /**
    * 生成回调签名(用于测试)
    */
-  generateCallbackSignature(
+  async generateCallbackSignature(
     timestamp: string,
     nonce: string,
-    callbackData: any
-  ): string {
+    callbackData: any,
+    tenantId: number
+  ): Promise<string> {
+    // 初始化微信支付SDK
+    await this.initializeWxPay(tenantId);
+
     return this.wxPay.getSignature(
       'POST',
       nonce,

+ 45 - 18
packages/mini-payment-mt/tests/integration/system-config-integration.test.ts

@@ -1,7 +1,16 @@
 import { describe, it, expect, beforeEach, afterEach } from 'vitest';
-import { AppDataSource } from '@d8d/shared-utils';
+import {
+  IntegrationTestDatabase,
+  setupIntegrationDatabaseHooksWithEntities
+} from '@d8d/shared-test-util';
 import { PaymentMtService } from '../../src/services/payment.mt.service';
 import { SystemConfigServiceMt } from '@d8d/core-module-mt/system-config-module-mt';
+import { SystemConfigMt } from '@d8d/core-module-mt/system-config-module-mt';
+import { UserEntityMt, RoleMt } from '@d8d/core-module-mt/user-module-mt';
+import { FileMt } from '@d8d/core-module-mt/file-module-mt';
+
+// 设置集成测试钩子
+setupIntegrationDatabaseHooksWithEntities([SystemConfigMt, UserEntityMt, RoleMt, FileMt])
 
 /**
  * 支付模块系统配置集成测试
@@ -13,13 +22,12 @@ describe('支付模块系统配置集成测试', () => {
   const testTenantId = 1;
 
   beforeEach(async () => {
-    // 确保数据库连接
-    if (!AppDataSource.isInitialized) {
-      await AppDataSource.initialize();
-    }
+    // 获取数据源
+    const dataSource = await IntegrationTestDatabase.getDataSource();
+    if (!dataSource) throw new Error('Database not initialized');
 
-    paymentMtService = new PaymentMtService(AppDataSource);
-    systemConfigService = new SystemConfigServiceMt(AppDataSource);
+    paymentMtService = new PaymentMtService(dataSource);
+    systemConfigService = new SystemConfigServiceMt(dataSource);
 
     // 清理测试配置
     await cleanupTestConfigs(testTenantId);
@@ -31,6 +39,8 @@ describe('支付模块系统配置集成测试', () => {
   });
 
   async function cleanupTestConfigs(tenantId: number) {
+    if (!systemConfigService) return;
+
     const configKeys = [
       'wx.payment.merchant.id',
       'wx.mini.app.id',
@@ -42,10 +52,24 @@ describe('支付模块系统配置集成测试', () => {
     ];
 
     for (const key of configKeys) {
-      await systemConfigService.deleteConfig(key, tenantId);
+      try {
+        await systemConfigService.deleteConfig(key, tenantId);
+      } catch (error) {
+        // 忽略删除失败的情况
+      }
     }
   }
 
+  async function setupCompletePaymentConfig(tenantId: number, tenantPrefix: string) {
+    await systemConfigService.setConfig('wx.payment.merchant.id', `${tenantPrefix}-merchant-id`, tenantId, `${tenantPrefix}商户ID`);
+    await systemConfigService.setConfig('wx.mini.app.id', `${tenantPrefix}-app-id`, tenantId, `${tenantPrefix}小程序AppID`);
+    await systemConfigService.setConfig('wx.payment.v3.key', `${tenantPrefix}-v3-key`, tenantId, `${tenantPrefix}V3密钥`);
+    await systemConfigService.setConfig('wx.payment.notify.url', `${tenantPrefix}-notify-url`, tenantId, `${tenantPrefix}回调URL`);
+    await systemConfigService.setConfig('wx.payment.cert.serial.no', `${tenantPrefix}-cert-serial`, tenantId, `${tenantPrefix}证书序列号`);
+    await systemConfigService.setConfig('wx.payment.public.key', `${tenantPrefix}-public-key`, tenantId, `${tenantPrefix}公钥`);
+    await systemConfigService.setConfig('wx.payment.private.key', `${tenantPrefix}-private-key`, tenantId, `${tenantPrefix}私钥`);
+  }
+
   describe('系统配置读取功能', () => {
     it('应该从系统配置获取支付配置', async () => {
       // 设置测试支付配置
@@ -90,13 +114,11 @@ describe('支付模块系统配置集成测试', () => {
     const tenant2Id = 2;
 
     it('应该支持不同租户的独立支付配置', async () => {
-      // 为租户1设置配置
-      await systemConfigService.setConfig('wx.payment.merchant.id', 'tenant1-merchant-id', tenant1Id, '租户1商户ID');
-      await systemConfigService.setConfig('wx.mini.app.id', 'tenant1-app-id', tenant1Id, '租户1小程序AppID');
+      // 为租户1设置完整的支付配置
+      await setupCompletePaymentConfig(tenant1Id, 'tenant1');
 
-      // 为租户2设置不同的配置
-      await systemConfigService.setConfig('wx.payment.merchant.id', 'tenant2-merchant-id', tenant2Id, '租户2商户ID');
-      await systemConfigService.setConfig('wx.mini.app.id', 'tenant2-app-id', tenant2Id, '租户2小程序AppID');
+      // 为租户2设置不同的完整支付配置
+      await setupCompletePaymentConfig(tenant2Id, 'tenant2');
 
       // 验证租户1的配置
       const tenant1Config = await paymentMtService['getPaymentConfig'](tenant1Id);
@@ -132,14 +154,16 @@ describe('支付模块系统配置集成测试', () => {
     });
 
     it('应该优先使用系统配置', async () => {
-      // 设置系统配置
-      await systemConfigService.setConfig('wx.payment.merchant.id', 'system-merchant-id', testTenantId, '系统配置商户ID');
+      // 设置完整的系统配置
+      await setupCompletePaymentConfig(testTenantId, 'system');
 
       // 获取配置,应该优先使用系统配置
       const config = await paymentMtService['getPaymentConfig'](testTenantId);
 
-      // 注意:这里只验证了商户ID,其他配置可能仍然依赖环境变量
+      // 验证所有配置都来自系统配置
       expect(config.merchantId).toBe('system-merchant-id');
+      expect(config.appId).toBe('system-app-id');
+      expect(config.v3Key).toBe('system-v3-key');
     });
   });
 
@@ -179,7 +203,10 @@ describe('支付模块系统配置集成测试', () => {
     });
 
     it('应该处理证书字符串格式', async () => {
-      // 设置包含转义字符的证书配置
+      // 设置完整的支付配置,包含包含转义字符的证书
+      await setupCompletePaymentConfig(testTenantId, 'cert-test');
+
+      // 覆盖证书配置为包含转义字符的格式
       await systemConfigService.setConfig('wx.payment.public.key', 'test\\npublic\\nkey', testTenantId, '测试公钥');
       await systemConfigService.setConfig('wx.payment.private.key', 'test\\nprivate\\nkey', testTenantId, '测试私钥');
 

+ 3 - 0
pnpm-lock.yaml

@@ -2866,6 +2866,9 @@ importers:
       '@d8d/auth-module-mt':
         specifier: workspace:*
         version: link:../auth-module-mt
+      '@d8d/core-module-mt':
+        specifier: workspace:*
+        version: link:../core-module-mt
       '@d8d/file-module-mt':
         specifier: workspace:*
         version: link:../file-module-mt