system-config.routes.integration.test.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. import { describe, it, expect, beforeEach } from 'vitest';
  2. import { testClient } from 'hono/testing';
  3. import {
  4. IntegrationTestDatabase,
  5. setupIntegrationDatabaseHooksWithEntities
  6. } from '@d8d/shared-test-util';
  7. import { systemConfigRoutesMt } from '../../src/routes/system-config.routes.mt';
  8. import { SystemConfigMt } from '../../src/entities/system-config.entity.mt';
  9. import { SystemConfigServiceMt } from '../../src/services/system-config.service.mt';
  10. import { UserEntityMt, RoleMt } from '@d8d/core-module-mt/user-module-mt';
  11. import { FileMt } from '@d8d/core-module-mt/file-module-mt';
  12. import { TestDataFactory } from '../utils/integration-test-db';
  13. import { AuthService } from '@d8d/core-module-mt/auth-module-mt';
  14. import { UserServiceMt } from '@d8d/core-module-mt/user-module-mt';
  15. import { redisUtil } from '@d8d/shared-utils';
  16. /**
  17. * 格式化租户系统配置的Redis键(测试用辅助函数)
  18. */
  19. function formatTenantConfigKey(tenantId: number, configKey: string): string {
  20. return `system_config:${tenantId}:${configKey}`;
  21. }
  22. // 设置集成测试钩子
  23. setupIntegrationDatabaseHooksWithEntities([SystemConfigMt, UserEntityMt, RoleMt, FileMt])
  24. describe('系统配置路由API集成测试 (使用hono/testing)', () => {
  25. let client: ReturnType<typeof testClient<typeof systemConfigRoutesMt>>;
  26. let authService: AuthService;
  27. let userService: UserServiceMt;
  28. let testToken: string;
  29. let testUser: any;
  30. beforeEach(async () => {
  31. // 创建测试客户端
  32. client = testClient(systemConfigRoutesMt);
  33. // 获取数据源
  34. const dataSource = await IntegrationTestDatabase.getDataSource();
  35. if (!dataSource) throw new Error('Database not initialized');
  36. // 初始化服务
  37. userService = new UserServiceMt(dataSource);
  38. authService = new AuthService(userService);
  39. // 创建测试用户并生成token
  40. testUser = await TestDataFactory.createTestUser(dataSource, {
  41. username: 'testuser_systemconfig',
  42. password: 'TestPassword123!',
  43. email: 'testuser_systemconfig@example.com'
  44. });
  45. // 生成测试用户的token
  46. testToken = authService.generateToken(testUser);
  47. });
  48. describe('CRUD Operations', () => {
  49. it('应该成功创建系统配置', async () => {
  50. const configData = {
  51. configKey: 'app.login.enabled',
  52. configValue: 'true',
  53. description: '控制小程序登录功能是否开启'
  54. };
  55. const response = await client.index.$post({
  56. json: configData
  57. }, {
  58. headers: {
  59. 'Authorization': `Bearer ${testToken}`
  60. }
  61. });
  62. expect(response.status).toBe(201);
  63. const result = await response.json();
  64. if ('configKey' in result) {
  65. expect(result.configKey).toBe(configData.configKey);
  66. expect(result.configValue).toBe(configData.configValue);
  67. }
  68. });
  69. it('应该根据ID获取系统配置', async () => {
  70. // 先创建配置
  71. const dataSource = await IntegrationTestDatabase.getDataSource();
  72. if (!dataSource) throw new Error('Database not initialized');
  73. const systemConfigService = new SystemConfigServiceMt(dataSource);
  74. const config = await systemConfigService.create({
  75. configKey: 'app.payment.enabled',
  76. configValue: 'true',
  77. description: '控制支付功能是否开启',
  78. tenantId: testUser.tenantId
  79. } as SystemConfigMt);
  80. const response = await client[':id'].$get({
  81. param: { id: config.id }
  82. }, {
  83. headers: {
  84. 'Authorization': `Bearer ${testToken}`
  85. }
  86. });
  87. expect(response.status).toBe(200);
  88. const result = await response.json();
  89. if ('id' in result) {
  90. expect(result.id).toBe(config.id);
  91. expect(result.configKey).toBe(config.configKey);
  92. }
  93. });
  94. it('应该更新系统配置', async () => {
  95. // 先创建配置
  96. const dataSource = await IntegrationTestDatabase.getDataSource();
  97. if (!dataSource) throw new Error('Database not initialized');
  98. const systemConfigService = new SystemConfigServiceMt(dataSource);
  99. const config = await systemConfigService.create({
  100. configKey: 'app.notification.enabled',
  101. configValue: 'true',
  102. description: '控制通知功能是否开启',
  103. tenantId: testUser.tenantId
  104. } as SystemConfigMt);
  105. const updateData = {
  106. configValue: 'false',
  107. description: '通知功能已关闭'
  108. };
  109. const response = await client[':id'].$put({
  110. param: { id: config.id },
  111. json: updateData
  112. }, {
  113. headers: {
  114. 'Authorization': `Bearer ${testToken}`
  115. }
  116. });
  117. expect(response.status).toBe(200);
  118. const result = await response.json();
  119. if ('configValue' in result) {
  120. expect(result.configValue).toBe(updateData.configValue);
  121. expect(result.description).toBe(updateData.description);
  122. }
  123. });
  124. it('应该删除系统配置', async () => {
  125. // 先创建配置
  126. const dataSource = await IntegrationTestDatabase.getDataSource();
  127. if (!dataSource) throw new Error('Database not initialized');
  128. const systemConfigService = new SystemConfigServiceMt(dataSource);
  129. const config = await systemConfigService.create({
  130. configKey: 'app.analytics.enabled',
  131. configValue: 'true',
  132. description: '控制分析功能是否开启',
  133. tenantId: testUser.tenantId
  134. } as SystemConfigMt);
  135. const response = await client[':id'].$delete({
  136. param: { id: config.id }
  137. }, {
  138. headers: {
  139. 'Authorization': `Bearer ${testToken}`
  140. }
  141. });
  142. expect(response.status).toBe(204);
  143. // 验证配置已被删除
  144. const deletedConfig = await systemConfigService.getById(config.id);
  145. expect(deletedConfig).toBeNull();
  146. });
  147. it('应该列出系统配置列表', async () => {
  148. // 创建多个配置
  149. const dataSource = await IntegrationTestDatabase.getDataSource();
  150. if (!dataSource) throw new Error('Database not initialized');
  151. const systemConfigService = new SystemConfigServiceMt(dataSource);
  152. await systemConfigService.create({
  153. configKey: 'app.feature1.enabled',
  154. configValue: 'true',
  155. tenantId: testUser.tenantId
  156. } as SystemConfigMt);
  157. await systemConfigService.create({
  158. configKey: 'app.feature2.enabled',
  159. configValue: 'false',
  160. tenantId: testUser.tenantId
  161. } as SystemConfigMt);
  162. const response = await client.index.$get({
  163. query: {}
  164. }, {
  165. headers: {
  166. 'Authorization': `Bearer ${testToken}`
  167. }
  168. });
  169. expect(response.status).toBe(200);
  170. const result = await response.json();
  171. if ('data' in result) {
  172. expect(result.data).toHaveLength(2);
  173. }
  174. });
  175. });
  176. describe('Multi-tenant Isolation', () => {
  177. let tenant1User: any;
  178. let tenant2User: any;
  179. let tenant1Token: string;
  180. let tenant2Token: string;
  181. beforeEach(async () => {
  182. const dataSource = await IntegrationTestDatabase.getDataSource();
  183. if (!dataSource) throw new Error('Database not initialized');
  184. // 创建租户1的用户
  185. tenant1User = await TestDataFactory.createTestUser(dataSource, {
  186. username: 'tenant1_user_systemconfig',
  187. password: 'TestPassword123!',
  188. email: 'tenant1_systemconfig@example.com',
  189. tenantId: 1
  190. });
  191. // 创建租户2的用户
  192. tenant2User = await TestDataFactory.createTestUser(dataSource, {
  193. username: 'tenant2_user_systemconfig',
  194. password: 'TestPassword123!',
  195. email: 'tenant2_systemconfig@example.com',
  196. tenantId: 2
  197. });
  198. // 生成租户用户的token
  199. tenant1Token = authService.generateToken(tenant1User);
  200. tenant2Token = authService.generateToken(tenant2User);
  201. });
  202. it('应该按租户隔离配置', async () => {
  203. const dataSource = await IntegrationTestDatabase.getDataSource();
  204. if (!dataSource) throw new Error('Database not initialized');
  205. const systemConfigService = new SystemConfigServiceMt(dataSource);
  206. // 为租户1创建配置
  207. await systemConfigService.create({
  208. configKey: 'app.shared.config',
  209. configValue: 'tenant1-value',
  210. tenantId: 1
  211. } as SystemConfigMt);
  212. // 为租户2创建配置
  213. await systemConfigService.create({
  214. configKey: 'app.shared.config',
  215. configValue: 'tenant2-value',
  216. tenantId: 2
  217. } as SystemConfigMt);
  218. // 查询租户1的配置
  219. const response1 = await client.index.$get({
  220. query: {}
  221. }, {
  222. headers: {
  223. 'Authorization': `Bearer ${tenant1Token}`
  224. }
  225. });
  226. expect(response1.status).toBe(200);
  227. const result1 = await response1.json();
  228. if ('data' in result1) {
  229. expect(result1.data).toHaveLength(1);
  230. expect(result1.data[0].configValue).toBe('tenant1-value');
  231. }
  232. // 查询租户2的配置
  233. const response2 = await client.index.$get({
  234. query: {}
  235. }, {
  236. headers: {
  237. 'Authorization': `Bearer ${tenant2Token}`
  238. }
  239. });
  240. expect(response2.status).toBe(200);
  241. const result2 = await response2.json();
  242. if ('data' in result2) {
  243. expect(result2.data).toHaveLength(1);
  244. expect(result2.data[0].configValue).toBe('tenant2-value');
  245. }
  246. });
  247. });
  248. describe('Data Validation', () => {
  249. it('应该验证必填字段', async () => {
  250. const invalidData = {
  251. configKey: '', // 空字符串
  252. configValue: 'true'
  253. };
  254. const response = await client.index.$post({
  255. json: invalidData
  256. }, {
  257. headers: {
  258. 'Authorization': `Bearer ${testToken}`
  259. }
  260. });
  261. expect(response.status).toBe(400);
  262. });
  263. it('应该验证字段长度', async () => {
  264. const invalidData = {
  265. configKey: 'a'.repeat(256), // 超过255字符
  266. configValue: 'true'
  267. };
  268. const response = await client.index.$post({
  269. json: invalidData
  270. }, {
  271. headers: {
  272. 'Authorization': `Bearer ${testToken}`
  273. }
  274. });
  275. expect(response.status).toBe(400);
  276. });
  277. });
  278. describe('自定义路由缓存刷新验证', () => {
  279. beforeEach(async () => {
  280. // 清除测试缓存
  281. await redisUtil.deleteSystemConfig(formatTenantConfigKey(testUser.tenantId, 'app.login.enabled'));
  282. await redisUtil.deleteSystemConfig(formatTenantConfigKey(testUser.tenantId, 'app.payment.enabled'));
  283. });
  284. it('应该通过自定义创建路由刷新缓存', async () => {
  285. const configData = {
  286. configKey: 'app.login.enabled',
  287. configValue: 'true',
  288. description: '控制小程序登录功能是否开启'
  289. };
  290. // 验证缓存初始为空
  291. const initialCache = await redisUtil.getSystemConfig(formatTenantConfigKey(testUser.tenantId, configData.configKey));
  292. expect(initialCache).toBeNull();
  293. // 通过自定义路由创建配置
  294. const response = await client.index.$post({
  295. json: configData
  296. }, {
  297. headers: {
  298. 'Authorization': `Bearer ${testToken}`
  299. }
  300. });
  301. expect(response.status).toBe(201);
  302. const result = await response.json() as any;
  303. expect(result.configKey).toBe(configData.configKey);
  304. expect(result.configValue).toBe(configData.configValue);
  305. // 验证缓存已被刷新(为空,因为setConfig会删除缓存)
  306. const afterCreateCache = await redisUtil.getSystemConfig(formatTenantConfigKey(testUser.tenantId, configData.configKey));
  307. expect(afterCreateCache).toBeNull();
  308. });
  309. it('应该通过自定义更新路由刷新缓存', async () => {
  310. // 先创建配置
  311. const dataSource = await IntegrationTestDatabase.getDataSource();
  312. if (!dataSource) throw new Error('Database not initialized');
  313. const systemConfigService = new SystemConfigServiceMt(dataSource);
  314. const config = await systemConfigService.create({
  315. configKey: 'app.payment.enabled',
  316. configValue: 'true',
  317. description: '控制支付功能是否开启',
  318. tenantId: testUser.tenantId
  319. } as any);
  320. // 设置缓存
  321. await redisUtil.setSystemConfig(formatTenantConfigKey(testUser.tenantId, config.configKey), 'cached-value', 3600);
  322. const initialCache = await redisUtil.getSystemConfig(formatTenantConfigKey(testUser.tenantId, config.configKey));
  323. expect(initialCache).toBe('cached-value');
  324. // 通过自定义路由更新配置
  325. const updateData = {
  326. configValue: 'false',
  327. description: '支付功能已关闭'
  328. };
  329. const response = await client[':id'].$put({
  330. param: { id: config.id },
  331. json: updateData
  332. }, {
  333. headers: {
  334. 'Authorization': `Bearer ${testToken}`
  335. }
  336. });
  337. expect(response.status).toBe(200);
  338. const result = await response.json() as any;
  339. expect(result.configValue).toBe(updateData.configValue);
  340. // 验证缓存已被刷新
  341. const afterUpdateCache = await redisUtil.getSystemConfig(formatTenantConfigKey(testUser.tenantId, config.configKey));
  342. expect(afterUpdateCache).toBeNull();
  343. });
  344. it('应该通过自定义删除路由刷新缓存', async () => {
  345. // 先创建配置
  346. const dataSource = await IntegrationTestDatabase.getDataSource();
  347. if (!dataSource) throw new Error('Database not initialized');
  348. const systemConfigService = new SystemConfigServiceMt(dataSource);
  349. const config = await systemConfigService.create({
  350. configKey: 'app.analytics.enabled',
  351. configValue: 'true',
  352. description: '控制分析功能是否开启',
  353. tenantId: testUser.tenantId
  354. } as any);
  355. // 设置缓存
  356. await redisUtil.setSystemConfig(formatTenantConfigKey(testUser.tenantId, config.configKey), 'cached-value', 3600);
  357. const initialCache = await redisUtil.getSystemConfig(formatTenantConfigKey(testUser.tenantId, config.configKey));
  358. expect(initialCache).toBe('cached-value');
  359. // 通过自定义路由删除配置
  360. const response = await client[':id'].$delete({
  361. param: { id: config.id }
  362. }, {
  363. headers: {
  364. 'Authorization': `Bearer ${testToken}`
  365. }
  366. });
  367. expect(response.status).toBe(204);
  368. // 验证缓存已被刷新
  369. const afterDeleteCache = await redisUtil.getSystemConfig(formatTenantConfigKey(testUser.tenantId, config.configKey));
  370. expect(afterDeleteCache).toBeNull();
  371. });
  372. });
  373. describe('自定义路由多租户隔离验证', () => {
  374. let tenant1User: any;
  375. let tenant2User: any;
  376. let tenant1Token: string;
  377. let tenant2Token: string;
  378. beforeEach(async () => {
  379. const dataSource = await IntegrationTestDatabase.getDataSource();
  380. if (!dataSource) throw new Error('Database not initialized');
  381. // 创建租户1的用户
  382. tenant1User = await TestDataFactory.createTestUser(dataSource, {
  383. username: 'tenant1_customroutes',
  384. password: 'TestPassword123!',
  385. email: 'tenant1_customroutes@example.com',
  386. tenantId: 1
  387. });
  388. // 创建租户2的用户
  389. tenant2User = await TestDataFactory.createTestUser(dataSource, {
  390. username: 'tenant2_customroutes',
  391. password: 'TestPassword123!',
  392. email: 'tenant2_customroutes@example.com',
  393. tenantId: 2
  394. });
  395. // 生成租户用户的token
  396. tenant1Token = authService.generateToken(tenant1User);
  397. tenant2Token = authService.generateToken(tenant2User);
  398. // 清除测试缓存
  399. await redisUtil.deleteSystemConfig(formatTenantConfigKey(1, 'app.shared.config'));
  400. await redisUtil.deleteSystemConfig(formatTenantConfigKey(2, 'app.shared.config'));
  401. });
  402. it('应该确保多租户缓存隔离', async () => {
  403. const configData = {
  404. configKey: 'app.shared.config',
  405. configValue: 'tenant1-value',
  406. description: '共享配置'
  407. };
  408. // 租户1创建配置
  409. const response1 = await client.index.$post({
  410. json: configData
  411. }, {
  412. headers: {
  413. 'Authorization': `Bearer ${tenant1Token}`
  414. }
  415. });
  416. expect(response1.status).toBe(201);
  417. // 租户2创建配置
  418. const response2 = await client.index.$post({
  419. json: { ...configData, configValue: 'tenant2-value' }
  420. }, {
  421. headers: {
  422. 'Authorization': `Bearer ${tenant2Token}`
  423. }
  424. });
  425. expect(response2.status).toBe(201);
  426. // 验证租户1的缓存操作不影响租户2
  427. const tenant1Cache = await redisUtil.getSystemConfig(formatTenantConfigKey(1, configData.configKey));
  428. const tenant2Cache = await redisUtil.getSystemConfig(formatTenantConfigKey(2, configData.configKey));
  429. // 两个租户的缓存都应该是空的,因为setConfig会删除缓存
  430. expect(tenant1Cache).toBeNull();
  431. expect(tenant2Cache).toBeNull();
  432. });
  433. it('应该验证租户ID正确提取', async () => {
  434. const configData = {
  435. configKey: 'app.tenant.specific',
  436. configValue: 'tenant-specific-value',
  437. description: '租户特定配置'
  438. };
  439. // 租户1创建配置
  440. const response = await client.index.$post({
  441. json: configData
  442. }, {
  443. headers: {
  444. 'Authorization': `Bearer ${tenant1Token}`
  445. }
  446. });
  447. expect(response.status).toBe(201);
  448. const result = await response.json() as any;
  449. // 验证配置属于正确的租户
  450. expect(result.tenantId).toBe(1);
  451. // 验证租户2无法访问租户1的配置
  452. const dataSource = await IntegrationTestDatabase.getDataSource();
  453. if (!dataSource) throw new Error('Database not initialized');
  454. const systemConfigService = new SystemConfigServiceMt(dataSource);
  455. const tenant2Configs = await systemConfigService.getAllConfigs(2);
  456. const tenant2HasConfig = tenant2Configs.some(config => config.configKey === configData.configKey);
  457. expect(tenant2HasConfig).toBe(false);
  458. });
  459. });
  460. });