From ddaf53641930295966f6744ce7e2246d517e439f Mon Sep 17 00:00:00 2001 From: zhengwr Date: Thu, 2 Jul 2026 11:03:59 +0800 Subject: [PATCH] =?UTF-8?q?test(gateway+auth):=20T1.6=20=E8=AF=84=E5=AE=A1?= =?UTF-8?q?=E6=94=B6=E5=8F=A3(I-1=20=E8=83=B6=E6=B0=B4=E6=B5=8B=E8=AF=95/M?= =?UTF-8?q?-2=20=E7=A9=BA=20userId/M-1=20=E6=97=B6=E6=95=88=E6=80=A7?= =?UTF-8?q?=E6=B3=A8=E9=87=8A)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - I-1(合 main 前必须):ProcessModelerControllerTest 补 4 个 MockMvc 胶水测试 (standaloneSetup + setControllerAdvice(FlowGatewayExceptionHandler) + 自定义超管参数解析器)—— by-id/by-key 各:①引擎不可达经既有 handler 出 503 通用信封(FlowGatewayExceptionHandler 全仓首测) ②正常路径 200 透传 ③MVC 层把 percent 编码中文 path variable(含冒号 procDefId)正确解码回原串交控制器。 - M-2:EngineContextBinderAdminTest 补「userId 空串/空白/null 且 account 非 admin → false」用例。 - M-1:EngineContextBinder.isSystemAdmin javadoc 补时效性说明(userId/account 取自 JWT claim 签发快照, 非实时查库;token 有效期内 admin 改名/停用/降权不即时生效,由 TTL/吊销兜底)。 测试(mvn -pl platform-flow-gateway,platform-auth -am test 全绿,既有零修改): ProcessModelerControllerTest 6→10 / EngineContextBinderAdminTest 8→9 / FlowableClientTest 12 不变。 Co-Authored-By: Claude Fable 5 --- .../auth/security/EngineContextBinder.java | 4 + .../EngineContextBinderAdminTest.java | 8 ++ .../ProcessModelerControllerTest.java | 83 +++++++++++++++++++ 3 files changed, 95 insertions(+) diff --git a/platform-auth/src/main/java/com/eip4/platform/auth/security/EngineContextBinder.java b/platform-auth/src/main/java/com/eip4/platform/auth/security/EngineContextBinder.java index 977414a..74dfe73 100644 --- a/platform-auth/src/main/java/com/eip4/platform/auth/security/EngineContextBinder.java +++ b/platform-auth/src/main/java/com/eip4/platform/auth/security/EngineContextBinder.java @@ -65,6 +65,10 @@ public final class EngineContextBinder { * 无唯一约束、建号去重又区分大小写而把 {@code 'Admin'} 这类变体账号静默提权(安全评审 M-1)。 * * + *

时效性(time-of-check):userId/account 均取自 JWT claim(token 签发时的身份快照), + * 不实时查库。故在 token 有效期内,若 admin 账号被改名/停用/降权,本判定仍按签发时结果放行—— + * 由 token TTL 兜底,安全敏感的提权变更需配合短 TTL 或主动吊销 token 方能即时生效。 + * *

公开为 {@code public static} 供网关层复用(流程 Modeler 超管门,flowable-service 裸 8089 * 无安全栈 → 超管门唯一落点在网关,T1.6)。{@code null} 主体安全返回 {@code false}(未认证即非超管)。 */ diff --git a/platform-auth/src/test/java/com/eip4/platform/auth/security/EngineContextBinderAdminTest.java b/platform-auth/src/test/java/com/eip4/platform/auth/security/EngineContextBinderAdminTest.java index b8eaad0..d45b044 100644 --- a/platform-auth/src/test/java/com/eip4/platform/auth/security/EngineContextBinderAdminTest.java +++ b/platform-auth/src/test/java/com/eip4/platform/auth/security/EngineContextBinderAdminTest.java @@ -60,6 +60,14 @@ class EngineContextBinderAdminTest { assertThat(EngineContextBinder.isSystemAdmin(user("U1", "zhangsan"))).isFalse(); } + @Test + void userId空串或空白且account非admin_非超管() { + // principal 非 null 但 userId 空/空白(trim 后非 ADMIN),account 也非 admin → 两分支均判假,不越权 + assertThat(EngineContextBinder.isSystemAdmin(user("", "zhangsan"))).isFalse(); + assertThat(EngineContextBinder.isSystemAdmin(user(" ", "zhangsan"))).isFalse(); + assertThat(EngineContextBinder.isSystemAdmin(user(null, "zhangsan"))).isFalse(); + } + @Test void null主体_null安全返回false() { assertThat(EngineContextBinder.isSystemAdmin(null)).isFalse(); diff --git a/platform-flow-gateway/src/test/java/com/eip4/platform/flowgateway/ProcessModelerControllerTest.java b/platform-flow-gateway/src/test/java/com/eip4/platform/flowgateway/ProcessModelerControllerTest.java index 6667fd8..76e617a 100644 --- a/platform-flow-gateway/src/test/java/com/eip4/platform/flowgateway/ProcessModelerControllerTest.java +++ b/platform-flow-gateway/src/test/java/com/eip4/platform/flowgateway/ProcessModelerControllerTest.java @@ -7,17 +7,28 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import java.util.List; import java.util.Map; import org.junit.jupiter.api.Test; +import org.springframework.core.MethodParameter; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.bind.support.WebDataBinderFactory; +import org.springframework.web.context.request.NativeWebRequest; +import org.springframework.web.method.support.HandlerMethodArgumentResolver; +import org.springframework.web.method.support.ModelAndViewContainer; import com.eip4.platform.auth.security.AuthPrincipal; import com.eip4.platform.flowgateway.client.FlowServiceUnavailableException; import com.eip4.platform.flowgateway.client.FlowableClient; +import com.eip4.platform.flowgateway.web.FlowGatewayExceptionHandler; import com.eip4.platform.flowgateway.web.ProcessModelerController; /** @@ -108,4 +119,76 @@ class ProcessModelerControllerTest { assertThat(resp.getBody()).containsEntry("status", 1).containsEntry("msg", "仅系统管理员可用"); verify(flowableClient, never()).procDefSourceByKey(anyString()); } + + // ---- 胶水层(MockMvc + FlowGatewayExceptionHandler):I-1 ---- + // 覆盖三件此前直调单测碰不到的真实 MVC 行为:①引擎不可达经既有 handler 出 503 通用信封 + // (FlowGatewayExceptionHandler 全仓首个测试) ②正常路径 200 透传 ③MVC 层把 percent 编码中文 path + // variable 正确解码回原串交给控制器。@AuthenticationPrincipal 在 standalone 无 Security 上下文, + // 用自定义参数解析器恒注入超管(门控通过与否由直调单测覆盖,这里只验 MVC 编码/异常映射管道)。 + + private MockMvc glueMvc() { + HandlerMethodArgumentResolver adminPrincipal = new HandlerMethodArgumentResolver() { + @Override + public boolean supportsParameter(MethodParameter parameter) { + return AuthPrincipal.class.equals(parameter.getParameterType()); + } + + @Override + public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mav, + NativeWebRequest req, WebDataBinderFactory bf) { + return admin(); + } + }; + return MockMvcBuilders.standaloneSetup(controller) + .setControllerAdvice(new FlowGatewayExceptionHandler()) + .setCustomArgumentResolvers(adminPrincipal) + .build(); + } + + @Test + void 胶水_byId_MVC解码percent中文pathVar并200透传() throws Exception { + Map downstream = Map.of("found", true, "procDefKey", "总包合同交底", "version", 3); + when(flowableClient.procDefSource(CN_PROCDEF)).thenReturn(downstream); + + glueMvc().perform(get("/api/workflow/process-definitions/{procDefId}/source", CN_PROCDEF)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.found").value(true)) + .andExpect(jsonPath("$.procDefKey").value("总包合同交底")); + + // 关键:MVC 层把 %E6.. / %3A 解码回「总包合同交底:3:abc123」原串交给控制器(③编码提取正确) + verify(flowableClient).procDefSource(CN_PROCDEF); + } + + @Test + void 胶水_byId_引擎不可达经handler出503通用信封() throws Exception { + when(flowableClient.procDefSource(CN_PROCDEF)) + .thenThrow(new FlowServiceUnavailableException("flowable-service 不可达", null)); + + glueMvc().perform(get("/api/workflow/process-definitions/{procDefId}/source", CN_PROCDEF)) + .andExpect(status().isServiceUnavailable()) + .andExpect(jsonPath("$.status").value(503)) + .andExpect(jsonPath("$.error").value("FLOW_SERVICE_UNAVAILABLE")); + } + + @Test + void 胶水_byKey_MVC解码percent中文pathVar并200透传() throws Exception { + Map downstream = Map.of("found", true, "procDefKey", "总包合同交底", "version", 2); + when(flowableClient.procDefSourceByKey(CN_KEY)).thenReturn(downstream); + + glueMvc().perform(get("/api/workflow/process-definitions/by-key/{key}/source", CN_KEY)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.found").value(true)); + + verify(flowableClient).procDefSourceByKey(CN_KEY); + } + + @Test + void 胶水_byKey_引擎不可达经handler出503通用信封() throws Exception { + when(flowableClient.procDefSourceByKey(CN_KEY)) + .thenThrow(new FlowServiceUnavailableException("flowable-service 不可达", null)); + + glueMvc().perform(get("/api/workflow/process-definitions/by-key/{key}/source", CN_KEY)) + .andExpect(status().isServiceUnavailable()) + .andExpect(jsonPath("$.error").value("FLOW_SERVICE_UNAVAILABLE")); + } }