private int createRestrictionPerContext(String roleName, String userName, String permissionName, Integer resourceGroupId, String resourceTypeName, ResourceTypePermission resourceTypePermission, List<String> contextNames, Action action, boolean delegated) throws AMWException { int count = 0; for (String contextName : contextNames) { if (!delegated || canDelegateThisPermission(permissionName, resourceGroupId, resourceTypeName, contextName, action)) { RestrictionEntity restriction = new RestrictionEntity(); if (createRestriction(roleName, userName, permissionName, resourceGroupId, resourceTypeName, resourceTypePermission, contextName, action, restriction) != null) { count++; } } } return count; }
@Test(expected=AMWException.class) public void shouldThrowAMWExceptionOnUpdateIfRolenameIsNull() throws AMWException { // given when(restrictionRepository.find(1)).thenReturn(new RestrictionEntity()); // when // then permissionBoundary.updateRestriction(1, null, null, null, null, null, null, null, null, true); }
@Test public void shouldCreateRoleOnUpdateIfRoleCanNotBeFound() throws AMWException { // given when(restrictionRepository.find(1)).thenReturn(new RestrictionEntity()); when(permissionRepository.getRoleByName("newRole")).thenReturn(null); when(permissionRepository.getPermissionByName("valid")).thenReturn(resourcePermission); // when permissionBoundary.updateRestriction(1, "newRole", null, "valid", null, null, null, null, null, true); // then verify(permissionRepository).createRole("newRole"); }
@Test(expected=AMWException.class) public void shouldThrowAMWExceptionOnUpdateIfResourceTypeCanNotBeFound() throws AMWException { // given when(restrictionRepository.find(1)).thenReturn(new RestrictionEntity()); when(permissionRepository.getRoleByName("existing")).thenReturn(new RoleEntity()); when(permissionRepository.getPermissionByName("good")).thenReturn(resourcePermission); when(resourceTypeRepository.getByName("bad")).thenReturn(null); // when // then permissionBoundary.updateRestriction(1, "existing", null, "good", null, "bad", null, null, null, true); }
@Test public void shouldUpdateIfContextAndActionAreNull() throws AMWException { // given when(restrictionRepository.find(1)).thenReturn(new RestrictionEntity()); when(permissionRepository.getRoleByName("existing")).thenReturn(new RoleEntity()); when(permissionRepository.getPermissionByName("good")).thenReturn(resourcePermission); // when permissionBoundary.updateRestriction(1, "existing", null, "good", null, null, null, null, null, true); // then verify(restrictionRepository).merge(any(RestrictionEntity.class)); verify(permissionRepository).forceReloadingOfLists(); }
@Test(expected=AMWException.class) public void shouldThrowAMWExceptionOnUpdateIfPermissionCanNotBeFound() throws AMWException { // given when(restrictionRepository.find(1)).thenReturn(new RestrictionEntity()); when(permissionRepository.getRoleByName("existing")).thenReturn(new RoleEntity()); when(permissionRepository.getPermissionByName("invalid")).thenReturn(null); // when // then permissionBoundary.updateRestriction(1, "existing", null, "invalid", null, null, null, null, null, true); }
@Test(expected=AMWException.class) public void shouldThrowAMWExceptionOnUpdateIfPermissionIsNull() throws AMWException { // given when(restrictionRepository.find(1)).thenReturn(new RestrictionEntity()); when(permissionRepository.getRoleByName("existing")).thenReturn(new RoleEntity()); // when // then permissionBoundary.updateRestriction(1, "existing", null, null, null, null, null, null, null, true); }
@Test public void shouldRemoveRestrictionToBeDeleted() throws AMWException { // given when(restrictionRepository.find(42)).thenReturn(new RestrictionEntity()); // when permissionBoundary.removeRestriction(42, true); // then verify(restrictionRepository).deleteRestrictionById(42); verify(permissionRepository).forceReloadingOfLists(); }
@Test(expected=AMWException.class) public void shouldThrowAMWExceptionOnUpdateIfContextCanNotBeFound() throws AMWException { // given when(restrictionRepository.find(1)).thenReturn(new RestrictionEntity()); when(permissionRepository.getRoleByName("existing")).thenReturn(new RoleEntity()); when(permissionRepository.getPermissionByName("good")).thenReturn(resourcePermission); when(contextLocator.getContextByName("bad")).thenThrow(new NoResultException()); // when // then permissionBoundary.updateRestriction(1, "existing", null, "good", null, null, null, "bad", null, true); }
@Test public void hasPermissionToAddResourceTypeTemplateWhenUserIsShakedownAdminAndIsTestingMode() { // given ResourceTypeEntity as = ResourceTypeEntityBuilder.APPLICATION_SERVER_TYPE; when(sessionContext.isCallerInRole(SHAKEDOWN_ADMIN)).thenReturn(true); when(sessionContext.getCallerPrincipal()).thenReturn(principal); myRoles = new HashMap<>(); RestrictionEntity res = new RestrictionEntity(); res.setAction(Action.ALL); myRoles.put(SHAKEDOWN_ADMIN, Arrays.asList(new RestrictionDTOBuilder().mockRestrictionDTO(Permission.SHAKEDOWN_TEST_MODE, res))); permissionService.rolesWithRestrictions = myRoles; // when boolean result = permissionService.hasPermissionToAddResourceTypeTemplate(as, true); // then Assert.assertTrue(result); }
@Test public void hasPermissionToAddResourceTypeTemplateWhenUserIsNotShakedownAdminAndIsTestingMode() { // given ResourceTypeEntity as = ResourceTypeEntityBuilder.APPLICATION_SERVER_TYPE; when(sessionContext.isCallerInRole(VIEWER)).thenReturn(true); when(sessionContext.getCallerPrincipal()).thenReturn(principal); myRoles = new HashMap<>(); RestrictionEntity res = new RestrictionEntity(); res.setAction(Action.ALL); myRoles.put(VIEWER, Arrays.asList(new RestrictionDTOBuilder().mockRestrictionDTO(Permission.DEFAULT, res))); permissionService.rolesWithRestrictions = myRoles; // when boolean result = permissionService.hasPermissionToAddResourceTypeTemplate(as, true); // then Assert.assertFalse(result); }
@Test public void hasPermissionToTemplateModifyWhenUserIsShakedownAdminAndIsNotTestingMode() { // given ResourceEntity as = resourceEntityBuilder.mockAppServerEntity("as", null, null, null); when(sessionContext.isCallerInRole(SHAKEDOWN_ADMIN)).thenReturn(true); when(sessionContext.getCallerPrincipal()).thenReturn(principal); myRoles = new HashMap<>(); RestrictionEntity res = new RestrictionEntity(); res.setAction(Action.ALL); myRoles.put(SHAKEDOWN_ADMIN, Arrays.asList(new RestrictionDTOBuilder().mockRestrictionDTO(Permission.SHAKEDOWN_TEST_MODE, res))); permissionService.rolesWithRestrictions = myRoles; // when boolean result = permissionService.hasPermissionToAddResourceTemplate(as, false); // then Assert.assertFalse(result); }
@Test public void hasPermissionToDeleteResourceRelationWhenUserIsAppDeveloperAndResourceIsInstanceOfApplicationResType(){ // given ResourceEntity app = resourceEntityBuilder.mockApplicationEntity("app", appResourceGroup, null); when(sessionContext.isCallerInRole(APP_DEVELOPER)).thenReturn(true); when(sessionContext.getCallerPrincipal()).thenReturn(principal); myRoles = new HashMap<>(); RestrictionEntity res = new RestrictionEntity(); res.setAction(Action.ALL); myRoles.put(APP_DEVELOPER, Arrays.asList(new RestrictionDTOBuilder().mockRestrictionDTO(Permission.RESOURCE, res))); permissionService.rolesWithRestrictions = myRoles; // when boolean result = permissionService.hasPermissionToDeleteRelation(app, new ContextEntity()); // then Assert.assertTrue(result); }
@Test public void hasPermissionToDeleteResourceRelationWhenUserHasPermissionToUpdateResourceAndResourceIsRuntimeType(){ // given ResourceEntity runtime = resourceEntityBuilder.mockRuntimeEntity("EAP6", null, null); when(sessionContext.isCallerInRole(SERVER_ADMIN)).thenReturn(true); when(sessionContext.getCallerPrincipal()).thenReturn(principal); myRoles = new HashMap<>(); RestrictionEntity res = new RestrictionEntity(); res.setAction(Action.ALL); myRoles.put(SERVER_ADMIN, Arrays.asList(new RestrictionDTOBuilder().mockRestrictionDTO(Permission.RESOURCE, res))); permissionService.rolesWithRestrictions = myRoles; // when boolean result = permissionService.hasPermissionToDeleteRelation(runtime, new ContextEntity()); // then Assert.assertTrue(result); }
@Test public void hasPermissionToAddResourceRelationWhenUserHasPermissionToUpdateResourceAndResourceIsInstanceOfApplicationResType(){ // given ResourceEntity app = resourceEntityBuilder.mockApplicationEntity("app", appResourceGroup, null); when(sessionContext.isCallerInRole(APP_DEVELOPER)).thenReturn(true); when(sessionContext.getCallerPrincipal()).thenReturn(principal); myRoles = new HashMap<>(); RestrictionEntity upd = new RestrictionEntity(); upd.setAction(Action.UPDATE); myRoles.put(APP_DEVELOPER, Arrays.asList(new RestrictionDTOBuilder().mockRestrictionDTO(Permission.RESOURCE, upd))); permissionService.rolesWithRestrictions = myRoles; // when boolean canAdd = permissionService.hasPermissionToAddRelation(app, new ContextEntity()); // then Assert.assertTrue(canAdd); }
@Test public void hasPermissionToDeleteResourceRelationWhenUserHasResourceUpdatePermissionAndResourceIsNotInstanceOfNodeResType(){ // given ResourceEntity node = resourceEntityBuilder.mockNodeEntity("node", null,null); when(sessionContext.isCallerInRole(SERVER_ADMIN)).thenReturn(true); when(sessionContext.getCallerPrincipal()).thenReturn(principal); myRoles = new HashMap<>(); RestrictionEntity res = new RestrictionEntity(); res.setAction(Action.UPDATE); myRoles.put(SERVER_ADMIN, Arrays.asList(new RestrictionDTOBuilder().mockRestrictionDTO(Permission.RESOURCE, res))); permissionService.rolesWithRestrictions = myRoles; // when boolean result = permissionService.hasPermissionToDeleteRelation(node, new ContextEntity()); // then Assert.assertTrue(result); }
@Test public void hasPermissionToDeleteResourceRelationWhenUserHasResourceUpdatePermissionAndResourceIsInstanceOfNotDefaultResourceType(){ // given ResourceEntity ws = resourceEntityBuilder.mockResourceEntity("ws", null, "webservice", null); when(sessionContext.isCallerInRole(CONFIG_ADMIN)).thenReturn(true); when(sessionContext.getCallerPrincipal()).thenReturn(principal); myRoles = new HashMap<>(); RestrictionEntity upd = new RestrictionEntity(); upd.setAction(Action.UPDATE); myRoles.put(CONFIG_ADMIN, Arrays.asList(new RestrictionDTOBuilder().mockRestrictionDTO(Permission.RESOURCE, upd))); permissionService.rolesWithRestrictions = myRoles; // when boolean result = permissionService.hasPermissionToDeleteRelation(ws, new ContextEntity()); // then Assert.assertTrue(result); }
@Test public void hasPermissionToAddResourceRelationWhenUserHasPermissionToUpdateResource() { // given ResourceEntity as = resourceEntityBuilder.mockAppServerEntity("as", null, null, null); when(sessionContext.isCallerInRole(CONFIG_ADMIN)).thenReturn(true); when(sessionContext.getCallerPrincipal()).thenReturn(principal); myRoles = new HashMap<>(); RestrictionEntity res = new RestrictionEntity(); res.setAction(Action.ALL); myRoles.put(CONFIG_ADMIN, Arrays.asList(new RestrictionDTOBuilder().mockRestrictionDTO(Permission.RESOURCE, res))); permissionService.rolesWithRestrictions = myRoles; // when boolean canAdd = permissionService.hasPermissionToAddRelation(as, new ContextEntity()); // then Assert.assertTrue(canAdd); }
@Test public void constructedWithRestrictionItShouldHaveRightAction(){ //given RestrictionEntity res = new RestrictionEntity(); res.setAction(Action.READ); res.setPermission(per); //when RestrictionDTO resDTO = new RestrictionDTO(res); //then Assert.assertEquals(per.getValue(), resDTO.getPermissionName()); Assert.assertEquals(res.getAction(), resDTO.getRestriction().getAction()); }
@Test public void constructedWithRestrictionItShouldHaveRightContext(){ //given RestrictionEntity res = new RestrictionEntity(); res.setAction(Action.CREATE); res.setContext(new ContextEntityBuilder().buildContextEntity("TEST", null, Collections.EMPTY_SET,false)); res.setPermission(per); res.setRole(rol); //when RestrictionDTO resDTO = new RestrictionDTO(res); //then Assert.assertEquals(rol.getName(), resDTO.getRestriction().getRole().getName()); Assert.assertEquals(res.getContext().getName(), resDTO.getRestriction().getContext().getName()); } }