system-config-integration.test.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import { describe, it, expect, beforeEach, afterEach } from 'vitest';
  2. import { AppDataSource } from '@d8d/shared-utils';
  3. import { MiniAuthService } from '../../src/services/index.mt';
  4. import { SystemConfigServiceMt } from '@d8d/core-module-mt/system-config-module-mt';
  5. import { SystemConfigMt } from '@d8d/core-module-mt/system-config-module-mt';
  6. /**
  7. * 认证模块系统配置集成测试
  8. * 验证MiniAuthService与SystemConfigServiceMt的集成
  9. */
  10. describe('认证模块系统配置集成测试', () => {
  11. let miniAuthService: MiniAuthService;
  12. let systemConfigService: SystemConfigServiceMt;
  13. const testTenantId = 1;
  14. beforeEach(async () => {
  15. // 确保数据库连接
  16. if (!AppDataSource.isInitialized) {
  17. await AppDataSource.initialize();
  18. }
  19. miniAuthService = new MiniAuthService(AppDataSource);
  20. systemConfigService = new SystemConfigServiceMt(AppDataSource);
  21. // 清理测试配置
  22. await systemConfigService.deleteConfig('wx.mini.app.id', testTenantId);
  23. await systemConfigService.deleteConfig('wx.mini.app.secret', testTenantId);
  24. });
  25. afterEach(async () => {
  26. // 清理测试配置
  27. await systemConfigService.deleteConfig('wx.mini.app.id', testTenantId);
  28. await systemConfigService.deleteConfig('wx.mini.app.secret', testTenantId);
  29. });
  30. describe('系统配置读取功能', () => {
  31. it('应该从系统配置获取小程序AppID和Secret', async () => {
  32. // 设置测试配置
  33. await systemConfigService.setConfig('wx.mini.app.id', 'test-app-id', testTenantId, '测试小程序AppID');
  34. await systemConfigService.setConfig('wx.mini.app.secret', 'test-app-secret', testTenantId, '测试小程序AppSecret');
  35. // 验证配置读取
  36. const appId = await systemConfigService.getConfigByKey('wx.mini.app.id', testTenantId);
  37. const appSecret = await systemConfigService.getConfigByKey('wx.mini.app.secret', testTenantId);
  38. expect(appId).toBe('test-app-id');
  39. expect(appSecret).toBe('test-app-secret');
  40. });
  41. it('应该支持批量获取配置', async () => {
  42. // 设置测试配置
  43. await systemConfigService.setConfig('wx.mini.app.id', 'test-app-id', testTenantId, '测试小程序AppID');
  44. await systemConfigService.setConfig('wx.mini.app.secret', 'test-app-secret', testTenantId, '测试小程序AppSecret');
  45. // 批量获取配置
  46. const configs = await systemConfigService.getConfigsByKeys(
  47. ['wx.mini.app.id', 'wx.mini.app.secret'],
  48. testTenantId
  49. );
  50. expect(configs['wx.mini.app.id']).toBe('test-app-id');
  51. expect(configs['wx.mini.app.secret']).toBe('test-app-secret');
  52. });
  53. });
  54. describe('租户配置隔离', () => {
  55. const tenant1Id = 1;
  56. const tenant2Id = 2;
  57. it('应该支持不同租户的独立配置', async () => {
  58. // 为租户1设置配置
  59. await systemConfigService.setConfig('wx.mini.app.id', 'tenant1-app-id', tenant1Id, '租户1小程序AppID');
  60. await systemConfigService.setConfig('wx.mini.app.secret', 'tenant1-app-secret', tenant1Id, '租户1小程序AppSecret');
  61. // 为租户2设置不同的配置
  62. await systemConfigService.setConfig('wx.mini.app.id', 'tenant2-app-id', tenant2Id, '租户2小程序AppID');
  63. await systemConfigService.setConfig('wx.mini.app.secret', 'tenant2-app-secret', tenant2Id, '租户2小程序AppSecret');
  64. // 验证租户1的配置
  65. const tenant1AppId = await systemConfigService.getConfigByKey('wx.mini.app.id', tenant1Id);
  66. const tenant1AppSecret = await systemConfigService.getConfigByKey('wx.mini.app.secret', tenant1Id);
  67. // 验证租户2的配置
  68. const tenant2AppId = await systemConfigService.getConfigByKey('wx.mini.app.id', tenant2Id);
  69. const tenant2AppSecret = await systemConfigService.getConfigByKey('wx.mini.app.secret', tenant2Id);
  70. expect(tenant1AppId).toBe('tenant1-app-id');
  71. expect(tenant1AppSecret).toBe('tenant1-app-secret');
  72. expect(tenant2AppId).toBe('tenant2-app-id');
  73. expect(tenant2AppSecret).toBe('tenant2-app-secret');
  74. // 验证配置隔离
  75. expect(tenant1AppId).not.toBe(tenant2AppId);
  76. expect(tenant1AppSecret).not.toBe(tenant2AppSecret);
  77. });
  78. });
  79. describe('配置缓存功能', () => {
  80. it('应该支持配置缓存', async () => {
  81. // 设置配置
  82. await systemConfigService.setConfig('wx.mini.app.id', 'cached-app-id', testTenantId, '缓存测试小程序AppID');
  83. // 第一次读取(应该写入缓存)
  84. const firstRead = await systemConfigService.getConfigByKey('wx.mini.app.id', testTenantId);
  85. expect(firstRead).toBe('cached-app-id');
  86. // 第二次读取(应该从缓存读取)
  87. const secondRead = await systemConfigService.getConfigByKey('wx.mini.app.id', testTenantId);
  88. expect(secondRead).toBe('cached-app-id');
  89. });
  90. it('应该支持空值缓存', async () => {
  91. // 读取不存在的配置(应该设置空值缓存)
  92. const nonExistentConfig = await systemConfigService.getConfigByKey('non.existent.key', testTenantId);
  93. expect(nonExistentConfig).toBeNull();
  94. // 再次读取(应该从空值缓存返回)
  95. const cachedRead = await systemConfigService.getConfigByKey('non.existent.key', testTenantId);
  96. expect(cachedRead).toBeNull();
  97. });
  98. });
  99. describe('MiniAuthService集成', () => {
  100. it('应该正确注入SystemConfigServiceMt依赖', () => {
  101. // 验证服务实例化
  102. expect(miniAuthService).toBeDefined();
  103. expect(miniAuthService).toBeInstanceOf(MiniAuthService);
  104. });
  105. it('应该支持租户ID参数', async () => {
  106. // 验证miniLogin方法支持tenantId参数
  107. const method = miniAuthService.miniLogin;
  108. expect(typeof method).toBe('function');
  109. // 验证方法签名包含tenantId参数
  110. expect(method.length).toBeGreaterThanOrEqual(1);
  111. });
  112. });
  113. });