use of org.junit.Assert.assertEquals in project CzechIdMng by bcvsolutions.
the class DefaultIdmRoleFormAttributeServiceIntegrationTest method testSubDefinitionOverrideValidationMax.
@Test
public void testSubDefinitionOverrideValidationMax() {
// Create role with attribute (include the sub-definition)
IdmRoleDto role = createRoleWithAttributes();
IdmRoleFormAttributeFilter filter = new IdmRoleFormAttributeFilter();
filter.setRole(role.getId());
List<IdmRoleFormAttributeDto> list = roleFormAttributeService.find(filter, null).getContent();
Assert.assertEquals(2, list.size());
IdmFormDefinitionDto formAttributeSubdefinition = roleService.getFormAttributeSubdefinition(role);
Assert.assertEquals(2, formAttributeSubdefinition.getFormAttributes().size());
// Delete IP attribute from the sub-definition
list.stream().filter(roleFormAttributeDto -> {
IdmFormAttributeDto formAttributeDto = DtoUtils.getEmbedded(roleFormAttributeDto, IdmRoleFormAttribute_.formAttribute.getName(), IdmFormAttributeDto.class);
return formAttributeDto.getCode().equals(IP);
}).forEach(roleFormAttributeDto -> roleFormAttributeService.delete(roleFormAttributeDto));
// Set Max
list.stream().filter(roleFormAttributeDto -> {
IdmFormAttributeDto formAttributeDto = DtoUtils.getEmbedded(roleFormAttributeDto, IdmRoleFormAttribute_.formAttribute.getName(), IdmFormAttributeDto.class);
return formAttributeDto.getCode().equals(NUMBER_OF_FINGERS);
}).forEach(roleFormAttributeDto -> {
Assert.assertEquals(BigDecimal.TEN.longValue(), roleFormAttributeDto.getMax().longValue());
roleFormAttributeDto.setMax(BigDecimal.valueOf(111));
roleFormAttributeService.save(roleFormAttributeDto);
});
// Load sub-definition by role
formAttributeSubdefinition = roleService.getFormAttributeSubdefinition(role);
Assert.assertEquals(1, formAttributeSubdefinition.getFormAttributes().size());
IdmFormAttributeDto numberAttribute = formAttributeSubdefinition.getFormAttributes().stream().filter(attributeDto -> {
return attributeDto.getCode().equals(NUMBER_OF_FINGERS);
}).findFirst().orElse(null);
Assert.assertNotNull(numberAttribute);
Assert.assertEquals(111, numberAttribute.getMax().intValue());
}
use of org.junit.Assert.assertEquals in project CzechIdMng by bcvsolutions.
the class DefaultIdmRoleFormAttributeServiceIntegrationTest method testSubDefinitionOverrideValidationRequired.
@Test
public void testSubDefinitionOverrideValidationRequired() {
// Create role with attribute (include the sub-definition)
IdmRoleDto role = createRoleWithAttributes();
IdmRoleFormAttributeFilter filter = new IdmRoleFormAttributeFilter();
filter.setRole(role.getId());
List<IdmRoleFormAttributeDto> list = roleFormAttributeService.find(filter, null).getContent();
Assert.assertEquals(2, list.size());
IdmFormDefinitionDto formAttributeSubdefinition = roleService.getFormAttributeSubdefinition(role);
Assert.assertEquals(2, formAttributeSubdefinition.getFormAttributes().size());
// Set required validation on false to IP attribute in the sub-definition
list.stream().filter(roleFormAttributeDto -> {
IdmFormAttributeDto formAttributeDto = DtoUtils.getEmbedded(roleFormAttributeDto, IdmRoleFormAttribute_.formAttribute.getName(), IdmFormAttributeDto.class);
return formAttributeDto.getCode().equals(IP);
}).forEach(roleFormAttributeDto -> {
Assert.assertTrue(roleFormAttributeDto.isRequired());
roleFormAttributeDto.setRequired(false);
roleFormAttributeService.save(roleFormAttributeDto);
});
// Load sub-definition by role
formAttributeSubdefinition = roleService.getFormAttributeSubdefinition(role);
Assert.assertEquals(2, formAttributeSubdefinition.getFormAttributes().size());
IdmFormAttributeDto ipFormAttribute = formAttributeSubdefinition.getFormAttributes().stream().filter(attributeDto -> {
return attributeDto.getCode().equals(IP);
}).findFirst().orElse(null);
Assert.assertNotNull(ipFormAttribute);
Assert.assertFalse(ipFormAttribute.isRequired());
}
use of org.junit.Assert.assertEquals in project CzechIdMng by bcvsolutions.
the class DefaultIdmRoleFormAttributeServiceIntegrationTest method testDeleteRoleAttributeValueViaRoleRequest.
@Test
public void testDeleteRoleAttributeValueViaRoleRequest() {
// Create role with attribute (include the sub-definition)
IdmRoleDto role = createRoleWithAttributes();
IdmRoleFormAttributeFilter filter = new IdmRoleFormAttributeFilter();
filter.setRole(role.getId());
List<IdmRoleFormAttributeDto> list = roleFormAttributeService.find(filter, null).getContent();
Assert.assertEquals(2, list.size());
IdmFormDefinitionDto formAttributeSubdefinition = roleService.getFormAttributeSubdefinition(role);
Assert.assertEquals(2, formAttributeSubdefinition.getFormAttributes().size());
// Delete IP attribute from the sub-definition
list.stream().filter(roleFormAttributeDto -> {
IdmFormAttributeDto formAttributeDto = DtoUtils.getEmbedded(roleFormAttributeDto, IdmRoleFormAttribute_.formAttribute.getName(), IdmFormAttributeDto.class);
return formAttributeDto.getCode().equals(IP);
}).forEach(roleFormAttributeDto -> roleFormAttributeService.delete(roleFormAttributeDto));
formAttributeSubdefinition = roleService.getFormAttributeSubdefinition(role);
Assert.assertEquals(1, formAttributeSubdefinition.getFormAttributes().size());
Assert.assertEquals(NUMBER_OF_FINGERS, formAttributeSubdefinition.getFormAttributes().get(0).getCode());
IdmIdentityDto identity = getHelper().createIdentity();
IdmIdentityContractDto contract = getHelper().getPrimeContract(identity);
IdmIdentityRoleFilter identityRoleFilter = new IdmIdentityRoleFilter();
identityRoleFilter.setIdentityContractId(contract.getId());
List<IdmIdentityRoleDto> identityRoles = identityRoleService.find(identityRoleFilter, null).getContent();
assertEquals(0, identityRoles.size());
// Create request
IdmRoleRequestDto request = getHelper().createRoleRequest(identity);
// Create change role-concept
IdmConceptRoleRequestDto conceptRoleRequest = new IdmConceptRoleRequestDto();
conceptRoleRequest.setRoleRequest(request.getId());
conceptRoleRequest.setIdentityContract(contract.getId());
// Change the valid from
conceptRoleRequest.setValidFrom(LocalDate.now());
conceptRoleRequest.setRole(role.getId());
conceptRoleRequest.setOperation(ConceptRoleRequestOperation.ADD);
conceptRoleRequest = conceptRoleRequestService.save(conceptRoleRequest);
conceptRoleRequest.getEavs().clear();
// Create role attribute value in concept
IdmFormInstanceDto formInstanceDto = conceptRoleRequestService.getRoleAttributeValues(conceptRoleRequest, false);
Assert.assertNotNull(formInstanceDto);
Assert.assertNotNull(formInstanceDto.getFormDefinition());
Assert.assertEquals(0, formInstanceDto.getValues().size());
IdmFormAttributeDto attribute = formInstanceDto.getMappedAttributeByCode(NUMBER_OF_FINGERS);
IdmFormValueDto formValueDto = new IdmFormValueDto(attribute);
formValueDto.setValue(BigDecimal.TEN);
List<IdmFormValueDto> values = Lists.newArrayList(formValueDto);
formInstanceDto.setValues(values);
List<IdmFormInstanceDto> forms = Lists.newArrayList(formInstanceDto);
conceptRoleRequest.setEavs(forms);
conceptRoleRequest = conceptRoleRequestService.save(conceptRoleRequest);
conceptRoleRequest.getEavs().clear();
formInstanceDto = conceptRoleRequestService.getRoleAttributeValues(conceptRoleRequest, false);
Assert.assertEquals(1, formInstanceDto.getValues().size());
Serializable value = formInstanceDto.toSinglePersistentValue(NUMBER_OF_FINGERS);
Assert.assertEquals(BigDecimal.TEN.longValue(), ((BigDecimal) value).longValue());
request = getHelper().executeRequest(request, false, true);
// Check request
assertEquals(RoleRequestState.EXECUTED, request.getState());
identityRoles = identityRoleService.find(identityRoleFilter, null).getContent();
assertEquals(1, identityRoles.size());
IdmIdentityRoleDto identityRole = identityRoles.get(0);
// Check identity-role
identityRole = identityRoleService.get(identityRole.getId());
assertEquals(conceptRoleRequest.getValidFrom(), identityRole.getValidFrom());
// Check role attribute value in identity-role
identityRole.getEavs().clear();
IdmFormInstanceDto identityRoleFormInstanceDto = identityRoleService.getRoleAttributeValues(identityRole);
Assert.assertEquals(1, identityRoleFormInstanceDto.getValues().size());
value = identityRoleFormInstanceDto.toSinglePersistentValue(NUMBER_OF_FINGERS);
Assert.assertEquals(BigDecimal.TEN.longValue(), ((BigDecimal) value).longValue());
// Create request for delete attribute value
IdmRoleRequestDto requestUpdate = getHelper().createRoleRequest(identity);
// Create change role-concept
IdmConceptRoleRequestDto updateConceptRoleRequest = new IdmConceptRoleRequestDto();
updateConceptRoleRequest.setRoleRequest(requestUpdate.getId());
updateConceptRoleRequest.setIdentityContract(contract.getId());
// Change the valid from
updateConceptRoleRequest.setValidFrom(LocalDate.now());
updateConceptRoleRequest.setRole(role.getId());
updateConceptRoleRequest.setIdentityRole(identityRole.getId());
updateConceptRoleRequest.setOperation(ConceptRoleRequestOperation.UPDATE);
updateConceptRoleRequest = conceptRoleRequestService.save(updateConceptRoleRequest);
updateConceptRoleRequest.getEavs().clear();
/**
* Erase role attribute value in concept
*/
formInstanceDto = conceptRoleRequestService.getRoleAttributeValues(updateConceptRoleRequest, false);
Assert.assertNotNull(formInstanceDto);
Assert.assertNotNull(formInstanceDto.getFormDefinition());
Assert.assertEquals(0, formInstanceDto.getValues().size());
attribute = formInstanceDto.getMappedAttributeByCode(NUMBER_OF_FINGERS);
formValueDto = new IdmFormValueDto(attribute);
formValueDto.setValue(null);
values = Lists.newArrayList(formValueDto);
formInstanceDto.setValues(values);
forms = Lists.newArrayList(formInstanceDto);
updateConceptRoleRequest.setEavs(forms);
updateConceptRoleRequest = conceptRoleRequestService.save(updateConceptRoleRequest);
updateConceptRoleRequest.getEavs().clear();
formInstanceDto = conceptRoleRequestService.getRoleAttributeValues(updateConceptRoleRequest, false);
// No form-value can exists
Assert.assertEquals(0, formInstanceDto.getValues().size());
requestUpdate = getHelper().executeRequest(requestUpdate, false, true);
// Check request
assertEquals(RoleRequestState.EXECUTED, requestUpdate.getState());
identityRoles = identityRoleService.find(identityRoleFilter, null).getContent();
assertEquals(1, identityRoles.size());
identityRole = identityRoles.get(0);
// Check identity-role
identityRole = identityRoleService.get(identityRole.getId());
assertEquals(updateConceptRoleRequest.getValidFrom(), identityRole.getValidFrom());
// Check role attribute value in identity-role
identityRole.getEavs().clear();
identityRoleFormInstanceDto = identityRoleService.getRoleAttributeValues(identityRole);
Assert.assertEquals(0, identityRoleFormInstanceDto.getValues().size());
}
use of org.junit.Assert.assertEquals in project CzechIdMng by bcvsolutions.
the class DefaultIdmRoleFormAttributeServiceIntegrationTest method testSubDefinition.
@Test
public void testSubDefinition() {
// Create role with attribute (include the sub-definition)
IdmRoleDto role = createRoleWithAttributes();
IdmRoleFormAttributeFilter filter = new IdmRoleFormAttributeFilter();
filter.setRole(role.getId());
List<IdmRoleFormAttributeDto> list = roleFormAttributeService.find(filter, null).getContent();
Assert.assertEquals(2, list.size());
IdmFormDefinitionDto formAttributeSubdefinition = roleService.getFormAttributeSubdefinition(role);
Assert.assertEquals(2, formAttributeSubdefinition.getFormAttributes().size());
// Delete shortText attribute from the sub-definition
list.stream().filter(roleFormAttributeDto -> {
IdmFormAttributeDto formAttributeDto = DtoUtils.getEmbedded(roleFormAttributeDto, IdmRoleFormAttribute_.formAttribute.getName(), IdmFormAttributeDto.class);
return formAttributeDto.getCode().equals(NUMBER_OF_FINGERS);
}).forEach(roleFormAttributeDto -> roleFormAttributeService.delete(roleFormAttributeDto));
formAttributeSubdefinition = roleService.getFormAttributeSubdefinition(role);
Assert.assertEquals(1, formAttributeSubdefinition.getFormAttributes().size());
Assert.assertEquals(IP, formAttributeSubdefinition.getFormAttributes().get(0).getCode());
}
use of org.junit.Assert.assertEquals in project CzechIdMng by bcvsolutions.
the class LoginControllerRestTest method testSwitchUser.
@Test
public void testSwitchUser() throws Exception {
IdmIdentityDto manager = getHelper().createIdentity();
getHelper().createIdentityRole(manager, roleConfiguration.getAdminRole());
//
// login as manager
Map<String, String> login = new HashMap<>();
login.put("username", manager.getUsername());
login.put("password", manager.getPassword().asString());
String response = getMockMvc().perform(post(BaseController.BASE_PATH + LoginController.AUTH_PATH).content(serialize(login)).contentType(TestHelper.HAL_CONTENT_TYPE)).andExpect(status().isOk()).andExpect(content().contentType(TestHelper.HAL_CONTENT_TYPE)).andReturn().getResponse().getContentAsString();
UUID tokenId = getTokenId(response);
String token = getToken(response);
//
Assert.assertNotNull(tokenId);
IdmTokenDto tokenDto = tokenManager.getToken(tokenId);
Assert.assertFalse(tokenDto.isDisabled());
List<DefaultGrantedAuthorityDto> dtoAuthorities = jwtTokenMapper.getDtoAuthorities(tokenDto);
//
// check token authorities - APP_ADMIN
Assert.assertTrue(dtoAuthorities.stream().anyMatch(a -> a.getAuthority().equals(IdmGroupPermission.APP_ADMIN)));
//
// create different identity - identity create
IdmIdentityDto identity = getHelper().createIdentity((GuardedString) null);
IdmRoleDto role = getHelper().createRole();
getHelper().createIdentityRole(identity, role);
getHelper().createBasePolicy(role.getId(), CoreGroupPermission.IDENTITY, IdmIdentity.class, IdmBasePermission.ADMIN);
response = getMockMvc().perform(put(BaseController.BASE_PATH + "/authentication/switch-user?username=" + identity.getUsername()).param(IdmAuthenticationFilter.AUTHENTICATION_TOKEN_NAME, token)).andExpect(status().isOk()).andExpect(content().contentType(TestHelper.HAL_CONTENT_TYPE)).andReturn().getResponse().getContentAsString();
//
// preserve token id
UUID switchTokenId = getTokenId(response);
token = getToken(response);
Assert.assertEquals(tokenId, switchTokenId);
IdmTokenDto switchTokenDto = tokenManager.getToken(switchTokenId);
Assert.assertFalse(switchTokenDto.isDisabled());
dtoAuthorities = jwtTokenMapper.getDtoAuthorities(switchTokenDto);
//
// check authorities - no APP_ADMIN
Assert.assertTrue(dtoAuthorities.stream().allMatch(a -> !a.getAuthority().equals(IdmGroupPermission.APP_ADMIN)));
//
// check token => same owner, same id, different username in properties
Assert.assertEquals(tokenDto.getOwnerId(), switchTokenDto.getOwnerId());
Assert.assertEquals(identity.getUsername(), switchTokenDto.getProperties().getString(JwtAuthenticationMapper.PROPERTY_CURRENT_USERNAME));
Assert.assertEquals(manager.getUsername(), switchTokenDto.getProperties().getString(JwtAuthenticationMapper.PROPERTY_ORIGINAL_USERNAME));
//
// test create identity with switched token + check audit fields
IdmIdentityDto createIdentity = new IdmIdentityDto(getHelper().createName());
getMockMvc().perform(post(BaseController.BASE_PATH + "/identities").param(IdmAuthenticationFilter.AUTHENTICATION_TOKEN_NAME, token).content(getMapper().writeValueAsString(createIdentity)).contentType(TestHelper.HAL_CONTENT_TYPE)).andExpect(status().isCreated()).andExpect(content().contentType(TestHelper.HAL_CONTENT_TYPE)).andReturn().getResponse().getContentAsString();
IdmIdentityDto createdIdentity = identityService.getByUsername(createIdentity.getUsername());
Assert.assertEquals(manager.getUsername(), createdIdentity.getOriginalCreator());
Assert.assertEquals(manager.getId(), createdIdentity.getOriginalCreatorId());
Assert.assertEquals(identity.getUsername(), createdIdentity.getCreator());
Assert.assertEquals(identity.getId(), createdIdentity.getCreatorId());
//
// rename identity - use id in logout phase
manager.setUsername(getHelper().createName());
manager = identityService.save(manager);
//
// switch logout => test token, authorities
response = getMockMvc().perform(delete(BaseController.BASE_PATH + "/authentication/switch-user").param(IdmAuthenticationFilter.AUTHENTICATION_TOKEN_NAME, token)).andExpect(status().isOk()).andExpect(content().contentType(TestHelper.HAL_CONTENT_TYPE)).andReturn().getResponse().getContentAsString();
tokenId = getTokenId(response);
token = getToken(response);
//
Assert.assertNotNull(tokenId);
tokenDto = tokenManager.getToken(tokenId);
Assert.assertFalse(tokenDto.isDisabled());
dtoAuthorities = jwtTokenMapper.getDtoAuthorities(tokenDto);
//
// check token authorities - APP_ADMIN
Assert.assertTrue(dtoAuthorities.stream().anyMatch(a -> a.getAuthority().equals(IdmGroupPermission.APP_ADMIN)));
Assert.assertEquals(tokenDto.getOwnerId(), switchTokenDto.getOwnerId());
Assert.assertEquals(manager.getUsername(), tokenDto.getProperties().getString(JwtAuthenticationMapper.PROPERTY_CURRENT_USERNAME));
Assert.assertEquals(manager.getUsername(), tokenDto.getProperties().getString(JwtAuthenticationMapper.PROPERTY_ORIGINAL_USERNAME));
}
Aggregations