user-goods-routes.integration.test.ts 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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 { SupplierMt } from '@d8d/supplier-module-mt';
  8. import { MerchantMt } from '@d8d/merchant-module-mt';
  9. import { userGoodsRoutesMt } from '../../src/routes/index.mt';
  10. import { GoodsMt, GoodsCategoryMt } from '../../src/entities/index.mt';
  11. import { GoodsTestFactory } from '../factories/goods-test-factory';
  12. // 设置集成测试钩子
  13. setupIntegrationDatabaseHooksWithEntities([
  14. UserEntityMt, RoleMt, GoodsMt, GoodsCategoryMt, FileMt, SupplierMt, MerchantMt
  15. ])
  16. describe('用户商品管理API集成测试', () => {
  17. let client: ReturnType<typeof testClient<typeof userGoodsRoutesMt>>;
  18. let userToken: string;
  19. let otherUserToken: string;
  20. let testUser: UserEntityMt;
  21. let otherUser: UserEntityMt;
  22. let testCategory: GoodsCategoryMt;
  23. let testSupplier: SupplierMt;
  24. let testMerchant: MerchantMt;
  25. let testFile: FileMt;
  26. let testFactory: GoodsTestFactory;
  27. beforeEach(async () => {
  28. // 创建测试客户端
  29. client = testClient(userGoodsRoutesMt);
  30. // 获取数据源并创建测试工厂
  31. const dataSource = await IntegrationTestDatabase.getDataSource();
  32. testFactory = new GoodsTestFactory(dataSource);
  33. // 使用测试工厂创建测试数据
  34. testUser = await testFactory.createTestUser();
  35. otherUser = await testFactory.createTestUser(1, { nickname: '其他用户' });
  36. testCategory = await testFactory.createTestCategory(testUser.id);
  37. testSupplier = await testFactory.createTestSupplier(testUser.id);
  38. testMerchant = await testFactory.createTestMerchant(testUser.id);
  39. // 生成测试用户的token
  40. userToken = JWTUtil.generateToken({
  41. id: testUser.id,
  42. username: testUser.username,
  43. roles: [{name:'user'}]
  44. });
  45. // 生成其他用户的token
  46. otherUserToken = JWTUtil.generateToken({
  47. id: otherUser.id,
  48. username: otherUser.username,
  49. roles: [{name:'user'}]
  50. });
  51. // 创建测试文件
  52. testFile = await testFactory.createTestFile(testUser.id);
  53. });
  54. describe('GET /goods', () => {
  55. it('应该返回当前用户的商品列表', async () => {
  56. // 为测试用户创建一些商品
  57. const userGoods1 = await testFactory.createTestGoods(testUser.id, {
  58. name: '用户商品1',
  59. price: 100.00,
  60. costPrice: 80.00,
  61. categoryId1: testCategory.id,
  62. categoryId2: testCategory.id,
  63. categoryId3: testCategory.id,
  64. goodsType: 1,
  65. supplierId: testSupplier.id,
  66. merchantId: testMerchant.id,
  67. imageFileId: testFile.id,
  68. state: 1,
  69. stock: 100,
  70. lowestBuy: 1
  71. });
  72. const userGoods2 = await testFactory.createTestGoods(testUser.id, {
  73. name: '用户商品2',
  74. price: 200.00,
  75. costPrice: 160.00,
  76. categoryId1: testCategory.id,
  77. categoryId2: testCategory.id,
  78. categoryId3: testCategory.id,
  79. goodsType: 1,
  80. supplierId: testSupplier.id,
  81. merchantId: testMerchant.id,
  82. imageFileId: testFile.id,
  83. state: 1,
  84. stock: 50,
  85. lowestBuy: 1
  86. });
  87. // 为其他用户创建一个商品,确保不会返回
  88. const otherUserGoods = await testFactory.createTestGoods(otherUser.id, {
  89. name: '其他用户商品',
  90. price: 300.00,
  91. costPrice: 240.00,
  92. categoryId1: testCategory.id,
  93. categoryId2: testCategory.id,
  94. categoryId3: testCategory.id,
  95. goodsType: 1,
  96. supplierId: testSupplier.id,
  97. merchantId: testMerchant.id,
  98. imageFileId: testFile.id,
  99. state: 1,
  100. stock: 30,
  101. lowestBuy: 1
  102. });
  103. const response = await client.index.$get({
  104. query: {
  105. page: 1,
  106. pageSize: 10
  107. }
  108. }, {
  109. headers: {
  110. 'Authorization': `Bearer ${userToken}`
  111. }
  112. });
  113. console.debug('用户商品列表响应状态:', response.status);
  114. if (response.status !== 200) {
  115. const errorData = await response.json();
  116. console.debug('用户商品列表错误响应:', errorData);
  117. }
  118. expect(response.status).toBe(200);
  119. if (response.status === 200) {
  120. const data = await response.json();
  121. expect(data).toHaveProperty('data');
  122. expect(Array.isArray(data.data)).toBe(true);
  123. // 验证只返回当前用户的商品
  124. data.data.forEach((goods: any) => {
  125. expect(goods.createdBy).toBe(testUser.id);
  126. });
  127. // 验证不包含其他用户的商品
  128. const otherUserGoodsInResponse = data.data.find((goods: any) => goods.createdBy === otherUser.id);
  129. expect(otherUserGoodsInResponse).toBeUndefined();
  130. }
  131. });
  132. it('应该拒绝未认证用户的访问', async () => {
  133. const response = await client.index.$get({
  134. query: {
  135. page: 1,
  136. pageSize: 10
  137. }
  138. });
  139. expect(response.status).toBe(401);
  140. });
  141. });
  142. describe('POST /goods', () => {
  143. it('应该成功创建商品并自动设置当前用户权限', async () => {
  144. const createData = {
  145. name: '用户创建商品',
  146. price: 150.00,
  147. costPrice: 120.00,
  148. categoryId1: testCategory.id,
  149. categoryId2: testCategory.id,
  150. categoryId3: testCategory.id,
  151. goodsType: 1,
  152. supplierId: testSupplier.id,
  153. merchantId: testMerchant.id,
  154. state: 1,
  155. stock: 80,
  156. lowestBuy: 1
  157. };
  158. const response = await client.index.$post({
  159. json: createData
  160. }, {
  161. headers: {
  162. 'Authorization': `Bearer ${userToken}`
  163. }
  164. });
  165. console.debug('用户创建商品响应状态:', response.status);
  166. if (response.status !== 201) {
  167. const errorData = await response.json();
  168. console.debug('用户创建商品错误响应:', errorData);
  169. }
  170. expect(response.status).toBe(201);
  171. if (response.status === 201) {
  172. const data = await response.json();
  173. expect(data).toHaveProperty('id');
  174. expect(data.name).toBe(createData.name);
  175. expect(data.price).toBe(Number(createData.price));
  176. expect(data.createdBy).toBe(testUser.id); // 验证自动设置当前用户权限
  177. }
  178. });
  179. it('应该验证创建商品的必填字段', async () => {
  180. const invalidData = {
  181. // 缺少必填字段
  182. name: '',
  183. price: -1,
  184. categoryId1: -1
  185. };
  186. const response = await client.index.$post({
  187. json: invalidData
  188. }, {
  189. headers: {
  190. 'Authorization': `Bearer ${userToken}`
  191. }
  192. });
  193. expect(response.status).toBe(400);
  194. });
  195. });
  196. describe('GET /goods/:id', () => {
  197. it('应该返回当前用户的商品详情', async () => {
  198. // 先为测试用户创建一个商品
  199. const testGoods = await testFactory.createTestGoods(testUser.id, {
  200. name: '测试用户商品详情',
  201. price: 100.00,
  202. costPrice: 80.00,
  203. categoryId1: testCategory.id,
  204. categoryId2: testCategory.id,
  205. categoryId3: testCategory.id,
  206. goodsType: 1,
  207. supplierId: testSupplier.id,
  208. merchantId: testMerchant.id,
  209. state: 1,
  210. stock: 100,
  211. lowestBuy: 1
  212. });
  213. const response = await client[':id'].$get({
  214. param: { id: testGoods.id }
  215. }, {
  216. headers: {
  217. 'Authorization': `Bearer ${userToken}`
  218. }
  219. });
  220. console.debug('用户商品详情响应状态:', response.status);
  221. if (response.status !== 200) {
  222. const errorData = await response.json();
  223. console.debug('用户商品详情错误响应:', errorData);
  224. }
  225. expect(response.status).toBe(200);
  226. if (response.status === 200) {
  227. const data = await response.json();
  228. expect(data.id).toBe(testGoods.id);
  229. expect(data.name).toBe(testGoods.name);
  230. expect(data.createdBy).toBe(testUser.id);
  231. }
  232. });
  233. it('应该拒绝访问其他用户的商品', async () => {
  234. // 为其他用户创建一个商品
  235. const otherUserGoods = await testFactory.createTestGoods(otherUser.id, {
  236. name: '其他用户商品',
  237. price: 100.00,
  238. costPrice: 80.00,
  239. categoryId1: testCategory.id,
  240. categoryId2: testCategory.id,
  241. categoryId3: testCategory.id,
  242. goodsType: 1,
  243. supplierId: testSupplier.id,
  244. merchantId: testMerchant.id,
  245. state: 1,
  246. stock: 100,
  247. lowestBuy: 1
  248. });
  249. const response = await client[':id'].$get({
  250. param: { id: otherUserGoods.id }
  251. }, {
  252. headers: {
  253. 'Authorization': `Bearer ${userToken}`
  254. }
  255. });
  256. expect(response.status).toBe(404); // 数据权限控制返回404(未找到,安全考虑)
  257. });
  258. it('应该处理不存在的商品', async () => {
  259. const response = await client[':id'].$get({
  260. param: { id: 999999 }
  261. }, {
  262. headers: {
  263. 'Authorization': `Bearer ${userToken}`
  264. }
  265. });
  266. expect(response.status).toBe(404);
  267. });
  268. });
  269. describe('PUT /goods/:id', () => {
  270. it('应该成功更新当前用户的商品', async () => {
  271. // 先为测试用户创建一个商品
  272. const testGoods = await testFactory.createTestGoods(testUser.id, {
  273. name: '测试更新商品',
  274. price: 100.00,
  275. costPrice: 80.00,
  276. categoryId1: testCategory.id,
  277. categoryId2: testCategory.id,
  278. categoryId3: testCategory.id,
  279. goodsType: 1,
  280. supplierId: testSupplier.id,
  281. merchantId: testMerchant.id,
  282. state: 1,
  283. stock: 100,
  284. lowestBuy: 1
  285. });
  286. const updateData = {
  287. name: '更新后的商品名称',
  288. price: 120.00,
  289. state: 2
  290. };
  291. const response = await client[':id'].$put({
  292. param: { id: testGoods.id },
  293. json: updateData
  294. }, {
  295. headers: {
  296. 'Authorization': `Bearer ${userToken}`
  297. }
  298. });
  299. console.debug('用户更新商品响应状态:', response.status);
  300. expect(response.status).toBe(200);
  301. if (response.status === 200) {
  302. const data = await response.json();
  303. expect(data.name).toBe(updateData.name);
  304. expect(data.price).toBe(Number(updateData.price));
  305. expect(data.state).toBe(updateData.state);
  306. expect(data.updatedBy).toBe(testUser.id); // 验证自动设置更新用户
  307. }
  308. });
  309. it('应该拒绝更新其他用户的商品', async () => {
  310. // 为其他用户创建一个商品
  311. const otherUserGoods = await testFactory.createTestGoods(otherUser.id, {
  312. name: '其他用户商品',
  313. price: 100.00,
  314. costPrice: 80.00,
  315. categoryId1: testCategory.id,
  316. categoryId2: testCategory.id,
  317. categoryId3: testCategory.id,
  318. goodsType: 1,
  319. supplierId: testSupplier.id,
  320. merchantId: testMerchant.id,
  321. state: 1,
  322. stock: 100,
  323. lowestBuy: 1
  324. });
  325. const updateData = {
  326. name: '尝试更新其他用户商品'
  327. };
  328. const response = await client[':id'].$put({
  329. param: { id: otherUserGoods.id },
  330. json: updateData
  331. }, {
  332. headers: {
  333. 'Authorization': `Bearer ${userToken}`
  334. }
  335. });
  336. expect(response.status).toBe(403); // 数据权限控制返回403
  337. });
  338. });
  339. describe('DELETE /goods/:id', () => {
  340. it('应该成功删除当前用户的商品', async () => {
  341. // 先为测试用户创建一个商品
  342. const testGoods = await testFactory.createTestGoods(testUser.id, {
  343. name: '测试删除商品',
  344. price: 100.00,
  345. costPrice: 80.00,
  346. categoryId1: testCategory.id,
  347. categoryId2: testCategory.id,
  348. categoryId3: testCategory.id,
  349. goodsType: 1,
  350. supplierId: testSupplier.id,
  351. merchantId: testMerchant.id,
  352. state: 1,
  353. stock: 100,
  354. lowestBuy: 1
  355. });
  356. const response = await client[':id'].$delete({
  357. param: { id: testGoods.id }
  358. }, {
  359. headers: {
  360. 'Authorization': `Bearer ${userToken}`
  361. }
  362. });
  363. console.debug('用户删除商品响应状态:', response.status);
  364. expect(response.status).toBe(204);
  365. });
  366. it('应该拒绝删除其他用户的商品', async () => {
  367. // 为其他用户创建一个商品
  368. const otherUserGoods = await testFactory.createTestGoods(otherUser.id, {
  369. name: '其他用户商品',
  370. price: 100.00,
  371. costPrice: 80.00,
  372. categoryId1: testCategory.id,
  373. categoryId2: testCategory.id,
  374. categoryId3: testCategory.id,
  375. goodsType: 1,
  376. supplierId: testSupplier.id,
  377. merchantId: testMerchant.id,
  378. state: 1,
  379. stock: 100,
  380. lowestBuy: 1
  381. });
  382. const response = await client[':id'].$delete({
  383. param: { id: otherUserGoods.id }
  384. }, {
  385. headers: {
  386. 'Authorization': `Bearer ${userToken}`
  387. }
  388. });
  389. expect(response.status).toBe(403); // 数据权限控制返回403
  390. });
  391. });
  392. describe('数据权限配置测试', () => {
  393. it('应该验证dataPermission配置正确工作', async () => {
  394. // 这个测试验证数据权限配置是否正常工作
  395. // 用户只能访问自己创建的商品
  396. // 创建测试用户和其他用户的商品
  397. const userGoods = await testFactory.createTestGoods(testUser.id, {
  398. name: '用户商品',
  399. price: 100.00,
  400. costPrice: 80.00,
  401. categoryId1: testCategory.id,
  402. categoryId2: testCategory.id,
  403. categoryId3: testCategory.id,
  404. goodsType: 1,
  405. supplierId: testSupplier.id,
  406. merchantId: testMerchant.id,
  407. state: 1,
  408. stock: 100,
  409. lowestBuy: 1
  410. });
  411. const otherUserGoods = await testFactory.createTestGoods(otherUser.id, {
  412. name: '其他用户商品',
  413. price: 200.00,
  414. costPrice: 160.00,
  415. categoryId1: testCategory.id,
  416. categoryId2: testCategory.id,
  417. categoryId3: testCategory.id,
  418. goodsType: 1,
  419. supplierId: testSupplier.id,
  420. merchantId: testMerchant.id,
  421. state: 1,
  422. stock: 50,
  423. lowestBuy: 1
  424. });
  425. // 使用测试用户token获取列表
  426. const response = await client.index.$get({
  427. query: {
  428. page: 1,
  429. pageSize: 10
  430. }
  431. }, {
  432. headers: {
  433. 'Authorization': `Bearer ${userToken}`
  434. }
  435. });
  436. if (response.status !== 200) {
  437. const errorData = await response.json();
  438. console.debug('数据权限配置测试错误响应:', errorData);
  439. }
  440. expect(response.status).toBe(200);
  441. const data = await response.json();
  442. // 类型检查确保data属性存在
  443. if ('data' in data && Array.isArray(data.data)) {
  444. // 验证只返回测试用户的商品
  445. const userGoodsInResponse = data.data.filter((goods: any) => goods.createdBy === testUser.id);
  446. const otherUserGoodsInResponse = data.data.filter((goods: any) => goods.createdBy === otherUser.id);
  447. expect(userGoodsInResponse.length).toBeGreaterThan(0);
  448. expect(otherUserGoodsInResponse.length).toBe(0);
  449. } else {
  450. // 如果响应是错误格式,应该失败
  451. expect(data).toHaveProperty('data');
  452. }
  453. });
  454. });
  455. describe('多租户数据隔离测试', () => {
  456. it('应该验证不同租户间的数据完全隔离', async () => {
  457. // 创建租户2的用户和商品
  458. const tenant2User = await testFactory.createTestUser(2, {
  459. username: 'tenant2_user',
  460. nickname: '租户2用户'
  461. });
  462. // 为租户2创建分类、供应商、商户
  463. const tenant2Category = await testFactory.createTestCategory(tenant2User.id, {
  464. tenantId: 2,
  465. name: '租户2分类'
  466. });
  467. const tenant2Supplier = await testFactory.createTestSupplier(tenant2User.id, {
  468. tenantId: 2,
  469. name: '租户2供应商',
  470. username: 'tenant2_supplier'
  471. });
  472. const tenant2Merchant = await testFactory.createTestMerchant(tenant2User.id, {
  473. tenantId: 2,
  474. name: '租户2商户'
  475. });
  476. const tenant2Goods = await testFactory.createTestGoods(tenant2User.id, {
  477. tenantId: 2,
  478. name: '租户2商品',
  479. price: 300.00,
  480. costPrice: 240.00,
  481. categoryId1: tenant2Category.id,
  482. categoryId2: tenant2Category.id,
  483. categoryId3: tenant2Category.id,
  484. goodsType: 1,
  485. supplierId: tenant2Supplier.id,
  486. merchantId: tenant2Merchant.id,
  487. state: 1,
  488. stock: 50,
  489. lowestBuy: 1
  490. });
  491. // 验证租户1用户无法访问租户2数据
  492. const response = await client[':id'].$get({
  493. param: { id: tenant2Goods.id }
  494. }, {
  495. headers: {
  496. 'Authorization': `Bearer ${userToken}`
  497. }
  498. });
  499. // 租户1用户应该无法访问租户2的商品
  500. expect(response.status).toBe(404); // 或者403,取决于实现
  501. });
  502. it('应该验证租户1用户只能看到租户1的商品', async () => {
  503. // 创建租户1的商品
  504. const tenant1Goods = await testFactory.createTestGoods(testUser.id, {
  505. tenantId: 1,
  506. name: '租户1商品',
  507. price: 100.00,
  508. costPrice: 80.00,
  509. categoryId1: testCategory.id,
  510. categoryId2: testCategory.id,
  511. categoryId3: testCategory.id,
  512. goodsType: 1,
  513. supplierId: testSupplier.id,
  514. merchantId: testMerchant.id,
  515. state: 1,
  516. stock: 100,
  517. lowestBuy: 1
  518. });
  519. // 创建租户2的商品
  520. const tenant2User = await testFactory.createTestUser(2, {
  521. username: 'tenant2_user_2',
  522. nickname: '租户2用户2'
  523. });
  524. const tenant2Category = await testFactory.createTestCategory(tenant2User.id, {
  525. tenantId: 2,
  526. name: '租户2分类2'
  527. });
  528. const tenant2Supplier = await testFactory.createTestSupplier(tenant2User.id, {
  529. tenantId: 2,
  530. name: '租户2供应商2',
  531. username: 'tenant2_supplier_2'
  532. });
  533. const tenant2Merchant = await testFactory.createTestMerchant(tenant2User.id, {
  534. tenantId: 2,
  535. name: '租户2商户2'
  536. });
  537. const tenant2Goods = await testFactory.createTestGoods(tenant2User.id, {
  538. tenantId: 2,
  539. name: '租户2商品2',
  540. price: 400.00,
  541. costPrice: 320.00,
  542. categoryId1: tenant2Category.id,
  543. categoryId2: tenant2Category.id,
  544. categoryId3: tenant2Category.id,
  545. goodsType: 1,
  546. supplierId: tenant2Supplier.id,
  547. merchantId: tenant2Merchant.id,
  548. state: 1,
  549. stock: 75,
  550. lowestBuy: 1
  551. });
  552. console.debug('租户2商品创建成功:', tenant2Goods);
  553. // 重新生成租户1用户的token,确保认证信息正确
  554. const currentUserToken = JWTUtil.generateToken({
  555. id: testUser.id,
  556. username: testUser.username,
  557. roles: [{name:'user'}],
  558. tenantId: 1
  559. });
  560. console.debug('生成的token:', currentUserToken);
  561. console.debug('Authorization头:', `Bearer ${currentUserToken}`);
  562. // 获取租户1用户的商品列表
  563. const response = await client.index.$get({}, {
  564. headers: {
  565. 'Authorization': `Bearer ${currentUserToken}`
  566. }
  567. });
  568. console.debug('响应状态码:', response.status);
  569. if (response.status !== 200) {
  570. console.debug('响应内容:', await response.text());
  571. }
  572. expect(response.status).toBe(200);
  573. const data = await response.json();
  574. console.debug('API返回的商品数据:', data.data);
  575. // 验证返回的商品都属于租户1
  576. if (data.data && Array.isArray(data.data)) {
  577. const allGoodsBelongToTenant1 = data.data.every((goods: any) => goods.tenantId === 1);
  578. console.debug('所有商品都属于租户1:', allGoodsBelongToTenant1);
  579. console.debug('商品租户ID列表:', data.data.map((g: any) => g.tenantId));
  580. expect(allGoodsBelongToTenant1).toBe(true);
  581. // 验证没有租户2的商品
  582. const tenant2GoodsInResponse = data.data.filter((goods: any) => goods.tenantId === 2);
  583. console.debug('租户2商品在响应中的数量:', tenant2GoodsInResponse.length);
  584. expect(tenant2GoodsInResponse.length).toBe(0);
  585. }
  586. });
  587. });
  588. });