180 lines
10 KiB
Java
180 lines
10 KiB
Java
|
|
package com.ruoyi.projectdev;
|
||
|
|
|
||
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||
|
|
import com.ruoyi.common.core.domain.entity.SysUser;
|
||
|
|
import com.ruoyi.common.core.domain.entity.SysRole;
|
||
|
|
import com.ruoyi.common.core.domain.entity.SysDept;
|
||
|
|
import com.ruoyi.common.core.domain.model.LoginUser;
|
||
|
|
import com.ruoyi.common.exception.ServiceException;
|
||
|
|
import com.ruoyi.framework.web.service.PermissionService;
|
||
|
|
import com.ruoyi.framework.web.service.SysPermissionService;
|
||
|
|
import com.ruoyi.system.service.ISysUserService;
|
||
|
|
import com.ruoyi.system.mapper.SysDeptMapper;
|
||
|
|
import com.ruoyi.web.controller.production.ProductionTaskController;
|
||
|
|
import com.ruoyi.web.controller.warehouse.PurchaseRequestController;
|
||
|
|
import com.ruoyi.web.service.assistant.AssistantPermissionGuard;
|
||
|
|
import com.ruoyi.web.service.assistant.BusinessActionRegistry;
|
||
|
|
import jakarta.validation.Validator;
|
||
|
|
import java.util.*;
|
||
|
|
import org.junit.jupiter.api.*;
|
||
|
|
import org.springframework.context.support.StaticApplicationContext;
|
||
|
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||
|
|
import org.springframework.mock.web.MockHttpServletRequest;
|
||
|
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||
|
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||
|
|
import org.springframework.web.context.request.RequestContextHolder;
|
||
|
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
||
|
|
import static org.junit.jupiter.api.Assertions.*;
|
||
|
|
import static org.mockito.Mockito.*;
|
||
|
|
|
||
|
|
/** Fresh authorization is tested independently of any model and without touching production data. */
|
||
|
|
class AssistantSecurityContractTest {
|
||
|
|
private final ISysUserService users = mock(ISysUserService.class);
|
||
|
|
private final SysPermissionService permissions = mock(SysPermissionService.class);
|
||
|
|
private final SysDeptMapper departments = mock(SysDeptMapper.class);
|
||
|
|
private final JdbcTemplate jdbc = mock(JdbcTemplate.class);
|
||
|
|
private final StaticApplicationContext context = new StaticApplicationContext();
|
||
|
|
private AssistantPermissionGuard guard;
|
||
|
|
private LoginUser login;
|
||
|
|
private SysUser current;
|
||
|
|
|
||
|
|
@BeforeEach void setup() {
|
||
|
|
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(new MockHttpServletRequest()));
|
||
|
|
current = new SysUser(); current.setUserId(101L); current.setUserName("assistant-tester");
|
||
|
|
current.setStatus("0"); current.setDelFlag("0");
|
||
|
|
login = new LoginUser(101L, 1L, current, Set.of("*:*:*"));
|
||
|
|
SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(login, null));
|
||
|
|
when(users.selectUserById(101L)).thenReturn(current);
|
||
|
|
when(permissions.getMenuPermission(current)).thenReturn(Set.of("ai:assistant:use"));
|
||
|
|
context.getBeanFactory().registerSingleton("ss", new PermissionService());
|
||
|
|
guard = new AssistantPermissionGuard(users, permissions, context, departments);
|
||
|
|
}
|
||
|
|
@AfterEach void cleanup() { SecurityContextHolder.clearContext(); RequestContextHolder.resetRequestAttributes(); context.close(); }
|
||
|
|
private BusinessActionRegistry registry() {
|
||
|
|
return new BusinessActionRegistry(context, new ObjectMapper().findAndRegisterModules(), jdbc, mock(Validator.class), guard);
|
||
|
|
}
|
||
|
|
|
||
|
|
@Test void staleAdministratorPermissionsAreReplacedNotCombined() {
|
||
|
|
guard.refresh();
|
||
|
|
assertEquals(Set.of("ai:assistant:use"), login.getPermissions());
|
||
|
|
assertEquals(101L, login.getUserId());
|
||
|
|
assertEquals("assistant-tester", login.getUsername());
|
||
|
|
}
|
||
|
|
|
||
|
|
@Test void customDepartmentAuthorizationIsRefreshedWithoutTreeLeafFiltering() {
|
||
|
|
SysRole role = new SysRole(); role.setRoleId(12L); role.setDataScope("2");
|
||
|
|
role.setDeptIds(new Long[] { 999L }); current.setRoles(List.of(role));
|
||
|
|
when(departments.selectDeptListByRoleId(12L, false)).thenReturn(List.of(10L, 11L), List.of(20L));
|
||
|
|
guard.refresh(); assertArrayEquals(new Long[] { 10L, 11L }, role.getDeptIds());
|
||
|
|
guard.refresh(); assertArrayEquals(new Long[] { 20L }, role.getDeptIds());
|
||
|
|
verify(departments, times(2)).selectDeptListByRoleId(12L, false);
|
||
|
|
verify(departments, never()).selectDeptListByRoleId(anyLong(), eq(true));
|
||
|
|
}
|
||
|
|
|
||
|
|
@Test void nonCustomDataScopeDoesNotNeedDepartmentAuthorizationLookup() {
|
||
|
|
SysRole role = new SysRole(); role.setRoleId(12L); role.setDataScope("3");
|
||
|
|
current.setRoles(List.of(role)); guard.refresh();
|
||
|
|
verifyNoInteractions(departments);
|
||
|
|
}
|
||
|
|
|
||
|
|
@Test void departmentAndChildrenScopeIncludesCurrentDepartmentAndRefreshesTree() {
|
||
|
|
SysRole first = new SysRole(); first.setRoleId(12L); first.setDataScope("4");
|
||
|
|
SysRole second = new SysRole(); second.setRoleId(13L); second.setDataScope("4");
|
||
|
|
current.setRoles(List.of(first, second)); current.setDeptId(10L);
|
||
|
|
SysDept child = new SysDept(); child.setDeptId(11L);
|
||
|
|
SysDept movedChild = new SysDept(); movedChild.setDeptId(20L);
|
||
|
|
when(departments.selectChildrenDeptById(10L)).thenReturn(List.of(child), List.of(movedChild));
|
||
|
|
guard.refresh();
|
||
|
|
assertArrayEquals(new Long[] { 10L, 11L }, first.getDeptIds());
|
||
|
|
assertArrayEquals(first.getDeptIds(), second.getDeptIds());
|
||
|
|
guard.refresh();
|
||
|
|
assertArrayEquals(new Long[] { 10L, 20L }, first.getDeptIds());
|
||
|
|
assertArrayEquals(first.getDeptIds(), second.getDeptIds());
|
||
|
|
// One lookup per refresh, even when multiple roles use the same descendant scope.
|
||
|
|
verify(departments, times(2)).selectChildrenDeptById(10L);
|
||
|
|
verify(departments, never()).selectDeptListByRoleId(anyLong(), anyBoolean());
|
||
|
|
}
|
||
|
|
|
||
|
|
@Test void emptyDepartmentTreeStillRetainsTheCurrentDepartment() {
|
||
|
|
SysRole role = new SysRole(); role.setRoleId(12L); role.setDataScope("4");
|
||
|
|
current.setRoles(List.of(role)); current.setDeptId(10L);
|
||
|
|
when(departments.selectChildrenDeptById(10L)).thenReturn(List.of());
|
||
|
|
guard.refresh(); assertArrayEquals(new Long[] { 10L }, role.getDeptIds());
|
||
|
|
}
|
||
|
|
|
||
|
|
@Test void disabledDeletedAndMissingUsersCannotUseEvenOldValidSession() {
|
||
|
|
current.setStatus("1"); assertThrows(ServiceException.class, () -> guard.refresh());
|
||
|
|
current.setStatus("0"); current.setDelFlag("2"); assertThrows(ServiceException.class, () -> guard.refresh());
|
||
|
|
when(users.selectUserById(101L)).thenReturn(null); assertThrows(ServiceException.class, () -> guard.refresh());
|
||
|
|
}
|
||
|
|
|
||
|
|
@Test void revokedAssistantEntryPermissionBlocksEverything() {
|
||
|
|
when(permissions.getMenuPermission(current)).thenReturn(Set.of("warehouse:purchase:manage"));
|
||
|
|
assertThrows(ServiceException.class, () -> guard.refresh());
|
||
|
|
assertThrows(ServiceException.class, () -> registry().catalog());
|
||
|
|
verifyNoInteractions(jdbc);
|
||
|
|
}
|
||
|
|
|
||
|
|
@Test void controllerAndPermissionExpressionRetainsBothRequirements() throws Exception {
|
||
|
|
var method = ProductionTaskController.class.getMethod("perform", com.ruoyi.production.domain.ProductionCommand.class);
|
||
|
|
when(permissions.getMenuPermission(current)).thenReturn(Set.of("ai:assistant:use", "warehouse:stock:out"));
|
||
|
|
guard.refresh(); assertFalse(guard.allows(method));
|
||
|
|
when(permissions.getMenuPermission(current)).thenReturn(Set.of("ai:assistant:use", "project:inventory:use"));
|
||
|
|
guard.refresh(); assertFalse(guard.allows(method));
|
||
|
|
when(permissions.getMenuPermission(current)).thenReturn(Set.of("ai:assistant:use", "warehouse:stock:out", "project:inventory:use"));
|
||
|
|
guard.refresh(); assertTrue(guard.allows(method));
|
||
|
|
}
|
||
|
|
|
||
|
|
@Test void permissionLossAfterDraftIsObservedAgainOnExecute() {
|
||
|
|
var registry = registry();
|
||
|
|
when(permissions.getMenuPermission(current)).thenReturn(Set.of("ai:assistant:use", "warehouse:purchase:edit"));
|
||
|
|
assertDoesNotThrow(() -> registry.requireActionPermission("arrival.register"));
|
||
|
|
when(permissions.getMenuPermission(current)).thenReturn(Set.of("ai:assistant:use"));
|
||
|
|
assertThrows(ServiceException.class, () -> registry.execute("arrival.register", Map.of()));
|
||
|
|
verifyNoInteractions(jdbc);
|
||
|
|
}
|
||
|
|
|
||
|
|
@Test void readOnlyPurchasePermissionCannotRegisterOrAcceptArrival() {
|
||
|
|
when(permissions.getMenuPermission(current)).thenReturn(Set.of("ai:assistant:use", "warehouse:purchase:list"));
|
||
|
|
var registry = registry();
|
||
|
|
assertDoesNotThrow(() -> registry.requireActionPermission("purchase.query"));
|
||
|
|
assertThrows(ServiceException.class, () -> registry.prepare("arrival.register", List.of(Map.of("purchaseRequestId", 7L))));
|
||
|
|
assertThrows(ServiceException.class, () -> registry.lookup("arrival.register", "purchaseRequestId", "螺丝"));
|
||
|
|
verifyNoInteractions(jdbc);
|
||
|
|
}
|
||
|
|
|
||
|
|
@Test void unknownActionNeverQueriesOrExecutesAnything() {
|
||
|
|
var registry = registry();
|
||
|
|
for (String action : List.of("http.request", "sql", "/system/user", "system.createAdmin", "pm.delete")) {
|
||
|
|
assertThrows(ServiceException.class, () -> registry.requireActionPermission(action));
|
||
|
|
}
|
||
|
|
verifyNoInteractions(jdbc);
|
||
|
|
}
|
||
|
|
|
||
|
|
@Test void catalogShowsCapabilitiesButDoesNotQueryBusinessData() {
|
||
|
|
var catalog = registry().catalog();
|
||
|
|
assertTrue(catalog.size() >= 50);
|
||
|
|
assertEquals(catalog.size(), catalog.stream().map(a -> a.get("key")).distinct().count());
|
||
|
|
assertTrue(catalog.stream().allMatch(a -> Boolean.FALSE.equals(a.get("allowed"))));
|
||
|
|
assertTrue(catalog.stream().allMatch(a -> a.get("fields") instanceof List<?>));
|
||
|
|
assertTrue(catalog.stream().noneMatch(a -> a.get("key").toString().startsWith("system.")));
|
||
|
|
verifyNoInteractions(jdbc);
|
||
|
|
}
|
||
|
|
|
||
|
|
@Test void serverControllerWithoutAuthorizationCannotBeInvokedByRegistryGuard() throws Exception {
|
||
|
|
guard.refresh();
|
||
|
|
assertFalse(guard.allows(Object.class.getMethod("toString")));
|
||
|
|
}
|
||
|
|
|
||
|
|
@Test void authenticatedFeedbackExpressionUsesCurrentIdentityAndDoesNotGrantAdminAccess() throws Exception {
|
||
|
|
var type = com.ruoyi.web.service.assistant.AssistantFeedbackActions.class;
|
||
|
|
var mine = type.getMethod("mine", String.class, String.class, String.class);
|
||
|
|
var all = type.getMethod("all", String.class, String.class, String.class);
|
||
|
|
SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(login, null, List.of()));
|
||
|
|
guard.refresh(); assertTrue(guard.allows(mine)); assertFalse(guard.allows(all));
|
||
|
|
SecurityContextHolder.getContext().setAuthentication(new org.springframework.security.authentication.AnonymousAuthenticationToken(
|
||
|
|
"test", "anonymousUser", List.of(new org.springframework.security.core.authority.SimpleGrantedAuthority("ROLE_ANONYMOUS"))));
|
||
|
|
assertFalse(guard.allows(mine));
|
||
|
|
}
|
||
|
|
}
|