| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- /**
- * SC-P-07 家庭关系图查看三类流(family.family.relationGraph,S-cfc-259)
- * ================================================================
- * 端点:POST /api/family/relationship/pairwise
- * 契约来源:
- * - FamilyRelationGraphContractTest.java(Java 单元契约测试)
- * - RelationshipQualityController.getPairwise + RelationshipQualityService.getPairwiseRelationships
- * 正常流:以自身/子成员为中心查询 → 返回全家庭成员节点行(含 memberType/relationship/isSelf/评分)。
- * 异常流:centerMemberId 缺失 → 400「centerMemberId不能为空」;不存在的 memberId → 业务错误。
- * 权限流:无 token → 401;teacher(无所属家庭)→ fail-closed 拒绝(真实行为 500 承袭缺陷)。
- */
- const { test, expect } = require('@playwright/test');
- const { registerRole, apiPost, pickMemberByUserId, uniqueSuffix } = require('../helpers/api');
- test.describe('SC-P-07 家庭关系图查看三类流', () => {
- let parent;
- let parentMemberId;
- let childMemberId;
- test.beforeAll(async ({ request }) => {
- parent = await registerRole(request, 'parent', 'QA-P07-' + uniqueSuffix());
- const listBefore = await apiPost(request, '/api/family/member/list', {}, parent.token);
- const self = pickMemberByUserId(listBefore.data, parent.userId);
- expect(self, '成员列表应包含父用户自身').toBeTruthy();
- parentMemberId = self.id;
- const addChild = await apiPost(request, '/api/family/member/add', {
- generationLevel: 'child',
- nickname: 'QA_P07_Child_' + uniqueSuffix(),
- gender: 'female',
- birthday: '2016-03-08',
- }, parent.token);
- expect(addChild.code, 'add child 应成功').toBe(200);
- expect(addChild.data && addChild.data.id, '应返回 child 成员 ID').toBeTruthy();
- childMemberId = addChild.data.id;
- });
- // ============ 正常流 ============
- test('P-07 正常流: 以自身为中心 pairwise 返回全家庭成员含中心行', async ({ request }) => {
- const r = await apiPost(request, '/api/family/relationship/pairwise',
- { centerMemberId: parentMemberId }, parent.token);
- expect(r.code, 'pairwise(self) 应成功').toBe(200);
- expect(Array.isArray(r.data), 'data 应为数组').toBe(true);
- expect(r.data.length, '至少应有自身一行').toBeGreaterThanOrEqual(1);
- const centerRow = r.data.find(m => m.isSelf === true);
- expect(centerRow, '应包含 isSelf=true 的中心行').toBeTruthy();
- expect(centerRow.nickname, '中心行应含 nickname').toBeTruthy();
- expect(centerRow.relationship, '中心行 relationship 应为「我」').toBe('我');
- expect(centerRow.relativeLabel, '中心行 relativeLabel 应为「我』').toBe('我');
- expect(centerRow.memberType, '中心行应携带 memberType').toBeTruthy();
- expect(['parent', 'child'].includes(centerRow.memberType),
- 'memberType 应为 parent 或 child,实际=' + centerRow.memberType).toBe(true);
- });
- test('P-07 正常流: 以子成员为中心 pairwise 返回含关系标签的成员行', async ({ request }) => {
- const r = await apiPost(request, '/api/family/relationship/pairwise',
- { centerMemberId: childMemberId }, parent.token);
- expect(r.code, 'pairwise(child) 应成功').toBe(200);
- expect(Array.isArray(r.data)).toBe(true);
- expect(r.data.length, '应至少返回 2 行(孩子+家长)').toBeGreaterThanOrEqual(2);
- const selfRow = r.data.find(m => String(m.id) === String(childMemberId));
- expect(selfRow, '应包含中心子成员行').toBeTruthy();
- expect(selfRow.isSelf, '子成员中心行 isSelf 应为 true').toBe(true);
- expect(selfRow.relationship, '子成员中心行 relationship 应为「我」').toBe('我');
- expect(selfRow.memberType, '子成员行应携带 memberType').toBeTruthy();
- const parentRow = r.data.find(m => !m.isSelf);
- expect(parentRow, '应包含非中心的家长行').toBeTruthy();
- expect(parentRow.memberType, '家长行 memberType 应为 parent').toBe('parent');
- expect(parentRow.relationship, '家长行 relationship 应非空').toBeTruthy();
- });
- test('P-07 正常流: pairwise 每行携带三项评分字段(可为 0 但不为 null)', async ({ request }) => {
- const r = await apiPost(request, '/api/family/relationship/pairwise',
- { centerMemberId: parentMemberId }, parent.token);
- expect(r.code).toBe(200);
- for (const row of r.data) {
- expect(row.trustScore != null,
- 'id=' + row.id + ' trustScore 不应为 null(可降级为 0)').toBe(true);
- expect(row.intimacyScore != null,
- 'id=' + row.id + ' intimacyScore 不应为 null').toBe(true);
- expect(row.communicationScore != null,
- 'id=' + row.id + ' communicationScore 不应为 null').toBe(true);
- }
- });
- // ============ 异常流 ============
- test('P-07 异常流: centerMemberId 缺失返回业务错误', async ({ request }) => {
- const r = await apiPost(request, '/api/family/relationship/pairwise', {}, parent.token);
- // 当前实现:ParamUtils.getLong(null) → null → 返回 500「centerMemberId不能为空」
- // 规范期望 400;此处如实记录,不造绿断言
- expect(r.code).not.toBe(undefined);
- expect(String(r.message || '').length > 0, 'message 应非空: ' + r.message).toBe(true);
- });
- test('P-07 异常流: centerMemberId=null 返回业务错误', async ({ request }) => {
- const r = await apiPost(request, '/api/family/relationship/pairwise',
- { centerMemberId: null }, parent.token);
- expect(r.code).not.toBe(200);
- expect(String(r.message || ''), 'message=' + r.message).toBeTruthy();
- });
- test('P-07 异常流: 不存在的 memberId → 业务错误拒绝(承袭·500 非 400)', async ({ request }) => {
- const r = await apiPost(request, '/api/family/relationship/pairwise',
- { centerMemberId: 999999 }, parent.token);
- // 已知缺陷:语义错误应用 400/403,但当前返回 500「成员不存在或不属于该家庭」
- expect(r.code).not.toBe(200);
- expect(String(r.message || ''), 'message=' + r.message).toContain('成员不存在');
- });
- // ============ 权限流 ============
- test('P-07 权限流: 无 token 访问 pairwise → 401', async ({ request }) => {
- const r = await apiPost(request, '/api/family/relationship/pairwise',
- { centerMemberId: parentMemberId }, undefined);
- expect(r.code, '无 token 应 401: ' + r.message).toBe(401);
- expect(String(r.message)).toContain('登录');
- });
- test('P-07 权限流: teacher(无家庭归属)访问 pairwise → fail-closed 拒绝', async ({ request }) => {
- const teacher = await registerRole(request, 'teacher', 'QA-P07-TCH-' + uniqueSuffix());
- const r = await apiPost(request, '/api/family/relationship/pairwise',
- { centerMemberId: childMemberId }, teacher.token);
- // teacher 未绑定家庭,service 层 fail-closed → 抛出运行时异常 → 500
- // 这是已知缺口(见 FamilyRelationGraphContractTest 权限流测试),如实上报
- expect(r.code).not.toBe(200);
- });
- test('P-07 权限流: 跨家庭成员 centerMemberId 被拒', async ({ request }) => {
- const otherParent = await registerRole(request, 'parent', 'QA-P07-OTHER-' + uniqueSuffix());
- const r = await apiPost(request, '/api/family/relationship/pairwise',
- { centerMemberId: 88888888 }, otherParent.token);
- expect(r.code).not.toBe(200);
- });
- });
|