test(gateway+auth): T1.6 评审收口(I-1 胶水测试/M-2 空 userId/M-1 时效性注释)
- 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 <noreply@anthropic.com>
This commit is contained in:
@@ -65,6 +65,10 @@ public final class EngineContextBinder {
|
|||||||
* 无唯一约束、建号去重又区分大小写而把 {@code 'Admin'} 这类变体账号静默提权(安全评审 M-1)。</li>
|
* 无唯一约束、建号去重又区分大小写而把 {@code 'Admin'} 这类变体账号静默提权(安全评审 M-1)。</li>
|
||||||
* </ul>
|
* </ul>
|
||||||
*
|
*
|
||||||
|
* <p><b>时效性(time-of-check)</b>:userId/account 均取自 JWT claim(token 签发时的身份快照),
|
||||||
|
* <strong>不实时查库</strong>。故在 token 有效期内,若 admin 账号被改名/停用/降权,本判定仍按签发时结果放行——
|
||||||
|
* 由 token TTL 兜底,安全敏感的提权变更需配合短 TTL 或主动吊销 token 方能即时生效。
|
||||||
|
*
|
||||||
* <p>公开为 {@code public static} 供网关层复用(流程 Modeler 超管门,flowable-service 裸 8089
|
* <p>公开为 {@code public static} 供网关层复用(流程 Modeler 超管门,flowable-service 裸 8089
|
||||||
* 无安全栈 → 超管门唯一落点在网关,T1.6)。{@code null} 主体安全返回 {@code false}(未认证即非超管)。
|
* 无安全栈 → 超管门唯一落点在网关,T1.6)。{@code null} 主体安全返回 {@code false}(未认证即非超管)。
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -60,6 +60,14 @@ class EngineContextBinderAdminTest {
|
|||||||
assertThat(EngineContextBinder.isSystemAdmin(user("U1", "zhangsan"))).isFalse();
|
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
|
@Test
|
||||||
void null主体_null安全返回false() {
|
void null主体_null安全返回false() {
|
||||||
assertThat(EngineContextBinder.isSystemAdmin(null)).isFalse();
|
assertThat(EngineContextBinder.isSystemAdmin(null)).isFalse();
|
||||||
|
|||||||
@@ -7,17 +7,28 @@ import static org.mockito.Mockito.mock;
|
|||||||
import static org.mockito.Mockito.never;
|
import static org.mockito.Mockito.never;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
import static org.mockito.Mockito.when;
|
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.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.core.MethodParameter;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
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.auth.security.AuthPrincipal;
|
||||||
import com.eip4.platform.flowgateway.client.FlowServiceUnavailableException;
|
import com.eip4.platform.flowgateway.client.FlowServiceUnavailableException;
|
||||||
import com.eip4.platform.flowgateway.client.FlowableClient;
|
import com.eip4.platform.flowgateway.client.FlowableClient;
|
||||||
|
import com.eip4.platform.flowgateway.web.FlowGatewayExceptionHandler;
|
||||||
import com.eip4.platform.flowgateway.web.ProcessModelerController;
|
import com.eip4.platform.flowgateway.web.ProcessModelerController;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -108,4 +119,76 @@ class ProcessModelerControllerTest {
|
|||||||
assertThat(resp.getBody()).containsEntry("status", 1).containsEntry("msg", "仅系统管理员可用");
|
assertThat(resp.getBody()).containsEntry("status", 1).containsEntry("msg", "仅系统管理员可用");
|
||||||
verify(flowableClient, never()).procDefSourceByKey(anyString());
|
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<String, Object> 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<String, Object> 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"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user