|
@@ -0,0 +1,475 @@
|
|
|
|
|
+package com.etotem.cfc.unit;
|
|
|
|
|
+
|
|
|
|
|
+import com.etotem.cfc.common.Result;
|
|
|
|
|
+import com.etotem.cfc.config.JwtInterceptor;
|
|
|
|
|
+import org.junit.jupiter.api.DisplayName;
|
|
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
|
|
+import org.mockito.Mockito;
|
|
|
|
|
+import org.springframework.web.bind.annotation.DeleteMapping;
|
|
|
|
|
+import org.springframework.web.bind.annotation.GetMapping;
|
|
|
|
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
|
|
|
+import org.springframework.web.bind.annotation.PutMapping;
|
|
|
|
|
+import org.springframework.web.bind.annotation.RequestAttribute;
|
|
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
|
+
|
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
+import java.io.IOException;
|
|
|
|
|
+import java.io.PrintWriter;
|
|
|
|
|
+import java.io.StringWriter;
|
|
|
|
|
+import java.lang.annotation.Annotation;
|
|
|
|
|
+import java.lang.reflect.Field;
|
|
|
|
|
+import java.lang.reflect.InvocationTargetException;
|
|
|
|
|
+import java.lang.reflect.Method;
|
|
|
|
|
+import java.lang.reflect.Modifier;
|
|
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
|
|
+import java.nio.file.Files;
|
|
|
|
|
+import java.nio.file.Path;
|
|
|
|
|
+import java.nio.file.Paths;
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
|
+import java.util.HashMap;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+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.Mockito.mock;
|
|
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 契约级单测(TDD-RED):爱云医 AI 检测接入层 Controller(转发代理),能力键
|
|
|
|
|
+ * {@code ai.aiyunyi.controller}(任务 S-cfc-452)。
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p><b>溯源</b></p>
|
|
|
|
|
+ * <ul>
|
|
|
|
|
+ * <li>需求:{@code docs/2026-11-24-爱云医AI检测接入方案.md} §4.1/§4.2 后端改动清单 ——
|
|
|
|
|
+ * {@code AicdsController} 以 {@code POST /api/aiyunyi/**} 作为前端转发代理,
|
|
|
|
|
+ * 统一收口爱云医接口调用;{@code docs/爱云医接入/爱云医接口契约-完整版.md} §A.1/§A.2/§A.4 接口约定。</li>
|
|
|
|
|
+ * <li>设计:同上 §4.1 —— 复用 {@code JwtInterceptor} + {@code @RequestAttribute("role")} 做登录/角色校验,
|
|
|
|
|
+ * 控制器只做「取参 → 调编排服务 → 包 {@link Result}」,不直接操作 Mapper / 不绕过服务层;
|
|
|
|
|
+ * 错误统一归一为 {@code Result} 契约(code/message/data),不得把原始异常/第三方报文直出前端。</li>
|
|
|
|
|
+ * <li>同批 RED 契约测试:{@code AiyunyiSignContractTest}(S-cfc-447)、
|
|
|
|
|
+ * {@code AiyunyiTokenContractTest}(S-cfc-448)、{@code AiyunyiGatewayContractTest}(S-cfc-449)、
|
|
|
|
|
+ * {@code AiYunyiDetectContractTest}(S-cfc-450)、{@code AiYunyiDetectVideoContractTest}(S-cfc-451)。</li>
|
|
|
|
|
+ * </ul>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p><b>契约 AC(GREEN 阶段实现方按此落地,方法名/注解/返回类型即测试锚点)</b></p>
|
|
|
|
|
+ * <ol>
|
|
|
|
|
+ * <li>AC1 控制器 {@code com.etotem.cfc.controller.AicdsController} 存在
|
|
|
|
|
+ * (候选 {@link #CONTROLLER_CANDIDATES}),无参可实例化。</li>
|
|
|
|
|
+ * <li>AC2 控制器标注 {@code @RestController}(或 {@code @Controller}),并声明类级
|
|
|
|
|
+ * {@code @RequestMapping("/api/aiyunyi")};端点全路径 = 前缀 + 方法级 {@code @PostMapping}。</li>
|
|
|
|
|
+ * <li>AC3 提供 {@code detect} 端点承接 {@code POST /api/aiyunyi/detect}(§5 照片路径时序),
|
|
|
|
|
+ * 方法级映射为 {@code @PostMapping("/detect")} 或等价全路径。</li>
|
|
|
|
|
+ * <li>AC4 所有端点方法用 {@code @PostMapping},禁止 {@code @GetMapping/@PutMapping/@DeleteMapping}
|
|
|
|
|
+ * (项目统一 POST 约定)。</li>
|
|
|
|
|
+ * <li>AC5 控制器通过 {@code @Resource} 持有编排/网关服务字段(候选 {@link #SERVICE_CANDIDATES}),
|
|
|
|
|
+ * 可由 Mockito mock 注入以做纯单测。</li>
|
|
|
|
|
+ * <li>AC6 所有端点方法返回统一 {@link Result}(或其子类),不得返回裸 Map / String / 第三方 DTO。</li>
|
|
|
|
|
+ * <li>AC7 端点方法含 {@code @RequestAttribute("role")} 参数(复用 JwtInterceptor 注入的 role),
|
|
|
|
|
+ * 据此做角色校验:无权角色返回 {@code Result.forbidden}(code=403),不进入服务层。</li>
|
|
|
|
|
+ * <li>AC8 正常流:端点取参 → 调服务编排 → 返回 {@code Result.success}(code=200),
|
|
|
|
|
+ * 且服务方法确有被调用(转发代理而非空壳)。</li>
|
|
|
|
|
+ * <li>AC9 鉴权接线:{@code JwtInterceptor.LOGIN_REQUIRED_PATHS} 收录 {@code /api/aiyunyi/},
|
|
|
|
|
+ * {@code PUBLIC_PATHS} 与 {@code WebConfig} 的 jwtInterceptor exclude 均不含该前缀;
|
|
|
|
|
+ * 未登录请求 {@code POST /api/aiyunyi/detect} 被拦截并返回 401。</li>
|
|
|
|
|
+ * </ol>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p><b>实现现状(RED 依据)</b>:本分支 {@code src/main} 中 {@code AicdsController / aiyunyi} 0 引用,
|
|
|
|
|
+ * 控制器未落地,{@code JwtInterceptor.LOGIN_REQUIRED_PATHS} 亦未收录 {@code /api/aiyunyi/}。
|
|
|
|
|
+ * 测试通过反射 + Mockito + 源码读取锚定契约(避免 import 不存在类导致整批测试编译失败),
|
|
|
|
|
+ * 可编译、可运行,断言失败(红)且失败原文明确指出「实现未落地」。GREEN 阶段补齐后自动转绿。</p>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p><b>测试风格</b>:纯 Mockito + 反射 + 源码读取,无 Spring 上下文 / 无 DB / 无网络 / 无真实密钥。
|
|
|
|
|
+ * 「未登录/无权/正常」以拦截器行为 + 控制器反射调用覆盖,不依赖 MockMvc 容器。</p>
|
|
|
|
|
+ */
|
|
|
|
|
+class AiyunyiControllerContractTest {
|
|
|
|
|
+
|
|
|
|
|
+ private static final String CONTROLLER_FQCN = "com.etotem.cfc.controller.AicdsController";
|
|
|
|
|
+ private static final String[] CONTROLLER_CANDIDATES = {
|
|
|
|
|
+ CONTROLLER_FQCN,
|
|
|
|
|
+ "com.etotem.cfc.controller.AiYunyiController",
|
|
|
|
|
+ "com.etotem.cfc.controller.AiyunyiController",
|
|
|
|
|
+ "com.etotem.cfc.controller.aiyunyi.AicdsController"
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ private static final String[] SERVICE_CANDIDATES = {
|
|
|
|
|
+ "com.etotem.cfc.service.AicdsDetectionService",
|
|
|
|
|
+ "com.etotem.cfc.service.AicdsGatewayService",
|
|
|
|
|
+ "com.etotem.cfc.service.AiYunyiService",
|
|
|
|
|
+ "com.etotem.cfc.service.AiyunyiService"
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ private static final String CONTROLLER_SOURCE =
|
|
|
|
|
+ "src/main/java/com/etotem/cfc/controller/AicdsController.java";
|
|
|
|
|
+ private static final String WEB_CONFIG_SOURCE =
|
|
|
|
|
+ "src/main/java/com/etotem/cfc/config/WebConfig.java";
|
|
|
|
|
+
|
|
|
|
|
+ private static final String BASE_PATH = "/api/aiyunyi";
|
|
|
|
|
+ private static final String DETECT_PATH_SUFFIX = "/detect";
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 正常流:控制器结构契约 ====================
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("正常流-1:转发代理控制器 AicdsController 应存在(§4.1/§4.2)")
|
|
|
|
|
+ void controllerClass_exists() {
|
|
|
|
|
+ assertNotNull(loadControllerClass(),
|
|
|
|
|
+ "[ai.aiyunyi.controller] " + CONTROLLER_FQCN + "(或候选控制器)不存在:"
|
|
|
|
|
+ + "§4.1 要求以 POST /api/aiyunyi/** 作为爱云医前端转发代理。实现未落地。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("正常流-2:控制器应标注 @RestController 并声明类级 @RequestMapping(/api/aiyunyi)")
|
|
|
|
|
+ void controller_hasRestControllerAndBaseMapping() {
|
|
|
|
|
+ Class<?> clazz = requireController();
|
|
|
|
|
+ assertTrue(clazz.isAnnotationPresent(org.springframework.web.bind.annotation.RestController.class)
|
|
|
|
|
+ || clazz.isAnnotationPresent(org.springframework.stereotype.Controller.class),
|
|
|
|
|
+ "[ai.aiyunyi.controller] 控制器应标注 @RestController(或 @Controller)。");
|
|
|
|
|
+ RequestMapping rm = clazz.getAnnotation(RequestMapping.class);
|
|
|
|
|
+ assertNotNull(rm,
|
|
|
|
|
+ "[ai.aiyunyi.controller] 控制器应声明类级 @RequestMapping(\"" + BASE_PATH + "\") "
|
|
|
|
|
+ + "以统一 /api/aiyunyi/** 路由。");
|
|
|
|
|
+ boolean hasBase = false;
|
|
|
|
|
+ for (String value : rm.value()) {
|
|
|
|
|
+ if (BASE_PATH.equals(value) || BASE_PATH.equals(value.replaceAll("/$", ""))) {
|
|
|
|
|
+ hasBase = true;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ assertTrue(hasBase,
|
|
|
|
|
+ "[ai.aiyunyi.controller] 类级映射前缀应为 " + BASE_PATH + ",实际 " + String.join(",", rm.value()));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("正常流-3:应提供 detect 端点承接 POST /api/aiyunyi/detect(§5)")
|
|
|
|
|
+ void detectEndpoint_existsWithPostMapping() {
|
|
|
|
|
+ Class<?> clazz = requireController();
|
|
|
|
|
+ Method detect = findMethodByName(clazz, "detect");
|
|
|
|
|
+ assertNotNull(detect,
|
|
|
|
|
+ "[ai.aiyunyi.controller] 缺少 detect(...) 端点方法:应承接 POST "
|
|
|
|
|
+ + BASE_PATH + DETECT_PATH_SUFFIX + "(照片路径时序入口)。实现未落地。");
|
|
|
|
|
+ PostMapping pm = detect.getAnnotation(PostMapping.class);
|
|
|
|
|
+ assertNotNull(pm,
|
|
|
|
|
+ "[ai.aiyunyi.controller] detect 必须标注 @PostMapping(项目统一 POST 约定)。");
|
|
|
|
|
+ boolean mapped = false;
|
|
|
|
|
+ for (String path : pm.value()) {
|
|
|
|
|
+ if (DETECT_PATH_SUFFIX.equals(path)
|
|
|
|
|
+ || (BASE_PATH + DETECT_PATH_SUFFIX).equals(path)) {
|
|
|
|
|
+ mapped = true;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ assertTrue(mapped,
|
|
|
|
|
+ "[ai.aiyunyi.controller] detect 应映射 " + DETECT_PATH_SUFFIX
|
|
|
|
|
+ + "(全路径 " + BASE_PATH + DETECT_PATH_SUFFIX + "),实际 " + String.join(",", pm.value()));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("边界流-1:控制器端点只允许 @PostMapping,禁止 GET/PUT/DELETE 映射")
|
|
|
|
|
+ void controller_hasNoNonPostMappings() {
|
|
|
|
|
+ Class<?> clazz = requireController();
|
|
|
|
|
+ for (Method m : clazz.getDeclaredMethods()) {
|
|
|
|
|
+ assertFalse(m.isAnnotationPresent(GetMapping.class),
|
|
|
|
|
+ "[ai.aiyunyi.controller] " + m.getName() + " 使用了 @GetMapping,项目统一 POST 约定禁止。");
|
|
|
|
|
+ assertFalse(m.isAnnotationPresent(PutMapping.class),
|
|
|
|
|
+ "[ai.aiyunyi.controller] " + m.getName() + " 使用了 @PutMapping,项目统一 POST 约定禁止。");
|
|
|
|
|
+ assertFalse(m.isAnnotationPresent(DeleteMapping.class),
|
|
|
|
|
+ "[ai.aiyunyi.controller] " + m.getName() + " 使用了 @DeleteMapping,项目统一 POST 约定禁止。");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("正常流-4:控制器应注入可 Mockito mock 的编排/网关服务(§4.1 分层)")
|
|
|
|
|
+ void controller_injectsServiceDependency() {
|
|
|
|
|
+ Class<?> clazz = requireController();
|
|
|
|
|
+ Class<?> serviceClass = loadFirstExisting(SERVICE_CANDIDATES);
|
|
|
|
|
+ assertNotNull(serviceClass,
|
|
|
|
|
+ "[ai.aiyunyi.controller] 编排服务(AicdsDetectionService / AicdsGatewayService 等)不存在:"
|
|
|
|
|
+ + "控制器须经服务层转发,不得直接调 Mapper。实现未落地。");
|
|
|
|
|
+ boolean injected = false;
|
|
|
|
|
+ for (Field f : clazz.getDeclaredFields()) {
|
|
|
|
|
+ if (serviceClass.isAssignableFrom(f.getType())) {
|
|
|
|
|
+ injected = true;
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ assertTrue(injected,
|
|
|
|
|
+ "[ai.aiyunyi.controller] 控制器应持有 " + serviceClass.getSimpleName()
|
|
|
|
|
+ + " 字段(@Resource 注入)以转发请求(§4.1 分层架构)。");
|
|
|
|
|
+ assertNotNull(Mockito.mock(serviceClass), "服务类型应可被 Mockito mock(纯单测、无 Spring)。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("正常流-5:所有端点方法应返回统一 Result<T> 契约")
|
|
|
|
|
+ void endpointMethods_returnUnifiedResult() {
|
|
|
|
|
+ Class<?> clazz = requireController();
|
|
|
|
|
+ int postEndpoints = 0;
|
|
|
|
|
+ for (Method m : clazz.getDeclaredMethods()) {
|
|
|
|
|
+ if (!m.isAnnotationPresent(PostMapping.class)) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ postEndpoints++;
|
|
|
|
|
+ assertTrue(Result.class.isAssignableFrom(m.getReturnType()),
|
|
|
|
|
+ "[ai.aiyunyi.controller] 端点 " + m.getName() + " 返回 " + m.getReturnType().getName()
|
|
|
|
|
+ + ",应返回统一 Result<T>(code/message/data)。");
|
|
|
|
|
+ }
|
|
|
|
|
+ assertTrue(postEndpoints > 0,
|
|
|
|
|
+ "[ai.aiyunyi.controller] 控制器应至少声明一个 @PostMapping 端点(detect)。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("正常流-6:端点应含 @RequestAttribute(\"role\") 参数(复用 JwtInterceptor 角色注入)")
|
|
|
|
|
+ void detectEndpoint_acceptsRoleRequestAttribute() {
|
|
|
|
|
+ Class<?> clazz = requireController();
|
|
|
|
|
+ Method detect = findMethodByName(clazz, "detect");
|
|
|
|
|
+ assertNotNull(detect, "[ai.aiyunyi.controller] detect 端点不存在,无法校验角色入参(AC7)。");
|
|
|
|
|
+ boolean hasRoleAttr = false;
|
|
|
|
|
+ for (Annotation[] paramAnns : detect.getParameterAnnotations()) {
|
|
|
|
|
+ for (Annotation a : paramAnns) {
|
|
|
|
|
+ if (a instanceof RequestAttribute && "role".equals(((RequestAttribute) a).value())) {
|
|
|
|
|
+ hasRoleAttr = true;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ assertTrue(hasRoleAttr,
|
|
|
|
|
+ "[ai.aiyunyi.controller] detect 应有 @RequestAttribute(\"role\") 参数,"
|
|
|
|
|
+ + "复用 JwtInterceptor 注入的 role 做角色校验,禁止自行解析 token 或硬编码角色。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 正常流:转发代理行为(Mockito mock 服务) ====================
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("正常流-7:正常角色调用 detect 应转发到服务并返回 Result.success(code=200)")
|
|
|
|
|
+ void detectEndpoint_delegatesAndReturnsSuccess() {
|
|
|
|
|
+ Object controller = newController();
|
|
|
|
|
+ Class<?> serviceClass = loadFirstExisting(SERVICE_CANDIDATES);
|
|
|
|
|
+ assertNotNull(serviceClass, "[ai.aiyunyi.controller] 编排服务不存在,无法验证转发(AC8)。");
|
|
|
|
|
+ Object serviceMock = Mockito.mock(serviceClass);
|
|
|
|
|
+ injectMockService(controller, serviceMock);
|
|
|
|
|
+
|
|
|
|
|
+ Method detect = findMethodByName(controller.getClass(), "detect");
|
|
|
|
|
+ assertNotNull(detect, "[ai.aiyunyi.controller] detect 端点不存在,无法验证转发(AC8)。");
|
|
|
|
|
+ Object result = invoke(detect, controller, sampleArgs(detect, "parent"));
|
|
|
|
|
+
|
|
|
|
|
+ assertTrue(result instanceof Result,
|
|
|
|
|
+ "[ai.aiyunyi.controller] detect 应返回统一 Result,实际 " + result);
|
|
|
|
|
+ assertEquals(200, ((Result<?>) result).getCode(),
|
|
|
|
|
+ "[ai.aiyunyi.controller] 正常流应返回 Result.success(code=200)。");
|
|
|
|
|
+ assertFalse(Mockito.mockingDetails(serviceMock).getInvocations().isEmpty(),
|
|
|
|
|
+ "[ai.aiyunyi.controller] detect 应调用编排服务完成转发,禁止空壳返回假成功(§4.1 转发代理)。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("权限流-1:无权角色(anonymous)调用 detect 应返回 403,且不进入服务层")
|
|
|
|
|
+ void detectEndpoint_unauthorizedRoleForbidden() {
|
|
|
|
|
+ Object controller = newController();
|
|
|
|
|
+ Class<?> serviceClass = loadFirstExisting(SERVICE_CANDIDATES);
|
|
|
|
|
+ Object serviceMock = serviceClass == null ? null : Mockito.mock(serviceClass);
|
|
|
|
|
+ if (serviceMock != null) {
|
|
|
|
|
+ injectMockService(controller, serviceMock);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Method detect = findMethodByName(controller.getClass(), "detect");
|
|
|
|
|
+ assertNotNull(detect, "[ai.aiyunyi.controller] detect 端点不存在,无法校验角色闸门(AC7)。");
|
|
|
|
|
+ Object result = invoke(detect, controller, sampleArgs(detect, "anonymous"));
|
|
|
|
|
+
|
|
|
|
|
+ assertTrue(result instanceof Result,
|
|
|
|
|
+ "[ai.aiyunyi.controller] 无权调用也应返回统一 Result(禁止抛出裸异常)。");
|
|
|
|
|
+ assertEquals(403, ((Result<?>) result).getCode(),
|
|
|
|
|
+ "[ai.aiyunyi.controller] 无权角色(anonymous)应返回 Result.forbidden(code=403)。");
|
|
|
|
|
+ if (serviceMock != null) {
|
|
|
|
|
+ assertTrue(Mockito.mockingDetails(serviceMock).getInvocations().isEmpty(),
|
|
|
|
|
+ "[ai.aiyunyi.controller] 无权请求不得进入服务层(角色闸门应在转发前生效)。");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 权限流:JwtInterceptor 登录门(Mockito mock request/response) ====================
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("权限流-2:JwtInterceptor.LOGIN_REQUIRED_PATHS 应收录 /api/aiyunyi/(未登录需 401)")
|
|
|
|
|
+ void jwtInterceptor_aiyunyiIsLoginRequired() throws Exception {
|
|
|
|
|
+ Field field = JwtInterceptor.class.getDeclaredField("LOGIN_REQUIRED_PATHS");
|
|
|
|
|
+ field.setAccessible(true);
|
|
|
|
|
+ String[] paths = (String[]) field.get(null);
|
|
|
|
|
+ boolean registered = false;
|
|
|
|
|
+ for (String path : paths) {
|
|
|
|
|
+ if (path != null && (path.equals(BASE_PATH) || path.startsWith(BASE_PATH + "/"))) {
|
|
|
|
|
+ registered = true;
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ assertTrue(registered,
|
|
|
|
|
+ "[ai.aiyunyi.controller] JwtInterceptor.LOGIN_REQUIRED_PATHS 未收录 " + BASE_PATH + "/:"
|
|
|
|
|
+ + "爱云医检测消耗第三方配额且为用户私有数据,无 token 必须 401(§4.1 复用 JwtInterceptor)。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("权限流-3:未登录 POST /api/aiyunyi/detect 应被 JwtInterceptor 拒绝并返回 401")
|
|
|
|
|
+ void jwtInterceptor_unauthenticatedAiyunyiReturns401() throws Exception {
|
|
|
|
|
+ HttpServletRequest request = mock(HttpServletRequest.class);
|
|
|
|
|
+ HttpServletResponse response = mock(HttpServletResponse.class);
|
|
|
|
|
+ when(request.getMethod()).thenReturn("POST");
|
|
|
|
|
+ when(request.getRequestURI()).thenReturn(BASE_PATH + DETECT_PATH_SUFFIX);
|
|
|
|
|
+ when(request.getHeader("Authorization")).thenReturn(null);
|
|
|
|
|
+ when(request.getParameter("token")).thenReturn(null);
|
|
|
|
|
+ when(response.getWriter()).thenReturn(new PrintWriter(new StringWriter()));
|
|
|
|
|
+
|
|
|
|
|
+ boolean passed = new JwtInterceptor().preHandle(request, response, new Object());
|
|
|
|
|
+
|
|
|
|
|
+ assertFalse(passed,
|
|
|
|
|
+ "[ai.aiyunyi.controller] 未登录访问 " + BASE_PATH + DETECT_PATH_SUFFIX
|
|
|
|
|
+ + " 必须被 JwtInterceptor 拒绝(preHandle=false),不得匿名放行到控制器。");
|
|
|
|
|
+ Mockito.verify(response).setStatus(HttpServletResponse.SC_UNAUTHORIZED);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("边界流-2:WebConfig 不得把 /api/aiyunyi/** 从 JwtInterceptor 排除")
|
|
|
|
|
+ void webConfig_doesNotExcludeAiyunyiFromJwt() {
|
|
|
|
|
+ String source = readSource(WEB_CONFIG_SOURCE);
|
|
|
|
|
+ assertNotNull(source, "[ai.aiyunyi.controller] 读取 WebConfig.java 失败,无法校验 JWT 白名单接线。");
|
|
|
|
|
+ assertFalse(source.contains(BASE_PATH),
|
|
|
|
|
+ "[ai.aiyunyi.controller] WebConfig 不得将 " + BASE_PATH
|
|
|
|
|
+ + " 加入 jwtInterceptor 的 excludePathPatterns(必须登录)。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 契约锚点:统一 Result 语义(已落地,GREEN 锚点) ====================
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("契约锚点:统一 Result.success=200 / Result.forbidden=403(§4.1 统一契约)")
|
|
|
|
|
+ void resultUnifiedContract_successAndForbidden() {
|
|
|
|
|
+ assertEquals(200, Result.success("ok").getCode(), "统一成功码应为 200。");
|
|
|
|
|
+ assertEquals(403, Result.forbidden("无权限访问").getCode(), "统一无权码应为 403。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 反射 / Mockito / 源码辅助 ====================
|
|
|
|
|
+
|
|
|
|
|
+ private static Class<?> loadClass(String name) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ return Class.forName(name, false, Thread.currentThread().getContextClassLoader());
|
|
|
|
|
+ } catch (ClassNotFoundException e) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static Class<?> loadControllerClass() {
|
|
|
|
|
+ return loadFirstExisting(CONTROLLER_CANDIDATES);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static Class<?> loadFirstExisting(String[] names) {
|
|
|
|
|
+ for (String name : names) {
|
|
|
|
|
+ Class<?> clazz = loadClass(name);
|
|
|
|
|
+ if (clazz != null) {
|
|
|
|
|
+ return clazz;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static Class<?> requireController() {
|
|
|
|
|
+ Class<?> clazz = loadControllerClass();
|
|
|
|
|
+ if (clazz == null) {
|
|
|
|
|
+ fail("RED[ai.aiyunyi.controller] 转发代理控制器未实现(实现未落地):期望 "
|
|
|
|
|
+ + CONTROLLER_FQCN + "(或候选)存在。"
|
|
|
|
|
+ + "请 GREEN 阶段按类注释 AC1~AC9 落地控制器、注解、角色闸门与转发行为。");
|
|
|
|
|
+ }
|
|
|
|
|
+ return clazz;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static Object newController() {
|
|
|
|
|
+ Class<?> clazz = requireController();
|
|
|
|
|
+ try {
|
|
|
|
|
+ return clazz.getDeclaredConstructor().newInstance();
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ fail("RED[ai.aiyunyi.controller] 控制器 " + clazz.getName() + " 无法无参实例化:" + e.getMessage());
|
|
|
|
|
+ throw new IllegalStateException("unreachable");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static Method findMethodByName(Class<?> type, String name) {
|
|
|
|
|
+ for (Class<?> c = type; c != null && c != Object.class; c = c.getSuperclass()) {
|
|
|
|
|
+ for (Method m : c.getDeclaredMethods()) {
|
|
|
|
|
+ if (m.getName().equals(name)) {
|
|
|
|
|
+ return m;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static Object invoke(Method method, Object target, Object... args) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ method.setAccessible(true);
|
|
|
|
|
+ return method.invoke(target, args);
|
|
|
|
|
+ } catch (InvocationTargetException e) {
|
|
|
|
|
+ Throwable cause = e.getCause() == null ? e : e.getCause();
|
|
|
|
|
+ throw new RuntimeException(cause);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 按参数类型合成简单入参;role 参数(含 @RequestAttribute("role"))用给定角色值。 */
|
|
|
|
|
+ private static Object[] sampleArgs(Method method, String role) {
|
|
|
|
|
+ Class<?>[] types = method.getParameterTypes();
|
|
|
|
|
+ Annotation[][] anns = method.getParameterAnnotations();
|
|
|
|
|
+ Object[] args = new Object[types.length];
|
|
|
|
|
+ for (int i = 0; i < types.length; i++) {
|
|
|
|
|
+ Class<?> t = types[i];
|
|
|
|
|
+ boolean roleParam = false;
|
|
|
|
|
+ for (Annotation a : anns[i]) {
|
|
|
|
|
+ if (a instanceof RequestAttribute && "role".equals(((RequestAttribute) a).value())) {
|
|
|
|
|
+ roleParam = true;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if (roleParam || t == String.class) {
|
|
|
|
|
+ args[i] = role;
|
|
|
|
|
+ } else if (t == long.class || t == Long.class) {
|
|
|
|
|
+ args[i] = 1L;
|
|
|
|
|
+ } else if (t == int.class || t == Integer.class) {
|
|
|
|
|
+ args[i] = 1;
|
|
|
|
|
+ } else if (Map.class.isAssignableFrom(t)) {
|
|
|
|
|
+ args[i] = new HashMap<String, Object>();
|
|
|
|
|
+ } else if (List.class.isAssignableFrom(t)) {
|
|
|
|
|
+ args[i] = new ArrayList<Object>();
|
|
|
|
|
+ } else {
|
|
|
|
|
+ args[i] = null;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return args;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static void injectMockService(Object controller, Object mock) throws RuntimeException {
|
|
|
|
|
+ for (Field f : controller.getClass().getDeclaredFields()) {
|
|
|
|
|
+ if (Modifier.isStatic(f.getModifiers())) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (f.getType().isInstance(mock)) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ f.setAccessible(true);
|
|
|
|
|
+ f.set(controller, mock);
|
|
|
|
|
+ } catch (IllegalAccessException e) {
|
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static String readSource(String relativePath) {
|
|
|
|
|
+ Path[] candidates = {
|
|
|
|
|
+ Paths.get(relativePath),
|
|
|
|
|
+ Paths.get("cfc-backend", relativePath),
|
|
|
|
|
+ Paths.get("..", "cfc-backend", relativePath)
|
|
|
|
|
+ };
|
|
|
|
|
+ for (Path path : candidates) {
|
|
|
|
|
+ if (Files.exists(path)) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ return new String(Files.readAllBytes(path), StandardCharsets.UTF_8);
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|