yanderong
2026-10-02 bca4733e4b0fcd9b6496e608304549a09f982463
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.ruoyi.system.controller;
 
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.system.api.domain.SysRole;
import com.ruoyi.system.domain.dto.RoleUpdateDto;
import com.ruoyi.system.service.ISysRoleService;
import org.junit.jupiter.api.Test;
import org.springframework.test.util.ReflectionTestUtils;
 
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
 
/**
 * 公司管理员内置角色(company_admin)编辑拦截
 */
class SysRoleControllerTest {
 
    @Test
    void roleUpdateRejectsCompanyAdminBuiltinRole() {
        SysRoleController controller = new SysRoleController();
        ISysRoleService roleService = mock(ISysRoleService.class);
        ReflectionTestUtils.setField(controller, "roleService", roleService);
        SysRole builtin = new SysRole();
        builtin.setRoleId(75L);
        builtin.setRoleKey("company_admin");
        builtin.setRoleName("公司管理员");
        when(roleService.selectRoleById(75L)).thenReturn(builtin);
        RoleUpdateDto dto = new RoleUpdateDto();
        dto.setRoleId(75L);
        dto.setRoleName("公司管理员");
 
        AjaxResult result = controller.roleUpdate(dto);
 
        assertEquals("公司管理员角色不可编辑", result.get("msg"));
        verify(roleService, never()).updateRole(any(SysRole.class));
    }
 
    @Test
    void roleUpdateRejectsCompanyAdminBuiltinRoleByName() {
        // 兼容历史数据:仅角色名匹配(如手工建的角色)同样拦截
        SysRoleController controller = new SysRoleController();
        ISysRoleService roleService = mock(ISysRoleService.class);
        ReflectionTestUtils.setField(controller, "roleService", roleService);
        SysRole builtin = new SysRole();
        builtin.setRoleId(76L);
        builtin.setRoleKey("custom_key");
        builtin.setRoleName("公司管理员");
        when(roleService.selectRoleById(76L)).thenReturn(builtin);
        RoleUpdateDto dto = new RoleUpdateDto();
        dto.setRoleId(76L);
        dto.setRoleName("公司管理员");
 
        AjaxResult result = controller.roleUpdate(dto);
 
        assertEquals("公司管理员角色不可编辑", result.get("msg"));
        verify(roleService, never()).updateRole(any(SysRole.class));
    }
}