system-config-integration.test.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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';
  7. import { SystemConfigService } from '@d8d/core-module/system-config-module';
  8. import { SystemConfig } from '@d8d/core-module/system-config-module';
  9. import { UserEntity, Role } from '@d8d/core-module/user-module';
  10. import { File } from '@d8d/core-module/file-module';
  11. // 设置集成测试钩子
  12. setupIntegrationDatabaseHooksWithEntities([SystemConfig, UserEntity, Role, File])
  13. /**
  14. * 认证模块系统配置集成测试
  15. * 验证MiniAuthService与SystemConfigService的集成
  16. */
  17. describe('认证模块系统配置集成测试', () => {
  18. let miniAuthService: MiniAuthService;
  19. let systemConfigService: SystemConfigService;
  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 SystemConfigService(dataSource);
  27. // 清理测试配置
  28. await systemConfigService.deleteConfig('wx.mini.app.id');
  29. await systemConfigService.deleteConfig('wx.mini.app.secret');
  30. });
  31. afterEach(async () => {
  32. // 清理测试配置
  33. await systemConfigService.deleteConfig('wx.mini.app.id');
  34. await systemConfigService.deleteConfig('wx.mini.app.secret');
  35. });
  36. describe('系统配置读取功能', () => {
  37. it('应该从系统配置获取小程序AppID和Secret', async () => {
  38. // 设置测试配置
  39. await systemConfigService.setConfig('wx.mini.app.id', 'test-app-id', '测试小程序AppID');
  40. await systemConfigService.setConfig('wx.mini.app.secret', 'test-app-secret', '测试小程序AppSecret');
  41. // 验证配置读取
  42. const appId = await systemConfigService.getConfigByKey('wx.mini.app.id');
  43. const appSecret = await systemConfigService.getConfigByKey('wx.mini.app.secret');
  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', '测试小程序AppID');
  50. await systemConfigService.setConfig('wx.mini.app.secret', 'test-app-secret', '测试小程序AppSecret');
  51. // 批量获取配置
  52. const configs = await systemConfigService.getConfigsByKeys(
  53. ['wx.mini.app.id', 'wx.mini.app.secret']
  54. );
  55. expect(configs['wx.mini.app.id']).toBe('test-app-id');
  56. expect(configs['wx.mini.app.secret']).toBe('test-app-secret');
  57. });
  58. });
  59. // 租户配置隔离测试已移除,因为core-module是非多租户版本
  60. describe('配置缓存功能', () => {
  61. it('应该支持配置缓存', async () => {
  62. // 设置配置
  63. await systemConfigService.setConfig('wx.mini.app.id', 'cached-app-id', '缓存测试小程序AppID');
  64. // 第一次读取(应该写入缓存)
  65. const firstRead = await systemConfigService.getConfigByKey('wx.mini.app.id');
  66. expect(firstRead).toBe('cached-app-id');
  67. // 第二次读取(应该从缓存读取)
  68. const secondRead = await systemConfigService.getConfigByKey('wx.mini.app.id');
  69. expect(secondRead).toBe('cached-app-id');
  70. });
  71. it('应该支持空值缓存', async () => {
  72. // 读取不存在的配置(应该设置空值缓存)
  73. const nonExistentConfig = await systemConfigService.getConfigByKey('non.existent.key');
  74. expect(nonExistentConfig).toBeNull();
  75. // 再次读取(应该从空值缓存返回)
  76. const cachedRead = await systemConfigService.getConfigByKey('non.existent.key');
  77. expect(cachedRead).toBeNull();
  78. });
  79. });
  80. describe('MiniAuthService集成', () => {
  81. it('应该正确注入SystemConfigService依赖', () => {
  82. // 验证服务实例化
  83. expect(miniAuthService).toBeDefined();
  84. expect(miniAuthService).toBeInstanceOf(MiniAuthService);
  85. });
  86. it('应该支持租户ID参数', async () => {
  87. const method = miniAuthService.miniLogin;
  88. expect(typeof method).toBe('function');
  89. expect(method.length).toBeGreaterThanOrEqual(1);
  90. });
  91. });
  92. });