use of org.junit.Assert.assertTrue in project CzechIdMng by bcvsolutions.
the class DefaultIdmIdentityContractServiceIntegrationTest method testAutomaticRolesRemovalAfterContractEnds.
@Test
public void testAutomaticRolesRemovalAfterContractEnds() {
// automatic roles by tree structure
prepareAutomaticRoles();
// automatic role by attribute on contract
String autoPosition = getHelper().createName();
IdmRoleDto autoAttributeRole = getHelper().createRole();
IdmAutomaticRoleAttributeDto automaticRole = getHelper().createAutomaticRole(autoAttributeRole.getId());
getHelper().createAutomaticRoleRule(automaticRole.getId(), AutomaticRoleAttributeRuleComparison.EQUALS, AutomaticRoleAttributeRuleType.CONTRACT, IdmIdentityContract_.position.getName(), null, autoPosition);
//
// prepare identity, contract, direct roles and automatic roles
IdmIdentityDto identity = getHelper().createIdentity((GuardedString) null);
IdmIdentityContractDto contract = service.getPrimeContract(identity.getId());
contract.setIdentity(identity.getId());
contract.setValidFrom(LocalDate.now().minusDays(1));
contract.setValidTill(LocalDate.now().plusMonths(1));
contract.setWorkPosition(nodeD.getId());
contract.setMain(true);
contract.setDescription("test-node-d");
contract.setPosition(autoPosition);
contract = service.save(contract);
UUID contractId = contract.getId();
IdmRoleDto directRole = getHelper().createRole();
getHelper().createIdentityRole(contract, directRole);
//
List<IdmIdentityRoleDto> identityRoles = identityRoleService.findAllByContract(contract.getId());
Assert.assertEquals(5, identityRoles.size());
Assert.assertTrue(identityRoles.stream().anyMatch(ir -> {
return roleA.getId().equals(ir.getRole());
}));
Assert.assertTrue(identityRoles.stream().anyMatch(ir -> {
return roleB.getId().equals(ir.getRole());
}));
Assert.assertTrue(identityRoles.stream().anyMatch(ir -> {
return roleC.getId().equals(ir.getRole());
}));
Assert.assertTrue(identityRoles.stream().anyMatch(ir -> {
return directRole.getId().equals(ir.getRole());
}));
Assert.assertTrue(identityRoles.stream().anyMatch(ir -> {
return autoAttributeRole.getId().equals(ir.getRole());
}));
//
try {
getHelper().setConfigurationValue(EventConfiguration.PROPERTY_EVENT_ASYNCHRONOUS_ENABLED, true);
//
// end contract - all roles should be removed, after asynchronous role request ends
contract.setValidTill(LocalDate.now().minusDays(1));
contract = service.save(contract);
//
Assert.assertFalse(contract.isValidNowOrInFuture());
//
getHelper().waitForResult(res -> {
return !identityRoleService.findAllByContract(contractId).isEmpty();
}, 300, Integer.MAX_VALUE);
getHelper().waitForResult(res -> {
IdmLongRunningTaskFilter filter = new IdmLongRunningTaskFilter();
filter.setRunning(Boolean.TRUE);
//
return taskManager.findLongRunningTasks(filter, null).getTotalElements() != 0;
});
//
identityRoles = identityRoleService.findAllByContract(contract.getId());
Assert.assertTrue(identityRoles.isEmpty());
//
service.delete(contract);
} finally {
getHelper().setConfigurationValue(EventConfiguration.PROPERTY_EVENT_ASYNCHRONOUS_ENABLED, false);
}
}
use of org.junit.Assert.assertTrue in project CzechIdMng by bcvsolutions.
the class DefaultIdmIdentityContractServiceIntegrationTest method testChangeContractPositionAndValidityWithAutomaticRolesAssigned.
@Test
public void testChangeContractPositionAndValidityWithAutomaticRolesAssigned() {
prepareAutomaticRoles();
//
// prepare identity and contract
IdmIdentityDto identity = getHelper().createIdentity((GuardedString) null);
IdmIdentityContractDto contract = new IdmIdentityContractDto();
contract.setIdentity(identity.getId());
contract.setWorkPosition(nodeD.getId());
LocalDate validTill = LocalDate.now().plusDays(1);
contract.setValidTill(validTill);
contract = service.save(contract);
//
// test after create
List<IdmIdentityRoleDto> identityRoles = identityRoleService.findAllByContract(contract.getId());
IdmIdentityRoleDto automaticRole = identityRoles.stream().filter(ir -> {
return roleA.getId().equals(ir.getRole());
}).findFirst().orElse(null);
Assert.assertNotNull(automaticRole);
Assert.assertEquals(validTill, automaticRole.getValidTill());
//
// => role A is the same => down recursion
contract.setWorkPosition(nodeB.getId());
LocalDate newValidTill = LocalDate.now().plusDays(3);
contract.setValidTill(newValidTill);
contract = service.save(contract);
//
// test after change
identityRoles = identityRoleService.findAllByContract(contract.getId());
Assert.assertTrue(identityRoles.stream().anyMatch(ir -> {
return roleA.getId().equals(ir.getRole()) && // prevent drop and create
ir.getId().equals(automaticRole.getId()) && // validity is changed
newValidTill.equals(ir.getValidTill());
}));
}
use of org.junit.Assert.assertTrue in project CzechIdMng by bcvsolutions.
the class DefaultIdmIdentityContractServiceIntegrationTest method testSkipAndAssignAutomaticRoleOnPositionAfterChange.
@Test
public void testSkipAndAssignAutomaticRoleOnPositionAfterChange() {
IdmTreeNodeDto otherNode = getHelper().createTreeNode();
IdmTreeNodeDto node = getHelper().createTreeNode();
// define automatic role for parent
IdmRoleDto role = getHelper().createRole();
IdmRoleTreeNodeDto automaticRole = getHelper().createRoleTreeNode(role, node, RecursionType.NO, true);
// create identity with contract on node
IdmIdentityDto identity = getHelper().createIdentity((GuardedString) null);
IdmContractPositionDto position = getHelper().createContractPosition(getHelper().getPrimeContract(identity), otherNode);
// no role should be assigned now
List<IdmIdentityRoleDto> assignedRoles = identityRoleService.findAllByIdentity(identity.getId());
Assert.assertTrue(assignedRoles.isEmpty());
//
position.setWorkPosition(node.getId());
EntityEvent<IdmContractPositionDto> event = new ContractPositionEvent(ContractPositionEventType.UPDATE, position);
event.getProperties().put(AutomaticRoleManager.SKIP_RECALCULATION, Boolean.TRUE);
position = contractPositionService.publish(event).getContent();
UUID positionId = position.getId();
IdmEntityStateFilter filter = new IdmEntityStateFilter();
filter.setStates(Lists.newArrayList(OperationState.BLOCKED));
filter.setResultCode(CoreResultCode.AUTOMATIC_ROLE_SKIPPED.getCode());
filter.setOwnerType(entityStateManager.getOwnerType(IdmContractPositionDto.class));
List<IdmEntityStateDto> skippedStates = entityStateManager.findStates(filter, null).getContent();
Assert.assertTrue(skippedStates.stream().anyMatch(s -> s.getOwnerId().equals(positionId)));
//
assignedRoles = identityRoleService.findAllByIdentity(identity.getId());
Assert.assertTrue(assignedRoles.isEmpty());
//
// recount skipped automatic roles
longRunningTaskManager.execute(new ProcessSkippedAutomaticRoleByTreeForContractTaskExecutor());
skippedStates = entityStateManager.findStates(filter, null).getContent();
Assert.assertFalse(skippedStates.stream().anyMatch(s -> s.getOwnerId().equals(automaticRole.getId())));
//
assignedRoles = identityRoleService.findAllByIdentity(identity.getId());
Assert.assertEquals(1, assignedRoles.size());
Assert.assertEquals(automaticRole.getId(), assignedRoles.get(0).getAutomaticRole());
}
use of org.junit.Assert.assertTrue in project CzechIdMng by bcvsolutions.
the class DefaultAuthorizationManagerIntegrationTest method testDistictPolicies.
@Test
@Transactional
public void testDistictPolicies() {
IdmIdentityDto identity = getHelper().createIdentity((GuardedString) null);
IdmRoleDto role = getHelper().createRole();
IdmRoleDto roleTwo = getHelper().createRole();
IdmAuthorizationPolicyDto policy = getHelper().createBasePolicy(role.getId(), CoreGroupPermission.IDENTITY, IdmIdentity.class, IdmBasePermission.AUTOCOMPLETE, IdmBasePermission.READ);
getHelper().createIdentityRole(identity, role);
getHelper().createIdentityRole(identity, role);
getHelper().createIdentityRole(identity, roleTwo);
getHelper().createIdentityRole(identity, roleTwo);
//
List<IdmAuthorizationPolicyDto> enabledDistinctPolicies = manager.getEnabledDistinctPolicies(identity.getId(), IdmIdentity.class);
Assert.assertEquals(1, enabledDistinctPolicies.size());
Assert.assertTrue(enabledDistinctPolicies.stream().anyMatch(p -> p.getId().equals(policy.getId())));
//
IdmAuthorizationPolicyDto policyTwo = getHelper().createBasePolicy(role.getId(), CoreGroupPermission.IDENTITY, IdmIdentity.class, IdmBasePermission.READ, IdmBasePermission.AUTOCOMPLETE);
//
enabledDistinctPolicies = manager.getEnabledDistinctPolicies(identity.getId(), IdmIdentity.class);
Assert.assertEquals(1, enabledDistinctPolicies.size());
Assert.assertTrue(enabledDistinctPolicies.stream().anyMatch(p -> p.getId().equals(policy.getId()) || p.getId().equals(policyTwo.getId())));
//
IdmAuthorizationPolicyDto policyThree = getHelper().createBasePolicy(role.getId(), CoreGroupPermission.IDENTITY, IdmIdentity.class, IdmBasePermission.AUTOCOMPLETE);
//
enabledDistinctPolicies = manager.getEnabledDistinctPolicies(identity.getId(), IdmIdentity.class);
Assert.assertEquals(2, enabledDistinctPolicies.size());
Assert.assertTrue(enabledDistinctPolicies.stream().anyMatch(p -> p.getId().equals(policy.getId()) || p.getId().equals(policyTwo.getId())));
Assert.assertTrue(enabledDistinctPolicies.stream().anyMatch(p -> p.getId().equals(policyThree.getId())));
//
// with parameters
ConfigurationMap propsFour = new ConfigurationMap();
propsFour.put("one", "valueOne");
propsFour.put("two", "valueTwo");
IdmAuthorizationPolicyDto policyFour = getHelper().createAuthorizationPolicy(roleTwo.getId(), CoreGroupPermission.IDENTITY, IdmIdentity.class, UuidEvaluator.class, propsFour, IdmBasePermission.READ, IdmBasePermission.AUTOCOMPLETE);
//
ConfigurationMap propsFive = new ConfigurationMap();
propsFive.put("two", "valueTwo");
propsFive.put("one", "valueOne");
IdmAuthorizationPolicyDto policyFive = getHelper().createAuthorizationPolicy(roleTwo.getId(), CoreGroupPermission.IDENTITY, IdmIdentity.class, UuidEvaluator.class, propsFive, IdmBasePermission.AUTOCOMPLETE, IdmBasePermission.READ);
//
enabledDistinctPolicies = manager.getEnabledDistinctPolicies(identity.getId(), IdmIdentity.class);
Assert.assertEquals(3, enabledDistinctPolicies.size());
Assert.assertTrue(enabledDistinctPolicies.stream().anyMatch(p -> p.getId().equals(policy.getId()) || p.getId().equals(policyTwo.getId())));
Assert.assertTrue(enabledDistinctPolicies.stream().anyMatch(p -> p.getId().equals(policyThree.getId())));
Assert.assertTrue(enabledDistinctPolicies.stream().anyMatch(p -> p.getId().equals(policyFour.getId()) || p.getId().equals(policyFive.getId())));
//
ConfigurationMap propsSix = new ConfigurationMap();
propsSix.put("one", "valueOneU");
propsSix.put("two", "valueTwo");
IdmAuthorizationPolicyDto policySix = getHelper().createAuthorizationPolicy(roleTwo.getId(), CoreGroupPermission.IDENTITY, IdmIdentity.class, UuidEvaluator.class, propsSix, IdmBasePermission.AUTOCOMPLETE, IdmBasePermission.READ);
//
ConfigurationMap propsSeven = new ConfigurationMap();
propsSeven.put("one", "valueOneU");
propsSeven.put("two", "valueTwo");
IdmAuthorizationPolicyDto policySeven = getHelper().createAuthorizationPolicy(roleTwo.getId(), CoreGroupPermission.IDENTITY, IdmIdentity.class, UuidEvaluator.class, propsSeven, IdmBasePermission.READ);
//
enabledDistinctPolicies = manager.getEnabledDistinctPolicies(identity.getId(), IdmIdentity.class);
Assert.assertEquals(5, enabledDistinctPolicies.size());
Assert.assertTrue(enabledDistinctPolicies.stream().anyMatch(p -> p.getId().equals(policy.getId()) || p.getId().equals(policyTwo.getId())));
Assert.assertTrue(enabledDistinctPolicies.stream().anyMatch(p -> p.getId().equals(policyThree.getId())));
Assert.assertTrue(enabledDistinctPolicies.stream().anyMatch(p -> p.getId().equals(policyFour.getId()) || p.getId().equals(policyFive.getId())));
Assert.assertTrue(enabledDistinctPolicies.stream().anyMatch(p -> p.getId().equals(policySix.getId())));
Assert.assertTrue(enabledDistinctPolicies.stream().anyMatch(p -> p.getId().equals(policySeven.getId())));
}
use of org.junit.Assert.assertTrue in project CzechIdMng by bcvsolutions.
the class DefaultAuthorizationManagerIntegrationTest method testCacheAfterContractIsChanged.
@Test
@Transactional
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testCacheAfterContractIsChanged() {
// create and login identity
IdmIdentityDto identity = getHelper().createIdentity();
UUID mockIdentity = UUID.randomUUID();
// prepare role
IdmRoleDto role = getHelper().createRole();
getHelper().createBasePolicy(role.getId(), IdmBasePermission.AUTOCOMPLETE, IdmBasePermission.READ);
getHelper().createIdentityRole(identity, role);
//
Assert.assertNull(cacheManager.getValue(AuthorizationManager.AUTHORIZATION_POLICY_CACHE_NAME, identity.getId()));
Assert.assertNull(cacheManager.getValue(AuthorizationManager.PERMISSION_CACHE_NAME, identity.getId()));
Assert.assertNull(cacheManager.getValue(AuthorizationManager.AUTHORIZATION_POLICY_CACHE_NAME, mockIdentity));
Assert.assertNull(cacheManager.getValue(AuthorizationManager.PERMISSION_CACHE_NAME, mockIdentity));
//
cacheManager.cacheValue(AuthorizationManager.AUTHORIZATION_POLICY_CACHE_NAME, mockIdentity, new HashMap<>());
cacheManager.cacheValue(AuthorizationManager.PERMISSION_CACHE_NAME, mockIdentity, new HashMap<>());
Assert.assertNotNull(cacheManager.getValue(AuthorizationManager.AUTHORIZATION_POLICY_CACHE_NAME, mockIdentity));
Assert.assertNotNull(cacheManager.getValue(AuthorizationManager.PERMISSION_CACHE_NAME, mockIdentity));
//
// without login
Set<String> permissions = manager.getPermissions(role);
Assert.assertTrue(permissions.isEmpty());
//
try {
getHelper().login(identity);
//
// new entity is not supported with cache, but permissions are evaluated
permissions = manager.getPermissions(new IdmRoleDto());
Assert.assertEquals(2, permissions.size());
Assert.assertTrue(permissions.stream().anyMatch(p -> p.equals(IdmBasePermission.AUTOCOMPLETE.getName())));
Assert.assertTrue(permissions.stream().anyMatch(p -> p.equals(IdmBasePermission.READ.getName())));
Assert.assertNull(cacheManager.getValue(AuthorizationManager.PERMISSION_CACHE_NAME, identity.getId()));
//
// load from db
permissions = manager.getPermissions(role);
Assert.assertEquals(2, permissions.size());
Assert.assertTrue(permissions.stream().anyMatch(p -> p.equals(IdmBasePermission.AUTOCOMPLETE.getName())));
Assert.assertTrue(permissions.stream().anyMatch(p -> p.equals(IdmBasePermission.READ.getName())));
Assert.assertNotNull(cacheManager.getValue(AuthorizationManager.AUTHORIZATION_POLICY_CACHE_NAME, identity.getId()));
Assert.assertNotNull(cacheManager.getValue(AuthorizationManager.PERMISSION_CACHE_NAME, identity.getId()));
// load from cache
permissions = manager.getPermissions(role);
Assert.assertEquals(2, permissions.size());
Assert.assertTrue(permissions.stream().anyMatch(p -> p.equals(IdmBasePermission.AUTOCOMPLETE.getName())));
Assert.assertTrue(permissions.stream().anyMatch(p -> p.equals(IdmBasePermission.READ.getName())));
Assert.assertNotNull(cacheManager.getValue(AuthorizationManager.AUTHORIZATION_POLICY_CACHE_NAME, identity.getId()));
Assert.assertNotNull(cacheManager.getValue(AuthorizationManager.PERMISSION_CACHE_NAME, identity.getId()));
// check cache content - one
ValueWrapper cacheValue = cacheManager.getValue(AuthorizationManager.AUTHORIZATION_POLICY_CACHE_NAME, identity.getId());
List<UUID> cachedPolicies = (List) ((Map) cacheValue.get()).get(role.getClass());
Assert.assertEquals(1, cachedPolicies.size());
Assert.assertEquals(BasePermissionEvaluator.class.getCanonicalName(), ((IdmAuthorizationPolicyDto) cacheManager.getValue(AuthorizationManager.AUTHORIZATION_POLICY_DEFINITION_CACHE_NAME, cachedPolicies.get(0)).get()).getEvaluatorType());
cacheValue = cacheManager.getValue(AuthorizationManager.PERMISSION_CACHE_NAME, identity.getId());
permissions = (Set) ((Map) cacheValue.get()).get(role.getId());
Assert.assertEquals(2, permissions.size());
Assert.assertTrue(permissions.stream().anyMatch(p -> p.equals(IdmBasePermission.AUTOCOMPLETE.getName())));
Assert.assertTrue(permissions.stream().anyMatch(p -> p.equals(IdmBasePermission.READ.getName())));
//
// change contract => evict cache of logged identity
getHelper().createContract(identity);
//
// check cache is evicted only for logged identity
Assert.assertNotNull(cacheManager.getValue(AuthorizationManager.AUTHORIZATION_POLICY_CACHE_NAME, identity.getId()));
Assert.assertNull(cacheManager.getValue(AuthorizationManager.PERMISSION_CACHE_NAME, identity.getId()));
Assert.assertNotNull(cacheManager.getValue(AuthorizationManager.AUTHORIZATION_POLICY_CACHE_NAME, mockIdentity));
Assert.assertNotNull(cacheManager.getValue(AuthorizationManager.PERMISSION_CACHE_NAME, mockIdentity));
} finally {
logout();
}
}
Aggregations