|
@@ -0,0 +1,295 @@
|
|
|
|
|
+package com.etotem.cfc.unit;
|
|
|
|
|
+
|
|
|
|
|
+import com.etotem.cfc.controller.auth.AuthController;
|
|
|
|
|
+import org.junit.jupiter.api.DisplayName;
|
|
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
|
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
|
|
|
+
|
|
|
|
|
+import java.lang.annotation.Annotation;
|
|
|
|
|
+import java.lang.reflect.Field;
|
|
|
|
|
+import java.lang.reflect.Method;
|
|
|
|
|
+
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.fail;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 契约级单测:个人资料编辑(能力键 {@code auth.profile.edit},S-cfc-285,TDD-RED 阶段)。
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p><b>契约锚点(溯源)</b></p>
|
|
|
|
|
+ * <ul>
|
|
|
|
|
+ * <li>需求:{@code tests/user-scenarios/应用场景总览.md} SC-P-64(个人资料编辑——
|
|
|
|
|
+ * <b>主流程:编辑昵称/头像/绑定手机;边界:手机格式校验;敏感字段二次确认</b>);
|
|
|
|
|
+ * {@code tests/user-scenarios/02-parent.md} SC-P-64、
|
|
|
|
|
+ * {@code tests/test-cases/parent.md} TC-P-64 / TC-P-64-E(绑手机填非 11 位 → 手机格式 400;
|
|
|
|
|
+ * 昵称含 emoji 超长 → 昵称超长/非法字符拦截)/ TC-P-64-P;业务域 H 设置与通知。</li>
|
|
|
|
|
+ * <li>设计:{@code docs/superpowers/api/API_REFERENCE.md} §4.1 认证与用户({@code /api/auth}、{@code /api/user})、
|
|
|
|
|
+ * §4.63.8(SC-P-64 个人资料编辑载体);编排键表 {@code sc-key-map.md}:
|
|
|
|
|
+ * SC-P-64 → {@code auth.profile.edit}(模块 {@code AuthController})。</li>
|
|
|
|
|
+ * </ul>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p><b>实现现状(origin/cfclub 实测)</b>:本分支仅有<b>分散的部分载体</b>,未形成 {@code auth.profile.edit}
|
|
|
|
|
+ * 完整契约:{@code UserController /api/user/update} 可改 nickname/avatar 但 {@code UpdateUserDTO}
|
|
|
|
|
+ * <b>无 phone 字段</b>;{@code /api/user/profile/backfill} 虽含 phone 正则校验,但走 {@code Result.error}
|
|
|
|
|
+ * (code=500),且属「信息回填」而非「个人资料编辑」入口;{@code AuthController}(18 个 @PostMapping)
|
|
|
|
|
+ * <b>无任何 profile 编辑端点</b>;全库无 {@code AuthProfileService}、无专用编辑 DTO、
|
|
|
|
|
+ * 无昵称 emoji/超长校验、无敏感字段(手机)二次确认载体。契约能力整体未落地。</p>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p><b>契约方法签名(实现方按此落地,GREEN 指引)</b></p>
|
|
|
|
|
+ * <ul>
|
|
|
|
|
+ * <li>{@code com.etotem.cfc.service.AuthProfileService}:
|
|
|
|
|
+ * {@code Result<Boolean> editProfile(Long userId, AuthProfileEditDTO dto)} —— 昵称/头像/绑定手机统一编辑入口;
|
|
|
|
|
+ * {@code void validatePhone(String phone)} —— 手机格式校验({@code ^1[3-9]\d{9}$});
|
|
|
|
|
+ * {@code void validateNickname(String nickname)} —— 昵称长度/非法字符(emoji)拦截;
|
|
|
|
|
+ * {@code boolean confirmSensitiveChange(Long userId, String phone, String verifyCode)} —— 敏感字段二次确认。</li>
|
|
|
|
|
+ * <li>{@code com.etotem.cfc.dto.AuthProfileEditDTO}:{@code nickname} / {@code avatar} / {@code phone} /
|
|
|
|
|
+ * {@code verifyCode} 字段,{@code phone} 携带 {@code @Pattern} 校验(非法 → 400)。</li>
|
|
|
|
|
+ * <li>{@code AuthController}:新增 {@code @PostMapping("/profile/edit")} 端点,注入
|
|
|
|
|
+ * {@code AuthProfileService} 并调用 {@code editProfile};非法手机/昵称返回 {@code Result.badRequest}(400)。</li>
|
|
|
|
|
+ * </ul>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p><b>测试范围</b>:纯契约级单测,反射锚定缺失实现 + 注解/字段结构探测
|
|
|
|
|
+ * (参考 {@code AuthSessionMutexContractTest} / {@code TeamProfitHookContractTest} 风格),
|
|
|
|
|
+ * 无 Spring 容器 / 无 DB / 无外部服务。</p>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p><b>RED 预期</b>:下述正常流/边界流/异常流断言应失败(红),失败断言原文清晰可贴;
|
|
|
|
|
+ * 权限流为说明性用例(绿)。若实现已意外落地导致全绿,按编排约定交登记,不私改实现。</p>
|
|
|
|
|
+ */
|
|
|
|
|
+class AuthProfileEditContractTest {
|
|
|
|
|
+
|
|
|
|
|
+ private static final String AUTH_PROFILE_SERVICE = "com.etotem.cfc.service.AuthProfileService";
|
|
|
|
|
+
|
|
|
|
|
+ private static final String AUTH_PROFILE_EDIT_DTO = "com.etotem.cfc.dto.AuthProfileEditDTO";
|
|
|
|
|
+
|
|
|
|
|
+ private static final String[] DTO_CANDIDATES = {
|
|
|
|
|
+ AUTH_PROFILE_EDIT_DTO,
|
|
|
|
|
+ "com.etotem.cfc.dto.ProfileEditDTO",
|
|
|
|
|
+ "com.etotem.cfc.dto.UpdateProfileDTO",
|
|
|
|
|
+ "com.etotem.cfc.dto.EditProfileDTO"
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ // ====================================================================
|
|
|
|
|
+ // 正常流(主流程):编辑昵称 / 头像 / 绑定手机
|
|
|
|
|
+ // ====================================================================
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("正常流-1:AuthController 应暴露个人资料编辑 POST 端点(昵称/头像/绑定手机)")
|
|
|
|
|
+ void authController_exposesProfileEditPostEndpoint() {
|
|
|
|
|
+ assertTrue(hasPostMappingContaining(AuthController.class, "profile"),
|
|
|
|
|
+ "[auth.profile.edit/SC-P-64] AuthController 应有 @PostMapping 个人资料编辑端点"
|
|
|
|
|
+ + "(如 /api/auth/profile/edit,承载编辑昵称/头像/绑定手机)。当前 18 个 POST 端点均无 profile 编辑路径。"
|
|
|
|
|
+ + "实现未落地。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("正常流-2:AuthController 应注入 AuthProfileService(编辑入口委托服务层)")
|
|
|
|
|
+ void authController_injectsAuthProfileService() {
|
|
|
|
|
+ assertTrue(hasFieldOfType(AuthController.class, AUTH_PROFILE_SERVICE),
|
|
|
|
|
+ "[auth.profile.edit/SC-P-64] AuthController 应注入 AuthProfileService 钩子,"
|
|
|
|
|
+ + "将资料编辑(昵称/头像/手机)委托服务层处理(Controller→Service 分层约定)。"
|
|
|
|
|
+ + "当前无该服务类与注入字段。实现未落地。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("正常流-3:AuthProfileService 应提供 editProfile 统一编辑入口")
|
|
|
|
|
+ void authProfileService_hasEditProfileEntry() {
|
|
|
|
|
+ Class<?> clazz = loadClass(AUTH_PROFILE_SERVICE);
|
|
|
|
|
+ if (clazz == null) {
|
|
|
|
|
+ fail("[auth.profile.edit/SC-P-64] AuthProfileService 类不存在,无法验证 editProfile 入口"
|
|
|
|
|
+ + "(编辑昵称/头像/绑定手机统一入口)。实现未落地。");
|
|
|
|
|
+ }
|
|
|
|
|
+ assertTrue(hasMethodNamed(clazz, "editProfile") || hasMethodNamed(clazz, "updateProfile"),
|
|
|
|
|
+ "[auth.profile.edit/SC-P-64] AuthProfileService 应提供 editProfile/updateProfile(...)"
|
|
|
|
|
+ + " 作为个人资料编辑统一入口(SC-P-64 主流程)。实现未落地。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("正常流-4:专用编辑 DTO 应同时承载 nickname / avatar / phone 三字段")
|
|
|
|
|
+ void profileEditDto_carriesNicknameAvatarPhone() {
|
|
|
|
|
+ Class<?> dto = findProfileEditDto();
|
|
|
|
|
+ if (dto == null) {
|
|
|
|
|
+ fail("[auth.profile.edit/SC-P-64] 专用个人资料编辑 DTO 不存在(如 AuthProfileEditDTO):"
|
|
|
|
|
+ + "应同时承载 nickname/avatar/phone 三字段(SC-P-64 主流程)。"
|
|
|
|
|
+ + "现有 UpdateUserDTO 无 phone 字段,无法绑定手机。实现未落地。");
|
|
|
|
|
+ }
|
|
|
|
|
+ assertTrue(hasField(dto, "nickname"), "[auth.profile.edit/SC-P-64] 编辑 DTO 应含 nickname 字段。实现未落地。");
|
|
|
|
|
+ assertTrue(hasField(dto, "avatar"), "[auth.profile.edit/SC-P-64] 编辑 DTO 应含 avatar 字段。实现未落地。");
|
|
|
|
|
+ assertTrue(hasField(dto, "phone"), "[auth.profile.edit/SC-P-64] 编辑 DTO 应含 phone 字段(绑定手机)。实现未落地。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ====================================================================
|
|
|
|
|
+ // 边界流:手机格式校验 / 昵称边界 / 敏感字段二次确认
|
|
|
|
|
+ // ====================================================================
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("边界流-1:资料编辑链路应具备手机号格式校验能力(^1[3-9]\\d{9}$)")
|
|
|
|
|
+ void profileEdit_hasPhoneFormatValidation() {
|
|
|
|
|
+ Class<?> clazz = loadClass(AUTH_PROFILE_SERVICE);
|
|
|
|
|
+ boolean hasValidator = false;
|
|
|
|
|
+ if (clazz != null) {
|
|
|
|
|
+ for (Method m : clazz.getDeclaredMethods()) {
|
|
|
|
|
+ String n = m.getName().toLowerCase();
|
|
|
|
|
+ if (n.contains("validatephone") || n.contains("checkphone") || n.contains("phoneformat")) {
|
|
|
|
|
+ hasValidator = true;
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ assertTrue(clazz != null && hasValidator,
|
|
|
|
|
+ "[auth.profile.edit/SC-P-64] 资料编辑链路应有 validatePhone/checkPhoneFormat 等手机格式校验能力"
|
|
|
|
|
+ + "(^1[3-9]\\d{9}$,非 11 位拒绝,TC-P-64-E)。当前仅 /api/user/profile/backfill 内含正则,"
|
|
|
|
|
+ + "编辑入口无校验。实现未落地。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("边界流-2:资料编辑链路应具备昵称长度/emoji 非法字符拦截能力")
|
|
|
|
|
+ void profileEdit_hasNicknameBoundaryValidation() {
|
|
|
|
|
+ Class<?> clazz = loadClass(AUTH_PROFILE_SERVICE);
|
|
|
|
|
+ boolean hasValidator = false;
|
|
|
|
|
+ if (clazz != null) {
|
|
|
|
|
+ for (Method m : clazz.getDeclaredMethods()) {
|
|
|
|
|
+ String n = m.getName().toLowerCase();
|
|
|
|
|
+ if (n.contains("nickname") && (n.contains("valid") || n.contains("check"))) {
|
|
|
|
|
+ hasValidator = true;
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ assertTrue(clazz != null && hasValidator,
|
|
|
|
|
+ "[auth.profile.edit/SC-P-64] 资料编辑链路应有 validateNickname/checkNickname 等昵称校验能力"
|
|
|
|
|
+ + "(超长/emoji 非法字符拦截,TC-P-64-E)。当前 src/main 无任何昵称校验载体。实现未落地。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("边界流-3:敏感字段(绑定手机)应有二次确认载体(verifyCode / confirm)")
|
|
|
|
|
+ void profileEdit_hasSensitiveSecondConfirmation() {
|
|
|
|
|
+ boolean hasConfirm = false;
|
|
|
|
|
+ Class<?> service = loadClass(AUTH_PROFILE_SERVICE);
|
|
|
|
|
+ if (service != null) {
|
|
|
|
|
+ for (Method m : service.getDeclaredMethods()) {
|
|
|
|
|
+ String n = m.getName().toLowerCase();
|
|
|
|
|
+ if (n.contains("confirm") || n.contains("second") || n.contains("verifycode")) {
|
|
|
|
|
+ hasConfirm = true;
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ Class<?> dto = findProfileEditDto();
|
|
|
|
|
+ boolean hasVerifyCodeField = dto != null && (hasField(dto, "verifyCode") || hasField(dto, "code"));
|
|
|
|
|
+ assertTrue(hasConfirm || hasVerifyCodeField,
|
|
|
|
|
+ "[auth.profile.edit/SC-P-64] 敏感字段(绑定手机)应有二次确认载体:"
|
|
|
|
|
+ + "服务层 confirmSensitiveChange/verifyCode 方法,或编辑 DTO 的 verifyCode/code 字段"
|
|
|
|
|
+ + "(02-parent.md SC-P-64 边界:敏感字段二次确认)。当前无。实现未落地。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ====================================================================
|
|
|
|
|
+ // 异常流(TC-P-64-E):非法手机 400 / 昵称 emoji 超长拦截
|
|
|
|
|
+ // ====================================================================
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("异常流-1:编辑 DTO 的 phone 字段应携带格式校验注解(非法 → 400)")
|
|
|
|
|
+ void profileEdit_invalidPhoneRejects400() {
|
|
|
|
|
+ Class<?> dto = findProfileEditDto();
|
|
|
|
|
+ if (dto == null) {
|
|
|
|
|
+ fail("[auth.profile.edit/SC-P-64] 编辑 DTO 不存在,无法验证 phone 格式校验注解"
|
|
|
|
|
+ + "(非 11 位 → 手机格式 400,TC-P-64-E)。实现未落地。");
|
|
|
|
|
+ }
|
|
|
|
|
+ Field phone = findField(dto, "phone");
|
|
|
|
|
+ assertNotNull(phone, "[auth.profile.edit/SC-P-64] 编辑 DTO 应含 phone 字段。实现未落地。");
|
|
|
|
|
+ boolean hasPattern = false;
|
|
|
|
|
+ for (Annotation a : phone.getAnnotations()) {
|
|
|
|
|
+ String text = a.toString();
|
|
|
|
|
+ if (text.contains("Pattern") || text.contains("1[3-9]") || text.contains("regex")) {
|
|
|
|
|
+ hasPattern = true;
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ assertTrue(hasPattern,
|
|
|
|
|
+ "[auth.profile.edit/SC-P-64] 编辑 DTO.phone 应携带 @Pattern 等格式校验注解"
|
|
|
|
|
+ + "(^1[3-9]\\d{9}$,非法手机号由 Result.badRequest 返回 400,TC-P-64-E)。"
|
|
|
|
|
+ + "当前无编辑 DTO。实现未落地。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ====================================================================
|
|
|
|
|
+ // 权限流(适用性说明)
|
|
|
|
|
+ // ====================================================================
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("权限流:资料编辑仅作用于登录账号自身(JWT userId),无跨用户业务权限维度")
|
|
|
|
|
+ void permissionFlow_selfProfileOnly() {
|
|
|
|
|
+ // 说明性用例:编辑昵称/头像/绑定手机均针对 JwtInterceptor 注入的当前 userId 自身,
|
|
|
|
|
+ // 权限随 JWT 认证本身(TC-P-64-P 的他人数据越权由统一 401/403 门禁覆盖),无独立权限断言。
|
|
|
|
|
+ // 与 AuthSessionMutexContractTest.permissionFlow_sessionMutexIsAuthLifecycle 同风格。
|
|
|
|
|
+ assertTrue(true, "权限流说明:auth.profile.edit 为登录账号自身资料编辑,权限随 JWT userId,无独立业务权限维度。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ====================================================================
|
|
|
|
|
+ // 反射辅助
|
|
|
|
|
+ // ====================================================================
|
|
|
|
|
+
|
|
|
|
|
+ private Class<?> loadClass(String name) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ return Class.forName(name, false, Thread.currentThread().getContextClassLoader());
|
|
|
|
|
+ } catch (ClassNotFoundException e) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Class<?> findProfileEditDto() {
|
|
|
|
|
+ for (String name : DTO_CANDIDATES) {
|
|
|
|
|
+ Class<?> clazz = loadClass(name);
|
|
|
|
|
+ if (clazz != null) {
|
|
|
|
|
+ return clazz;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private boolean hasField(Class<?> clazz, String name) {
|
|
|
|
|
+ return findField(clazz, name) != null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Field findField(Class<?> clazz, String name) {
|
|
|
|
|
+ for (Field f : clazz.getDeclaredFields()) {
|
|
|
|
|
+ if (f.getName().equals(name)) {
|
|
|
|
|
+ return f;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private boolean hasMethodNamed(Class<?> clazz, String name) {
|
|
|
|
|
+ for (Method m : clazz.getDeclaredMethods()) {
|
|
|
|
|
+ if (m.getName().equals(name)) {
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private boolean hasFieldOfType(Class<?> clazz, String fieldTypeFqcn) {
|
|
|
|
|
+ for (Field f : clazz.getDeclaredFields()) {
|
|
|
|
|
+ if (fieldTypeFqcn.equals(f.getType().getName())) {
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 探测指定类是否声明了 value/path 含关键字的 @PostMapping(统一 POST 约定)。 */
|
|
|
|
|
+ private boolean hasPostMappingContaining(Class<?> clazz, String keyword) {
|
|
|
|
|
+ for (Method m : clazz.getDeclaredMethods()) {
|
|
|
|
|
+ PostMapping mapping = m.getAnnotation(PostMapping.class);
|
|
|
|
|
+ if (mapping == null) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ String[] paths = mapping.value().length > 0 ? mapping.value() : mapping.path();
|
|
|
|
|
+ for (String path : paths) {
|
|
|
|
|
+ if (path != null && path.contains(keyword)) {
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|