Search in sources :

Example 1 with IAMPolicyPermissionVO

use of org.apache.cloudstack.iam.server.IAMPolicyPermissionVO in project cloudstack by apache.

the class IAMApiServiceTest method addRemovePermissionToPolicyTest.

@Test
public void addRemovePermissionToPolicyTest() {
    IAMPolicy policy = new IAMPolicyVO("policy1", "tester policy1");
    List<IAMPolicy> policies = new ArrayList<IAMPolicy>();
    policies.add(policy);
    Long policyId = policy.getId();
    Long resId = 200L;
    Class clz = ListVMsCmd.class;
    when(_apiServer.getCmdClass("listVirtualMachines")).thenReturn(clz);
    when(_iamSrv.addIAMPermissionToIAMPolicy(policyId, VirtualMachine.class.getSimpleName(), PermissionScope.RESOURCE.toString(), resId, "listVirtualMachines", AccessType.UseEntry.toString(), Permission.Allow, false)).thenReturn(policy);
    _aclSrv.addIAMPermissionToIAMPolicy(policyId, VirtualMachine.class.getSimpleName(), PermissionScope.RESOURCE, resId, "listVirtualMachines", Permission.Allow, false, false);
    Pair<List<IAMPolicy>, Integer> policyList = new Pair<List<IAMPolicy>, Integer>(policies, 1);
    List<IAMPolicyPermission> policyPerms = new ArrayList<IAMPolicyPermission>();
    IAMPolicyPermission perm = new IAMPolicyPermissionVO(policyId, "listVirtualMachines", VirtualMachine.class.getSimpleName(), AccessType.UseEntry.toString(), PermissionScope.RESOURCE.toString(), resId, Permission.Allow, false);
    policyPerms.add(perm);
    when(_iamSrv.listIAMPolicies(null, "policy1", callerDomainPath, 0L, 20L)).thenReturn(policyList);
    when(_iamSrv.listPolicyPermissions(policyId)).thenReturn(policyPerms);
    ListResponse<IAMPolicyResponse> policyResp = _aclSrv.listIAMPolicies(null, "policy1", callerDomainId, 0L, 20L);
    assertTrue("No. of response items should be one", policyResp.getCount() == 1);
    IAMPolicyResponse resp = policyResp.getResponses().get(0);
    Set<IAMPermissionResponse> permList = resp.getPermissionList();
    assertTrue("Permission list should not be empty", permList != null && permList.size() > 0);
    IAMPermissionResponse permResp = permList.iterator().next();
    assertEquals("There should be one permission for listVirtualMachines", "listVirtualMachines", permResp.getAction());
    //remove permission from policy
    policyPerms.remove(perm);
    _aclSrv.removeIAMPermissionFromIAMPolicy(policyId, VirtualMachine.class.getSimpleName(), PermissionScope.RESOURCE, resId, "listVirtualMachines");
    policyResp = _aclSrv.listIAMPolicies(null, "policy1", callerDomainId, 0L, 20L);
    assertTrue("No. of response items should be one", policyResp.getCount() == 1);
    resp = policyResp.getResponses().get(0);
    permList = resp.getPermissionList();
    assertTrue("Permission list should be empty", permList != null && permList.size() == 0);
}
Also used : IAMPolicy(org.apache.cloudstack.iam.api.IAMPolicy) IAMPolicyVO(org.apache.cloudstack.iam.server.IAMPolicyVO) ArrayList(java.util.ArrayList) IAMPolicyPermissionVO(org.apache.cloudstack.iam.server.IAMPolicyPermissionVO) IAMPolicyResponse(org.apache.cloudstack.api.response.iam.IAMPolicyResponse) ListVMsCmd(org.apache.cloudstack.api.command.user.vm.ListVMsCmd) IAMPolicyPermission(org.apache.cloudstack.iam.api.IAMPolicyPermission) IAMPermissionResponse(org.apache.cloudstack.api.response.iam.IAMPermissionResponse) BeforeClass(org.junit.BeforeClass) List(java.util.List) ArrayList(java.util.ArrayList) VirtualMachine(com.cloud.vm.VirtualMachine) Pair(com.cloud.utils.Pair) Test(org.junit.Test)

Aggregations

Pair (com.cloud.utils.Pair)1 VirtualMachine (com.cloud.vm.VirtualMachine)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ListVMsCmd (org.apache.cloudstack.api.command.user.vm.ListVMsCmd)1 IAMPermissionResponse (org.apache.cloudstack.api.response.iam.IAMPermissionResponse)1 IAMPolicyResponse (org.apache.cloudstack.api.response.iam.IAMPolicyResponse)1 IAMPolicy (org.apache.cloudstack.iam.api.IAMPolicy)1 IAMPolicyPermission (org.apache.cloudstack.iam.api.IAMPolicyPermission)1 IAMPolicyPermissionVO (org.apache.cloudstack.iam.server.IAMPolicyPermissionVO)1 IAMPolicyVO (org.apache.cloudstack.iam.server.IAMPolicyVO)1 BeforeClass (org.junit.BeforeClass)1 Test (org.junit.Test)1