|
|
@@ -0,0 +1,312 @@
|
|
|
+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.FamilyMember;
|
|
|
+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.InOrder;
|
|
|
+import org.mockito.Mock;
|
|
|
+import org.mockito.MockitoAnnotations;
|
|
|
+import org.springframework.test.util.ReflectionTestUtils;
|
|
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestAttribute;
|
|
|
+
|
|
|
+import java.lang.annotation.Annotation;
|
|
|
+import java.lang.reflect.InvocationTargetException;
|
|
|
+import java.lang.reflect.Method;
|
|
|
+import java.text.ParseException;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
+
|
|
|
+import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
|
|
+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.doAnswer;
|
|
|
+import static org.mockito.Mockito.inOrder;
|
|
|
+import static org.mockito.Mockito.never;
|
|
|
+import static org.mockito.Mockito.verify;
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 契约级单测(TDD-RED,补充用例):能力键 {@code family.invite.memberJoin}(S-cfc-257)。
|
|
|
+ *
|
|
|
+ * <p><b>与同批 {@link FamilyInviteMemberJoinContractTest} 的分工</b>:那个类锁定「regenerate 端点/服务入口不存在」与
|
|
|
+ * 「失效文案缺失」两个最表层缺口;本类锁定 SC-P-09「失效旧码 → 生成新码」的<b>行为顺序与数据边界</b>,
|
|
|
+ * 以及 SC-P-02「成员加入后关系编辑」场景下旧码作废不得波及已加入成员/关系记录:</p>
|
|
|
+ * <ol>
|
|
|
+ * <li>绿地锚点:普通 {@code generate} 会复用同家庭有效令牌 —— 所以 regenerate 必须先吊销旧码才能真正「换新」。</li>
|
|
|
+ * <li>{@code POST /api/family/invite/regenerate} 须从 {@code @RequestAttribute("userId")} 取邀请人,而非 body 直传。</li>
|
|
|
+ * <li>regenerate 须「先吊销旧令牌、后签发新令牌」(InOrder),且新令牌绑定同一家庭/同一创建人、状态 active、未过期。</li>
|
|
|
+ * <li>旧令牌作废后不得再用于申请加入(SC-P-09「旧码作废」),不得落加入请求。</li>
|
|
|
+ * <li>regenerate 不得增删改已加入成员/关系记录(SC-P-09 边界:旧码已加入的会员不受影响)。</li>
|
|
|
+ * </ol>
|
|
|
+ *
|
|
|
+ * <p><b>实现现状(RED 依据)</b>:{@code FamilyInvitationService} 只有 generate / validate / accept / revoke / transfer,
|
|
|
+ * {@code FamilyInviteController} 只有 generate / revoke,二者均无 regenerate 入口;故 5 个缺口用例通过反射探测 +
|
|
|
+ * Mockito 交互断言记录契约失败(红),GREEN 阶段补齐实现后自动翻绿。</p>
|
|
|
+ *
|
|
|
+ * <p><b>溯源</b>:{@code tests/user-scenarios/应用场景总览.md} SC-P-02 / SC-P-09;
|
|
|
+ * {@code docs/superpowers/api/API_REFERENCE.md} §4.2(generate / revoke / request-join-by-code / approve-request);
|
|
|
+ * {@code docs/tdd/orchestration/RED-pending-dispatch-2026-11-14.md} S-cfc-257 设计列
|
|
|
+ * (FamilyInviteController generate/revoke/<b>regenerate</b> + FamilyMembersController + RelationshipTypes)。</p>
|
|
|
+ *
|
|
|
+ * <p><b>测试范围</b>:纯 JUnit 5 + Mockito + 反射,无 Spring 容器 / 无 DB / 无外部服务。</p>
|
|
|
+ */
|
|
|
+class FamilyInviteRegenerateContractRedTest {
|
|
|
+
|
|
|
+ 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";
|
|
|
+
|
|
|
+ @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("锚点:普通 generate 复用同家庭有效令牌(故 regenerate 必须先吊销旧码)")
|
|
|
+ void generate_reusesActiveToken_sameFamily() {
|
|
|
+ when(familyMapper.selectById(FAMILY_ID)).thenReturn(family());
|
|
|
+ when(familyInvitationMapper.selectList(any()))
|
|
|
+ .thenReturn(Collections.singletonList(oldInvitation()));
|
|
|
+
|
|
|
+ String token = inviteService.generateInvitation(FAMILY_ID, CREATOR_ID);
|
|
|
+
|
|
|
+ assertEquals(OLD_TOKEN, token, KEY + " generate 当前复用同家庭未过期令牌;"
|
|
|
+ + "SC-P-09「换新码」必须靠 regenerate 先吊销旧码,不能只调 generate。");
|
|
|
+ verify(familyInvitationMapper, never()).insert(any(FamilyInvitation.class));
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 重生成契约缺口(预期红) ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("重生成端点:@PostMapping(\"/regenerate\") 须从 @RequestAttribute(\"userId\") 取邀请人")
|
|
|
+ void regenerateEndpoint_usesRequestAttributeUserId() {
|
|
|
+ Method endpoint = findRegenerateEndpoint();
|
|
|
+ assertNotNull(endpoint, KEY + " FamilyInviteController 缺少 @PostMapping(\"/regenerate\"):"
|
|
|
+ + "SC-P-09 要求失效旧码后由服务端按登录态生成新码,不能由 body 直传 userId。");
|
|
|
+
|
|
|
+ boolean hasUserIdAttribute = false;
|
|
|
+ for (Annotation[] annotations : endpoint.getParameterAnnotations()) {
|
|
|
+ for (Annotation annotation : annotations) {
|
|
|
+ if (annotation instanceof RequestAttribute
|
|
|
+ && "userId".equals(((RequestAttribute) annotation).value())) {
|
|
|
+ hasUserIdAttribute = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ assertTrue(hasUserIdAttribute, KEY + " regenerate 端点缺少 @RequestAttribute(\"userId\") 参数:"
|
|
|
+ + "邀请人身份必须来自 JWT 注入,避免越权为他人家庭换码。");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("重生成顺序:先吊销旧令牌,再签发新令牌(SC-P-09「失效旧码 → 生成新码」)")
|
|
|
+ void regenerate_revokesOldBeforeIssuingNew() {
|
|
|
+ stubActiveOldInvitation();
|
|
|
+ when(familyMapper.selectById(FAMILY_ID)).thenReturn(family());
|
|
|
+
|
|
|
+ invokeRegenerate(FAMILY_ID, CREATOR_ID);
|
|
|
+
|
|
|
+ InOrder order = inOrder(familyInvitationMapper);
|
|
|
+ order.verify(familyInvitationMapper).updateById(org.mockito.ArgumentMatchers.argThat(
|
|
|
+ inv -> OLD_TOKEN.equals(inv.getToken()) && "revoked".equals(inv.getStatus())));
|
|
|
+ order.verify(familyInvitationMapper).insert(any(FamilyInvitation.class));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("重生成结果:新令牌绑定同一家庭/创建人、状态 active 且未过期,并不同于旧令牌")
|
|
|
+ void regenerate_newTokenBoundToSameFamilyAndCreator() {
|
|
|
+ stubActiveOldInvitation();
|
|
|
+ when(familyMapper.selectById(FAMILY_ID)).thenReturn(family());
|
|
|
+
|
|
|
+ invokeRegenerate(FAMILY_ID, CREATOR_ID);
|
|
|
+
|
|
|
+ ArgumentCaptor<FamilyInvitation> inserted = ArgumentCaptor.forClass(FamilyInvitation.class);
|
|
|
+ verify(familyInvitationMapper).insert(inserted.capture());
|
|
|
+ FamilyInvitation created = inserted.getValue();
|
|
|
+ assertNotEquals(OLD_TOKEN, created.getToken(), KEY + " 新令牌不得与旧令牌相同(旧码必须作废)。");
|
|
|
+ assertEquals(FAMILY_ID, created.getFamilyId(), KEY + " 新令牌必须仍绑定目标家庭。");
|
|
|
+ assertEquals(CREATOR_ID, created.getCreatedBy(), KEY + " 新令牌必须仍绑定原邀请人(审批人不变)。");
|
|
|
+ assertEquals("active", created.getStatus(), KEY + " 新令牌应为可校验的 active 状态。");
|
|
|
+ assertNotNull(created.getExpiresAt(), KEY + " 新令牌必须带有效期。");
|
|
|
+ assertTrue(created.getExpiresAt().after(new Date()), KEY + " 新令牌不得签发即过期。");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("旧码作废:regenerate 后旧令牌不得再用于申请加入,也不得落加入请求")
|
|
|
+ void regenerate_oldTokenCanNoLongerBeUsedToJoin() {
|
|
|
+ FamilyInvitation old = oldInvitation();
|
|
|
+ AtomicBoolean oldRevoked = new AtomicBoolean(false);
|
|
|
+ when(familyInvitationMapper.selectList(any())).thenReturn(Collections.singletonList(old));
|
|
|
+ when(familyInvitationMapper.selectOne(any())).thenAnswer(inv -> oldRevoked.get() ? null : old);
|
|
|
+ doAnswer(inv -> {
|
|
|
+ oldRevoked.set(true);
|
|
|
+ return null;
|
|
|
+ }).when(familyInvitationMapper).updateById(any(FamilyInvitation.class));
|
|
|
+ doAnswer(inv -> {
|
|
|
+ oldRevoked.set(true);
|
|
|
+ return 0;
|
|
|
+ }).when(familyInvitationMapper).update(any(), any());
|
|
|
+ when(familyMapper.selectById(FAMILY_ID)).thenReturn(family());
|
|
|
+ when(userMapper.selectById(JOINER_ID)).thenReturn(validJoiner());
|
|
|
+ when(familyJoinRequestMapper.selectOne(any())).thenReturn(null);
|
|
|
+
|
|
|
+ invokeRegenerate(FAMILY_ID, CREATOR_ID);
|
|
|
+
|
|
|
+ RuntimeException ex = assertThrows(RuntimeException.class,
|
|
|
+ () -> joinRequestService.createJoinRequest(JOINER_ID, OLD_TOKEN, null));
|
|
|
+ assertEquals("邀请令牌无效或已过期", ex.getMessage(),
|
|
|
+ KEY + " SC-P-09 旧码作废后不得再创建待审批加入请求。");
|
|
|
+ verify(familyJoinRequestMapper, never()).insert(any(FamilyJoinRequest.class));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("成员边界:regenerate 不得增删改已加入成员/关系记录(旧码已加入会员不受影响)")
|
|
|
+ void regenerate_doesNotMutateJoinedMembers() {
|
|
|
+ stubActiveOldInvitation();
|
|
|
+ when(familyMapper.selectById(FAMILY_ID)).thenReturn(family());
|
|
|
+
|
|
|
+ invokeRegenerate(FAMILY_ID, CREATOR_ID);
|
|
|
+
|
|
|
+ verify(familyMemberMapper, never()).insert(any(FamilyMember.class));
|
|
|
+ verify(familyMemberMapper, never()).updateById(any(FamilyMember.class));
|
|
|
+ verify(familyMemberMapper, never()).deleteById(any());
|
|
|
+ verify(userMapper, never()).updateById(any(User.class));
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 辅助构造与反射 ====================
|
|
|
+
|
|
|
+ private Method findRegenerateEndpoint() {
|
|
|
+ for (Method method : FamilyInviteController.class.getDeclaredMethods()) {
|
|
|
+ PostMapping mapping = method.getAnnotation(PostMapping.class);
|
|
|
+ if (mapping != null && java.util.Arrays.asList(mapping.value()).contains("/regenerate")) {
|
|
|
+ return method;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ 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 Object invokeRegenerate(Long familyId, Long userId) {
|
|
|
+ Method regenerate = findRegenerateMethod();
|
|
|
+ assertNotNull(regenerate, KEY + " FamilyInvitationService 缺少 regenerateInvitation(familyId, userId):"
|
|
|
+ + "SC-P-09 需先吊销该家庭有效旧令牌,再签发新令牌。");
|
|
|
+ try {
|
|
|
+ regenerate.setAccessible(true);
|
|
|
+ return regenerate.invoke(inviteService, familyId, userId);
|
|
|
+ } catch (InvocationTargetException e) {
|
|
|
+ fail(KEY + " regenerateInvitation(familyId, userId) 调用抛异常:"
|
|
|
+ + (e.getCause() != null ? e.getCause() : e));
|
|
|
+ return null;
|
|
|
+ } catch (ReflectiveOperationException e) {
|
|
|
+ fail(KEY + " regenerateInvitation 签名不是 (Long familyId, Long userId):" + e);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void stubActiveOldInvitation() {
|
|
|
+ when(familyInvitationMapper.selectList(any()))
|
|
|
+ .thenReturn(Collections.singletonList(oldInvitation()));
|
|
|
+ }
|
|
|
+
|
|
|
+ private FamilyInvitation oldInvitation() {
|
|
|
+ FamilyInvitation invitation = new FamilyInvitation();
|
|
|
+ invitation.setId(7L);
|
|
|
+ invitation.setFamilyId(FAMILY_ID);
|
|
|
+ invitation.setToken(OLD_TOKEN);
|
|
|
+ invitation.setCreatedBy(CREATOR_ID);
|
|
|
+ invitation.setStatus("active");
|
|
|
+ invitation.setExpiresAt(future());
|
|
|
+ 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("female");
|
|
|
+ user.setBirthday(date("1992-06-15"));
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+}
|