|
|
@@ -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, '测试私钥');
|
|
|
|