|
@@ -0,0 +1,115 @@
|
|
|
|
|
+/**
|
|
|
|
|
+ * SC-C-02 任务完成三类流(child.task.complete / S-cfc-12)
|
|
|
|
|
+ * ================================================================
|
|
|
|
|
+ * 正常流:家长下发任务「睡前阅读」→ 孩子打开待执行任务 → 点「完成」提交 → 状态待家长审核,完成动作写入记录;
|
|
|
|
|
+ * 异常流:同一任务并发双点「完成」→ 幂等仅计 1 次、仅 1 条流水;超期任务提交被拒;
|
|
|
|
|
+ * 权限流:孩子调用编辑/删除/切家长接口或拼家长路由 → role 校验拒绝(403/401),不越权操作家庭数据。
|
|
|
|
|
+ */
|
|
|
|
|
+const { test, expect } = require('@playwright/test');
|
|
|
|
|
+const { registerRole, apiPost, uniqueSuffix } = require('../helpers/api');
|
|
|
|
|
+
|
|
|
|
|
+test.describe('SC-C-02 任务完成三类流', () => {
|
|
|
|
|
+ let parent;
|
|
|
|
|
+ let childB;
|
|
|
|
|
+
|
|
|
|
|
+ test.beforeAll(async ({ request }) => {
|
|
|
|
|
+ parent = await registerRole(request, 'parent', 'QA-C02-P');
|
|
|
|
|
+ const memList = await apiPost(request, '/api/family/member/list', {}, parent.token);
|
|
|
|
|
+ expect(memList.code, memList.message).toBe(200);
|
|
|
|
|
+ const self = (memList.data || []).find((m) => String(m.userId) === String(parent.userId));
|
|
|
|
|
+ expect(self, '家长本人 member 记录').toBeTruthy();
|
|
|
|
|
+ const addChild = await apiPost(request, '/api/family/member/add', {
|
|
|
|
|
+ relativeMemberId: self.id,
|
|
|
|
|
+ generationLevel: 'child',
|
|
|
|
|
+ nickname: 'B-阅读' + uniqueSuffix(),
|
|
|
|
|
+ gender: 'male',
|
|
|
|
|
+ birthday: '2015-06-01',
|
|
|
|
|
+ }, parent.token);
|
|
|
|
|
+ expect(addChild.code, '添加孩子成员: ' + addChild.message).toBe(200);
|
|
|
|
|
+ childB = addChild.data.id;
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ test('[C-02 正常] 孩子完成待执行任务 → 状态待审核', async ({ request }) => {
|
|
|
|
|
+ // 家长下发任务
|
|
|
|
|
+ const created = await apiPost(request, '/api/tasks/create', {
|
|
|
|
|
+ title: '睡前阅读' + uniqueSuffix(),
|
|
|
|
|
+ points: 10,
|
|
|
|
|
+ memberId: childB,
|
|
|
|
|
+ frequency: 'daily',
|
|
|
|
|
+ needReview: 1,
|
|
|
|
|
+ }, parent.token);
|
|
|
|
|
+ expect(created.code, '创建任务: ' + created.message).toBe(200);
|
|
|
|
|
+ const taskId = created.data;
|
|
|
|
|
+ expect(taskId).toBeTruthy();
|
|
|
|
|
+
|
|
|
|
|
+ // 家长切换到孩子视角,获得孩子 token
|
|
|
|
|
+ const sw = await apiPost(request, '/api/family/member/switch', { userId: childB }, parent.token);
|
|
|
|
|
+ expect(sw.code, '切换到孩子: ' + sw.message).toBe(200);
|
|
|
|
|
+ const childToken = (sw.data && (sw.data.token || sw.data.tokenValue)) || sw.data;
|
|
|
|
|
+ expect(childToken, '切换应返回孩子 token').toBeTruthy();
|
|
|
|
|
+
|
|
|
|
|
+ // 孩子点「完成」
|
|
|
|
|
+ const done = await apiPost(request, '/api/tasks/' + taskId + '/complete', {
|
|
|
|
|
+ memberId: childB,
|
|
|
|
|
+ }, childToken);
|
|
|
|
|
+ expect(done.code, '完成任务: ' + done.message).toBe(200);
|
|
|
|
|
+ // 状态进入待家长审核
|
|
|
|
|
+ const today = await apiPost(request, '/api/tasks/today', { memberId: childB }, parent.token);
|
|
|
|
|
+ const hit = (today.data || []).find((t) => String(t.id) === String(taskId));
|
|
|
|
|
+ if (hit) {
|
|
|
|
|
+ expect(['pending_review', 'review'].includes(hit.status), '完成状态应为待审核: ' + hit.status).toBe(true);
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ test('[C-02-E 异常] 并发双点完成 → 幂等仅计 1 次', async ({ request }) => {
|
|
|
|
|
+ const created = await apiPost(request, '/api/tasks/create', {
|
|
|
|
|
+ title: '并发完成' + uniqueSuffix(),
|
|
|
|
|
+ points: 5,
|
|
|
|
|
+ memberId: childB,
|
|
|
|
|
+ frequency: 'daily',
|
|
|
|
|
+ needReview: 1,
|
|
|
|
|
+ }, parent.token);
|
|
|
|
|
+ const taskId = created.data;
|
|
|
|
|
+ const sw = await apiPost(request, '/api/family/member/switch', { userId: childB }, parent.token);
|
|
|
|
|
+ const childToken = (sw.data && (sw.data.token || sw.data.tokenValue)) || sw.data;
|
|
|
|
|
+
|
|
|
|
|
+ const [a, b] = await Promise.all([
|
|
|
|
|
+ apiPost(request, '/api/tasks/' + taskId + '/complete', { memberId: childB }, childToken),
|
|
|
|
|
+ apiPost(request, '/api/tasks/' + taskId + '/complete', { memberId: childB }, childToken),
|
|
|
|
|
+ ]);
|
|
|
|
|
+ // 契约:并发双点幂等,仅计 1 次(200 或业务拒绝,不得重复发奖导致 500)
|
|
|
|
|
+ expect(a.code).not.toBe(500);
|
|
|
|
|
+ expect(b.code).not.toBe(500);
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ test('[C-02-P 权限] 越权完成他人任务被拒', async ({ request }) => {
|
|
|
|
|
+ // 家庭2 家长 + 孩子,尝试完成家庭1 的任务
|
|
|
|
|
+ const outsider = await registerRole(request, 'parent', 'QA-C02-X');
|
|
|
|
|
+ const oList = await apiPost(request, '/api/family/member/list', {}, outsider.token);
|
|
|
|
|
+ const oSelf = (oList.data || []).find((m) => String(m.userId) === String(outsider.userId));
|
|
|
|
|
+ const oChild = await apiPost(request, '/api/family/member/add', {
|
|
|
|
|
+ relativeMemberId: oSelf.id,
|
|
|
|
|
+ generationLevel: 'child',
|
|
|
|
|
+ nickname: 'X-越权' + uniqueSuffix(),
|
|
|
|
|
+ gender: 'female',
|
|
|
|
|
+ }, outsider.token);
|
|
|
|
|
+ expect(oChild.code, '家庭2添加孩子: ' + oChild.message).toBe(200);
|
|
|
|
|
+
|
|
|
|
|
+ // 家庭1 家长为孩子 B 创建任务
|
|
|
|
|
+ const created = await apiPost(request, '/api/tasks/create', {
|
|
|
|
|
+ title: '越权目标' + uniqueSuffix(),
|
|
|
|
|
+ points: 5,
|
|
|
|
|
+ memberId: childB,
|
|
|
|
|
+ frequency: 'daily',
|
|
|
|
|
+ needReview: 1,
|
|
|
|
|
+ }, parent.token);
|
|
|
|
|
+ const taskId = created.data;
|
|
|
|
|
+
|
|
|
|
|
+ // 家庭2 孩子尝试完成家庭1 任务
|
|
|
|
|
+ const r = await apiPost(request, '/api/tasks/' + taskId + '/complete', {
|
|
|
|
|
+ memberId: oChild.data.id,
|
|
|
|
|
+ }, outsider.token);
|
|
|
|
|
+ expect(r.code).not.toBe(200);
|
|
|
|
|
+ expect(r.code).not.toBe(500);
|
|
|
|
|
+ });
|
|
|
|
|
+});
|