|
|
@@ -185,4 +185,185 @@ describe('多租户用户订单管理API集成测试', () => {
|
|
|
expect(createdOrder.userId).toBe(testUser.id);
|
|
|
});
|
|
|
});
|
|
|
+
|
|
|
+ describe('取消订单功能验证', () => {
|
|
|
+ it('应该成功取消未支付订单', async () => {
|
|
|
+ // 创建未支付订单
|
|
|
+ const order = await testFactory.createTestOrder(testUser.id, {
|
|
|
+ tenantId: 1,
|
|
|
+ payState: 0, // 未支付
|
|
|
+ state: 0
|
|
|
+ });
|
|
|
+
|
|
|
+ const cancelData = {
|
|
|
+ orderId: order.id,
|
|
|
+ reason: '用户主动取消'
|
|
|
+ };
|
|
|
+
|
|
|
+ const response = await client.cancelOrder.$post({
|
|
|
+ json: cancelData
|
|
|
+ }, {
|
|
|
+ headers: {
|
|
|
+ 'Authorization': `Bearer ${userToken}`
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ expect(response.status).toBe(200);
|
|
|
+ const result = await response.json();
|
|
|
+
|
|
|
+ expect(result.success).toBe(true);
|
|
|
+ expect(result.message).toBe('订单取消成功');
|
|
|
+
|
|
|
+ // 验证订单状态已更新
|
|
|
+ const dataSource = await IntegrationTestDatabase.getDataSource();
|
|
|
+ const updatedOrder = await dataSource.getRepository(OrderMt).findOne({
|
|
|
+ where: { id: order.id, tenantId: 1 }
|
|
|
+ });
|
|
|
+
|
|
|
+ expect(updatedOrder?.payState).toBe(5); // 订单关闭
|
|
|
+ expect(updatedOrder?.cancelReason).toBe('用户主动取消');
|
|
|
+ expect(updatedOrder?.cancelTime).toBeInstanceOf(Date);
|
|
|
+ });
|
|
|
+
|
|
|
+ it('应该成功取消已支付订单', async () => {
|
|
|
+ // 创建已支付订单
|
|
|
+ const order = await testFactory.createTestOrder(testUser.id, {
|
|
|
+ tenantId: 1,
|
|
|
+ payState: 2, // 支付成功
|
|
|
+ state: 0
|
|
|
+ });
|
|
|
+
|
|
|
+ const cancelData = {
|
|
|
+ orderId: order.id,
|
|
|
+ reason: '用户主动取消(已支付)'
|
|
|
+ };
|
|
|
+
|
|
|
+ const response = await client.cancelOrder.$post({
|
|
|
+ json: cancelData
|
|
|
+ }, {
|
|
|
+ headers: {
|
|
|
+ 'Authorization': `Bearer ${userToken}`
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ expect(response.status).toBe(200);
|
|
|
+ const result = await response.json();
|
|
|
+
|
|
|
+ expect(result.success).toBe(true);
|
|
|
+ expect(result.message).toBe('订单取消成功');
|
|
|
+
|
|
|
+ // 验证订单状态已更新
|
|
|
+ const dataSource = await IntegrationTestDatabase.getDataSource();
|
|
|
+ const updatedOrder = await dataSource.getRepository(OrderMt).findOne({
|
|
|
+ where: { id: order.id, tenantId: 1 }
|
|
|
+ });
|
|
|
+
|
|
|
+ expect(updatedOrder?.payState).toBe(5); // 订单关闭
|
|
|
+ expect(updatedOrder?.cancelReason).toBe('用户主动取消(已支付)');
|
|
|
+ expect(updatedOrder?.cancelTime).toBeInstanceOf(Date);
|
|
|
+ });
|
|
|
+
|
|
|
+ it('应该拒绝取消不允许的订单状态', async () => {
|
|
|
+ // 创建已发货订单(支付状态=2,订单状态=1)
|
|
|
+ const order = await testFactory.createTestOrder(testUser.id, {
|
|
|
+ tenantId: 1,
|
|
|
+ payState: 2, // 支付成功
|
|
|
+ state: 1 // 已发货
|
|
|
+ });
|
|
|
+
|
|
|
+ const cancelData = {
|
|
|
+ orderId: order.id,
|
|
|
+ reason: '尝试取消已发货订单'
|
|
|
+ };
|
|
|
+
|
|
|
+ const response = await client.cancelOrder.$post({
|
|
|
+ json: cancelData
|
|
|
+ }, {
|
|
|
+ headers: {
|
|
|
+ 'Authorization': `Bearer ${userToken}`
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ // 应该返回403,因为已发货订单不允许取消
|
|
|
+ expect(response.status).toBe(403);
|
|
|
+ const result = await response.json();
|
|
|
+
|
|
|
+ expect(result.error).toBe('当前订单状态不允许取消');
|
|
|
+ });
|
|
|
+
|
|
|
+ it('应该拒绝取消不存在的订单', async () => {
|
|
|
+ const cancelData = {
|
|
|
+ orderId: 99999, // 不存在的订单ID
|
|
|
+ reason: '取消不存在的订单'
|
|
|
+ };
|
|
|
+
|
|
|
+ const response = await client.cancelOrder.$post({
|
|
|
+ json: cancelData
|
|
|
+ }, {
|
|
|
+ headers: {
|
|
|
+ 'Authorization': `Bearer ${userToken}`
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ // 应该返回404
|
|
|
+ expect(response.status).toBe(404);
|
|
|
+ const result = await response.json();
|
|
|
+
|
|
|
+ expect(result.error).toBe('订单不存在');
|
|
|
+ });
|
|
|
+
|
|
|
+ it('应该拒绝跨租户取消订单', async () => {
|
|
|
+ // 创建租户2的订单
|
|
|
+ const otherTenantOrder = await testFactory.createTestOrder(otherTenantUser.id, {
|
|
|
+ tenantId: 2,
|
|
|
+ payState: 0
|
|
|
+ });
|
|
|
+
|
|
|
+ const cancelData = {
|
|
|
+ orderId: otherTenantOrder.id,
|
|
|
+ reason: '跨租户取消尝试'
|
|
|
+ };
|
|
|
+
|
|
|
+ const response = await client.cancelOrder.$post({
|
|
|
+ json: cancelData
|
|
|
+ }, {
|
|
|
+ headers: {
|
|
|
+ 'Authorization': `Bearer ${userToken}`
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ // 应该返回404,因为订单不在当前租户
|
|
|
+ expect(response.status).toBe(404);
|
|
|
+ const result = await response.json();
|
|
|
+
|
|
|
+ expect(result.error).toBe('订单不存在');
|
|
|
+ });
|
|
|
+
|
|
|
+ it('应该拒绝跨用户取消订单', async () => {
|
|
|
+ // 创建其他用户的订单(同一租户)
|
|
|
+ const otherUserOrder = await testFactory.createTestOrder(otherUser.id, {
|
|
|
+ tenantId: 1,
|
|
|
+ payState: 0
|
|
|
+ });
|
|
|
+
|
|
|
+ const cancelData = {
|
|
|
+ orderId: otherUserOrder.id,
|
|
|
+ reason: '跨用户取消尝试'
|
|
|
+ };
|
|
|
+
|
|
|
+ const response = await client.cancelOrder.$post({
|
|
|
+ json: cancelData
|
|
|
+ }, {
|
|
|
+ headers: {
|
|
|
+ 'Authorization': `Bearer ${userToken}`
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ // 应该返回404,因为无权访问其他用户的订单
|
|
|
+ expect(response.status).toBe(404);
|
|
|
+ const result = await response.json();
|
|
|
+
|
|
|
+ expect(result.error).toBe('订单不存在');
|
|
|
+ });
|
|
|
+ });
|
|
|
});
|