✨ 应收账单接入角色数据权限并同步在线缓存
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
package com.bms.system;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
import com.bms.common.constant.CacheConstants;
|
||||
import com.bms.common.core.domain.entity.SysRole;
|
||||
import com.bms.common.core.domain.entity.SysUser;
|
||||
import com.bms.common.core.domain.model.LoginUser;
|
||||
import com.bms.common.core.redis.RedisCache;
|
||||
import com.bms.framework.web.service.SysPermissionService;
|
||||
import com.bms.framework.web.service.TokenService;
|
||||
import com.bms.system.service.ISysRoleService;
|
||||
import com.bms.web.controller.system.SysRoleController;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
class SysRoleDataScopeRefreshTest {
|
||||
@Test
|
||||
void savingDataScopeRefreshesOnlineUsersWithPersistedScope() {
|
||||
ISysRoleService roles = mock(ISysRoleService.class);
|
||||
TokenService tokens = mock(TokenService.class);
|
||||
SysPermissionService permissions = mock(SysPermissionService.class);
|
||||
SysRoleController controller = new SysRoleController();
|
||||
ReflectionTestUtils.setField(controller, "roleService", roles);
|
||||
ReflectionTestUtils.setField(controller, "tokenService", tokens);
|
||||
ReflectionTestUtils.setField(controller, "permissionService", permissions);
|
||||
SysRole submitted = new SysRole(10L);
|
||||
submitted.setDataScope("5");
|
||||
SysRole persisted = new SysRole(10L);
|
||||
persisted.setDataScope("5");
|
||||
when(roles.authDataScope(submitted)).thenReturn(1);
|
||||
when(roles.selectRoleById(10L)).thenReturn(persisted);
|
||||
controller.dataScope(submitted);
|
||||
verify(tokens).refreshPermissionByRoleId(10L, permissions, "5");
|
||||
}
|
||||
|
||||
@Test
|
||||
void replacesCachedDataScopeBeforeSavingSessionAndPreservesOtherRoles() {
|
||||
RedisCache redis = mock(RedisCache.class);
|
||||
SysPermissionService permissions = mock(SysPermissionService.class);
|
||||
TokenService tokens = spy(new TokenService());
|
||||
ReflectionTestUtils.setField(tokens, "redisCache", redis);
|
||||
doNothing().when(tokens).refreshToken(any());
|
||||
SysRole target = new SysRole(10L);
|
||||
target.setDataScope("1");
|
||||
SysRole other = new SysRole(11L);
|
||||
other.setDataScope("3");
|
||||
SysUser user = new SysUser(42L);
|
||||
user.setRoles(List.of(target, other));
|
||||
LoginUser login = new LoginUser(42L, 200L, user, Set.of("ar:bill:list"));
|
||||
String key = CacheConstants.LOGIN_TOKEN_KEY + "test";
|
||||
when(redis.keys(CacheConstants.LOGIN_TOKEN_KEY + "*")).thenReturn(List.of(key));
|
||||
when(redis.getCacheObject(key)).thenReturn(login);
|
||||
when(permissions.getMenuPermission(user)).thenAnswer(invocation -> {
|
||||
assertEquals("5", target.getDataScope());
|
||||
return Set.of("ar:bill:list");
|
||||
});
|
||||
tokens.refreshPermissionByRoleId(10L, permissions, "5");
|
||||
assertEquals("5", login.getUser().getRoles().get(0).getDataScope());
|
||||
assertEquals("3", other.getDataScope());
|
||||
verify(tokens).refreshToken(login);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user