system-config-integration.test.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import { describe, it, expect, beforeEach, afterEach } from 'vitest';
  2. import { AppDataSource } from '@d8d/shared-utils';
  3. import { PaymentMtService } from '../../src/services/payment.mt.service';
  4. import { SystemConfigServiceMt } from '@d8d/core-module-mt/system-config-module-mt';
  5. /**
  6. * 支付模块系统配置集成测试
  7. * 验证PaymentMtService与SystemConfigServiceMt的集成
  8. */
  9. describe('支付模块系统配置集成测试', () => {
  10. let paymentMtService: PaymentMtService;
  11. let systemConfigService: SystemConfigServiceMt;
  12. const testTenantId = 1;
  13. beforeEach(async () => {
  14. // 确保数据库连接
  15. if (!AppDataSource.isInitialized) {
  16. await AppDataSource.initialize();
  17. }
  18. paymentMtService = new PaymentMtService(AppDataSource);
  19. systemConfigService = new SystemConfigServiceMt(AppDataSource);
  20. // 清理测试配置
  21. await cleanupTestConfigs(testTenantId);
  22. });
  23. afterEach(async () => {
  24. // 清理测试配置
  25. await cleanupTestConfigs(testTenantId);
  26. });
  27. async function cleanupTestConfigs(tenantId: number) {
  28. const configKeys = [
  29. 'wx.payment.merchant.id',
  30. 'wx.mini.app.id',
  31. 'wx.payment.v3.key',
  32. 'wx.payment.notify.url',
  33. 'wx.payment.cert.serial.no',
  34. 'wx.payment.public.key',
  35. 'wx.payment.private.key'
  36. ];
  37. for (const key of configKeys) {
  38. await systemConfigService.deleteConfig(key, tenantId);
  39. }
  40. }
  41. describe('系统配置读取功能', () => {
  42. it('应该从系统配置获取支付配置', async () => {
  43. // 设置测试支付配置
  44. await systemConfigService.setConfig('wx.payment.merchant.id', 'test-merchant-id', testTenantId, '测试商户ID');
  45. await systemConfigService.setConfig('wx.mini.app.id', 'test-app-id', testTenantId, '测试小程序AppID');
  46. await systemConfigService.setConfig('wx.payment.v3.key', 'test-v3-key', testTenantId, '测试V3密钥');
  47. await systemConfigService.setConfig('wx.payment.notify.url', 'https://test.com/notify', testTenantId, '测试回调URL');
  48. await systemConfigService.setConfig('wx.payment.cert.serial.no', 'test-serial-no', testTenantId, '测试证书序列号');
  49. await systemConfigService.setConfig('wx.payment.public.key', 'test-public-key', testTenantId, '测试公钥');
  50. await systemConfigService.setConfig('wx.payment.private.key', 'test-private-key', testTenantId, '测试私钥');
  51. // 验证配置读取
  52. const config = await paymentMtService['getPaymentConfig'](testTenantId);
  53. expect(config.merchantId).toBe('test-merchant-id');
  54. expect(config.appId).toBe('test-app-id');
  55. expect(config.v3Key).toBe('test-v3-key');
  56. expect(config.notifyUrl).toBe('https://test.com/notify');
  57. expect(config.certSerialNo).toBe('test-serial-no');
  58. expect(config.publicKey).toBe('test-public-key');
  59. expect(config.privateKey).toBe('test-private-key');
  60. });
  61. it('应该支持批量获取支付配置', async () => {
  62. // 设置测试配置
  63. await systemConfigService.setConfig('wx.payment.merchant.id', 'test-merchant-id', testTenantId, '测试商户ID');
  64. await systemConfigService.setConfig('wx.mini.app.id', 'test-app-id', testTenantId, '测试小程序AppID');
  65. // 批量获取配置
  66. const configs = await systemConfigService.getConfigsByKeys(
  67. ['wx.payment.merchant.id', 'wx.mini.app.id'],
  68. testTenantId
  69. );
  70. expect(configs['wx.payment.merchant.id']).toBe('test-merchant-id');
  71. expect(configs['wx.mini.app.id']).toBe('test-app-id');
  72. });
  73. });
  74. describe('租户配置隔离', () => {
  75. const tenant1Id = 1;
  76. const tenant2Id = 2;
  77. it('应该支持不同租户的独立支付配置', async () => {
  78. // 为租户1设置配置
  79. await systemConfigService.setConfig('wx.payment.merchant.id', 'tenant1-merchant-id', tenant1Id, '租户1商户ID');
  80. await systemConfigService.setConfig('wx.mini.app.id', 'tenant1-app-id', tenant1Id, '租户1小程序AppID');
  81. // 为租户2设置不同的配置
  82. await systemConfigService.setConfig('wx.payment.merchant.id', 'tenant2-merchant-id', tenant2Id, '租户2商户ID');
  83. await systemConfigService.setConfig('wx.mini.app.id', 'tenant2-app-id', tenant2Id, '租户2小程序AppID');
  84. // 验证租户1的配置
  85. const tenant1Config = await paymentMtService['getPaymentConfig'](tenant1Id);
  86. // 验证租户2的配置
  87. const tenant2Config = await paymentMtService['getPaymentConfig'](tenant2Id);
  88. expect(tenant1Config.merchantId).toBe('tenant1-merchant-id');
  89. expect(tenant1Config.appId).toBe('tenant1-app-id');
  90. expect(tenant2Config.merchantId).toBe('tenant2-merchant-id');
  91. expect(tenant2Config.appId).toBe('tenant2-app-id');
  92. // 验证配置隔离
  93. expect(tenant1Config.merchantId).not.toBe(tenant2Config.merchantId);
  94. expect(tenant1Config.appId).not.toBe(tenant2Config.appId);
  95. });
  96. });
  97. describe('配置回退机制', () => {
  98. it('应该在系统配置不存在时回退到环境变量', async () => {
  99. // 不设置系统配置,依赖环境变量
  100. // 注意:这里假设环境变量已设置,实际测试时需要确保环境变量存在
  101. try {
  102. const config = await paymentMtService['getPaymentConfig'](testTenantId);
  103. // 如果环境变量存在,应该能获取到配置
  104. expect(config).toBeDefined();
  105. } catch (error) {
  106. // 如果环境变量不存在,应该抛出配置不完整的错误
  107. expect(error).toBeInstanceOf(Error);
  108. expect((error as Error).message).toContain('微信支付配置不完整');
  109. }
  110. });
  111. it('应该优先使用系统配置', async () => {
  112. // 设置系统配置
  113. await systemConfigService.setConfig('wx.payment.merchant.id', 'system-merchant-id', testTenantId, '系统配置商户ID');
  114. // 获取配置,应该优先使用系统配置
  115. const config = await paymentMtService['getPaymentConfig'](testTenantId);
  116. // 注意:这里只验证了商户ID,其他配置可能仍然依赖环境变量
  117. expect(config.merchantId).toBe('system-merchant-id');
  118. });
  119. });
  120. describe('PaymentMtService集成', () => {
  121. it('应该正确注入SystemConfigServiceMt依赖', () => {
  122. // 验证服务实例化
  123. expect(paymentMtService).toBeDefined();
  124. expect(paymentMtService).toBeInstanceOf(PaymentMtService);
  125. });
  126. it('应该支持动态配置初始化', async () => {
  127. // 验证initializeWxPay方法存在
  128. const initializeMethod = paymentMtService['initializeWxPay'];
  129. expect(typeof initializeMethod).toBe('function');
  130. // 验证getPaymentConfig方法存在
  131. const getConfigMethod = paymentMtService['getPaymentConfig'];
  132. expect(typeof getConfigMethod).toBe('function');
  133. });
  134. it('应该支持租户ID参数', async () => {
  135. // 验证createPayment方法支持tenantId参数
  136. const method = paymentMtService.createPayment;
  137. expect(typeof method).toBe('function');
  138. // 验证方法签名包含tenantId参数
  139. expect(method.length).toBeGreaterThanOrEqual(6);
  140. });
  141. });
  142. describe('配置验证', () => {
  143. it('应该验证配置完整性', async () => {
  144. // 不设置任何配置,应该抛出配置不完整的错误
  145. await expect(paymentMtService['getPaymentConfig'](testTenantId))
  146. .rejects
  147. .toThrow('微信支付配置不完整');
  148. });
  149. it('应该处理证书字符串格式', async () => {
  150. // 设置包含转义字符的证书配置
  151. await systemConfigService.setConfig('wx.payment.public.key', 'test\\npublic\\nkey', testTenantId, '测试公钥');
  152. await systemConfigService.setConfig('wx.payment.private.key', 'test\\nprivate\\nkey', testTenantId, '测试私钥');
  153. // 获取配置并验证格式处理
  154. const config = await paymentMtService['getPaymentConfig'](testTenantId);
  155. // 验证转义字符被正确处理
  156. expect(config.publicKey).toBe('test\npublic\nkey');
  157. expect(config.privateKey).toBe('test\nprivate\nkey');
  158. });
  159. });
  160. });