|
49 | 49 | import java.util.Map; |
50 | 50 | import java.util.UUID; |
51 | 51 |
|
| 52 | +import com.cloud.exception.PermissionDeniedException; |
| 53 | +import com.cloud.user.AccountService; |
52 | 54 | import org.apache.cloudstack.acl.Role; |
53 | 55 | import org.apache.cloudstack.acl.RoleService; |
54 | 56 | import org.apache.cloudstack.acl.RoleType; |
@@ -176,6 +178,8 @@ public class ExtensionsManagerImplTest { |
176 | 178 | private VMTemplateDao templateDao; |
177 | 179 | @Mock |
178 | 180 | private RoleService roleService; |
| 181 | + @Mock |
| 182 | + private AccountService accountService; |
179 | 183 |
|
180 | 184 | @Before |
181 | 185 | public void setUp() { |
@@ -1640,6 +1644,35 @@ public void runCustomAction_ExecutionThrowsException() throws Exception { |
1640 | 1644 | } |
1641 | 1645 | } |
1642 | 1646 |
|
| 1647 | + @Test(expected = PermissionDeniedException.class) |
| 1648 | + public void runCustomAction_CheckAccessThrowsException() throws Exception { |
| 1649 | + RunCustomActionCmd cmd = mock(RunCustomActionCmd.class); |
| 1650 | + when(cmd.getCustomActionId()).thenReturn(1L); |
| 1651 | + when(cmd.getResourceId()).thenReturn("vm-123"); |
| 1652 | + when(cmd.getParameters()).thenReturn(Map.of("param1", "value1")); |
| 1653 | + |
| 1654 | + ExtensionCustomActionVO actionVO = mock(ExtensionCustomActionVO.class); |
| 1655 | + when(extensionCustomActionDao.findById(1L)).thenReturn(actionVO); |
| 1656 | + when(actionVO.isEnabled()).thenReturn(true); |
| 1657 | + when(actionVO.getResourceType()).thenReturn(ExtensionCustomAction.ResourceType.VirtualMachine); |
| 1658 | + when(actionVO.getAllowedRoleTypes()).thenReturn(RoleType.toCombinedMask(List.of(RoleType.Admin, RoleType.DomainAdmin, RoleType.User))); |
| 1659 | + |
| 1660 | + ExtensionVO extensionVO = mock(ExtensionVO.class); |
| 1661 | + when(extensionDao.findById(anyLong())).thenReturn(extensionVO); |
| 1662 | + when(extensionVO.getState()).thenReturn(Extension.State.Enabled); |
| 1663 | + |
| 1664 | + VirtualMachine vm = mock(VirtualMachine.class); |
| 1665 | + when(entityManager.findByUuid(eq(VirtualMachine.class), anyString())).thenReturn(vm); |
| 1666 | + doThrow(PermissionDeniedException.class).when(accountService).checkAccess(any(Account.class), eq(null), eq(true), eq(vm)); |
| 1667 | + |
| 1668 | + try (MockedStatic<CallContext> ignored = mockStatic(CallContext.class)) { |
| 1669 | + mockCallerRole(RoleType.User); |
| 1670 | + CustomActionResultResponse result = extensionsManager.runCustomAction(cmd); |
| 1671 | + |
| 1672 | + assertFalse(result.getSuccess()); |
| 1673 | + } |
| 1674 | + } |
| 1675 | + |
1643 | 1676 | @Test |
1644 | 1677 | public void createCustomActionResponse_SetsBasicFields() { |
1645 | 1678 | ExtensionCustomAction action = mock(ExtensionCustomAction.class); |
|
0 commit comments