public-goods-routes.integration.test.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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 { publicGoodsRoutesMt } 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 publicGoodsRoutesMt>>;
  18. let testUser: UserEntityMt;
  19. let testCategory: GoodsCategoryMt;
  20. let testSupplier: SupplierMt;
  21. let testMerchant: MerchantMt;
  22. let testFactory: GoodsTestFactory;
  23. beforeEach(async () => {
  24. // 创建测试客户端
  25. client = testClient(publicGoodsRoutesMt);
  26. // 获取数据源并创建测试工厂
  27. const dataSource = await IntegrationTestDatabase.getDataSource();
  28. testFactory = new GoodsTestFactory(dataSource);
  29. // 使用测试工厂创建测试数据
  30. testUser = await testFactory.createTestUser();
  31. testCategory = await testFactory.createTestCategory(testUser.id);
  32. testSupplier = await testFactory.createTestSupplier(testUser.id);
  33. testMerchant = await testFactory.createTestMerchant(testUser.id);
  34. // 创建测试商品
  35. // 创建可用状态的商品
  36. const activeGoods1 = await testFactory.createTestGoods(testUser.id, {
  37. name: '可用商品1',
  38. price: 100.00,
  39. costPrice: 80.00,
  40. categoryId1: testCategory.id,
  41. categoryId2: testCategory.id,
  42. categoryId3: testCategory.id,
  43. supplierId: testSupplier.id,
  44. merchantId: testMerchant.id,
  45. state: 1,
  46. stock: 100
  47. });
  48. const activeGoods2 = await testFactory.createTestGoods(testUser.id, {
  49. name: '可用商品2',
  50. price: 200.00,
  51. costPrice: 160.00,
  52. categoryId1: testCategory.id,
  53. categoryId2: testCategory.id,
  54. categoryId3: testCategory.id,
  55. supplierId: testSupplier.id,
  56. merchantId: testMerchant.id,
  57. state: 1,
  58. stock: 50
  59. });
  60. const activeGoods3 = await testFactory.createTestGoods(testUser.id, {
  61. name: '可用商品3',
  62. price: 300.00,
  63. costPrice: 240.00,
  64. categoryId1: testCategory.id,
  65. categoryId2: testCategory.id,
  66. categoryId3: testCategory.id,
  67. goodsType: 2,
  68. supplierId: testSupplier.id,
  69. merchantId: testMerchant.id,
  70. state: 1,
  71. stock: 30
  72. });
  73. // 创建不可用状态的商品(不应该被公开路由返回)
  74. const inactiveGoods = await testFactory.createTestGoods(testUser.id, {
  75. name: '不可用商品',
  76. price: 400.00,
  77. costPrice: 320.00,
  78. categoryId1: testCategory.id,
  79. categoryId2: testCategory.id,
  80. categoryId3: testCategory.id,
  81. supplierId: testSupplier.id,
  82. merchantId: testMerchant.id,
  83. state: 2,
  84. stock: 10
  85. });
  86. });
  87. describe('GET /goods', () => {
  88. it('应该返回可用状态的商品列表', async () => {
  89. const response = await client.index.$get({
  90. query: {}
  91. });
  92. console.debug('公开商品列表响应状态:', response.status);
  93. expect(response.status).toBe(200);
  94. if (response.status === 200) {
  95. const data = await response.json();
  96. expect(data).toHaveProperty('data');
  97. expect(Array.isArray(data.data)).toBe(true);
  98. // 验证只返回可用状态的商品
  99. data.data.forEach((goods: any) => {
  100. expect(goods.state).toBe(1);
  101. });
  102. // 验证不包含不可用状态的商品
  103. const inactiveGoodsInResponse = data.data.find((goods: any) => goods.state === 2);
  104. expect(inactiveGoodsInResponse).toBeUndefined();
  105. }
  106. });
  107. it('应该支持按名称搜索', async () => {
  108. const response = await client.index.$get({
  109. query: { keyword: '可用商品1' }
  110. });
  111. expect(response.status).toBe(200);
  112. if (response.status === 200) {
  113. const data = await response.json();
  114. expect(Array.isArray(data.data)).toBe(true);
  115. // 验证只返回匹配搜索的商品
  116. data.data.forEach((goods: any) => {
  117. expect(goods.name).toContain('可用商品1');
  118. expect(goods.state).toBe(1);
  119. });
  120. }
  121. });
  122. it('应该支持按分类过滤', async () => {
  123. const response = await client.index.$get({
  124. query: { filters: JSON.stringify({ categoryId1: testCategory.id }) }
  125. });
  126. expect(response.status).toBe(200);
  127. if (response.status === 200) {
  128. const data = await response.json();
  129. expect(Array.isArray(data.data)).toBe(true);
  130. // 验证只返回指定分类的商品
  131. data.data.forEach((goods: any) => {
  132. expect(goods.categoryId1).toBe(testCategory.id);
  133. expect(goods.state).toBe(1);
  134. });
  135. }
  136. });
  137. it('应该支持按商品类型过滤', async () => {
  138. const response = await client.index.$get({
  139. query: { filters: JSON.stringify({ goodsType: 2 }) }
  140. });
  141. expect(response.status).toBe(200);
  142. if (response.status === 200) {
  143. const data = await response.json();
  144. expect(Array.isArray(data.data)).toBe(true);
  145. // 验证只返回指定类型的商品
  146. data.data.forEach((goods: any) => {
  147. expect(goods.goodsType).toBe(2);
  148. expect(goods.state).toBe(1);
  149. });
  150. }
  151. });
  152. it('应该支持分页查询', async () => {
  153. const response = await client.index.$get({
  154. query: { page: 1, pageSize: 2 }
  155. });
  156. expect(response.status).toBe(200);
  157. if (response.status === 200) {
  158. const data = await response.json();
  159. expect(Array.isArray(data.data)).toBe(true);
  160. expect(data.data.length).toBeLessThanOrEqual(2);
  161. // 验证只返回可用状态的商品
  162. data.data.forEach((goods: any) => {
  163. expect(goods.state).toBe(1);
  164. });
  165. }
  166. });
  167. it('应该无需认证即可访问', async () => {
  168. const response = await client.index.$get({
  169. query: {}
  170. });
  171. expect(response.status).toBe(200);
  172. });
  173. });
  174. describe('GET /goods/:id', () => {
  175. it('应该返回指定商品的详情', async () => {
  176. // 先获取一个可用商品的ID
  177. const dataSource = await IntegrationTestDatabase.getDataSource();
  178. const goodsRepository = dataSource.getRepository(GoodsMt);
  179. const activeGoods = await goodsRepository.findOne({
  180. where: { state: 1 }
  181. });
  182. if (activeGoods) {
  183. const response = await client[':id'].$get({
  184. param: { id: activeGoods.id }
  185. });
  186. console.debug('公开商品详情响应状态:', response.status);
  187. expect(response.status).toBe(200);
  188. if (response.status === 200) {
  189. const data = await response.json();
  190. expect(data.id).toBe(activeGoods.id);
  191. expect(data.name).toBe(activeGoods.name);
  192. expect(data.state).toBe(1); // 确保是可用状态
  193. }
  194. }
  195. });
  196. it('应该拒绝访问不可用状态的商品', async () => {
  197. // 先获取一个不可用商品的ID
  198. const dataSource = await IntegrationTestDatabase.getDataSource();
  199. const goodsRepository = dataSource.getRepository(GoodsMt);
  200. const inactiveGoods = await goodsRepository.findOne({
  201. where: { state: 2 }
  202. });
  203. if (inactiveGoods) {
  204. const response = await client[':id'].$get({
  205. param: { id: inactiveGoods.id }
  206. });
  207. expect(response.status).toBe(404); // 不可用状态的商品应该返回404
  208. }
  209. });
  210. it('应该处理不存在的商品', async () => {
  211. const response = await client[':id'].$get({
  212. param: { id: 999999 }
  213. });
  214. expect(response.status).toBe(404);
  215. });
  216. });
  217. describe('POST /goods', () => {
  218. it('应该拒绝创建商品操作', async () => {
  219. const createData = {
  220. name: '尝试创建商品',
  221. price: 100.00,
  222. categoryId1: testCategory.id,
  223. state: 1
  224. };
  225. const response = await client.index.$post({
  226. json: createData
  227. });
  228. expect(response.status).toBe(404); // 只读模式下路由不存在
  229. });
  230. });
  231. describe('PUT /goods/:id', () => {
  232. it('应该拒绝更新商品操作', async () => {
  233. // 先获取一个可用商品的ID
  234. const dataSource = await IntegrationTestDatabase.getDataSource();
  235. const goodsRepository = dataSource.getRepository(GoodsMt);
  236. const activeGoods = await goodsRepository.findOne({
  237. where: { state: 1 }
  238. });
  239. if (activeGoods) {
  240. const updateData = {
  241. name: '尝试更新商品'
  242. };
  243. const response = await client[':id'].$put({
  244. param: { id: activeGoods.id },
  245. json: updateData
  246. });
  247. expect(response.status).toBe(404); // 只读模式下路由不存在
  248. }
  249. });
  250. });
  251. describe('DELETE /goods/:id', () => {
  252. it('应该拒绝删除商品操作', async () => {
  253. // 先获取一个可用商品的ID
  254. const dataSource = await IntegrationTestDatabase.getDataSource();
  255. const goodsRepository = dataSource.getRepository(GoodsMt);
  256. const activeGoods = await goodsRepository.findOne({
  257. where: { state: 1 }
  258. });
  259. if (activeGoods) {
  260. const response = await client[':id'].$delete({
  261. param: { id: activeGoods.id }
  262. });
  263. expect(response.status).toBe(404); // 只读模式下路由不存在
  264. }
  265. });
  266. });
  267. describe('公开路由权限验证测试', () => {
  268. it('应该验证公开路由无需认证', async () => {
  269. const response = await client.index.$get({
  270. query: {}
  271. });
  272. expect(response.status).toBe(200);
  273. if (response.status === 200) {
  274. const data = await response.json();
  275. expect(Array.isArray(data.data)).toBe(true);
  276. // 验证只返回可用状态的商品
  277. data.data.forEach((goods: any) => {
  278. expect(goods.state).toBe(1);
  279. });
  280. }
  281. });
  282. it('应该验证只支持查询操作', async () => {
  283. // 测试所有非查询操作都应该被拒绝
  284. const createResponse = await client.index.$post({
  285. json: { name: '测试' }
  286. });
  287. expect(createResponse.status).toBe(404);
  288. const updateResponse = await client[':id'].$put({
  289. param: { id: 1 },
  290. json: { name: '测试' }
  291. });
  292. expect(updateResponse.status).toBe(404);
  293. const deleteResponse = await client[':id'].$delete({
  294. param: { id: 1 }
  295. });
  296. expect(deleteResponse.status).toBe(404);
  297. });
  298. });
  299. describe('商品关联关系测试', () => {
  300. it('应该正确加载商品关联关系', async () => {
  301. const response = await client.index.$get({
  302. query: {}
  303. });
  304. expect(response.status).toBe(200);
  305. if (response.status === 200) {
  306. const data = await response.json();
  307. expect(Array.isArray(data.data)).toBe(true);
  308. // 验证至少返回一个商品
  309. if (data.data.length > 0) {
  310. const goods = data.data[0];
  311. // 验证基础字段
  312. expect(goods).toHaveProperty('id');
  313. expect(goods).toHaveProperty('name');
  314. expect(goods).toHaveProperty('price');
  315. expect(goods).toHaveProperty('state', 1);
  316. // 验证关联关系字段存在
  317. expect(goods).toHaveProperty('category1');
  318. expect(goods).toHaveProperty('supplier');
  319. expect(goods).toHaveProperty('merchant');
  320. }
  321. }
  322. });
  323. });
  324. });