|
|
@@ -37,7 +37,7 @@ describe('用户商品管理API集成测试', () => {
|
|
|
|
|
|
// 使用测试工厂创建测试数据
|
|
|
testUser = await testFactory.createTestUser();
|
|
|
- otherUser = await testFactory.createTestUser({ nickname: '其他用户' });
|
|
|
+ otherUser = await testFactory.createTestUser(1, { nickname: '其他用户' });
|
|
|
testCategory = await testFactory.createTestCategory(testUser.id);
|
|
|
testSupplier = await testFactory.createTestSupplier(testUser.id);
|
|
|
testMerchant = await testFactory.createTestMerchant(testUser.id);
|
|
|
@@ -507,4 +507,154 @@ describe('用户商品管理API集成测试', () => {
|
|
|
}
|
|
|
});
|
|
|
});
|
|
|
+
|
|
|
+ describe('多租户数据隔离测试', () => {
|
|
|
+ it('应该验证不同租户间的数据完全隔离', async () => {
|
|
|
+ // 创建租户2的用户和商品
|
|
|
+ const tenant2User = await testFactory.createTestUser(2, {
|
|
|
+ username: 'tenant2_user',
|
|
|
+ nickname: '租户2用户'
|
|
|
+ });
|
|
|
+
|
|
|
+ // 为租户2创建分类、供应商、商户
|
|
|
+ const tenant2Category = await testFactory.createTestCategory(tenant2User.id, {
|
|
|
+ tenantId: 2,
|
|
|
+ name: '租户2分类'
|
|
|
+ });
|
|
|
+ const tenant2Supplier = await testFactory.createTestSupplier(tenant2User.id, {
|
|
|
+ tenantId: 2,
|
|
|
+ name: '租户2供应商',
|
|
|
+ username: 'tenant2_supplier'
|
|
|
+ });
|
|
|
+ const tenant2Merchant = await testFactory.createTestMerchant(tenant2User.id, {
|
|
|
+ tenantId: 2,
|
|
|
+ name: '租户2商户'
|
|
|
+ });
|
|
|
+
|
|
|
+ const tenant2Goods = await testFactory.createTestGoods(tenant2User.id, {
|
|
|
+ tenantId: 2,
|
|
|
+ name: '租户2商品',
|
|
|
+ price: 300.00,
|
|
|
+ costPrice: 240.00,
|
|
|
+ categoryId1: tenant2Category.id,
|
|
|
+ categoryId2: tenant2Category.id,
|
|
|
+ categoryId3: tenant2Category.id,
|
|
|
+ goodsType: 1,
|
|
|
+ supplierId: tenant2Supplier.id,
|
|
|
+ merchantId: tenant2Merchant.id,
|
|
|
+ state: 1,
|
|
|
+ stock: 50,
|
|
|
+ lowestBuy: 1
|
|
|
+ });
|
|
|
+
|
|
|
+ // 验证租户1用户无法访问租户2数据
|
|
|
+ const response = await client[':id'].$get({
|
|
|
+ param: { id: tenant2Goods.id }
|
|
|
+ }, {
|
|
|
+ headers: {
|
|
|
+ 'Authorization': `Bearer ${userToken}`
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ // 租户1用户应该无法访问租户2的商品
|
|
|
+ expect(response.status).toBe(404); // 或者403,取决于实现
|
|
|
+ });
|
|
|
+
|
|
|
+ it('应该验证租户1用户只能看到租户1的商品', async () => {
|
|
|
+ // 创建租户1的商品
|
|
|
+ const tenant1Goods = await testFactory.createTestGoods(testUser.id, {
|
|
|
+ tenantId: 1,
|
|
|
+ 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
|
|
|
+ });
|
|
|
+
|
|
|
+ // 创建租户2的商品
|
|
|
+ const tenant2User = await testFactory.createTestUser(2, {
|
|
|
+ username: 'tenant2_user_2',
|
|
|
+ nickname: '租户2用户2'
|
|
|
+ });
|
|
|
+ const tenant2Category = await testFactory.createTestCategory(tenant2User.id, {
|
|
|
+ tenantId: 2,
|
|
|
+ name: '租户2分类2'
|
|
|
+ });
|
|
|
+ const tenant2Supplier = await testFactory.createTestSupplier(tenant2User.id, {
|
|
|
+ tenantId: 2,
|
|
|
+ name: '租户2供应商2',
|
|
|
+ username: 'tenant2_supplier_2'
|
|
|
+ });
|
|
|
+ const tenant2Merchant = await testFactory.createTestMerchant(tenant2User.id, {
|
|
|
+ tenantId: 2,
|
|
|
+ name: '租户2商户2'
|
|
|
+ });
|
|
|
+
|
|
|
+ const tenant2Goods = await testFactory.createTestGoods(tenant2User.id, {
|
|
|
+ tenantId: 2,
|
|
|
+ name: '租户2商品2',
|
|
|
+ price: 400.00,
|
|
|
+ costPrice: 320.00,
|
|
|
+ categoryId1: tenant2Category.id,
|
|
|
+ categoryId2: tenant2Category.id,
|
|
|
+ categoryId3: tenant2Category.id,
|
|
|
+ goodsType: 1,
|
|
|
+ supplierId: tenant2Supplier.id,
|
|
|
+ merchantId: tenant2Merchant.id,
|
|
|
+ state: 1,
|
|
|
+ stock: 75,
|
|
|
+ lowestBuy: 1
|
|
|
+ });
|
|
|
+
|
|
|
+ console.debug('租户2商品创建成功:', tenant2Goods);
|
|
|
+
|
|
|
+ // 重新生成租户1用户的token,确保认证信息正确
|
|
|
+ const currentUserToken = JWTUtil.generateToken({
|
|
|
+ id: testUser.id,
|
|
|
+ username: testUser.username,
|
|
|
+ roles: [{name:'user'}],
|
|
|
+ tenantId: 1
|
|
|
+ });
|
|
|
+
|
|
|
+ console.debug('生成的token:', currentUserToken);
|
|
|
+ console.debug('Authorization头:', `Bearer ${currentUserToken}`);
|
|
|
+
|
|
|
+ // 获取租户1用户的商品列表
|
|
|
+ const response = await client.index.$get({}, {
|
|
|
+ headers: {
|
|
|
+ 'Authorization': `Bearer ${currentUserToken}`
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ console.debug('响应状态码:', response.status);
|
|
|
+ if (response.status !== 200) {
|
|
|
+ console.debug('响应内容:', await response.text());
|
|
|
+ }
|
|
|
+
|
|
|
+ expect(response.status).toBe(200);
|
|
|
+ const data = await response.json();
|
|
|
+
|
|
|
+ console.debug('API返回的商品数据:', data.data);
|
|
|
+
|
|
|
+ // 验证返回的商品都属于租户1
|
|
|
+ if (data.data && Array.isArray(data.data)) {
|
|
|
+ const allGoodsBelongToTenant1 = data.data.every((goods: any) => goods.tenantId === 1);
|
|
|
+ console.debug('所有商品都属于租户1:', allGoodsBelongToTenant1);
|
|
|
+ console.debug('商品租户ID列表:', data.data.map((g: any) => g.tenantId));
|
|
|
+ expect(allGoodsBelongToTenant1).toBe(true);
|
|
|
+
|
|
|
+ // 验证没有租户2的商品
|
|
|
+ const tenant2GoodsInResponse = data.data.filter((goods: any) => goods.tenantId === 2);
|
|
|
+ console.debug('租户2商品在响应中的数量:', tenant2GoodsInResponse.length);
|
|
|
+ expect(tenant2GoodsInResponse.length).toBe(0);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ });
|
|
|
});
|