public RoleEntity createRole(String roleName) { RoleEntity roleEntity = new RoleEntity(); roleEntity.setName(roleName.toLowerCase()); roleEntity.setDeletable(true); entityManager.persist(roleEntity); return roleEntity; }
@Test(expected=AMWException.class) public void shouldThrowAMWExceptionOnCreateIfResourceGroupAndResourceTypeAreSet() throws AMWException { // given when(permissionRepository.getRoleByName("existing")).thenReturn(new RoleEntity()); when(permissionRepository.getPermissionByName("good")).thenReturn(resourcePermission); // when // then permissionBoundary.createRestriction("existing", null, "good", 7, "bad", null, null, null, false, true); }
@Test public void shouldCreateIfContextIsNull() throws AMWException { // given when(permissionRepository.getRoleByName("existing")).thenReturn(new RoleEntity()); when(permissionRepository.getPermissionByName("good")).thenReturn(resourcePermission); // when permissionBoundary.createRestriction("existing", null, "good", null, null, null, null, CREATE, false, true); // then verify(restrictionRepository).create(any(RestrictionEntity.class)); }
@Test(expected=AMWException.class) public void shouldThrowAMWExceptionOnCreateIfResourceTypePermissionIsNotEmptyAndResourceGroupIsSet() throws AMWException { // given when(permissionRepository.getRoleByName("existing")).thenReturn(new RoleEntity()); when(permissionRepository.getPermissionByName("good")).thenReturn(resourcePermission); // when // then permissionBoundary.createRestriction("existing", null, "good", 7, null, DEFAULT_ONLY, null, null, false, true); }
@Test(expected=AMWException.class) public void shouldThrowAMWExceptionOnCreateIfResourceTypePermissionIsNotEmptyAndResourceTypeIsSet() throws AMWException { // given when(permissionRepository.getRoleByName("existing")).thenReturn(new RoleEntity()); when(permissionRepository.getPermissionByName("good")).thenReturn(resourcePermission); // when // then permissionBoundary.createRestriction("existing", null, "good", null, "bad", NON_DEFAULT_ONLY, null, null, false, true); }
@Test(expected=AMWException.class) public void shouldThrowAMWExceptionOnCreateIfResourceIdCanNotBeFound() throws AMWException { // given when(permissionRepository.getRoleByName("existing")).thenReturn(new RoleEntity()); when(permissionRepository.getPermissionByName("good")).thenReturn(resourcePermission); when(resourceGroupRepository.find(7)).thenReturn(null); // when // then permissionBoundary.createRestriction("existing", null, "good", 7, null, null, null, null, false, true); }
@Test public void shouldCreateIfContextAndActionAreNull() throws AMWException { // given when(permissionRepository.getRoleByName("existing")).thenReturn(new RoleEntity()); when(permissionRepository.getPermissionByName("good")).thenReturn(resourcePermission); // when permissionBoundary.createRestriction("existing", null, "good", null, null, null, null, null, false, true); // then verify(restrictionRepository).create(any(RestrictionEntity.class)); verify(permissionRepository).forceReloadingOfLists(); }
@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(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 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 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(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); }
@Before public void setUp(){ rol = new RoleEntity(); rol.setName("testRole"); per = new PermissionEntity(); per.setValue("testPermission"); }
@Test public void shouldPreserveRestrictionPropertiesIfPermissionIsNotOld() throws AMWException { // given ContextEntity envX = new ContextEntity(); envX.setName("X"); RestrictionEntity restriction = new RestrictionEntity(); when(permissionRepository.getRoleByName("existing")).thenReturn(new RoleEntity()); when(permissionRepository.getPermissionByName("good")).thenReturn(resourcePermission); when(contextLocator.getContextByName("X")).thenReturn(envX); // when permissionBoundary.validateRestriction("existing", null, "good", null, null, ResourceTypePermission.NON_DEFAULT_ONLY, "X", CREATE, restriction); // then assertThat(restriction.getResourceTypePermission(), is(ResourceTypePermission.NON_DEFAULT_ONLY)); assertThat(restriction.getAction(), is(CREATE)); assertThat(restriction.getContext(), is(envX)); assertNull(restriction.getResourceGroup()); assertNull(restriction.getResourceType()); }
@Test public void shouldResetRestrictionPropertiesIfPermissionIsOld() throws AMWException { // given PermissionEntity globalPerm = new PermissionEntity(); globalPerm.setValue("APP_TAB"); RestrictionEntity restriction = new RestrictionEntity(); when(permissionRepository.getRoleByName("existing")).thenReturn(new RoleEntity()); when(permissionRepository.getPermissionByName("good")).thenReturn(globalPerm); // when permissionBoundary.validateRestriction("existing", null, "good", 1, null, ResourceTypePermission.NON_DEFAULT_ONLY, "X", CREATE, restriction); // then assertThat(restriction.getResourceTypePermission(), is(ResourceTypePermission.ANY)); assertThat(restriction.getAction(), is(ALL)); assertNull(restriction.getContext()); assertNull(restriction.getResourceGroup()); assertNull(restriction.getResourceType()); }
@Test public void shouldReturnTrueIfASameRoleRestrictionAlreadyExists() { // given RoleEntity role = new RoleEntity(); role.setName(CONFIG_ADMIN); PermissionEntity permission = new PermissionEntity(); permission.setValue(Permission.RESOURCE.name()); RestrictionEntity restriction = new RestrictionEntity(); restriction.setRole(role); restriction.setAction(Action.UPDATE); restriction.setContext(envC); restriction.setPermission(permission); myRoles = new HashMap<>(); myRoles.put(role.getName(), Arrays.asList(new RestrictionDTOBuilder().buildRestrictionDTO(Permission.RESOURCE, restriction))); permissionService.rolesWithRestrictions = myRoles; // when boolean exists = permissionService.identicalOrMoreGeneralRestrictionExists(restriction); // then Assert.assertTrue(exists); }
@Test public void shouldReturnFalseIfASimilarButMoreResourceGroupRestrictedRoleRestrictionExists() { // given RoleEntity role = new RoleEntity(); role.setName(CONFIG_ADMIN); PermissionEntity permission = new PermissionEntity(); permission.setValue(Permission.RESOURCE.name()); ResourceTypeEntity resourceType = new ResourceTypeEntityBuilder().id(7).build(); ResourceGroupEntity resourceGroup = new ResourceGroupEntity(); resourceGroup.setId(23); resourceGroup.setResourceType(resourceType); RestrictionEntity existingRestriction = new RestrictionEntity(); existingRestriction.setRole(role); existingRestriction.setAction(Action.UPDATE); existingRestriction.setContext(envC); existingRestriction.setPermission(permission); existingRestriction.setResourceGroup(resourceGroup); RestrictionEntity newRestriction = new RestrictionEntity(); newRestriction.setRole(role); newRestriction.setAction(Action.UPDATE); newRestriction.setContext(envC); newRestriction.setPermission(permission); myRoles = new HashMap<>(); myRoles.put(role.getName(), Arrays.asList(new RestrictionDTOBuilder().buildRestrictionDTO(Permission.RESOURCE, existingRestriction))); permissionService.rolesWithRestrictions = myRoles; // when boolean exists = permissionService.identicalOrMoreGeneralRestrictionExists(newRestriction); // then Assert.assertFalse(exists); }
@Test public void shouldReturnTrueIfASimilarRoleRestrictionAlreadyExists() { // given RoleEntity role = new RoleEntity(); role.setName(CONFIG_ADMIN); PermissionEntity permission = new PermissionEntity(); permission.setValue(Permission.RESOURCE.name()); ResourceTypeEntity resourceType = new ResourceTypeEntityBuilder().id(7).build(); ResourceGroupEntity resourceGroup = new ResourceGroupEntity(); resourceGroup.setId(23); resourceGroup.setResourceType(resourceType); RestrictionEntity existingRestriction = new RestrictionEntity(); existingRestriction.setRole(role); existingRestriction.setAction(Action.UPDATE); existingRestriction.setContext(envC); existingRestriction.setPermission(permission); RestrictionEntity newRestriction = new RestrictionEntity(); newRestriction.setRole(role); newRestriction.setAction(Action.UPDATE); newRestriction.setContext(envC); newRestriction.setPermission(permission); newRestriction.setResourceGroup(resourceGroup); myRoles = new HashMap<>(); myRoles.put(role.getName(), Arrays.asList(new RestrictionDTOBuilder().buildRestrictionDTO(Permission.RESOURCE, existingRestriction))); permissionService.rolesWithRestrictions = myRoles; // when boolean exists = permissionService.identicalOrMoreGeneralRestrictionExists(newRestriction); // then Assert.assertTrue(exists); }
@Test public void shouldReturnFalseIfUpdatingExistingRoleRestriction() { RoleEntity role = new RoleEntity(); role.setName(CONFIG_ADMIN); PermissionEntity permission = new PermissionEntity();
@Test public void shouldReturnTrueIfUpdatingExistingRoleRestrictionButAnotherMoreGeneralRestrictionExists() { RoleEntity role = new RoleEntity(); role.setName(CONFIG_ADMIN); PermissionEntity permission = new PermissionEntity();