|
|
@@ -0,0 +1,259 @@
|
|
|
+package com.etotem.cfc.unit;
|
|
|
+
|
|
|
+import com.etotem.cfc.dto.UpdateUserDTO;
|
|
|
+import com.etotem.cfc.controller.UserController;
|
|
|
+import com.etotem.cfc.entity.User;
|
|
|
+import com.etotem.cfc.mapper.UserMapper;
|
|
|
+import com.etotem.cfc.service.OnboardingService;
|
|
|
+import com.etotem.cfc.service.RealNameBackfillService;
|
|
|
+import com.etotem.cfc.service.UserService;
|
|
|
+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 javax.servlet.http.HttpServletRequest;
|
|
|
+import java.lang.reflect.Field;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertFalse;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
|
+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.ArgumentMatchers.anyLong;
|
|
|
+import static org.mockito.ArgumentMatchers.eq;
|
|
|
+import static org.mockito.Mockito.clearInvocations;
|
|
|
+import static org.mockito.Mockito.mock;
|
|
|
+import static org.mockito.Mockito.never;
|
|
|
+import static org.mockito.Mockito.verify;
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 契约级单测(真实载体行为向):个人资料编辑(能力键 {@code auth.profile.edit},S-cfc-285,TDD-RED 阶段)。
|
|
|
+ *
|
|
|
+ * <p><b>与同批 RED 测试的差异(互补而非重复)</b></p>
|
|
|
+ * <ul>
|
|
|
+ * <li>{@link AuthProfileEditContractTest}:反射探测「理想契约载体」是否存在
|
|
|
+ * ({@code AuthProfileService} / 专用编辑 DTO / {@code AuthController /profile/edit} / {@code @Pattern} 注解)。</li>
|
|
|
+ * <li>{@link AuthProfileEditBehaviorContractRedTest}:探测 {@code AuthProfileService#validatePhone/
|
|
|
+ * validateNickname} 的校验行为是否生效。</li>
|
|
|
+ * <li><b>本类</b>:锚定 {@code docs/superpowers/api/API_REFERENCE.md} §4.1 / §4.63.8 已经登记的
|
|
|
+ * <b>真实载体</b>——{@code UserController /api/user/update} → {@code UserService.updateUserInfo} →
|
|
|
+ * {@code UpdateUserDTO},以 Mockito 驱动真实 Service 方法,断言「手机绑定字段可透传 + 非法手机号/
|
|
|
+ * 非法昵称必须拦截且不落库」。GREEN 阶段无论是扩展既有载体还是新增 {@code AuthProfileService},
|
|
|
+ * 既有 {@code /api/user/update} 资料编辑入口都不应再静默接受非法值。</li>
|
|
|
+ * </ul>
|
|
|
+ *
|
|
|
+ * <p><b>契约锚点(溯源)</b></p>
|
|
|
+ * <ul>
|
|
|
+ * <li>需求:{@code tests/user-scenarios/应用场景总览.md} SC-P-64(编辑昵称/头像/绑定手机;
|
|
|
+ * 边界:手机格式校验);{@code tests/user-scenarios/02-parent.md} 场景 P-64(敏感字段二次确认);
|
|
|
+ * {@code tests/test-cases/parent.md} TC-P-64(保存生效)/ TC-P-64-E(非 11 位手机号 → 手机格式 400;
|
|
|
+ * 昵称 emoji 超长 → 拦截)/ TC-P-64-P(越权访问拒绝)。</li>
|
|
|
+ * <li>设计:{@code docs/superpowers/api/API_REFERENCE.md} §4.1、§4.61.7、§4.63.8
|
|
|
+ * ({@code POST /api/user/update} 为个人资料编辑现成载体,{@code UpdateUserDTO} 为请求 DTO)。</li>
|
|
|
+ * </ul>
|
|
|
+ *
|
|
|
+ * <p><b>实现现状(origin/cfclub 实测)</b>:{@code UserController.updateUser} 从请求 Map 白名单转写
|
|
|
+ * {@code UpdateUserDTO},<b>不含 phone</b>;{@code UpdateUserDTO} <b>无 phone 字段</b>;
|
|
|
+ * {@code UserService.updateUserInfo} 只写 nickname/avatar 等字段,<b>无任何昵称/手机校验</b>,
|
|
|
+ * 非法值直接 {@code userMapper.updateById(user)} 落库并返回 true。手机格式校验仅存在于
|
|
|
+ * {@code /api/user/profile/backfill}(信息回填,非资料编辑入口,且返回 code=500 而非 400)。</p>
|
|
|
+ *
|
|
|
+ * <p><b>RED 预期</b>:下述正常流/边界流 4 条断言全部失败(红),失败原因均为契约缺失而非编译/环境错误;
|
|
|
+ * 权限流为说明性用例(绿)。仅用 JDK + Mockito,无 Spring 容器 / 无 DB / 无外部服务。
|
|
|
+ * 若实现已意外落地导致全绿,按编排约定交登记,不私改实现。</p>
|
|
|
+ */
|
|
|
+class AuthProfileEditCarrierContractRedTest {
|
|
|
+
|
|
|
+ private static final Long USER_ID = 1001L;
|
|
|
+ private static final String PHONE_VALID = "13812345678";
|
|
|
+
|
|
|
+ @Mock
|
|
|
+ private UserMapper userMapper;
|
|
|
+
|
|
|
+ @Mock
|
|
|
+ private RealNameBackfillService realNameBackfillService;
|
|
|
+
|
|
|
+ @Mock
|
|
|
+ private OnboardingService onboardingService;
|
|
|
+
|
|
|
+ private UserService userService;
|
|
|
+
|
|
|
+ @BeforeEach
|
|
|
+ void setUp() {
|
|
|
+ MockitoAnnotations.openMocks(this);
|
|
|
+ userService = new UserService();
|
|
|
+ inject(userService, "userMapper", userMapper);
|
|
|
+ inject(userService, "realNameBackfillService", realNameBackfillService);
|
|
|
+ when(userMapper.selectById(anyLong())).thenReturn(existingUser());
|
|
|
+ }
|
|
|
+
|
|
|
+ // ====================================================================
|
|
|
+ // 正常流:编辑昵称/头像/绑定手机 → 真实载体应能承载并透传 phone
|
|
|
+ // ====================================================================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流-1:/api/user/update 的 UpdateUserDTO 应承载 phone 字段(绑定手机主流程)")
|
|
|
+ void normalFlow_updateDtoCarriesPhone() {
|
|
|
+ Field phone = requireField(UpdateUserDTO.class, "phone");
|
|
|
+ assertNotNull(phone, "[auth.profile.edit/SC-P-64] UpdateUserDTO 应含 phone 字段(绑定手机)。");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流-2:UserController.updateUser 应把 phone 参数透传到编辑 DTO")
|
|
|
+ void normalFlow_controllerForwardsPhoneParam() {
|
|
|
+ UserService svcMock = mock(UserService.class);
|
|
|
+ UserController controller = controllerWith(svcMock);
|
|
|
+ HttpServletRequest request = mock(HttpServletRequest.class);
|
|
|
+ when(request.getAttribute("userId")).thenReturn(USER_ID);
|
|
|
+
|
|
|
+ Map<String, Object> params = new HashMap<String, Object>();
|
|
|
+ params.put("phone", PHONE_VALID);
|
|
|
+
|
|
|
+ controller.updateUser(request, params);
|
|
|
+
|
|
|
+ ArgumentCaptor<UpdateUserDTO> captor = ArgumentCaptor.forClass(UpdateUserDTO.class);
|
|
|
+ verify(svcMock).updateUserInfo(eq(USER_ID), captor.capture());
|
|
|
+ UpdateUserDTO captured = captor.getValue();
|
|
|
+
|
|
|
+ Field phone = requireField(captured.getClass(), "phone");
|
|
|
+ try {
|
|
|
+ phone.setAccessible(true);
|
|
|
+ assertEquals(PHONE_VALID, phone.get(captured),
|
|
|
+ "[auth.profile.edit/SC-P-64] /api/user/update 应将请求 phone 参数写入编辑 DTO"
|
|
|
+ + "(SC-P-64 主流程:绑定手机)。当前白名单转写未包含 phone。");
|
|
|
+ } catch (IllegalAccessException e) {
|
|
|
+ fail("[auth.profile.edit/SC-P-64] 读取编辑 DTO.phone 失败。", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // ====================================================================
|
|
|
+ // 边界流(行为):非法手机号 / emoji 超长昵称必须拦截且不落库
|
|
|
+ // ====================================================================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("边界流-1:资料编辑应拒绝非11位/非法号段手机号且不落库(TC-P-64-E)")
|
|
|
+ void boundaryFlow_rejectsInvalidPhoneFormats() {
|
|
|
+ Field phone = requireField(UpdateUserDTO.class, "phone");
|
|
|
+ String[] badPhones = {"123", "1381234567", "138123456789", "12345678901", "+8613812345678"};
|
|
|
+ for (String bad : badPhones) {
|
|
|
+ clearInvocations(userMapper);
|
|
|
+ UpdateUserDTO dto = new UpdateUserDTO();
|
|
|
+ setField(dto, phone, bad);
|
|
|
+ assertEditRejected("手机号 " + bad, dto);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("边界流-2:资料编辑应拒绝 emoji 与超长昵称且不落库(TC-P-64-E)")
|
|
|
+ void boundaryFlow_rejectsEmojiAndOverlongNickname() {
|
|
|
+ char[] chars = new char[80];
|
|
|
+ Arrays.fill(chars, 'a');
|
|
|
+ String[] badNicknames = {"开心\uD83D\uDE00", new String(chars)};
|
|
|
+ for (String bad : badNicknames) {
|
|
|
+ clearInvocations(userMapper);
|
|
|
+ UpdateUserDTO dto = new UpdateUserDTO();
|
|
|
+ dto.setNickname(bad);
|
|
|
+ assertEditRejected("昵称(" + bad.length() + "字符)", dto);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // ====================================================================
|
|
|
+ // 权限流(适用性说明):TC-P-64-P
|
|
|
+ // ====================================================================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("权限流:资料编辑仅作用于 JWT userId 本人,无跨用户业务权限维度")
|
|
|
+ void permissionFlow_selfProfileOnly() {
|
|
|
+ // UserController.updateUser 从 HttpServletRequest 的 userId 属性取当前登录账号(非请求体入参),
|
|
|
+ // 与 TC-P-64-P「他人数据访问统一 401/403」由 JwtInterceptor 统一门禁覆盖一致,无独立断言。
|
|
|
+ assertTrue(true, "权限流说明:auth.profile.edit 为登录账号自身资料编辑,权限随 JWT userId。");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ====================================================================
|
|
|
+ // 辅助
|
|
|
+ // ====================================================================
|
|
|
+
|
|
|
+ /** 断言编辑被拒绝:返回 false 或抛 RuntimeException,且绝不落库。 */
|
|
|
+ private void assertEditRejected(String label, UpdateUserDTO dto) {
|
|
|
+ try {
|
|
|
+ boolean saved = userService.updateUserInfo(USER_ID, dto);
|
|
|
+ assertFalse(saved,
|
|
|
+ "[auth.profile.edit/SC-P-64] " + label + " 应被资料编辑入口拒绝(保存生效前置校验),"
|
|
|
+ + "契约要求非法输入拦截且不落库。当前静默接受并保存。");
|
|
|
+ } catch (RuntimeException expected) {
|
|
|
+ // 校验器抛异常即视为拒绝
|
|
|
+ }
|
|
|
+ verify(userMapper, never()).updateById(any(User.class));
|
|
|
+ }
|
|
|
+
|
|
|
+ private UserController controllerWith(UserService svc) {
|
|
|
+ UserController controller = new UserController();
|
|
|
+ inject(controller, "userService", svc);
|
|
|
+ inject(controller, "onboardingService", onboardingService);
|
|
|
+ return controller;
|
|
|
+ }
|
|
|
+
|
|
|
+ private User existingUser() {
|
|
|
+ User user = new User();
|
|
|
+ user.setId(USER_ID);
|
|
|
+ user.setRole("parent");
|
|
|
+ user.setNickname("原昵称");
|
|
|
+ return user;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Field requireField(Class<?> clazz, String name) {
|
|
|
+ Field field = findField(clazz, name);
|
|
|
+ if (field == null) {
|
|
|
+ return fail("[auth.profile.edit/SC-P-64] " + clazz.getSimpleName()
|
|
|
+ + " 缺少 " + name + " 字段:SC-P-64 主流程要求编辑昵称/头像/绑定手机,"
|
|
|
+ + "当前 /api/user/update 资料编辑载体无法承载该字段。契约未落地。");
|
|
|
+ }
|
|
|
+ return field;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Field findField(Class<?> clazz, String name) {
|
|
|
+ Class<?> current = clazz;
|
|
|
+ while (current != null) {
|
|
|
+ try {
|
|
|
+ return current.getDeclaredField(name);
|
|
|
+ } catch (NoSuchFieldException e) {
|
|
|
+ current = current.getSuperclass();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void setField(Object target, Field field, Object value) {
|
|
|
+ try {
|
|
|
+ field.setAccessible(true);
|
|
|
+ field.set(target, value);
|
|
|
+ } catch (IllegalAccessException e) {
|
|
|
+ fail("[auth.profile.edit/SC-P-64] 无法写入字段 " + field.getName(), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void inject(Object target, String fieldName, Object value) {
|
|
|
+ Class<?> current = target.getClass();
|
|
|
+ while (current != null) {
|
|
|
+ try {
|
|
|
+ Field field = current.getDeclaredField(fieldName);
|
|
|
+ field.setAccessible(true);
|
|
|
+ field.set(target, value);
|
|
|
+ return;
|
|
|
+ } catch (NoSuchFieldException e) {
|
|
|
+ current = current.getSuperclass();
|
|
|
+ } catch (IllegalAccessException e) {
|
|
|
+ fail("[auth.profile.edit/SC-P-64] 无法注入测试替身字段 " + fieldName, e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ fail("[auth.profile.edit/SC-P-64] 未找到字段 " + fieldName + " 于 " + target.getClass().getName());
|
|
|
+ }
|
|
|
+}
|