import { describe, it, expect, beforeEach } from 'vitest'; import { testClient } from 'hono/testing'; import { IntegrationTestDatabase, setupIntegrationDatabaseHooksWithEntities } from '@d8d/shared-test-util'; import { JWTUtil } from '@d8d/shared-utils'; import { UserEntity, Role } from '@d8d/user-module'; import { File } from '@d8d/file-module'; import { Supplier } from '@d8d/supplier-module'; import { Merchant } from '@d8d/merchant-module'; import { adminGoodsRoutes } from '../../src/routes'; import { Goods, GoodsCategory } from '../../src/entities'; // 设置集成测试钩子 setupIntegrationDatabaseHooksWithEntities([ UserEntity, Role, Goods, GoodsCategory, File, Supplier, Merchant ]) describe('管理员商品管理API集成测试', () => { let client: ReturnType>; let adminToken: string; let testUser: UserEntity; let testAdmin: UserEntity; let testCategory: GoodsCategory; let testSupplier: Supplier; let testMerchant: Merchant; beforeEach(async () => { // 创建测试客户端 client = testClient(adminGoodsRoutes); // 获取数据源 const dataSource = await IntegrationTestDatabase.getDataSource(); // 创建测试用户 const userRepository = dataSource.getRepository(UserEntity); testUser = userRepository.create({ username: `test_user_${Math.floor(Math.random() * 100000)}`, password: 'test_password', nickname: '测试用户', registrationSource: 'web' }); await userRepository.save(testUser); // 创建测试管理员用户 testAdmin = userRepository.create({ username: `test_admin_${Math.floor(Math.random() * 100000)}`, password: 'admin_password', nickname: '测试管理员', registrationSource: 'web' }); await userRepository.save(testAdmin); // 生成测试管理员的token adminToken = JWTUtil.generateToken({ id: testAdmin.id, username: testAdmin.username, roles: [{name:'admin'}] }); // 创建测试商品分类 const categoryRepository = dataSource.getRepository(GoodsCategory); testCategory = categoryRepository.create({ name: '测试分类', parentId: 0, level: 1, state: 1, createdBy: testUser.id }); await categoryRepository.save(testCategory); // 创建测试供应商 const supplierRepository = dataSource.getRepository(Supplier); testSupplier = supplierRepository.create({ name: '测试供应商', username: `test_supplier_${Math.floor(Math.random() * 100000)}`, password: 'password123', phone: '13800138000', realname: '测试供应商', state: 1, createdBy: testUser.id }); await supplierRepository.save(testSupplier); // 创建测试商户 const merchantRepository = dataSource.getRepository(Merchant); testMerchant = merchantRepository.create({ name: '测试商户', username: `test_merchant_${Math.floor(Math.random() * 100000)}`, password: 'password123', phone: '13800138001', realname: '测试商户', state: 1, createdBy: testUser.id }); await merchantRepository.save(testMerchant); }); describe('GET /goods', () => { it('应该返回所有商品列表', async () => { // 创建多个用户的商品 const dataSource = await IntegrationTestDatabase.getDataSource(); const goodsRepository = dataSource.getRepository(Goods); const userGoods1 = goodsRepository.create({ name: '用户商品1', price: 100.00, costPrice: 80.00, categoryId1: testCategory.id, categoryId2: testCategory.id, categoryId3: testCategory.id, goodsType: 1, supplierId: testSupplier.id, merchantId: testMerchant.id, state: 1, stock: 100, lowestBuy: 1, createdBy: testUser.id }); await goodsRepository.save(userGoods1); const userGoods2 = goodsRepository.create({ name: '用户商品2', price: 200.00, costPrice: 160.00, categoryId1: testCategory.id, categoryId2: testCategory.id, categoryId3: testCategory.id, goodsType: 1, supplierId: testSupplier.id, merchantId: testMerchant.id, state: 1, stock: 50, lowestBuy: 1, createdBy: testAdmin.id }); await goodsRepository.save(userGoods2); const response = await client.index.$get({ query: {} }, { headers: { 'Authorization': `Bearer ${adminToken}` } }); console.debug('管理员商品列表响应状态:', response.status); expect(response.status).toBe(200); if (response.status === 200) { const data = await response.json(); expect(data).toHaveProperty('data'); expect(Array.isArray(data.data)).toBe(true); // 验证返回所有用户的商品(管理员可以访问所有数据) const userGoodsCount = data.data.filter((goods: any) => goods.createdBy === testUser.id).length; const adminGoodsCount = data.data.filter((goods: any) => goods.createdBy === testAdmin.id).length; expect(userGoodsCount).toBeGreaterThan(0); expect(adminGoodsCount).toBeGreaterThan(0); } }); it('应该拒绝未认证用户的访问', async () => { const response = await client.index.$get({ query: {} }); expect(response.status).toBe(401); }); }); describe('POST /goods', () => { it('应该成功创建商品并可以指定权限', async () => { const createData = { name: '管理员创建商品', price: 150.00, costPrice: 120.00, categoryId1: testCategory.id, categoryId2: testCategory.id, categoryId3: testCategory.id, goodsType: 1, supplierId: testSupplier.id, merchantId: testMerchant.id, state: 1, stock: 80, lowestBuy: 1, createdBy: testUser.id // 管理员可以指定创建人 }; const response = await client.index.$post({ json: createData }, { headers: { 'Authorization': `Bearer ${adminToken}` } }); console.debug('管理员创建商品响应状态:', response.status); if (response.status !== 201) { const errorData = await response.json(); console.debug('管理员创建商品错误响应:', errorData); } expect(response.status).toBe(201); if (response.status === 201) { const data = await response.json(); expect(data).toHaveProperty('id'); expect(data.name).toBe(createData.name); expect(data.price).toBe(Number(createData.price)); expect(data.createdBy).toBe(testUser.id); // 验证可以指定创建人 } }); it('应该验证创建商品的必填字段', async () => { const invalidData = { // 缺少必填字段 name: '', price: -1, categoryId1: -1 }; const response = await client.index.$post({ json: invalidData }, { headers: { 'Authorization': `Bearer ${adminToken}` } }); expect(response.status).toBe(400); }); }); describe('GET /goods/:id', () => { it('应该返回指定商品的详情', async () => { // 先创建一个商品 const dataSource = await IntegrationTestDatabase.getDataSource(); const goodsRepository = dataSource.getRepository(Goods); const testGoods = goodsRepository.create({ name: '测试商品详情', price: 100.00, costPrice: 80.00, categoryId1: testCategory.id, categoryId2: testCategory.id, categoryId3: testCategory.id, goodsType: 1, supplierId: testSupplier.id, merchantId: testMerchant.id, state: 1, stock: 100, lowestBuy: 1, createdBy: testUser.id }); await goodsRepository.save(testGoods); const response = await client[':id'].$get({ param: { id: testGoods.id } }, { headers: { 'Authorization': `Bearer ${adminToken}` } }); console.debug('管理员商品详情响应状态:', response.status); expect(response.status).toBe(200); if (response.status === 200) { const data = await response.json(); expect(data.id).toBe(testGoods.id); expect(data.name).toBe(testGoods.name); expect(data.createdBy).toBe(testUser.id); } }); it('应该处理不存在的商品', async () => { const response = await client[':id'].$get({ param: { id: 999999 } }, { headers: { 'Authorization': `Bearer ${adminToken}` } }); expect(response.status).toBe(404); }); }); describe('PUT /goods/:id', () => { it('应该成功更新任何商品', async () => { // 先创建一个商品 const dataSource = await IntegrationTestDatabase.getDataSource(); const goodsRepository = dataSource.getRepository(Goods); const testGoods = goodsRepository.create({ name: '测试更新商品', price: 100.00, costPrice: 80.00, categoryId1: testCategory.id, categoryId2: testCategory.id, categoryId3: testCategory.id, goodsType: 1, supplierId: testSupplier.id, merchantId: testMerchant.id, state: 1, stock: 100, lowestBuy: 1, createdBy: testUser.id }); await goodsRepository.save(testGoods); const updateData = { name: '管理员更新后的商品名称', price: 120.00, state: 2, updatedBy: testAdmin.id // 管理员可以指定更新人 }; const response = await client[':id'].$put({ param: { id: testGoods.id }, json: updateData }, { headers: { 'Authorization': `Bearer ${adminToken}` } }); console.debug('管理员更新商品响应状态:', response.status); expect(response.status).toBe(200); if (response.status === 200) { const data = await response.json(); expect(data.name).toBe(updateData.name); expect(data.price).toBe(Number(updateData.price)); expect(data.state).toBe(updateData.state); expect(data.updatedBy).toBe(testAdmin.id); // 验证可以指定更新人 } }); }); describe('DELETE /goods/:id', () => { it('应该成功删除任何商品', async () => { // 先创建一个商品 const dataSource = await IntegrationTestDatabase.getDataSource(); const goodsRepository = dataSource.getRepository(Goods); const testGoods = goodsRepository.create({ name: '测试删除商品', price: 100.00, costPrice: 80.00, categoryId1: testCategory.id, categoryId2: testCategory.id, categoryId3: testCategory.id, goodsType: 1, supplierId: testSupplier.id, merchantId: testMerchant.id, state: 1, stock: 100, lowestBuy: 1, createdBy: testUser.id }); await goodsRepository.save(testGoods); const response = await client[':id'].$delete({ param: { id: testGoods.id } }, { headers: { 'Authorization': `Bearer ${adminToken}` } }); console.debug('管理员删除商品响应状态:', response.status); expect(response.status).toBe(204); }); }); describe('商品状态管理测试', () => { it('应该正确处理商品状态变更', async () => { const dataSource = await IntegrationTestDatabase.getDataSource(); const goodsRepository = dataSource.getRepository(Goods); // 创建可用状态的商品 const activeGoods = goodsRepository.create({ name: '可用商品', price: 100.00, costPrice: 80.00, categoryId1: testCategory.id, categoryId2: testCategory.id, categoryId3: testCategory.id, goodsType: 1, supplierId: testSupplier.id, merchantId: testMerchant.id, state: 1, stock: 100, lowestBuy: 1, createdBy: testUser.id }); await goodsRepository.save(activeGoods); // 创建不可用状态的商品 const inactiveGoods = goodsRepository.create({ name: '不可用商品', price: 200.00, costPrice: 160.00, categoryId1: testCategory.id, categoryId2: testCategory.id, categoryId3: testCategory.id, goodsType: 1, supplierId: testSupplier.id, merchantId: testMerchant.id, state: 2, stock: 50, lowestBuy: 1, createdBy: testUser.id }); await goodsRepository.save(inactiveGoods); // 验证状态过滤 const response = await client.index.$get({ query: { filters: JSON.stringify({ state: 1 }) } }, { headers: { 'Authorization': `Bearer ${adminToken}` } }); expect(response.status).toBe(200); const data = await response.json(); // 类型检查确保data属性存在 if ('data' in data && Array.isArray(data.data)) { // 应该只返回可用状态的商品 const activeGoodsInResponse = data.data.filter((goods: any) => goods.state === 1); const inactiveGoodsInResponse = data.data.filter((goods: any) => goods.state === 2); expect(activeGoodsInResponse.length).toBeGreaterThan(0); expect(inactiveGoodsInResponse.length).toBe(0); } else { // 如果响应是错误格式,应该失败 expect(data).toHaveProperty('data'); } }); }); describe('商品库存管理测试', () => { it('应该正确处理商品库存更新', async () => { const dataSource = await IntegrationTestDatabase.getDataSource(); const goodsRepository = dataSource.getRepository(Goods); // 创建一个商品 const testGoods = goodsRepository.create({ name: '库存测试商品', price: 100.00, costPrice: 80.00, categoryId1: testCategory.id, categoryId2: testCategory.id, categoryId3: testCategory.id, goodsType: 1, supplierId: testSupplier.id, merchantId: testMerchant.id, state: 1, stock: 100, lowestBuy: 1, createdBy: testUser.id }); await goodsRepository.save(testGoods); // 更新库存 const updateData = { stock: 50 }; const response = await client[':id'].$put({ param: { id: testGoods.id }, json: updateData }, { headers: { 'Authorization': `Bearer ${adminToken}` } }); expect(response.status).toBe(200); if (response.status === 200) { const data = await response.json(); expect(data.stock).toBe(Number(updateData.stock)); } }); }); describe('管理员权限验证测试', () => { it('应该验证管理员可以访问所有数据', async () => { const dataSource = await IntegrationTestDatabase.getDataSource(); const goodsRepository = dataSource.getRepository(Goods); // 创建多个用户的商品 const userGoods = goodsRepository.create({ name: '用户商品', price: 100.00, costPrice: 80.00, categoryId1: testCategory.id, categoryId2: testCategory.id, categoryId3: testCategory.id, goodsType: 1, supplierId: testSupplier.id, merchantId: testMerchant.id, state: 1, stock: 100, lowestBuy: 1, createdBy: testUser.id }); await goodsRepository.save(userGoods); const adminGoods = goodsRepository.create({ name: '管理员商品', price: 200.00, costPrice: 160.00, categoryId1: testCategory.id, categoryId2: testCategory.id, categoryId3: testCategory.id, goodsType: 1, supplierId: testSupplier.id, merchantId: testMerchant.id, state: 1, stock: 50, lowestBuy: 1, createdBy: testAdmin.id }); await goodsRepository.save(adminGoods); // 使用管理员token获取列表 const response = await client.index.$get({ query: {} }, { headers: { 'Authorization': `Bearer ${adminToken}` } }); expect(response.status).toBe(200); const data = await response.json(); // 类型检查确保data属性存在 if ('data' in data && Array.isArray(data.data)) { // 验证返回所有用户的商品 const userGoodsInResponse = data.data.filter((goods: any) => goods.createdBy === testUser.id); const adminGoodsInResponse = data.data.filter((goods: any) => goods.createdBy === testAdmin.id); expect(userGoodsInResponse.length).toBeGreaterThan(0); expect(adminGoodsInResponse.length).toBeGreaterThan(0); } else { // 如果响应是错误格式,应该失败 expect(data).toHaveProperty('data'); } }); }); });