Browse Source

test(e2e): SC-X-22 CF 值获取预估三类流用例(正常2/异常2/权限3,契约源自 RewardEstimateController,B类补写三流证据)

E2E Test Bot 2 hours ago
parent
commit
3fdbc65ae9
1 changed files with 77 additions and 0 deletions
  1. 77 0
      tests/e2e/api/reward-estimate-sc-x22.spec.js

+ 77 - 0
tests/e2e/api/reward-estimate-sc-x22.spec.js

@@ -0,0 +1,77 @@
+// 用例:SC-X-22 CF 值获取预估三类流(真实契约源自 RewardEstimateController 源码 §24~176,无需联网探针)
+// 端点:POST /api/reward/estimate(/api/** 由 JwtInterceptor 拦截,未登录 401)
+// 请求体:{ operationType, resourceType, resourceId, idempotencyKey };客户端禁传 expectedCf(服务端计算,传入被忽略)。
+// 角色闸门:孩子 token 调家长侧产出点 PURCHASE → 403;跨家庭 TASK/WISH → 403;规则未配置/超时 → 降级不 500。
+// 正常流:公开/模板资源(CHECKIN / GROWTH_TASK)无归属校验,旁路读即使降级仍 200;
+// 异常流:operationType/resourceType/resourceId 任一缺失 → 400 字段校验;
+// 权限流:无 token → 401;孩子调 PURCHASE → 403;孩子调公开资源 GROWTH_TASK → 200(对照允许)。
+// 断言纪律:不硬编码 expectedCf 具体数值(旁路读可降级为 null),仅断言类型合法;权限/异常用契约固定码。
+const { test, expect } = require('@playwright/test');
+const { registerRole, apiPost, uniqueSuffix } = require('../helpers/api');
+
+test.describe('SC-X-22 CF 值获取预估三类流', () => {
+  let parent;
+  let child;
+
+  test.beforeAll(async ({ request }) => {
+    parent = await registerRole(request, 'parent', 'QA-X22-P-' + uniqueSuffix());
+    child = await registerRole(request, 'child', 'QA-X22-C-' + uniqueSuffix());
+  });
+
+  // ===== 正常流(TC-X-22 / -E 前置)=====
+  test('[X-22 正常] 家长预估公开资源(CHECKIN) 返回 200 + currency=CF + op 回显', async ({ request }) => {
+    const r = await apiPost(request, '/api/reward/estimate',
+      { operationType: 'CHECKIN', resourceType: 'ACTIVITY_CHECKIN', resourceId: '1', idempotencyKey: 'x22-n1' },
+      parent.token);
+    expect(r.code).toBe(200);
+    expect(r.data).toBeTruthy();
+    expect(r.data.currency).toBe('CF');
+    expect(r.data.operationType).toBe('CHECKIN');
+    expect(r.data.resourceType).toBe('ACTIVITY_CHECKIN');
+    // expectedCf 为 Integer 或 null(旁路读降级),只断言类型合法,不锁具体数值
+    expect(r.data.expectedCf === null || typeof r.data.expectedCf === 'number').toBe(true);
+  });
+
+  test('[X-22 正常] 家长预估公开资源(GROWTH_TASK) 返回 200 且不 500', async ({ request }) => {
+    const r = await apiPost(request, '/api/reward/estimate',
+      { operationType: 'GROWTH_TASK', resourceType: 'GROWTH_TASK', resourceId: '1', idempotencyKey: 'x22-n2' },
+      parent.token);
+    expect(r.code).toBe(200);
+    expect(r.data).toBeTruthy();
+    expect(r.data.operationType).toBe('GROWTH_TASK');
+    expect(r.data.expectedCf === null || typeof r.data.expectedCf === 'number').toBe(true);
+  });
+
+  // ===== 异常流(TC-X-22-E)=====
+  test('[X-22 异常] 缺 operationType → 400 字段校验', async ({ request }) => {
+    const r = await apiPost(request, '/api/reward/estimate',
+      { resourceType: 'ACTIVITY_CHECKIN', resourceId: '1' }, parent.token);
+    expect(r.code).toBe(400);
+  });
+
+  test('[X-22 异常] 缺 resourceId → 400 字段校验(不 500)', async ({ request }) => {
+    const r = await apiPost(request, '/api/reward/estimate',
+      { operationType: 'CHECKIN', resourceType: 'ACTIVITY_CHECKIN' }, parent.token);
+    expect(r.code).toBe(400);
+  });
+
+  // ===== 权限流(TC-X-22-P)=====
+  test('[X-22 权限] 未登录(无 token) → 401 拦截', async ({ request }) => {
+    const r = await apiPost(request, '/api/reward/estimate',
+      { operationType: 'CHECKIN', resourceType: 'ACTIVITY_CHECKIN', resourceId: '1' });
+    expect(r.code).toBe(401);
+  });
+
+  test('[X-22 权限] 孩子调家长侧 PURCHASE → 403 拒绝(越权闸门)', async ({ request }) => {
+    const r = await apiPost(request, '/api/reward/estimate',
+      { operationType: 'PURCHASE', resourceType: 'PRODUCT_ORDER', resourceId: '1' }, child.token);
+    expect(r.code).toBe(403);
+    expect(/无权|孩子/.test(r.message || '')).toBe(true);
+  });
+
+  test('[X-22 权限] 孩子调公开资源 GROWTH_TASK → 200 允许(对照:非 PURCHASE 不 403)', async ({ request }) => {
+    const r = await apiPost(request, '/api/reward/estimate',
+      { operationType: 'GROWTH_TASK', resourceType: 'GROWTH_TASK', resourceId: '1' }, child.token);
+    expect(r.code).toBe(200);
+  });
+});