|
@@ -0,0 +1,662 @@
|
|
|
|
|
+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.mockito.invocation.InvocationOnMock;
|
|
|
|
|
+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 org.springframework.web.bind.annotation.RequestParam;
|
|
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
+
|
|
|
|
|
+import java.io.ByteArrayInputStream;
|
|
|
|
|
+import java.lang.annotation.Annotation;
|
|
|
|
|
+import java.lang.reflect.Array;
|
|
|
|
|
+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.assertNotEquals;
|
|
|
|
|
+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):爱云医照片上传代理,能力键 {@code ai.aiyunyi.upload}(任务 S-cfc-489)。
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p><b>溯源</b></p>
|
|
|
|
|
+ * <ul>
|
|
|
|
|
+ * <li>需求:{@code docs/爱云医接入/爱云医接口契约-完整版.md} 附录 B.7 —— 平台确认的
|
|
|
|
|
+ * {@code uploadFile} 接口:{@code POST /app-api/detection/package/enter/upload}
|
|
|
|
|
+ * (<b>注意 {@code /app-api/} 前缀,非 {@code /open-api/}</b>);multipart/form-data;
|
|
|
|
|
+ * {@code file} 必填 / {@code path} 选填;免登录 PermitAll,不带
|
|
|
|
|
+ * {@code Authorization} / {@code X-User-Id-Encrypt};
|
|
|
|
|
+ * 返回 {@code {"code":0,"data":"https://minioapi.aicds.com.cn/xxx.jpg","msg":""}},
|
|
|
|
|
+ * {@code data} 即图片 URL,直接填入 {@code enter/detection_data} 的 {@code *UrlList[].url}。</li>
|
|
|
|
|
+ * <li>需求:{@code docs/2026-11-24-爱云医AI检测接入方案.md} §3.3(第一版照片上传走平台
|
|
|
|
|
+ * uploadFile)+ §4.1({@code AicdsController} 行)+ §9.1 剩余阻塞项;用户诉求:报告上传处
|
|
|
|
|
+ * 「AI 快速识别」需先把照片上传拿到 URL 再提交检测数据。</li>
|
|
|
|
|
+ * </ul>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p><b>契约 AC(GREEN 阶段实现方按此落地,方法名/注解/返回类型即测试锚点)</b></p>
|
|
|
|
|
+ * <ol>
|
|
|
|
|
+ * <li>AC1 {@code com.etotem.cfc.controller.AicdsController} 类级
|
|
|
|
|
+ * {@code @RequestMapping("/api/aiyunyi")}(S-cfc-474 已落地,锚点)。</li>
|
|
|
|
|
+ * <li>AC2 提供 {@code upload}(或 {@code uploadFile})端点,标注
|
|
|
|
|
+ * {@code @PostMapping("/upload")},全路径 {@code POST /api/aiyunyi/upload}。</li>
|
|
|
|
|
+ * <li>AC3 端点只允许 POST,禁止 {@code @GetMapping/@PutMapping/@DeleteMapping}。</li>
|
|
|
|
|
+ * <li>AC4 端点含必填文件入参 {@code MultipartFile file},标注
|
|
|
|
|
+ * {@code @RequestParam("file")}(或 {@code @RequestPart("file")})。</li>
|
|
|
|
|
+ * <li>AC5 端点含选填 {@code path} 入参,标注
|
|
|
|
|
+ * {@code @RequestParam(value="path", required=false)}。</li>
|
|
|
|
|
+ * <li>AC6 端点返回统一 {@link Result};正常流把平台返回的图片 URL 透传到 {@code Result.data}</li>
|
|
|
|
|
+ * <li>AC7 端点经服务层转发(不直连 Mapper);Mockito mock 服务后,正常角色调用
|
|
|
|
|
+ * 应触发服务调用且返回 {@code code=200},平台失败归一为错误码(非 200)。</li>
|
|
|
|
|
+ * <li>AC8 端点含 {@code @RequestAttribute("role")} 入参并做角色闸门:
|
|
|
|
|
+ * {@code anonymous} 返回 403 且不进入服务层(与 {@code detect} 同口径)。</li>
|
|
|
|
|
+ * <li>AC9 转发目标必须是 {@code /app-api/detection/package/enter/upload},
|
|
|
|
|
+ * <b>禁止</b>误用 {@code /open-api/detection/package/enter/upload}(B.7 明确避雷)。</li>
|
|
|
|
|
+ * <li>AC10 uploadFile 属平台免登录接口,转发请求不得设置
|
|
|
|
|
+ * {@code Authorization} / {@code X-User-Id-Encrypt} 头(只保留网关签名头)。</li>
|
|
|
|
|
+ * <li>AC11 小程序 token 校验复用 {@code JwtInterceptor} 登录门,
|
|
|
|
|
+ * {@code LOGIN_REQUIRED_PATHS} 收录 {@code /api/aiyunyi/},覆盖 upload(§4.1)。</li>
|
|
|
|
|
+ * </ol>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p><b>实现现状(RED 依据)</b>:本分支 {@code src/main} 中 {@code AicdsController} 只有
|
|
|
|
|
+ * {@code detect} 端点,{@code AicdsDetectionService / AicdsGatewayService} 均无 upload 方法,
|
|
|
|
|
+ * src/main 全库 0 处出现 {@code detection/package/enter/upload}。本测试通过反射 + Mockito +
|
|
|
|
|
+ * 源码读取锚定契约(避免 import 不存在类导致整批测试编译失败),可编译、可运行,
|
|
|
|
|
+ * 断言失败(红)且失败原文明确指出「实现未落地 / 契约缺失」。GREEN 阶段补齐后本测试自动转绿。</p>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p><b>测试风格</b>:纯 Mockito + 反射 + 源码读取,无 Spring 上下文 / 无 DB / 无网络 / 无真实密钥。
|
|
|
|
|
+ * 「正常 / 无权 / 失败」三流以反射调用 + mock 服务覆盖,不依赖 MockMvc 容器。</p>
|
|
|
|
|
+ */
|
|
|
|
|
+class AiyunyiUploadContractTest {
|
|
|
|
|
+
|
|
|
|
|
+ private static final String CAPABILITY = "ai.aiyunyi.upload";
|
|
|
|
|
+ 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[] UPLOAD_METHOD_NAMES = {"upload", "uploadFile"};
|
|
|
|
|
+
|
|
|
|
|
+ private static final String BASE_PATH = "/api/aiyunyi";
|
|
|
|
|
+ private static final String UPLOAD_SUFFIX = "/upload";
|
|
|
|
|
+ private static final String PLATFORM_UPLOAD_PATH = "/app-api/detection/package/enter/upload";
|
|
|
|
|
+ private static final String WRONG_PLATFORM_UPLOAD_PATH = "/open-api/detection/package/enter/upload";
|
|
|
|
|
+ private static final String IMAGE_URL = "https://minioapi.aicds.com.cn/photo.jpg";
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 正常流:控制器 / 端点结构契约 ====================
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("正常流-0(锚点):AicdsController 存在并声明类级 @RequestMapping(/api/aiyunyi)")
|
|
|
|
|
+ void controller_hasBaseMapping() {
|
|
|
|
|
+ Class<?> clazz = requireController();
|
|
|
|
|
+ assertTrue(clazz.isAnnotationPresent(org.springframework.web.bind.annotation.RestController.class)
|
|
|
|
|
+ || clazz.isAnnotationPresent(org.springframework.stereotype.Controller.class),
|
|
|
|
|
+ "[" + CAPABILITY + "] 控制器应标注 @RestController(或 @Controller)。");
|
|
|
|
|
+ RequestMapping rm = clazz.getAnnotation(RequestMapping.class);
|
|
|
|
|
+ assertNotNull(rm, "[" + CAPABILITY + "] 控制器应声明类级 @RequestMapping(\"" + BASE_PATH + "\")。");
|
|
|
|
|
+ boolean hasBase = false;
|
|
|
|
|
+ for (String value : rm.value()) {
|
|
|
|
|
+ if (BASE_PATH.equals(value) || BASE_PATH.equals(value.replaceAll("/$", ""))) {
|
|
|
|
|
+ hasBase = true;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ assertTrue(hasBase, "[" + CAPABILITY + "] 类级映射前缀应为 " + BASE_PATH + ",实际 "
|
|
|
|
|
+ + String.join(",", rm.value()));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("正常流-1:应提供 upload 端点承接 POST /api/aiyunyi/upload(§4.1 / B.7)")
|
|
|
|
|
+ void uploadEndpoint_existsWithPostMapping() {
|
|
|
|
|
+ requireController();
|
|
|
|
|
+ Method upload = findUploadMethod(loadControllerClass());
|
|
|
|
|
+ assertNotNull(upload,
|
|
|
|
|
+ "[" + CAPABILITY + "] 缺少 upload(...)(或 uploadFile)端点:应承接 POST "
|
|
|
|
|
+ + BASE_PATH + UPLOAD_SUFFIX + ",把照片转发到平台 uploadFile 拿 URL。实现未落地。");
|
|
|
|
|
+ PostMapping pm = upload.getAnnotation(PostMapping.class);
|
|
|
|
|
+ assertNotNull(pm,
|
|
|
|
|
+ "[" + CAPABILITY + "] upload 必须标注 @PostMapping(项目统一 POST 约定,禁止 GET/PUT/DELETE)。");
|
|
|
|
|
+ boolean mapped = false;
|
|
|
|
|
+ for (String path : pm.value()) {
|
|
|
|
|
+ if (UPLOAD_SUFFIX.equals(path) || (BASE_PATH + UPLOAD_SUFFIX).equals(path)) {
|
|
|
|
|
+ mapped = true;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ assertTrue(mapped,
|
|
|
|
|
+ "[" + CAPABILITY + "] upload 应映射 " + UPLOAD_SUFFIX + "(全路径 "
|
|
|
|
|
+ + BASE_PATH + UPLOAD_SUFFIX + "),实际 " + String.join(",", pm.value()));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("边界流-1:upload 端点只允许 POST,禁止 GET/PUT/DELETE 映射")
|
|
|
|
|
+ void uploadEndpoint_usesPostOnly() {
|
|
|
|
|
+ Method upload = requireUploadMethod();
|
|
|
|
|
+ assertFalse(upload.isAnnotationPresent(GetMapping.class),
|
|
|
|
|
+ "[" + CAPABILITY + "] upload 使用了 @GetMapping,项目统一 POST 约定禁止。");
|
|
|
|
|
+ assertFalse(upload.isAnnotationPresent(PutMapping.class),
|
|
|
|
|
+ "[" + CAPABILITY + "] upload 使用了 @PutMapping,项目统一 POST 约定禁止。");
|
|
|
|
|
+ assertFalse(upload.isAnnotationPresent(DeleteMapping.class),
|
|
|
|
|
+ "[" + CAPABILITY + "] upload 使用了 @DeleteMapping,项目统一 POST 约定禁止。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("正常流-2:upload 应声明必填文件入参 MultipartFile file(@RequestParam(\"file\"))")
|
|
|
|
|
+ void uploadEndpoint_acceptsRequiredFileParam() {
|
|
|
|
|
+ Method upload = requireUploadMethod();
|
|
|
|
|
+ Class<?>[] types = upload.getParameterTypes();
|
|
|
|
|
+ boolean found = false;
|
|
|
|
|
+ for (int i = 0; i < types.length; i++) {
|
|
|
|
|
+ if (!MultipartFile.class.isAssignableFrom(types[i])) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ String paramName = requestParamName(upload, i);
|
|
|
|
|
+ boolean isPart = hasAnnotation(upload, i, "RequestPart");
|
|
|
|
|
+ if ("file".equals(paramName) || isPart) {
|
|
|
|
|
+ found = true;
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ assertTrue(found,
|
|
|
|
|
+ "[" + CAPABILITY + "] upload 缺少 MultipartFile file 入参(@RequestParam(\"file\")"
|
|
|
|
|
+ + " 或 @RequestPart(\"file\")):B.7 要求 multipart/form-data 的 file 必填。实现未落地。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("正常流-3:upload 应声明选填 path 入参(@RequestParam(value=\"path\", required=false))")
|
|
|
|
|
+ void uploadEndpoint_acceptsOptionalPathParam() {
|
|
|
|
|
+ Method upload = requireUploadMethod();
|
|
|
|
|
+ Class<?>[] types = upload.getParameterTypes();
|
|
|
|
|
+ boolean found = false;
|
|
|
|
|
+ for (int i = 0; i < types.length; i++) {
|
|
|
|
|
+ if (types[i] != String.class || hasRoleParam(upload, i)) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ for (Annotation a : upload.getParameterAnnotations()[i]) {
|
|
|
|
|
+ if (a instanceof RequestParam) {
|
|
|
|
|
+ RequestParam rp = (RequestParam) a;
|
|
|
|
|
+ String name = isBlank(rp.value()) ? rp.name() : rp.value();
|
|
|
|
|
+ if ("path".equals(name) && !rp.required()) {
|
|
|
|
|
+ found = true;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ assertTrue(found,
|
|
|
|
|
+ "[" + CAPABILITY + "] upload 缺少选填 path 入参(@RequestParam(value=\"path\","
|
|
|
|
|
+ + " required=false)):B.7 要求 path 选填(存储路径/文件名,如 a.jpg)。实现未落地。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("正常流-4:upload 应返回统一 Result<T>(code/message/data)")
|
|
|
|
|
+ void uploadEndpoint_returnsUnifiedResult() {
|
|
|
|
|
+ Method upload = requireUploadMethod();
|
|
|
|
|
+ assertTrue(Result.class.isAssignableFrom(upload.getReturnType()),
|
|
|
|
|
+ "[" + CAPABILITY + "] upload 返回 " + upload.getReturnType().getName()
|
|
|
|
|
+ + ",应返回统一 Result<T>(code/message/data),禁止裸 Map/String/第三方 DTO。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 正常 / 异常流:转发代理行为(Mockito mock 服务) ====================
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("正常流-5:正常角色调用 upload 应转发服务并把平台图片 URL 透传到 Result.data")
|
|
|
|
|
+ void uploadEndpoint_delegatesAndPassesThroughImageUrl() {
|
|
|
|
|
+ Object controller = newController();
|
|
|
|
|
+ Method upload = requireUploadMethod();
|
|
|
|
|
+ Class<?> serviceClass = findServiceFieldType(controller.getClass());
|
|
|
|
|
+ assertNotNull(serviceClass,
|
|
|
|
|
+ "[" + CAPABILITY + "] AicdsController 未注入服务字段(com.etotem.cfc.service.*):"
|
|
|
|
|
+ + "§4.1 要求控制器经服务层转发,不得直连第三方 / Mapper。实现未落地。");
|
|
|
|
|
+ Object serviceMock = mock(serviceClass, AiyunyiUploadContractTest::defaultServiceAnswer);
|
|
|
|
|
+ injectField(controller, serviceClass, serviceMock);
|
|
|
|
|
+
|
|
|
|
|
+ Object result = invoke(upload, controller, sampleArgs(upload, "parent"));
|
|
|
|
|
+
|
|
|
|
|
+ assertTrue(result instanceof Result,
|
|
|
|
|
+ "[" + CAPABILITY + "] upload 应返回统一 Result,实际 " + result);
|
|
|
|
|
+ assertEquals(200, ((Result<?>) result).getCode(),
|
|
|
|
|
+ "[" + CAPABILITY + "] 正常流应返回 Result.success(code=200)。");
|
|
|
|
|
+ Object data = ((Result<?>) result).getData();
|
|
|
|
|
+ assertNotNull(data, "[" + CAPABILITY + "] Result.data 应承载平台返回的图片 URL,实际为 null。");
|
|
|
|
|
+ assertTrue(String.valueOf(data).contains("minioapi.aicds.com.cn"),
|
|
|
|
|
+ "[" + CAPABILITY + "] 应把平台 uploadFile 返回的 data(图片 URL)原样透传到 Result.data,"
|
|
|
|
|
+ + "实际 " + data);
|
|
|
|
|
+ assertFalse(Mockito.mockingDetails(serviceMock).getInvocations().isEmpty(),
|
|
|
|
|
+ "[" + CAPABILITY + "] upload 应调用服务层完成转发,禁止空壳返回假 URL(§4.1 转发代理)。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("异常流-1:平台返回非 0 / 上传失败时 upload 应归一为错误 Result(非 200)")
|
|
|
|
|
+ void uploadEndpoint_platformFailureReturnsError() {
|
|
|
|
|
+ Object controller = newController();
|
|
|
|
|
+ Method upload = requireUploadMethod();
|
|
|
|
|
+ Class<?> serviceClass = findServiceFieldType(controller.getClass());
|
|
|
|
|
+ assertNotNull(serviceClass,
|
|
|
|
|
+ "[" + CAPABILITY + "] AicdsController 未注入服务字段,无法验证失败归一(§4.5)。");
|
|
|
|
|
+ Object serviceMock = mock(serviceClass, invocation -> {
|
|
|
|
|
+ throw new RuntimeException("平台 uploadFile 返回 code=1001");
|
|
|
|
|
+ });
|
|
|
|
|
+ injectField(controller, serviceClass, serviceMock);
|
|
|
|
|
+
|
|
|
|
|
+ Object result = invoke(upload, controller, sampleArgs(upload, "parent"));
|
|
|
|
|
+
|
|
|
|
|
+ assertTrue(result instanceof Result,
|
|
|
|
|
+ "[" + CAPABILITY + "] 平台失败也应返回统一 Result(不得抛出裸异常到前端)。");
|
|
|
|
|
+ assertNotEquals(200, ((Result<?>) result).getCode(),
|
|
|
|
|
+ "[" + CAPABILITY + "] 平台返回非 0 / 上传失败时不得返回成功码 200(§4.5 错误归一)。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 权限流 ====================
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("权限流-1:upload 应校验 @RequestAttribute(\"role\");anonymous 返回 403 且不进服务层")
|
|
|
|
|
+ void uploadEndpoint_forbidsAnonymousRole() {
|
|
|
|
|
+ Method upload = requireUploadMethod();
|
|
|
|
|
+ boolean hasRoleAttr = false;
|
|
|
|
|
+ for (Annotation[] anns : upload.getParameterAnnotations()) {
|
|
|
|
|
+ for (Annotation a : anns) {
|
|
|
|
|
+ if (a instanceof RequestAttribute && "role".equals(((RequestAttribute) a).value())) {
|
|
|
|
|
+ hasRoleAttr = true;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ assertTrue(hasRoleAttr,
|
|
|
|
|
+ "[" + CAPABILITY + "] upload 应含 @RequestAttribute(\"role\") 参数,"
|
|
|
|
|
+ + "复用 JwtInterceptor 注入的 role 做角色闸门(与 detect 同口径)。");
|
|
|
|
|
+
|
|
|
|
|
+ Object controller = newController();
|
|
|
|
|
+ Class<?> serviceClass = findServiceFieldType(controller.getClass());
|
|
|
|
|
+ Object serviceMock = serviceClass == null ? null
|
|
|
|
|
+ : mock(serviceClass, AiyunyiUploadContractTest::defaultServiceAnswer);
|
|
|
|
|
+ if (serviceMock != null) {
|
|
|
|
|
+ injectField(controller, serviceClass, serviceMock);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Object result = invoke(upload, controller, sampleArgs(upload, "anonymous"));
|
|
|
|
|
+
|
|
|
|
|
+ assertTrue(result instanceof Result,
|
|
|
|
|
+ "[" + CAPABILITY + "] 无权调用也应返回统一 Result(禁止抛出裸异常)。");
|
|
|
|
|
+ assertEquals(403, ((Result<?>) result).getCode(),
|
|
|
|
|
+ "[" + CAPABILITY + "] anonymous 角色应返回 Result.forbidden(code=403)。");
|
|
|
|
|
+ if (serviceMock != null) {
|
|
|
|
|
+ assertTrue(Mockito.mockingDetails(serviceMock).getInvocations().isEmpty(),
|
|
|
|
|
+ "[" + CAPABILITY + "] 无权请求不得进入服务层(角色闸门应在转发前生效)。");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("权限流-2:JwtInterceptor 登录门覆盖 /api/aiyunyi/(含 upload 端点)")
|
|
|
|
|
+ void jwtInterceptor_uploadIsLoginRequired() 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 && (BASE_PATH.equals(path) || path.startsWith(BASE_PATH + "/"))) {
|
|
|
|
|
+ registered = true;
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ assertTrue(registered,
|
|
|
|
|
+ "[" + CAPABILITY + "] JwtInterceptor.LOGIN_REQUIRED_PATHS 未收录 " + BASE_PATH + "/:"
|
|
|
|
|
+ + "upload 校验小程序 token 依赖该登录门,无 token 必须 401(§4.1)。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 边界流:平台转发路径 / 免登录头 ====================
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("边界流-2:转发目标必须是 /app-api/ 前缀的 uploadFile,禁止误用 /open-api/")
|
|
|
|
|
+ void platformPath_usesAppApiPrefix() {
|
|
|
|
|
+ Map<String, String> sources = readMainSources();
|
|
|
|
|
+ assertFalse(sources.isEmpty(),
|
|
|
|
|
+ "[" + CAPABILITY + "] 读取 src/main/java 失败,无法校验平台转发路径(B.7)。");
|
|
|
|
|
+ boolean found = false;
|
|
|
|
|
+ boolean wrongPrefix = false;
|
|
|
|
|
+ for (String src : sources.values()) {
|
|
|
|
|
+ if (src.contains(PLATFORM_UPLOAD_PATH)) {
|
|
|
|
|
+ found = true;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (src.contains(WRONG_PLATFORM_UPLOAD_PATH)) {
|
|
|
|
|
+ wrongPrefix = true;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ assertTrue(found,
|
|
|
|
|
+ "[" + CAPABILITY + "] src/main 未出现 " + PLATFORM_UPLOAD_PATH + ":"
|
|
|
|
|
+ + "照片上传必须先转发到平台 uploadFile 拿 URL。实现未落地(B.7 / §3.3)。");
|
|
|
|
|
+ assertFalse(wrongPrefix,
|
|
|
|
|
+ "[" + CAPABILITY + "] 转发路径误用 /open-api/ 前缀:" + WRONG_PLATFORM_UPLOAD_PATH
|
|
|
|
|
+ + ";B.7 明确要求 /app-api/。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("边界流-3:uploadFile 转发请求不得携带 Authorization / X-User-Id-Encrypt(平台免登录)")
|
|
|
|
|
+ void platformUpload_omitsBusinessAuthHeaders() {
|
|
|
|
|
+ Map<String, String> sources = readMainSources();
|
|
|
|
|
+ boolean checked = false;
|
|
|
|
|
+ for (Map.Entry<String, String> entry : sources.entrySet()) {
|
|
|
|
|
+ String src = entry.getValue();
|
|
|
|
|
+ int idx = src.indexOf(PLATFORM_UPLOAD_PATH);
|
|
|
|
|
+ if (idx < 0) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ checked = true;
|
|
|
|
|
+ int from = Math.max(0, idx - 1500);
|
|
|
|
|
+ int to = Math.min(src.length(), idx + 1500);
|
|
|
|
|
+ String window = stripComments(src.substring(from, to));
|
|
|
|
|
+ assertFalse(window.contains("\"Authorization\"") || window.contains("HttpHeaders.AUTHORIZATION"),
|
|
|
|
|
+ "[" + CAPABILITY + "] " + entry.getKey() + ":平台 uploadFile 免登录(PermitAll),"
|
|
|
|
|
+ + "转发请求不得设置 Authorization 头(B.7 / §3.3)。");
|
|
|
|
|
+ assertFalse(window.contains("\"X-User-Id-Encrypt\""),
|
|
|
|
|
+ "[" + CAPABILITY + "] " + entry.getKey() + ":平台 uploadFile 免登录(PermitAll),"
|
|
|
|
|
+ + "转发请求不得设置 X-User-Id-Encrypt 头(B.7 / §3.3)。");
|
|
|
|
|
+ }
|
|
|
|
|
+ assertTrue(checked,
|
|
|
|
|
+ "[" + CAPABILITY + "] 未找到 " + PLATFORM_UPLOAD_PATH + " 转发代码,"
|
|
|
|
|
+ + "无法校验免登录请求头契约。实现未落地。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 反射 / 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() {
|
|
|
|
|
+ for (String name : CONTROLLER_CANDIDATES) {
|
|
|
|
|
+ Class<?> clazz = loadClass(name);
|
|
|
|
|
+ if (clazz != null) {
|
|
|
|
|
+ return clazz;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static Class<?> requireController() {
|
|
|
|
|
+ Class<?> clazz = loadControllerClass();
|
|
|
|
|
+ if (clazz == null) {
|
|
|
|
|
+ fail("RED[" + CAPABILITY + "] 转发代理控制器未实现:期望 " + CONTROLLER_FQCN
|
|
|
|
|
+ + "(或候选)存在。请 GREEN 阶段按 AC1~AC11 落地 upload 端点。");
|
|
|
|
|
+ }
|
|
|
|
|
+ return clazz;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static Method findUploadMethod(Class<?> clazz) {
|
|
|
|
|
+ for (Class<?> c = clazz; c != null && c != Object.class; c = c.getSuperclass()) {
|
|
|
|
|
+ for (Method m : c.getDeclaredMethods()) {
|
|
|
|
|
+ for (String name : UPLOAD_METHOD_NAMES) {
|
|
|
|
|
+ if (m.getName().equals(name)) {
|
|
|
|
|
+ return m;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static Method requireUploadMethod() {
|
|
|
|
|
+ Class<?> clazz = requireController();
|
|
|
|
|
+ Method upload = findUploadMethod(clazz);
|
|
|
|
|
+ if (upload == null) {
|
|
|
|
|
+ fail("RED[" + CAPABILITY + "] " + clazz.getName() + " 缺少 upload(...)(或 uploadFile)端点:"
|
|
|
|
|
+ + "应标注 @PostMapping(\"/upload\") 承接 POST " + BASE_PATH + UPLOAD_SUFFIX
|
|
|
|
|
+ + ",把照片转发到平台 uploadFile 拿 URL。实现未落地。");
|
|
|
|
|
+ }
|
|
|
|
|
+ return upload;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static Object newController() {
|
|
|
|
|
+ Class<?> clazz = requireController();
|
|
|
|
|
+ try {
|
|
|
|
|
+ return clazz.getDeclaredConstructor().newInstance();
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ fail("RED[" + CAPABILITY + "] 控制器 " + clazz.getName() + " 无法无参实例化:" + e.getMessage());
|
|
|
|
|
+ throw new IllegalStateException("unreachable");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 服务层默认返回 URL,使「正常流透传」断言与具体实现的方法签名解耦。 */
|
|
|
|
|
+ private static Object defaultServiceAnswer(InvocationOnMock invocation) {
|
|
|
|
|
+ Class<?> rt = invocation.getMethod().getReturnType();
|
|
|
|
|
+ if (rt == String.class) {
|
|
|
|
|
+ return IMAGE_URL;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (Result.class.isAssignableFrom(rt)) {
|
|
|
|
|
+ return Result.success(IMAGE_URL);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (Map.class.isAssignableFrom(rt)) {
|
|
|
|
|
+ Map<String, Object> map = new HashMap<String, Object>();
|
|
|
|
|
+ map.put("code", 0);
|
|
|
|
|
+ map.put("data", IMAGE_URL);
|
|
|
|
|
+ map.put("msg", "");
|
|
|
|
|
+ return map;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (rt == boolean.class || rt == Boolean.class) {
|
|
|
|
|
+ return Boolean.TRUE;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (rt == long.class || rt == Long.class) {
|
|
|
|
|
+ return 1L;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (rt == int.class || rt == Integer.class) {
|
|
|
|
|
+ return 1;
|
|
|
|
|
+ }
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 优先取 Aicds* 服务字段;否则取第一个 com.etotem.cfc.service.* 非静态字段。 */
|
|
|
|
|
+ private static Class<?> findServiceFieldType(Class<?> controllerClass) {
|
|
|
|
|
+ Class<?> fallback = null;
|
|
|
|
|
+ for (Field f : controllerClass.getDeclaredFields()) {
|
|
|
|
|
+ if (Modifier.isStatic(f.getModifiers())) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ String typeName = f.getType().getName();
|
|
|
|
|
+ if (!typeName.startsWith("com.etotem.cfc.service.")) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (typeName.contains("Aicds") || typeName.contains("AiYunyi")) {
|
|
|
|
|
+ return f.getType();
|
|
|
|
|
+ }
|
|
|
|
|
+ if (fallback == null) {
|
|
|
|
|
+ fallback = f.getType();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return fallback;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static void injectField(Object target, Class<?> type, Object mock) {
|
|
|
|
|
+ for (Field f : target.getClass().getDeclaredFields()) {
|
|
|
|
|
+ if (Modifier.isStatic(f.getModifiers())) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (f.getType().isInstance(mock)) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ f.setAccessible(true);
|
|
|
|
|
+ f.set(target, mock);
|
|
|
|
|
+ return;
|
|
|
|
|
+ } catch (IllegalAccessException e) {
|
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ 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];
|
|
|
|
|
+ if (hasRoleParam(method, i) || t == String.class) {
|
|
|
|
|
+ args[i] = hasRoleParam(method, i) ? role : "a.jpg";
|
|
|
|
|
+ } else if (t == MultipartFile.class || MultipartFile.class.isAssignableFrom(t)) {
|
|
|
|
|
+ args[i] = mockMultipartFile();
|
|
|
|
|
+ } else if (t.isArray()) {
|
|
|
|
|
+ Object element = defaultArg(t.getComponentType());
|
|
|
|
|
+ Object array = Array.newInstance(t.getComponentType(), 1);
|
|
|
|
|
+ Array.set(array, 0, element);
|
|
|
|
|
+ args[i] = array;
|
|
|
|
|
+ } else if (List.class.isAssignableFrom(t)) {
|
|
|
|
|
+ List<Object> list = new ArrayList<Object>();
|
|
|
|
|
+ list.add(defaultArg(Object.class));
|
|
|
|
|
+ args[i] = list;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ args[i] = defaultArg(t);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return args;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static Object defaultArg(Class<?> t) {
|
|
|
|
|
+ if (t == long.class || t == Long.class) {
|
|
|
|
|
+ return 1L;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (t == int.class || t == Integer.class) {
|
|
|
|
|
+ return 1;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (t == boolean.class || t == Boolean.class) {
|
|
|
|
|
+ return Boolean.TRUE;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (Map.class.isAssignableFrom(t)) {
|
|
|
|
|
+ return new HashMap<String, Object>();
|
|
|
|
|
+ }
|
|
|
|
|
+ if (MultipartFile.class.isAssignableFrom(t)) {
|
|
|
|
|
+ return mockMultipartFile();
|
|
|
|
|
+ }
|
|
|
|
|
+ if (t == String.class) {
|
|
|
|
|
+ return "a.jpg";
|
|
|
|
|
+ }
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static MultipartFile mockMultipartFile() {
|
|
|
|
|
+ MultipartFile file = mock(MultipartFile.class);
|
|
|
|
|
+ when(file.getOriginalFilename()).thenReturn("a.jpg");
|
|
|
|
|
+ when(file.getContentType()).thenReturn("image/jpeg");
|
|
|
|
|
+ when(file.getSize()).thenReturn(3L);
|
|
|
|
|
+ when(file.isEmpty()).thenReturn(false);
|
|
|
|
|
+ try {
|
|
|
|
|
+ when(file.getBytes()).thenReturn(new byte[]{1, 2, 3});
|
|
|
|
|
+ when(file.getInputStream()).thenReturn(new ByteArrayInputStream(new byte[]{1, 2, 3}));
|
|
|
|
|
+ } catch (java.io.IOException e) {
|
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
|
+ }
|
|
|
|
|
+ return file;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static boolean hasRoleParam(Method method, int index) {
|
|
|
|
|
+ for (Annotation a : method.getParameterAnnotations()[index]) {
|
|
|
|
|
+ if (a instanceof RequestAttribute && "role".equals(((RequestAttribute) a).value())) {
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static String requestParamName(Method method, int index) {
|
|
|
|
|
+ for (Annotation a : method.getParameterAnnotations()[index]) {
|
|
|
|
|
+ if (a instanceof RequestParam) {
|
|
|
|
|
+ RequestParam rp = (RequestParam) a;
|
|
|
|
|
+ return isBlank(rp.value()) ? rp.name() : rp.value();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static boolean hasAnnotation(Method method, int index, String simpleName) {
|
|
|
|
|
+ for (Annotation a : method.getParameterAnnotations()[index]) {
|
|
|
|
|
+ if (a.annotationType().getSimpleName().equals(simpleName)) {
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static boolean isBlank(String s) {
|
|
|
|
|
+ return s == null || s.trim().isEmpty();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 读取 src/main/java 下全部 .java 源码(兼容从仓库根 / 模块根执行 surefire)。 */
|
|
|
|
|
+ private static Map<String, String> readMainSources() {
|
|
|
|
|
+ Map<String, String> result = new HashMap<String, String>();
|
|
|
|
|
+ Path[] roots = {
|
|
|
|
|
+ Paths.get("src", "main", "java"),
|
|
|
|
|
+ Paths.get("cfc-backend", "src", "main", "java"),
|
|
|
|
|
+ Paths.get("..", "cfc-backend", "src", "main", "java")
|
|
|
|
|
+ };
|
|
|
|
|
+ Path root = null;
|
|
|
|
|
+ for (Path candidate : roots) {
|
|
|
|
|
+ if (Files.isDirectory(candidate)) {
|
|
|
|
|
+ root = candidate;
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if (root == null) {
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
|
|
+ final Path sourceRoot = root;
|
|
|
|
|
+ try {
|
|
|
|
|
+ java.util.stream.Stream<Path> paths = Files.walk(sourceRoot);
|
|
|
|
|
+ try {
|
|
|
|
|
+ paths.filter(p -> p.toString().endsWith(".java")).forEach(p -> {
|
|
|
|
|
+ try {
|
|
|
|
|
+ result.put(sourceRoot.relativize(p).toString(),
|
|
|
|
|
+ new String(Files.readAllBytes(p), StandardCharsets.UTF_8));
|
|
|
|
|
+ } catch (java.io.IOException ignored) {
|
|
|
|
|
+ // 单个文件读取失败不影响整体契约检查
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ paths.close();
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (java.io.IOException e) {
|
|
|
|
|
+ return new HashMap<String, String>();
|
|
|
|
|
+ }
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 去掉 // 行注释与 /* *\/ 块注释,避免注释中的「Authorization」字样造成误判。 */
|
|
|
|
|
+ private static String stripComments(String src) {
|
|
|
|
|
+ String result = src.replaceAll("/\\*[\\s\\S]*?\\*/", " ");
|
|
|
|
|
+ return result.replaceAll("//[^\\r\\n]*", " ");
|
|
|
|
|
+ }
|
|
|
|
|
+}
|