wish-flow-sc-p13-p14-c04.spec.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /**
  2. * SC-P-13/P-14/C-04 心愿域三类流(wish.parent.closedLoop / wish.parent.reject / wish.child.create)
  3. * ================================================================
  4. * 正常流:
  5. * - C-04:孩子创建 draft → 家长定价 active → 进入待审批;
  6. * - P-13:parent 直接创建 active → child 申请兑换 → parent 批准(扣 CF)→ 标记购买 → 孩子确认收货 → fulfilled;
  7. * - P-14:parent 驳回 draft → rejected,孩子可重新提。
  8. * 异常流:
  9. * - C-04-E:余额不足/超长/注入创建被拒;
  10. * - P-13-E:余额不足 → 拦截;并发双发 → 仅一次成功;已兑换再申请 → 拒绝;
  11. * - P-14-E:已兑换/active 状态不可驳回;cancel 失效。
  12. * 权限流(TC-P-07-P):
  13. * - 他人家庭跨家庭操作均 500 拒绝;
  14. * - 非归属成员申请兑换 → 500 拒绝;
  15. * - 越权查他人心愿 → 403。
  16. * 运行:
  17. * 远端:npx playwright test api/wish-flow-sc-p13-p14-c04.spec.js -c playwright.config.api.js --project=desktop
  18. * 本地:CFC_BASE_URL=http://127.0.0.1:18082 npx playwright test api/wish-flow-sc-p13-p14-c04.spec.js -c playwright.config.api.local.js --project=desktop
  19. */
  20. const { test, expect } = require('@playwright/test');
  21. const { registerRole, apiPost, uniqueSuffix, pickMemberByUserId } = require('../helpers/api');
  22. // 辅助:家长给孩子创建需审核为0的任务并完成,以发放 CF(心愿兑换走 CF 账本)
  23. async function grantCfViaTask(request, parent, childId) {
  24. const created = await apiPost(request, '/api/tasks/create', {
  25. title: 'CF预置任务' + uniqueSuffix(), points: 5, memberId: childId, frequency: 'none', needReview: 0,
  26. }, parent.token);
  27. expect(created.code, '预置任务创建: ' + created.message).toBe(200);
  28. const taskId = created.data;
  29. const completed = await apiPost(request, `/api/tasks/${taskId}/complete`, { memberId: childId }, parent.token);
  30. expect(completed.code, '预置任务完成: ' + completed.message).toBe(200);
  31. expect(completed.data.pointsEarned, '应获得正积分').toBeGreaterThan(0);
  32. }
  33. test.describe('SC-P-13/P-14/C-04 心愿域三类流(TC-P-07)', () => {
  34. let parent;
  35. let childA;
  36. let outsider;
  37. test.beforeAll(async ({ request }) => {
  38. parent = await registerRole(request, 'parent', 'QA-WISH');
  39. const memList = await apiPost(request, '/api/family/member/list', {}, parent.token);
  40. expect(memList.code).toBe(200);
  41. const self = pickMemberByUserId(memList.data, parent.userId);
  42. expect(self).toBeTruthy();
  43. const addA = await apiPost(request, '/api/family/member/add', {
  44. relativeMemberId: self.id, generationLevel: 'child',
  45. nickname: 'A-Wish' + uniqueSuffix(), gender: 'female', birthday: '2016-03-01',
  46. }, parent.token);
  47. expect(addA.code, 'addA: ' + addA.message).toBe(200);
  48. childA = addA.data.id;
  49. outsider = await registerRole(request, 'parent', 'QA-WISH-X');
  50. });
  51. // ===== SC-C-04 提心愿(正常流 TC-C-04-N)=====
  52. test('[C-04 正常] 孩子创建 draft 心愿 → 家长定价 → active 待审批', async ({ request }) => {
  53. const title = '孩子提的心愿' + uniqueSuffix();
  54. const create = await apiPost(request, '/api/wishes/create', { title, memberId: childA }, parent.token);
  55. expect(create.code, 'create: ' + create.message).toBe(200);
  56. const wishId = create.data;
  57. expect(wishId).toBeTruthy();
  58. // 家长定价(draft → active)
  59. const priced = await apiPost(request, `/api/wishes/${wishId}/set-price`, { pointsRequired: 100 }, parent.token);
  60. expect(priced.code, 'set-price: ' + priced.message).toBe(200);
  61. expect(priced.data).toBe(true);
  62. // 待审批列表可见
  63. const pend = await apiPost(request, '/api/wishes/pending', {}, parent.token);
  64. expect(pend.code, pend.message).toBe(200);
  65. const found = (pend.data || []).find((w) => String(w.id) === String(wishId));
  66. expect(found).toBeTruthy();
  67. expect(found.status).toBe('active');
  68. expect(found.pointsRequired).toBe(100);
  69. });
  70. // ===== SC-P-13 心愿闭环(正常流 TC-P-07-N)=====
  71. test('[P-13 正常] 家长创建 active 心愿 → 孩子申请兑换 → 家长批准扣 CF → fulfilled', async ({ request }) => {
  72. const title = 'P13心愿乐高' + uniqueSuffix();
  73. const price = 50;
  74. // 预置 CF
  75. await grantCfViaTask(request, parent, childA);
  76. const create = await apiPost(request, '/api/wishes/create', {
  77. title, memberId: childA, pointsRequired: price, category: '玩具',
  78. }, parent.token);
  79. expect(create.code, 'create: ' + create.message).toBe(200);
  80. const wishId = create.data;
  81. expect(wishId).toBeTruthy();
  82. // 申请兑换(active → pending_exchange)
  83. const req = await apiPost(request, `/api/wishes/${wishId}/exchange`, { memberId: childA }, parent.token);
  84. expect(req.code, 'requestExchange: ' + req.message).toBe(200);
  85. expect(req.data).toBe(true);
  86. // 家长批准(pending_exchange → approved,扣 CF)
  87. const approve = await apiPost(request, `/api/wishes/${wishId}/approve-exchange`, {
  88. memberId: childA, approved: true,
  89. }, parent.token);
  90. expect(approve.code, 'approve: ' + approve.message).toBe(200);
  91. expect(approve.data).toBe(true);
  92. // 家长标记购买 → 孩子确认收货
  93. const fulfill = await apiPost(request, `/api/wishes/${wishId}/fulfill`, {}, parent.token);
  94. expect(fulfill.code, 'fulfill: ' + fulfill.message).toBe(200);
  95. const confirm = await apiPost(request, `/api/wishes/${wishId}/confirm`, {}, parent.token);
  96. expect(confirm.code, 'confirm: ' + confirm.message).toBe(200);
  97. // 心愿最终状态 fulfilled
  98. const detail = await apiPost(request, `/api/wishes/${wishId}`, {}, parent.token);
  99. expect(detail.code, detail.message).toBe(200);
  100. expect(detail.data.status).toBe('fulfilled');
  101. });
  102. // ===== SC-P-14 心愿驳回/失效(正常流 TC-P-14-N)=====
  103. test('[P-14 正常] 驳回 draft → rejected;取消 active → cancelled;孩子可重新提', async ({ request }) => {
  104. // 1) 驳回 draft
  105. const create1 = await apiPost(request, '/api/wishes/create', {
  106. title: '驳回心愿' + uniqueSuffix(), memberId: childA,
  107. }, parent.token);
  108. expect(create1.code).toBe(200);
  109. const w1 = create1.data;
  110. const reject = await apiPost(request, `/api/wishes/${w1}/reject`, { reason: '价格不合理' }, parent.token);
  111. expect(reject.code).toBe(200);
  112. expect(reject.data).toBe(true);
  113. const d1 = await apiPost(request, `/api/wishes/${w1}`, {}, parent.token);
  114. expect(d1.data.status).toBe('rejected');
  115. // 2) 取消 active
  116. const create2 = await apiPost(request, '/api/wishes/create', {
  117. title: '取消心愿' + uniqueSuffix(), memberId: childA, pointsRequired: 20,
  118. }, parent.token);
  119. expect(create2.code).toBe(200);
  120. const w2 = create2.data;
  121. const cancel = await apiPost(request, `/api/wishes/${w2}/cancel`, { reason: '孩子不想要了' }, parent.token);
  122. expect(cancel.code).toBe(200);
  123. expect(cancel.data).toBe(true);
  124. const d2 = await apiPost(request, `/api/wishes/${w2}`, {}, parent.token);
  125. expect(d2.data.status).toBe('cancelled');
  126. // 3) 孩子可重新提(同名可用新名称)
  127. const reCreate = await apiPost(request, '/api/wishes/create', {
  128. title: '重新提的心愿' + uniqueSuffix(), memberId: childA,
  129. }, parent.token);
  130. expect(reCreate.code).toBe(200);
  131. expect(reCreate.data).toBeTruthy();
  132. });
  133. // ===== 异常流 =====
  134. test('[P-13-E] 余额不足申请兑换 → 业务拒绝(非 200)', async ({ request }) => {
  135. const title = '高价心愿' + uniqueSuffix();
  136. const create = await apiPost(request, '/api/wishes/create', {
  137. title, memberId: childA, pointsRequired: 99999,
  138. }, parent.token);
  139. expect(create.code).toBe(200);
  140. const wishId = create.data;
  141. const req = await apiPost(request, `/api/wishes/${wishId}/exchange`, { memberId: childA }, parent.token);
  142. // 业务错误:code ≠ 200(远端 Result.error → 500)
  143. expect(req.code).not.toBe(200);
  144. });
  145. test('[P-13-E] 并发双发同一心愿兑换申请 → 仅一次成功(幂等)', async ({ request }) => {
  146. const title = '并发心愿' + uniqueSuffix();
  147. const create = await apiPost(request, '/api/wishes/create', {
  148. title, memberId: childA, pointsRequired: 5,
  149. }, parent.token);
  150. expect(create.code).toBe(200);
  151. const wishId = create.data;
  152. const [a, b] = await Promise.all([
  153. apiPost(request, `/api/wishes/${wishId}/exchange`, { memberId: childA }, parent.token),
  154. apiPost(request, `/api/wishes/${wishId}/exchange`, { memberId: childA }, parent.token),
  155. ]);
  156. const success = [a, b].filter((r) => r.code === 200 && r.data === true);
  157. expect(success.length, '并发兑换请求应有 1 个成功').toBe(1);
  158. });
  159. test('[P-13-E] 已兑换心愿再次申请 → 拒绝', async ({ request }) => {
  160. const title = '再兑心愿' + uniqueSuffix();
  161. const create = await apiPost(request, '/api/wishes/create', {
  162. title, memberId: childA, pointsRequired: 5,
  163. }, parent.token);
  164. expect(create.code).toBe(200);
  165. const wishId = create.data;
  166. await apiPost(request, `/api/wishes/${wishId}/exchange`, { memberId: childA }, parent.token);
  167. await apiPost(request, `/api/wishes/${wishId}/approve-exchange`, {
  168. memberId: childA, approved: true,
  169. }, parent.token);
  170. const again = await apiPost(request, `/api/wishes/${wishId}/exchange`, { memberId: childA }, parent.token);
  171. expect(again.code).not.toBe(200);
  172. });
  173. test('[P-14-E] 已兑换心愿不可驳回', async ({ request }) => {
  174. const title = '驳回已兑' + uniqueSuffix();
  175. const create = await apiPost(request, '/api/wishes/create', {
  176. title, memberId: childA, pointsRequired: 5,
  177. }, parent.token);
  178. expect(create.code).toBe(200);
  179. const wishId = create.data;
  180. await apiPost(request, `/api/wishes/${wishId}/exchange`, { memberId: childA }, parent.token);
  181. await apiPost(request, `/api/wishes/${wishId}/approve-exchange`, {
  182. memberId: childA, approved: true,
  183. }, parent.token);
  184. const rejected = await apiPost(request, `/api/wishes/${wishId}/reject`, { reason: '误驳回' }, parent.token);
  185. expect(rejected.code).not.toBe(200);
  186. });
  187. // ===== 权限流(TC-P-07-P)=====
  188. test('[P-13-P] 他人家庭家长为孩子创建心愿 → 拒绝', async ({ request }) => {
  189. const r = await apiPost(request, '/api/wishes/create', {
  190. title: 'X越权' + uniqueSuffix(), memberId: childA, pointsRequired: 10,
  191. }, outsider.token);
  192. expect(r.code).not.toBe(200);
  193. });
  194. test('[P-13-P] 他人家庭家长审批我的心愿 → 拒绝', async ({ request }) => {
  195. const create = await apiPost(request, '/api/wishes/create', {
  196. title: '我心愿' + uniqueSuffix(), memberId: childA, pointsRequired: 10,
  197. }, parent.token);
  198. expect(create.code).toBe(200);
  199. const wishId = create.data;
  200. const setPrice = await apiPost(request, `/api/wishes/${wishId}/set-price`, { pointsRequired: 10 }, parent.token);
  201. expect(setPrice.code).toBe(200);
  202. // 他人定价
  203. const xPrice = await apiPost(request, `/api/wishes/${wishId}/set-price`, { pointsRequired: 10 }, outsider.token);
  204. expect(xPrice.code).not.toBe(200);
  205. });
  206. test('[P-13-P] 越权家长代他人家庭孩子申请兑换 → 拒绝', async ({ request }) => {
  207. // outsider 自己的心愿
  208. const memX = await apiPost(request, '/api/family/member/list', {}, outsider.token);
  209. const selfX = pickMemberByUserId(memX.data, outsider.userId);
  210. const addY = await apiPost(request, '/api/family/member/add', {
  211. relativeMemberId: selfX.id, generationLevel: 'child',
  212. nickname: 'Y-Out' + uniqueSuffix(), gender: 'male',
  213. }, outsider.token);
  214. expect(addY.code).toBe(200);
  215. const yId = addY.data.id;
  216. const create = await apiPost(request, '/api/wishes/create', {
  217. title: 'Y-Own' + uniqueSuffix(), memberId: yId, pointsRequired: 2,
  218. }, outsider.token);
  219. expect(create.code).toBe(200);
  220. const yWishId = create.data;
  221. // parent 越权申请
  222. const ex = await apiPost(request, `/api/wishes/${yWishId}/exchange`, { memberId: yId }, parent.token);
  223. expect(ex.code).not.toBe(200);
  224. });
  225. test('[P-13-P] 越权查看他人家庭心愿 → 403', async ({ request }) => {
  226. const memX = await apiPost(request, '/api/family/member/list', {}, outsider.token);
  227. const selfX = pickMemberByUserId(memX.data, outsider.userId);
  228. const addY = await apiPost(request, '/api/family/member/add', {
  229. relativeMemberId: selfX.id, generationLevel: 'child',
  230. nickname: 'Y-View' + uniqueSuffix(), gender: 'male',
  231. }, outsider.token);
  232. expect(addY.code).toBe(200);
  233. const yId = addY.data.id;
  234. const create = await apiPost(request, '/api/wishes/create', {
  235. title: 'Y-Wish' + uniqueSuffix(), memberId: yId, pointsRequired: 2,
  236. }, outsider.token);
  237. expect(create.code).toBe(200);
  238. const yWishId = create.data;
  239. // parent 试图查询
  240. const list = await apiPost(request, '/api/wishes/list', { memberId: yId }, parent.token);
  241. expect(list.code).not.toBe(200);
  242. // 直接详情查看
  243. const detail = await apiPost(request, `/api/wishes/${yWishId}`, {}, parent.token);
  244. // 业务层可能返回 500 或 403;契约:非归属数据访问被拒
  245. expect(detail.code).not.toBe(200);
  246. });
  247. });