system-config-integration.test.ts 6.1 KB

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