use of com.synopsys.integration.alert.common.persistence.model.UserModel in project hub-alert by blackducksoftware.
the class UserActionsTest method testExternalUserNoEmailValidation.
@Test
public void testExternalUserNoEmailValidation() throws Exception {
AuthenticationType authenticationTypeLDAP = AuthenticationType.LDAP;
UserModel userModel = UserModel.existingUser(id, name, password, null, authenticationTypeLDAP, roles, true);
Mockito.when(authorizationManager.hasExecutePermission(Mockito.any(ConfigContextEnum.class), Mockito.any(DescriptorKey.class))).thenReturn(true);
Mockito.when(userAccessor.getUser(Mockito.anyLong())).thenReturn(Optional.of(userModel));
Set<String> roleNames = roles.stream().map(UserRoleModel::getName).collect(Collectors.toSet());
roleNames.add(DefaultUserRole.ALERT_ADMIN.name());
UserConfig userConfig = new UserConfig(id.toString(), name, "newPassword", null, roleNames, false, false, false, true, false, authenticationTypeLDAP.name(), true);
UserActions userActions = new UserActions(userManagementDescriptorKey, userAccessor, roleAccessor, authorizationManager, authenticationTypeAccessor, userSystemValidator);
ValidationActionResponse validationActionResponse = userActions.validate(userConfig);
assertFalse(validationActionResponse.isError());
assertTrue(validationActionResponse.hasContent());
ValidationResponseModel validationResponseModel = validationActionResponse.getContent().get();
assertFalse(validationResponseModel.hasErrors());
assertEquals("The user is valid", validationActionResponse.getMessage().get());
}
use of com.synopsys.integration.alert.common.persistence.model.UserModel in project hub-alert by blackducksoftware.
the class DefaultUserAccessorTest method updateUserTest.
@Test
public void updateUserTest() throws Exception {
final String roleName = "userName";
AuthenticationType authenticationType = AuthenticationType.DATABASE;
UserEntity userEntity = new UserEntity(username, password, emailAddress, 2L);
userEntity.setId(1L);
UserRoleModel roles = createUserRoleModel(1L, roleName, true);
UserModel userModel = UserModel.existingUser(1L, username, password, emailAddress, authenticationType, Set.of(roles), true);
UserRoleRelation userRoleRelation = new UserRoleRelation(1L, 2L);
UserRoleModel userRoleModel = createUserRoleModel(1L, roleName, true);
Mockito.when(userRepository.findById(Mockito.any())).thenReturn(Optional.of(userEntity));
Mockito.when(authenticationTypeAccessor.getAuthenticationType(Mockito.any())).thenReturn(Optional.of(authenticationType));
Mockito.when(userRepository.save(Mockito.any())).thenReturn(userEntity);
createModelMocks(userRoleRelation, userRoleModel, authenticationType);
DefaultUserAccessor defaultUserAccessor = new DefaultUserAccessor(userRepository, userRoleRepository, defaultPasswordEncoder, roleAccessor, authenticationTypeAccessor);
UserModel newUserModel = defaultUserAccessor.updateUser(userModel, false);
Mockito.verify(roleAccessor).updateUserRoles(Mockito.eq(userEntity.getId()), Mockito.any());
testUserModel(userEntity.getId(), username, emailAddress, roleName, newUserModel);
}
use of com.synopsys.integration.alert.common.persistence.model.UserModel in project hub-alert by blackducksoftware.
the class DefaultUserAccessorTest method updateUserNonDatabaseAuthTest.
@Test
public void updateUserNonDatabaseAuthTest() throws Exception {
final String roleName = "roleName";
AuthenticationType authenticationType = AuthenticationType.LDAP;
UserEntity userEntity = new UserEntity(username, password, emailAddress, 2L);
userEntity.setId(1L);
UserRoleModel roles = createUserRoleModel(1L, roleName, true);
UserModel userModel = UserModel.existingUser(1L, username, "", emailAddress, authenticationType, Set.of(roles), true);
UserRoleRelation userRoleRelation = new UserRoleRelation(1L, 2L);
UserRoleModel userRoleModel = createUserRoleModel(1L, roleName, true);
Mockito.when(userRepository.findById(Mockito.any())).thenReturn(Optional.of(userEntity));
Mockito.when(authenticationTypeAccessor.getAuthenticationType(Mockito.any())).thenReturn(Optional.of(authenticationType));
Mockito.when(userRepository.save(Mockito.any())).thenReturn(userEntity);
createModelMocks(userRoleRelation, userRoleModel, authenticationType);
DefaultUserAccessor defaultUserAccessor = new DefaultUserAccessor(userRepository, userRoleRepository, defaultPasswordEncoder, roleAccessor, authenticationTypeAccessor);
UserModel updatedUserModel = defaultUserAccessor.updateUser(userModel, false);
Mockito.verify(roleAccessor).updateUserRoles(Mockito.eq(userEntity.getId()), Mockito.any());
testUserModel(userEntity.getId(), username, emailAddress, roleName, updatedUserModel);
}
use of com.synopsys.integration.alert.common.persistence.model.UserModel in project hub-alert by blackducksoftware.
the class DefaultUserAccessorTest method addUserTest.
@Test
public void addUserTest() throws Exception {
final String roleName = "userName";
UserEntity userEntity = new UserEntity(username, password, emailAddress, 2L);
userEntity.setId(1L);
AuthenticationTypeDetails authenticationTypeDetails = new AuthenticationTypeDetails(2L, "authentication-name");
UserRoleRelation userRoleRelation = new UserRoleRelation(1L, 2L);
UserRoleModel userRoleModel = createUserRoleModel(1L, roleName, true);
Mockito.when(userRepository.findByUserName(Mockito.any())).thenReturn(Optional.empty());
Mockito.when(authenticationTypeAccessor.getAuthenticationTypeDetails(Mockito.any())).thenReturn(Optional.of(authenticationTypeDetails));
Mockito.when(userRepository.save(Mockito.any())).thenReturn(userEntity);
createModelMocks(userRoleRelation, userRoleModel, AuthenticationType.DATABASE);
DefaultUserAccessor defaultUserAccessor = new DefaultUserAccessor(userRepository, userRoleRepository, defaultPasswordEncoder, roleAccessor, authenticationTypeAccessor);
UserModel userModel = defaultUserAccessor.addUser(username, password, emailAddress);
testUserModel(userEntity.getId(), username, emailAddress, roleName, userModel);
}
use of com.synopsys.integration.alert.common.persistence.model.UserModel in project hub-alert by blackducksoftware.
the class UserActions method updateWithoutChecks.
@Override
protected ActionResponse<UserConfig> updateWithoutChecks(Long id, UserConfig resource) {
Optional<UserModel> userModel = userAccessor.getUser(id);
if (userModel.isPresent()) {
UserModel existingUser = userModel.get();
boolean passwordMissing = StringUtils.isBlank(resource.getPassword());
String userName = resource.getUsername();
String password = passwordMissing ? existingUser.getPassword() : resource.getPassword();
String emailAddress = resource.getEmailAddress();
UserModel newUserModel = UserModel.existingUser(existingUser.getId(), userName, password, emailAddress, existingUser.getAuthenticationType(), existingUser.getRoles(), existingUser.isEnabled());
try {
logger.debug(actionMessageCreator.updateStartMessage("user", userName));
userAccessor.updateUser(newUserModel, passwordMissing);
Set<String> configuredRoleNames = resource.getRoleNames();
if (null != configuredRoleNames && !configuredRoleNames.isEmpty()) {
Collection<UserRoleModel> roleNames = roleAccessor.getRoles().stream().filter(role -> configuredRoleNames.contains(role.getName())).collect(Collectors.toList());
authorizationManager.updateUserRoles(existingUser.getId(), roleNames);
}
userSystemValidator.validateDefaultAdminUser(id);
UserConfig user = userAccessor.getUser(id).map(this::convertDatabaseModelToRestModel).orElse(resource);
logger.debug(actionMessageCreator.updateSuccessMessage("User", userName));
return new ActionResponse<>(HttpStatus.NO_CONTENT);
} catch (AlertException ex) {
logger.error(actionMessageCreator.updateErrorMessage("User", userName));
return new ActionResponse<>(HttpStatus.INTERNAL_SERVER_ERROR, ex.getMessage());
}
}
logger.warn(actionMessageCreator.updateNotFoundMessage("User", id));
return new ActionResponse<>(HttpStatus.NOT_FOUND);
}
Aggregations