admin-routes.integration.test.ts 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  1. import { describe, it, expect, beforeEach } from 'vitest';
  2. import { testClient } from 'hono/testing';
  3. import { IntegrationTestDatabase, setupIntegrationDatabaseHooksWithEntities } from '@d8d/shared-test-util';
  4. import { JWTUtil } from '@d8d/shared-utils';
  5. import { UserEntityMt, RoleMt } from '@d8d/user-module-mt';
  6. import { FileMt } from '@d8d/file-module-mt';
  7. import { adminSupplierRoutes } from '../../src/routes';
  8. import { SupplierMt } from '../../src/entities';
  9. // 设置集成测试钩子
  10. setupIntegrationDatabaseHooksWithEntities([UserEntityMt, RoleMt, SupplierMt, FileMt])
  11. describe('管理员供应商管理API集成测试', () => {
  12. let client: ReturnType<typeof testClient<typeof adminSupplierRoutes>>;
  13. let adminToken: string;
  14. let testUser: UserEntityMt;
  15. let testAdmin: UserEntityMt;
  16. beforeEach(async () => {
  17. // 创建测试客户端
  18. client = testClient(adminSupplierRoutes);
  19. // 获取数据源
  20. const dataSource = await IntegrationTestDatabase.getDataSource();
  21. // 创建测试用户
  22. const userRepository = dataSource.getRepository(UserEntityMt);
  23. testUser = userRepository.create({
  24. username: `test_user_${Math.floor(Math.random() * 100000)}`,
  25. password: 'test_password',
  26. nickname: '测试用户',
  27. registrationSource: 'web',
  28. tenantId: 1
  29. });
  30. await userRepository.save(testUser);
  31. // 创建测试管理员用户
  32. testAdmin = userRepository.create({
  33. username: `test_admin_${Math.floor(Math.random() * 100000)}`,
  34. password: 'admin_password',
  35. nickname: '测试管理员',
  36. registrationSource: 'web',
  37. tenantId: 1
  38. });
  39. await userRepository.save(testAdmin);
  40. // 生成测试管理员的token
  41. adminToken = JWTUtil.generateToken({
  42. id: testAdmin.id,
  43. username: testAdmin.username,
  44. roles: [{name:'admin'}],
  45. tenantId: testAdmin.tenantId
  46. });
  47. });
  48. describe('GET /suppliers', () => {
  49. it('应该返回供应商列表', async () => {
  50. const response = await client.index.$get({
  51. query: {}
  52. }, {
  53. headers: {
  54. 'Authorization': `Bearer ${adminToken}`
  55. }
  56. });
  57. console.debug('供应商列表响应状态:', response.status);
  58. expect(response.status).toBe(200);
  59. if (response.status === 200) {
  60. const data = await response.json();
  61. expect(data).toHaveProperty('data');
  62. expect(Array.isArray(data.data)).toBe(true);
  63. }
  64. });
  65. it('应该拒绝未认证用户的访问', async () => {
  66. const response = await client.index.$get({
  67. query: {}
  68. });
  69. expect(response.status).toBe(401);
  70. });
  71. });
  72. describe('POST /suppliers', () => {
  73. it('应该成功创建供应商', async () => {
  74. const createData = {
  75. name: '管理员创建供应商',
  76. username: `admin_created_supplier_${Math.floor(Math.random() * 100000)}`,
  77. password: 'password123',
  78. phone: '13800138000',
  79. realname: '管理员创建供应商',
  80. state: 1
  81. };
  82. const response = await client.index.$post({
  83. json: createData
  84. }, {
  85. headers: {
  86. 'Authorization': `Bearer ${adminToken}`
  87. }
  88. });
  89. console.debug('创建供应商响应状态:', response.status);
  90. if (response.status !== 201) {
  91. const errorData = await response.json();
  92. console.debug('创建供应商错误响应:', errorData);
  93. }
  94. expect(response.status).toBe(201);
  95. if (response.status === 201) {
  96. const data = await response.json();
  97. expect(data).toHaveProperty('id');
  98. expect(data.name).toBe(createData.name);
  99. expect(data.username).toBe(createData.username);
  100. expect(data.phone).toBe(createData.phone);
  101. expect(data.realname).toBe(createData.realname);
  102. expect(data.state).toBe(createData.state);
  103. }
  104. });
  105. it('应该验证创建供应商的必填字段', async () => {
  106. const invalidData = {
  107. // 缺少必填字段
  108. name: '',
  109. username: '',
  110. password: '',
  111. phone: '',
  112. realname: ''
  113. };
  114. const response = await client.index.$post({
  115. json: invalidData
  116. }, {
  117. headers: {
  118. 'Authorization': `Bearer ${adminToken}`
  119. }
  120. });
  121. expect(response.status).toBe(400);
  122. });
  123. });
  124. describe('GET /suppliers/:id', () => {
  125. it('应该返回指定供应商的详情', async () => {
  126. // 先创建一个供应商
  127. const dataSource = await IntegrationTestDatabase.getDataSource();
  128. const supplierRepository = dataSource.getRepository(SupplierMt);
  129. const testSupplier = supplierRepository.create({
  130. tenantId: 1,
  131. name: '测试供应商详情',
  132. username: `test_supplier_detail_${Math.floor(Math.random() * 100000)}`,
  133. password: 'password123',
  134. phone: '13600136000',
  135. realname: '测试供应商详情',
  136. loginNum: 5,
  137. loginTime: new Date('2024-01-01T12:00:00Z'),
  138. loginIp: '192.168.1.1',
  139. lastLoginTime: new Date('2024-01-01T12:00:00Z'),
  140. lastLoginIp: '192.168.1.1',
  141. state: 1,
  142. createdBy: testUser.id
  143. });
  144. await supplierRepository.save(testSupplier);
  145. const response = await client[':id'].$get({
  146. param: { id: testSupplier.id }
  147. }, {
  148. headers: {
  149. 'Authorization': `Bearer ${adminToken}`
  150. }
  151. });
  152. console.debug('供应商详情响应状态:', response.status);
  153. expect(response.status).toBe(200);
  154. if (response.status === 200) {
  155. const data = await response.json();
  156. expect(data.id).toBe(testSupplier.id);
  157. expect(data.name).toBe(testSupplier.name);
  158. expect(data.username).toBe(testSupplier.username);
  159. expect(data.phone).toBe(testSupplier.phone);
  160. expect(data.realname).toBe(testSupplier.realname);
  161. }
  162. });
  163. it('应该处理不存在的供应商', async () => {
  164. const response = await client[':id'].$get({
  165. param: { id: 999999 }
  166. }, {
  167. headers: {
  168. 'Authorization': `Bearer ${adminToken}`
  169. }
  170. });
  171. expect(response.status).toBe(404);
  172. });
  173. });
  174. describe('PUT /suppliers/:id', () => {
  175. it('应该成功更新供应商', async () => {
  176. // 先创建一个供应商
  177. const dataSource = await IntegrationTestDatabase.getDataSource();
  178. const supplierRepository = dataSource.getRepository(SupplierMt);
  179. const testSupplier = supplierRepository.create({
  180. tenantId: 1,
  181. name: '原始供应商',
  182. username: `original_supplier_${Math.floor(Math.random() * 100000)}`,
  183. password: 'password123',
  184. phone: '13500135000',
  185. realname: '原始供应商',
  186. loginNum: 0,
  187. loginTime: null,
  188. loginIp: null,
  189. lastLoginTime: null,
  190. lastLoginIp: null,
  191. state: 1,
  192. createdBy: testUser.id
  193. });
  194. await supplierRepository.save(testSupplier);
  195. const updateData = {
  196. name: '更新后的供应商',
  197. phone: '13700137000',
  198. realname: '更新后的供应商',
  199. state: 2
  200. };
  201. const response = await client[':id'].$put({
  202. param: { id: testSupplier.id },
  203. json: updateData
  204. }, {
  205. headers: {
  206. 'Authorization': `Bearer ${adminToken}`
  207. }
  208. });
  209. console.debug('更新供应商响应状态:', response.status);
  210. expect(response.status).toBe(200);
  211. if (response.status === 200) {
  212. const data = await response.json();
  213. expect(data.name).toBe(updateData.name);
  214. expect(data.phone).toBe(updateData.phone);
  215. expect(data.realname).toBe(updateData.realname);
  216. expect(data.state).toBe(updateData.state);
  217. }
  218. });
  219. });
  220. describe('DELETE /suppliers/:id', () => {
  221. it('应该成功删除供应商', async () => {
  222. // 先创建一个供应商
  223. const dataSource = await IntegrationTestDatabase.getDataSource();
  224. const supplierRepository = dataSource.getRepository(SupplierMt);
  225. const testSupplier = supplierRepository.create({
  226. tenantId: 1,
  227. name: '待删除供应商',
  228. username: `delete_supplier_${Math.floor(Math.random() * 100000)}`,
  229. password: 'password123',
  230. phone: '13400134000',
  231. realname: '待删除供应商',
  232. loginNum: 0,
  233. loginTime: null,
  234. loginIp: null,
  235. lastLoginTime: null,
  236. lastLoginIp: null,
  237. state: 1,
  238. createdBy: testUser.id
  239. });
  240. await supplierRepository.save(testSupplier);
  241. const response = await client[':id'].$delete({
  242. param: { id: testSupplier.id }
  243. }, {
  244. headers: {
  245. 'Authorization': `Bearer ${adminToken}`
  246. }
  247. });
  248. console.debug('删除供应商响应状态:', response.status);
  249. expect(response.status).toBe(204);
  250. // 验证供应商确实被删除
  251. const deletedSupplier = await supplierRepository.findOne({
  252. where: { id: testSupplier.id }
  253. });
  254. expect(deletedSupplier).toBeNull();
  255. });
  256. });
  257. describe('管理员权限测试', () => {
  258. it('管理员应该可以为其他用户创建供应商', async () => {
  259. const createData = {
  260. name: '为其他用户创建供应商',
  261. username: `other_user_supplier_${Math.floor(Math.random() * 100000)}`,
  262. password: 'password123',
  263. phone: '13800138001',
  264. realname: '为其他用户创建供应商',
  265. state: 1,
  266. createdBy: testUser.id // 管理员可以指定创建者
  267. };
  268. const response = await client.index.$post({
  269. json: createData
  270. }, {
  271. headers: {
  272. 'Authorization': `Bearer ${adminToken}`
  273. }
  274. });
  275. console.debug('管理员为其他用户创建供应商响应状态:', response.status);
  276. expect(response.status).toBe(201);
  277. if (response.status === 201) {
  278. const data = await response.json();
  279. expect(data.createdBy).toBe(testUser.id); // 验证供应商确实属于其他用户
  280. expect(data.name).toBe(createData.name);
  281. }
  282. });
  283. it('管理员应该可以访问所有用户的供应商', async () => {
  284. // 为测试用户创建一些供应商
  285. const dataSource = await IntegrationTestDatabase.getDataSource();
  286. const supplierRepository = dataSource.getRepository(SupplierMt);
  287. const userSupplier1 = supplierRepository.create({
  288. tenantId: 1,
  289. name: '用户供应商1',
  290. username: `user_supplier1_${Math.floor(Math.random() * 100000)}`,
  291. password: 'password123',
  292. phone: '13800138002',
  293. realname: '用户供应商1',
  294. loginNum: 0,
  295. loginTime: null,
  296. loginIp: null,
  297. lastLoginTime: null,
  298. lastLoginIp: null,
  299. state: 1,
  300. createdBy: testUser.id
  301. });
  302. await supplierRepository.save(userSupplier1);
  303. const userSupplier2 = supplierRepository.create({
  304. tenantId: 1,
  305. name: '用户供应商2',
  306. username: `user_supplier2_${Math.floor(Math.random() * 100000)}`,
  307. password: 'password123',
  308. phone: '13800138003',
  309. realname: '用户供应商2',
  310. loginNum: 0,
  311. loginTime: null,
  312. loginIp: null,
  313. lastLoginTime: null,
  314. lastLoginIp: null,
  315. state: 1,
  316. createdBy: testUser.id
  317. });
  318. await supplierRepository.save(userSupplier2);
  319. // 管理员应该能看到所有供应商
  320. const response = await client.index.$get({
  321. query: {}
  322. }, {
  323. headers: {
  324. 'Authorization': `Bearer ${adminToken}`
  325. }
  326. });
  327. expect(response.status).toBe(200);
  328. const data = await response.json();
  329. if (data && 'data' in data) {
  330. expect(Array.isArray(data.data)).toBe(true);
  331. expect(data.data.length).toBeGreaterThanOrEqual(2); // 至少包含我们创建的两个供应商
  332. }
  333. });
  334. it('管理员应该可以更新其他用户的供应商', async () => {
  335. // 先为测试用户创建一个供应商
  336. const dataSource = await IntegrationTestDatabase.getDataSource();
  337. const supplierRepository = dataSource.getRepository(SupplierMt);
  338. const testSupplier = supplierRepository.create({
  339. tenantId: 1,
  340. name: '原始供应商',
  341. username: `original_supplier_admin_${Math.floor(Math.random() * 100000)}`,
  342. password: 'password123',
  343. phone: '13800138004',
  344. realname: '原始供应商',
  345. loginNum: 0,
  346. loginTime: null,
  347. loginIp: null,
  348. lastLoginTime: null,
  349. lastLoginIp: null,
  350. state: 1,
  351. createdBy: testUser.id
  352. });
  353. await supplierRepository.save(testSupplier);
  354. const updateData = {
  355. name: '管理员更新的供应商',
  356. phone: '13900139000',
  357. realname: '管理员更新的供应商'
  358. };
  359. const response = await client[':id'].$put({
  360. param: { id: testSupplier.id },
  361. json: updateData
  362. }, {
  363. headers: {
  364. 'Authorization': `Bearer ${adminToken}`
  365. }
  366. });
  367. console.debug('管理员更新其他用户供应商响应状态:', response.status);
  368. expect(response.status).toBe(200);
  369. if (response.status === 200) {
  370. const data = await response.json();
  371. expect(data.name).toBe(updateData.name);
  372. expect(data.phone).toBe(updateData.phone);
  373. expect(data.realname).toBe(updateData.realname);
  374. }
  375. });
  376. it('管理员应该可以删除其他用户的供应商', async () => {
  377. // 先为测试用户创建一个供应商
  378. const dataSource = await IntegrationTestDatabase.getDataSource();
  379. const supplierRepository = dataSource.getRepository(SupplierMt);
  380. const testSupplier = supplierRepository.create({
  381. tenantId: 1,
  382. name: '待删除供应商',
  383. username: `delete_supplier_admin_${Math.floor(Math.random() * 100000)}`,
  384. password: 'password123',
  385. phone: '13800138005',
  386. realname: '待删除供应商',
  387. loginNum: 0,
  388. loginTime: null,
  389. loginIp: null,
  390. lastLoginTime: null,
  391. lastLoginIp: null,
  392. state: 1,
  393. createdBy: testUser.id
  394. });
  395. await supplierRepository.save(testSupplier);
  396. const response = await client[':id'].$delete({
  397. param: { id: testSupplier.id }
  398. }, {
  399. headers: {
  400. 'Authorization': `Bearer ${adminToken}`
  401. }
  402. });
  403. console.debug('管理员删除其他用户供应商响应状态:', response.status);
  404. expect(response.status).toBe(204);
  405. // 验证供应商确实被删除
  406. const deletedSupplier = await supplierRepository.findOne({
  407. where: { id: testSupplier.id }
  408. });
  409. expect(deletedSupplier).toBeNull();
  410. });
  411. it('管理员应该可以查询指定用户的供应商', async () => {
  412. // 为测试用户创建一些供应商
  413. const dataSource = await IntegrationTestDatabase.getDataSource();
  414. const supplierRepository = dataSource.getRepository(SupplierMt);
  415. const userSupplier = supplierRepository.create({
  416. tenantId: 1,
  417. name: '指定用户供应商',
  418. username: `specified_user_supplier_${Math.floor(Math.random() * 100000)}`,
  419. password: 'password123',
  420. phone: '13800138006',
  421. realname: '指定用户供应商',
  422. loginNum: 0,
  423. loginTime: null,
  424. loginIp: null,
  425. lastLoginTime: null,
  426. lastLoginIp: null,
  427. state: 1,
  428. createdBy: testUser.id
  429. });
  430. await supplierRepository.save(userSupplier);
  431. // 管理员可以查询指定用户的供应商
  432. const response = await client.index.$get({
  433. query: { filters: JSON.stringify({ createdBy: testUser.id }) }
  434. }, {
  435. headers: {
  436. 'Authorization': `Bearer ${adminToken}`
  437. }
  438. });
  439. expect(response.status).toBe(200);
  440. const data = await response.json();
  441. if (data && 'data' in data) {
  442. expect(Array.isArray(data.data)).toBe(true);
  443. // 验证返回的供应商都属于指定用户
  444. if (data.data.length > 0) {
  445. data.data.forEach((supplier: any) => {
  446. expect(supplier.createdBy).toBe(testUser.id);
  447. });
  448. }
  449. }
  450. });
  451. });
  452. describe('供应商状态管理', () => {
  453. it('应该支持供应商状态管理', async () => {
  454. // 创建启用状态的供应商
  455. const createData = {
  456. name: '状态测试供应商',
  457. username: `status_test_supplier_${Math.floor(Math.random() * 100000)}`,
  458. password: 'password123',
  459. phone: '13800138007',
  460. realname: '状态测试供应商',
  461. state: 1 // 启用状态
  462. };
  463. const createResponse = await client.index.$post({
  464. json: createData
  465. }, {
  466. headers: {
  467. 'Authorization': `Bearer ${adminToken}`
  468. }
  469. });
  470. expect(createResponse.status).toBe(201);
  471. const createdData = await createResponse.json();
  472. // 更新为禁用状态
  473. if (typeof createdData === 'object' && createdData !== null && 'id' in createdData) {
  474. const updateResponse = await client[':id'].$put({
  475. param: { id: createdData.id },
  476. json: { state: 2 } // 禁用状态
  477. }, {
  478. headers: {
  479. 'Authorization': `Bearer ${adminToken}`
  480. }
  481. });
  482. expect(updateResponse.status).toBe(200);
  483. const updatedData = await updateResponse.json();
  484. if (typeof updatedData === 'object' && updatedData !== null && 'state' in updatedData) {
  485. expect(updatedData.state).toBe(2);
  486. }
  487. }
  488. });
  489. });
  490. describe('供应商登录统计', () => {
  491. it('应该支持供应商登录统计功能', async () => {
  492. // 创建供应商
  493. const createData = {
  494. name: '登录统计供应商',
  495. username: `login_stat_supplier_${Math.floor(Math.random() * 100000)}`,
  496. password: 'password123',
  497. phone: '13800138008',
  498. realname: '登录统计供应商',
  499. state: 1
  500. };
  501. const createResponse = await client.index.$post({
  502. json: createData
  503. }, {
  504. headers: {
  505. 'Authorization': `Bearer ${adminToken}`
  506. }
  507. });
  508. expect(createResponse.status).toBe(201);
  509. const createdData = await createResponse.json();
  510. // 验证初始登录统计
  511. if (typeof createdData === 'object' && createdData !== null) {
  512. if ('loginNum' in createdData) expect(createdData.loginNum).toBe(0);
  513. if ('loginTime' in createdData) expect(createdData.loginTime).toBeNull();
  514. if ('lastLoginTime' in createdData) expect(createdData.lastLoginTime).toBeNull();
  515. if ('loginIp' in createdData) expect(createdData.loginIp).toBeNull();
  516. if ('lastLoginIp' in createdData) expect(createdData.lastLoginIp).toBeNull();
  517. }
  518. // 获取供应商详情验证字段存在
  519. if (typeof createdData === 'object' && createdData !== null && 'id' in createdData) {
  520. const getResponse = await client[':id'].$get({
  521. param: { id: createdData.id }
  522. }, {
  523. headers: {
  524. 'Authorization': `Bearer ${adminToken}`
  525. }
  526. });
  527. expect(getResponse.status).toBe(200);
  528. const supplierData = await getResponse.json();
  529. if (typeof supplierData === 'object' && supplierData !== null) {
  530. expect(supplierData).toHaveProperty('loginNum');
  531. expect(supplierData).toHaveProperty('loginTime');
  532. expect(supplierData).toHaveProperty('lastLoginTime');
  533. expect(supplierData).toHaveProperty('loginIp');
  534. expect(supplierData).toHaveProperty('lastLoginIp');
  535. }
  536. }
  537. });
  538. });
  539. describe('跨租户数据隔离', () => {
  540. let tenant2AdminToken: string;
  541. let tenant2Admin: UserEntityMt;
  542. beforeEach(async () => {
  543. // 获取数据源
  544. const dataSource = await IntegrationTestDatabase.getDataSource();
  545. const userRepository = dataSource.getRepository(UserEntityMt);
  546. // 创建租户2的管理员
  547. tenant2Admin = userRepository.create({
  548. username: `tenant2_admin_${Math.floor(Math.random() * 100000)}`,
  549. password: 'admin_password',
  550. nickname: '租户2管理员',
  551. registrationSource: 'web',
  552. tenantId: 2
  553. });
  554. await userRepository.save(tenant2Admin);
  555. // 生成租户2管理员的token
  556. tenant2AdminToken = JWTUtil.generateToken({
  557. id: tenant2Admin.id,
  558. username: tenant2Admin.username,
  559. roles: [{name:'admin'}],
  560. tenantId: tenant2Admin.tenantId
  561. });
  562. // 为租户1创建一些供应商
  563. const supplierRepository = dataSource.getRepository(SupplierMt);
  564. const tenant1Supplier1 = supplierRepository.create({
  565. name: '租户1供应商1',
  566. username: `tenant1_supplier1_${Math.floor(Math.random() * 100000)}`,
  567. password: 'password123',
  568. phone: '13800138009',
  569. realname: '租户1供应商1',
  570. state: 1,
  571. tenantId: 1,
  572. createdBy: testUser.id
  573. });
  574. await supplierRepository.save(tenant1Supplier1);
  575. const tenant1Supplier2 = supplierRepository.create({
  576. name: '租户1供应商2',
  577. username: `tenant1_supplier2_${Math.floor(Math.random() * 100000)}`,
  578. password: 'password123',
  579. phone: '13800138010',
  580. realname: '租户1供应商2',
  581. state: 1,
  582. tenantId: 1,
  583. createdBy: testUser.id
  584. });
  585. await supplierRepository.save(tenant1Supplier2);
  586. // 为租户2创建一些供应商
  587. const tenant2Supplier1 = supplierRepository.create({
  588. name: '租户2供应商1',
  589. username: `tenant2_supplier1_${Math.floor(Math.random() * 100000)}`,
  590. password: 'password123',
  591. phone: '13800138011',
  592. realname: '租户2供应商1',
  593. state: 1,
  594. tenantId: 2,
  595. createdBy: tenant2Admin.id
  596. });
  597. await supplierRepository.save(tenant2Supplier1);
  598. });
  599. it('应该实现租户数据隔离 - 租户1管理员只能看到租户1的供应商', async () => {
  600. const response = await client.index.$get({
  601. query: {}
  602. }, {
  603. headers: {
  604. 'Authorization': `Bearer ${adminToken}`
  605. }
  606. });
  607. expect(response.status).toBe(200);
  608. const data = await response.json();
  609. // 验证租户1管理员只能看到租户1的供应商
  610. if (data.data && Array.isArray(data.data)) {
  611. data.data.forEach((supplier: any) => {
  612. expect(supplier.tenantId).toBe(1);
  613. });
  614. }
  615. });
  616. it('应该实现租户数据隔离 - 租户2管理员只能看到租户2的供应商', async () => {
  617. const response = await client.index.$get({
  618. query: {}
  619. }, {
  620. headers: {
  621. 'Authorization': `Bearer ${tenant2AdminToken}`
  622. }
  623. });
  624. expect(response.status).toBe(200);
  625. const data = await response.json();
  626. // 验证租户2管理员只能看到租户2的供应商
  627. if (data.data && Array.isArray(data.data)) {
  628. data.data.forEach((supplier: any) => {
  629. expect(supplier.tenantId).toBe(2);
  630. });
  631. }
  632. });
  633. it('应该阻止跨租户访问供应商详情', async () => {
  634. // 获取租户1的供应商列表
  635. const listResponse = await client.index.$get({
  636. query: {}
  637. }, {
  638. headers: {
  639. 'Authorization': `Bearer ${adminToken}`
  640. }
  641. });
  642. expect(listResponse.status).toBe(200);
  643. const listData = await listResponse.json();
  644. if (listData.data && Array.isArray(listData.data) && listData.data.length > 0) {
  645. const tenant1SupplierId = listData.data[0].id;
  646. // 租户2管理员尝试访问租户1的供应商
  647. const getResponse = await client[':id'].$get({
  648. param: { id: tenant1SupplierId }
  649. }, {
  650. headers: {
  651. 'Authorization': `Bearer ${tenant2AdminToken}`
  652. }
  653. });
  654. // 跨租户访问应该返回404
  655. expect(getResponse.status).toBe(404);
  656. }
  657. });
  658. it('应该阻止跨租户更新供应商', async () => {
  659. // 获取租户1的供应商列表
  660. const listResponse = await client.index.$get({
  661. query: {}
  662. }, {
  663. headers: {
  664. 'Authorization': `Bearer ${adminToken}`
  665. }
  666. });
  667. expect(listResponse.status).toBe(200);
  668. const listData = await listResponse.json();
  669. if (listData.data && Array.isArray(listData.data) && listData.data.length > 0) {
  670. const tenant1SupplierId = listData.data[0].id;
  671. // 租户2管理员尝试更新租户1的供应商
  672. const updateResponse = await client[':id'].$put({
  673. param: { id: tenant1SupplierId },
  674. json: { name: '跨租户更新测试' }
  675. }, {
  676. headers: {
  677. 'Authorization': `Bearer ${tenant2AdminToken}`
  678. }
  679. });
  680. // 跨租户更新应该返回404
  681. expect(updateResponse.status).toBe(404);
  682. }
  683. });
  684. it('应该阻止跨租户删除供应商', async () => {
  685. // 获取租户1的供应商列表
  686. const listResponse = await client.index.$get({
  687. query: {}
  688. }, {
  689. headers: {
  690. 'Authorization': `Bearer ${adminToken}`
  691. }
  692. });
  693. expect(listResponse.status).toBe(200);
  694. const listData = await listResponse.json();
  695. if (listData.data && Array.isArray(listData.data) && listData.data.length > 0) {
  696. const tenant1SupplierId = listData.data[0].id;
  697. // 租户2管理员尝试删除租户1的供应商
  698. const deleteResponse = await client[':id'].$delete({
  699. param: { id: tenant1SupplierId }
  700. }, {
  701. headers: {
  702. 'Authorization': `Bearer ${tenant2AdminToken}`
  703. }
  704. });
  705. // 跨租户删除应该返回404
  706. expect(deleteResponse.status).toBe(404);
  707. }
  708. });
  709. });
  710. });