|
@@ -0,0 +1,290 @@
|
|
|
|
|
+package com.etotem.cfc.unit;
|
|
|
|
|
+
|
|
|
|
|
+import com.etotem.cfc.controller.family.FamilyInviteController;
|
|
|
|
|
+import com.etotem.cfc.entity.Family;
|
|
|
|
|
+import com.etotem.cfc.entity.FamilyInvitation;
|
|
|
|
|
+import com.etotem.cfc.entity.FamilyJoinRequest;
|
|
|
|
|
+import com.etotem.cfc.entity.User;
|
|
|
|
|
+import com.etotem.cfc.mapper.FamilyInvitationMapper;
|
|
|
|
|
+import com.etotem.cfc.mapper.FamilyJoinRequestMapper;
|
|
|
|
|
+import com.etotem.cfc.mapper.FamilyMapper;
|
|
|
|
|
+import com.etotem.cfc.mapper.FamilyMemberMapper;
|
|
|
|
|
+import com.etotem.cfc.mapper.UserMapper;
|
|
|
|
|
+import com.etotem.cfc.service.FamilyInvitationService;
|
|
|
|
|
+import com.etotem.cfc.service.FamilyJoinRequestService;
|
|
|
|
|
+import org.junit.jupiter.api.BeforeEach;
|
|
|
|
|
+import org.junit.jupiter.api.DisplayName;
|
|
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
|
|
+import org.mockito.ArgumentCaptor;
|
|
|
|
|
+import org.mockito.Mock;
|
|
|
|
|
+import org.mockito.MockitoAnnotations;
|
|
|
|
|
+import org.springframework.test.util.ReflectionTestUtils;
|
|
|
|
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
|
|
|
+
|
|
|
|
|
+import java.lang.reflect.Method;
|
|
|
|
|
+import java.text.ParseException;
|
|
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
|
|
+import java.util.Arrays;
|
|
|
|
|
+import java.util.Collections;
|
|
|
|
|
+import java.util.Date;
|
|
|
|
|
+
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.assertThrows;
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.fail;
|
|
|
|
|
+import static org.mockito.ArgumentMatchers.any;
|
|
|
|
|
+import static org.mockito.Mockito.never;
|
|
|
|
|
+import static org.mockito.Mockito.verify;
|
|
|
|
|
+import static org.mockito.Mockito.verifyNoInteractions;
|
|
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 契约级单测(TDD-RED):能力键 {@code family.invite.memberJoin}(S-cfc-257)。
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p><b>契约锚点</b></p>
|
|
|
|
|
+ * <ul>
|
|
|
|
|
+ * <li>需求:{@code tests/user-scenarios/应用场景总览.md} SC-P-02(邀请成员 + 关系编辑:分享邀请 → 成员加入 →
|
|
|
|
|
+ * RelationshipPicker 选关系 → 关系图更新)与 SC-P-09(邀请码失效/重生成:失效旧码 → 生成新码,
|
|
|
|
|
+ * 边界:旧码已加入的会员不受影响);{@code tests/user-scenarios/02-parent.md} 场景 P-2;
|
|
|
|
|
+ * {@code tests/test-cases/parent.md} TC-P-02 / TC-P-02-E(过期码提示「邀请已失效,请重新获取」)。</li>
|
|
|
|
|
+ * <li>设计:{@code docs/superpowers/api/API_REFERENCE.md} §4.2(Family Invite 接口详细:
|
|
|
|
|
+ * request-join-by-code / approve-request / generate / validate / revoke);
|
|
|
|
|
+ * {@code docs/tdd/orchestration/RED-pending-dispatch-2026-11-14.md} S-cfc-257 设计列明确
|
|
|
|
|
+ * FamilyInviteController(generate/revoke/<b>regenerate</b>)+ FamilyMembersController + RelationshipTypes 关系选择器。</li>
|
|
|
|
|
+ * </ul>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p><b>实现现状(RED 依据)</b>:主链已落地「令牌申请 → 审批加入 → 关系写入」与 revoke 主语义
|
|
|
|
|
+ * (见正常流/权限流锚点用例),但契约要求的 <b>regenerate(旧码失效+新码生成)</b> 端点与服务入口均不存在,
|
|
|
|
|
+ * 且过期码提示缺少「请重新获取」引导。因此本类以反射结构探测 + Mockito 行为断言记录缺口:
|
|
|
|
|
+ * 能编译、能运行,缺口断言失败(红),失败消息可直接贴 RED 报告;GREEN 阶段实现后自动翻绿。</p>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p><b>GREEN 升级指引</b>:① FamilyInviteController 增加 {@code @PostMapping("/regenerate")} 端点;
|
|
|
|
|
+ * ② FamilyInvitationService 增加 {@code regenerateInvitation(familyId, userId)}(先吊销该用户在该家庭的
|
|
|
|
|
+ * 有效旧令牌,再签发新令牌并返回新令牌/二维码);③ 过期令牌校验信息补齐「邀请已失效,请重新获取」。
|
|
|
|
|
+ * 已加入成员的用户/成员记录不得因 revoke/regenerate 被改动。</p>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p><b>测试范围</b>:纯 Mockito + 反射(JUnit 5),无 Spring 容器 / 无 DB / 无外部服务。
|
|
|
|
|
+ * 风格与同批已合入 RED 契约测试(FamilyJoinValidationRulesContractTest / TeamProfitHookContractTest /
|
|
|
|
|
+ * ButlerResourceMatchContractTest)一致。</p>
|
|
|
|
|
+ */
|
|
|
|
|
+class FamilyInviteMemberJoinContractTest {
|
|
|
|
|
+
|
|
|
|
|
+ private static final String KEY = "[family.invite.memberJoin]";
|
|
|
|
|
+ private static final Long FAMILY_ID = 100L;
|
|
|
|
|
+ private static final Long CREATOR_ID = 1L;
|
|
|
|
|
+ private static final Long JOINER_ID = 2L;
|
|
|
|
|
+ private static final String OLD_TOKEN = "0123456789abcdef0123456789abcdef";
|
|
|
|
|
+ private static final String MSG_EXPIRED_REGUIDE = "邀请已失效,请重新获取";
|
|
|
|
|
+
|
|
|
|
|
+ @Mock
|
|
|
|
|
+ private FamilyMapper familyMapper;
|
|
|
|
|
+ @Mock
|
|
|
|
|
+ private UserMapper userMapper;
|
|
|
|
|
+ @Mock
|
|
|
|
|
+ private FamilyMemberMapper familyMemberMapper;
|
|
|
|
|
+ @Mock
|
|
|
|
|
+ private FamilyInvitationMapper familyInvitationMapper;
|
|
|
|
|
+ @Mock
|
|
|
|
|
+ private FamilyJoinRequestMapper familyJoinRequestMapper;
|
|
|
|
|
+
|
|
|
|
|
+ private FamilyInvitationService inviteService;
|
|
|
|
|
+ private FamilyJoinRequestService joinRequestService;
|
|
|
|
|
+
|
|
|
|
|
+ @BeforeEach
|
|
|
|
|
+ void setUp() {
|
|
|
|
|
+ MockitoAnnotations.openMocks(this);
|
|
|
|
|
+ inviteService = new FamilyInvitationService();
|
|
|
|
|
+ ReflectionTestUtils.setField(inviteService, "familyInvitationMapper", familyInvitationMapper);
|
|
|
|
|
+ ReflectionTestUtils.setField(inviteService, "familyMapper", familyMapper);
|
|
|
|
|
+ ReflectionTestUtils.setField(inviteService, "userMapper", userMapper);
|
|
|
|
|
+ ReflectionTestUtils.setField(inviteService, "familyMemberMapper", familyMemberMapper);
|
|
|
|
|
+
|
|
|
|
|
+ joinRequestService = new FamilyJoinRequestService();
|
|
|
|
|
+ ReflectionTestUtils.setField(joinRequestService, "familyInvitationMapper", familyInvitationMapper);
|
|
|
|
|
+ ReflectionTestUtils.setField(joinRequestService, "familyMapper", familyMapper);
|
|
|
|
|
+ ReflectionTestUtils.setField(joinRequestService, "userMapper", userMapper);
|
|
|
|
|
+ ReflectionTestUtils.setField(joinRequestService, "familyJoinRequestMapper", familyJoinRequestMapper);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 正常流锚点(已实现部分,须保持绿) ====================
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("正常流-1:有效令牌申请加入 → 待审批请求并绑定邀请人")
|
|
|
|
|
+ void validToken_requestJoinByCode_createsPendingRequestBoundToInviter() {
|
|
|
|
|
+ FamilyInvitation invitation = invitation(OLD_TOKEN, "active", future());
|
|
|
|
|
+ invitation.setFamilyId(FAMILY_ID);
|
|
|
|
|
+ invitation.setCreatedBy(CREATOR_ID);
|
|
|
|
|
+ when(familyInvitationMapper.selectOne(any())).thenReturn(invitation);
|
|
|
|
|
+ when(familyMapper.selectById(FAMILY_ID)).thenReturn(family());
|
|
|
|
|
+ when(userMapper.selectById(JOINER_ID)).thenReturn(validJoiner());
|
|
|
|
|
+ when(familyJoinRequestMapper.selectOne(any())).thenReturn(null);
|
|
|
|
|
+
|
|
|
|
|
+ FamilyJoinRequest request = joinRequestService.createJoinRequest(JOINER_ID, OLD_TOKEN, null);
|
|
|
|
|
+
|
|
|
|
|
+ assertNotNull(request, KEY + " 有效令牌申请加入应创建加入请求(SC-P-02「成员加入」)。");
|
|
|
|
|
+ assertEquals("pending", request.getStatus());
|
|
|
|
|
+ assertEquals(FAMILY_ID, request.getTargetFamilyId());
|
|
|
|
|
+ assertEquals(CREATOR_ID, request.getInviterUserId(), KEY + " 令牌路径必须绑定邀请人为审批人。");
|
|
|
|
|
+ assertEquals("by_token", request.getJoinMethod());
|
|
|
|
|
+ verify(familyJoinRequestMapper).insert(any(FamilyJoinRequest.class));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("正常流-2:吊销旧邀请只改邀请状态,已加入成员不受影响(SC-P-09 边界)")
|
|
|
|
|
+ void revokeInvitation_doesNotTouchJoinedMembers() {
|
|
|
|
|
+ FamilyInvitation invitation = invitation(OLD_TOKEN, "active", future());
|
|
|
|
|
+ invitation.setId(7L);
|
|
|
|
|
+ invitation.setCreatedBy(CREATOR_ID);
|
|
|
|
|
+ when(familyInvitationMapper.selectById(7L)).thenReturn(invitation);
|
|
|
|
|
+
|
|
|
|
|
+ inviteService.revokeInvitation(7L, CREATOR_ID);
|
|
|
|
|
+
|
|
|
|
|
+ assertEquals("revoked", invitation.getStatus());
|
|
|
|
|
+ verify(familyInvitationMapper).updateById(invitation);
|
|
|
|
|
+ verifyNoInteractions(familyMemberMapper, userMapper);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 异常流 ====================
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("异常流-1:过期/失效令牌应提示「邀请已失效,请重新获取」")
|
|
|
|
|
+ void validateInvitation_expiredToken_givesRegenerateGuidance() {
|
|
|
|
|
+ when(familyInvitationMapper.selectOne(any())).thenReturn(null);
|
|
|
|
|
+
|
|
|
|
|
+ RuntimeException ex = assertThrows(RuntimeException.class,
|
|
|
|
|
+ () -> inviteService.validateInvitation(OLD_TOKEN));
|
|
|
|
|
+
|
|
|
|
|
+ assertEquals(MSG_EXPIRED_REGUIDE, ex.getMessage(),
|
|
|
|
|
+ KEY + " SC-P-09 失效旧码应引导重新获取新码(TC-P-02-E);当前缺少 regenerate 引导文案。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("权限流:非邀请创建者不能吊销邀请")
|
|
|
|
|
+ void revokeInvitation_byNonCreator_isRejected() {
|
|
|
|
|
+ FamilyInvitation invitation = invitation(OLD_TOKEN, "active", future());
|
|
|
|
|
+ invitation.setId(8L);
|
|
|
|
|
+ invitation.setCreatedBy(CREATOR_ID);
|
|
|
|
|
+ when(familyInvitationMapper.selectById(8L)).thenReturn(invitation);
|
|
|
|
|
+
|
|
|
|
|
+ RuntimeException ex = assertThrows(RuntimeException.class,
|
|
|
|
|
+ () -> inviteService.revokeInvitation(8L, JOINER_ID));
|
|
|
|
|
+
|
|
|
|
|
+ assertEquals("只有邀请创建者才能吊销", ex.getMessage());
|
|
|
|
|
+ verify(familyInvitationMapper, never()).updateById(any(FamilyInvitation.class));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 重生成契约(未落地 → 预期红) ====================
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("重生成-1:FamilyInviteController 应提供 POST /api/family/invite/regenerate 端点")
|
|
|
|
|
+ void inviteController_hasRegeneratePostMapping() {
|
|
|
|
|
+ boolean found = false;
|
|
|
|
|
+ for (Method method : FamilyInviteController.class.getDeclaredMethods()) {
|
|
|
|
|
+ PostMapping mapping = method.getAnnotation(PostMapping.class);
|
|
|
|
|
+ if (mapping != null && Arrays.asList(mapping.value()).contains("/regenerate")) {
|
|
|
|
|
+ found = true;
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ assertTrue(found, KEY + " FamilyInviteController 缺少 @PostMapping(\"/regenerate\"):"
|
|
|
|
|
+ + "SC-P-09「失效旧码 → 生成新码」要求 generate/revoke/regenerate 三端点齐备(API_REFERENCE §4.2)。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("重生成-2:FamilyInvitationService 应提供 regenerateInvitation 入口")
|
|
|
|
|
+ void inviteService_hasRegenerateInvitationEntry() {
|
|
|
|
|
+ assertTrue(findRegenerateMethod() != null, KEY + " FamilyInvitationService 缺少 regenerateInvitation(...) 入口:"
|
|
|
|
|
+ + "SC-P-09 需先吊销家庭内有效旧令牌、再签发新令牌,不能仅复用旧令牌。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("重生成-3:regenerate 应吊销旧令牌并签发新令牌")
|
|
|
|
|
+ void regenerateInvitation_revokesOldAndIssuesNewToken() throws Exception {
|
|
|
|
|
+ Method regenerate = findRegenerateMethod();
|
|
|
|
|
+ if (regenerate == null) {
|
|
|
|
|
+ fail(KEY + " regenerateInvitation(...) 不存在,无法验证「旧码作废换新」行为(SC-P-09)。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ FamilyInvitation oldInvitation = invitation(OLD_TOKEN, "active", future());
|
|
|
|
|
+ oldInvitation.setFamilyId(FAMILY_ID);
|
|
|
|
|
+ oldInvitation.setCreatedBy(CREATOR_ID);
|
|
|
|
|
+ when(familyMapper.selectById(FAMILY_ID)).thenReturn(family());
|
|
|
|
|
+ when(familyInvitationMapper.selectList(any())).thenReturn(Collections.singletonList(oldInvitation));
|
|
|
|
|
+
|
|
|
|
|
+ Object result;
|
|
|
|
|
+ try {
|
|
|
|
|
+ regenerate.setAccessible(true);
|
|
|
|
|
+ result = regenerate.invoke(inviteService, FAMILY_ID, CREATOR_ID);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ fail(KEY + " regenerateInvitation(familyId, userId) 调用失败(期望签名),实现请按此方法签名落地:"
|
|
|
|
|
+ + e);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ ArgumentCaptor<FamilyInvitation> updated = ArgumentCaptor.forClass(FamilyInvitation.class);
|
|
|
|
|
+ verify(familyInvitationMapper).updateById(updated.capture());
|
|
|
|
|
+ assertEquals("revoked", updated.getValue().getStatus(),
|
|
|
|
|
+ KEY + " 重生成必须先把旧令牌置为 revoked(SC-P-09 失效旧码)。");
|
|
|
|
|
+
|
|
|
|
|
+ ArgumentCaptor<FamilyInvitation> inserted = ArgumentCaptor.forClass(FamilyInvitation.class);
|
|
|
|
|
+ verify(familyInvitationMapper).insert(inserted.capture());
|
|
|
|
|
+ String newToken = inserted.getValue().getToken();
|
|
|
|
|
+ assertNotNull(newToken, KEY + " 重生成必须签发新令牌。");
|
|
|
|
|
+ assertTrue(!OLD_TOKEN.equals(newToken), KEY + " 新令牌不得与旧令牌相同(旧码必须失效)。");
|
|
|
|
|
+ assertNotNull(result, KEY + " regenerateInvitation 应返回新令牌供二维码/分享使用。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 辅助构造 ====================
|
|
|
|
|
+
|
|
|
|
|
+ private Method findRegenerateMethod() {
|
|
|
|
|
+ for (Method method : FamilyInvitationService.class.getDeclaredMethods()) {
|
|
|
|
|
+ String name = method.getName();
|
|
|
|
|
+ if ("regenerateInvitation".equals(name) || "regenerate".equals(name)) {
|
|
|
|
|
+ return method;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private FamilyInvitation invitation(String token, String status, Date expiresAt) {
|
|
|
|
|
+ FamilyInvitation invitation = new FamilyInvitation();
|
|
|
|
|
+ invitation.setToken(token);
|
|
|
|
|
+ invitation.setStatus(status);
|
|
|
|
|
+ invitation.setExpiresAt(expiresAt);
|
|
|
|
|
+ invitation.setMaxUseCount(1);
|
|
|
|
|
+ invitation.setUsedCount(0);
|
|
|
|
|
+ return invitation;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Family family() {
|
|
|
|
|
+ Family family = new Family();
|
|
|
|
|
+ family.setId(FAMILY_ID);
|
|
|
|
|
+ family.setName("契约测试家庭");
|
|
|
|
|
+ family.setCreatorId(CREATOR_ID);
|
|
|
|
|
+ return family;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private User validJoiner() {
|
|
|
|
|
+ User user = new User();
|
|
|
|
|
+ user.setId(JOINER_ID);
|
|
|
|
|
+ user.setRealName("张三");
|
|
|
|
|
+ user.setGender("male");
|
|
|
|
|
+ user.setBirthday(date("1990-05-01"));
|
|
|
|
|
+ return user;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Date date(String yyyyMMdd) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
|
|
+ sdf.setLenient(false);
|
|
|
|
|
+ return sdf.parse(yyyyMMdd);
|
|
|
|
|
+ } catch (ParseException e) {
|
|
|
|
|
+ throw new IllegalArgumentException("非法测试日期: " + yyyyMMdd, e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Date future() {
|
|
|
|
|
+ return new Date(System.currentTimeMillis() + 24 * 60 * 60 * 1000L);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|