Explorar el Código

✅ test(payment): 完善支付相关测试用例

- 为测试用户添加固定openid字段,确保支付测试环境一致性
- 移除支付请求中冗余的openid参数,优化测试数据结构
- 添加新测试用例:验证系统拒绝没有绑定微信小程序(无openid)的用户进行支付
- 统一调整多个支付测试场景的请求参数格式
yourname hace 3 meses
padre
commit
ccee30d7c5
Se han modificado 1 ficheros con 39 adiciones y 8 borrados
  1. 39 8
      web/tests/integration/server/payment.integration.test.ts

+ 39 - 8
web/tests/integration/server/payment.integration.test.ts

@@ -40,8 +40,10 @@ describe('支付API集成测试', () => {
     const userService = new UserService(dataSource);
     const authService = new AuthService(userService);
 
-    // 创建测试用户
-    testUser = await TestDataFactory.createTestUser(dataSource);
+    // 创建测试用户,确保有openid字段
+    testUser = await TestDataFactory.createTestUser(dataSource, {
+      openid: 'oJy1-16IIG18XZLl7G32k1hHMUFg'
+    });
 
     // 生成测试用户的token
     testToken = authService.generateToken(testUser);
@@ -91,8 +93,7 @@ describe('支付API集成测试', () => {
         json: {
           orderId: testOrder.id,
           totalAmount: 20000, // 200元,单位分
-          description: '测试支付订单',
-          openid: 'oJy1-16IIG18XZLl7G32k1hHMUFg'
+          description: '测试支付订单'
         },
       },
         {
@@ -199,6 +200,38 @@ describe('支付API集成测试', () => {
         expect(result.message).toContain('订单支付状态不正确');
       }
     });
+
+    it('应该拒绝没有openid的用户支付', async () => {
+      // 创建没有openid的测试用户
+      const dataSource = await IntegrationTestDatabase.getDataSource();
+      const userService = new UserService(dataSource);
+      const authService = new AuthService(userService);
+
+      const userWithoutOpenid = await TestDataFactory.createTestUser(dataSource, {
+        openid: null
+      });
+
+      const tokenWithoutOpenid = authService.generateToken(userWithoutOpenid);
+
+      const response = await client.payment.$post({
+        json: {
+          orderId: testOrder.id,
+          totalAmount: 20000,
+          description: '测试支付订单'
+        },
+      },
+        {
+          headers: {
+            'Authorization': `Bearer ${tokenWithoutOpenid}`
+          }
+        });
+
+      expect(response.status).toBe(400);
+      if (response.status === 400) {
+        const result = await response.json();
+        expect(result.message).toContain('用户未绑定微信小程序');
+      }
+    });
   });
 
   describe('POST /payment/callback - 支付回调', () => {
@@ -297,8 +330,7 @@ describe('支付API集成测试', () => {
         json: {
           orderId: testOrder.id,
           totalAmount: 20000,
-          description: '测试支付订单',
-          openid: 'oJy1-16IIG18XZLl7G32k1hHMUFg'
+          description: '测试支付订单'
         },
       },
         {
@@ -326,8 +358,7 @@ describe('支付API集成测试', () => {
         json: {
           orderId: testOrder.id,
           totalAmount: 20000,
-          description: '测试支付订单',
-          openid: 'oJy1-16IIG18XZLl7G32k1hHMUFg'
+          description: '测试支付订单'
         },
       },
         {