use of com.synopsys.integration.alert.database.user.UserEntity 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.database.user.UserEntity 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.database.user.UserEntity in project hub-alert by blackducksoftware.
the class DefaultUserAccessorTest method getUserByUserIdTest.
@Test
public void getUserByUserIdTest() {
final Long userId = 1L;
final Long emptyUserId = 5L;
final Long authenticationTypeId = 1L;
final String roleName = "userName";
UserEntity userEntity = new UserEntity(username, password, emailAddress, authenticationTypeId);
userEntity.setId(1L);
UserRoleRelation userRoleRelation = new UserRoleRelation(1L, 2L);
UserRoleModel userRoleModel = createUserRoleModel(1L, roleName, true);
Mockito.when(userRepository.findById(userId)).thenReturn(Optional.of(userEntity));
Mockito.when(userRepository.findById(emptyUserId)).thenReturn(Optional.empty());
createModelMocks(userRoleRelation, userRoleModel, AuthenticationType.DATABASE);
DefaultUserAccessor defaultUserAccessor = new DefaultUserAccessor(userRepository, userRoleRepository, defaultPasswordEncoder, roleAccessor, authenticationTypeAccessor);
Optional<UserModel> userModelOptional = defaultUserAccessor.getUser(userId);
Optional<UserModel> userModelOptionalEmpty = defaultUserAccessor.getUser(emptyUserId);
assertTrue(userModelOptional.isPresent());
assertFalse(userModelOptionalEmpty.isPresent());
UserModel userModel = userModelOptional.get();
testUserModel(userEntity.getId(), username, emailAddress, roleName, userModel);
}
use of com.synopsys.integration.alert.database.user.UserEntity in project hub-alert by blackducksoftware.
the class DefaultUserAccessorTest method updateUserNonDatabaseAuthInvalidTest.
@Test
public void updateUserNonDatabaseAuthInvalidTest() throws Exception {
final String roleName = "roleName";
AuthenticationType authenticationType = AuthenticationType.LDAP;
UserEntity userEntity = new UserEntity(username, password, emailAddress, 2L);
userEntity.setId(1L);
UserEntity existingUserEntity = new UserEntity("usernam-teste", "existing-password", "old-email.noreply@blackducksoftware.com", 2L);
existingUserEntity.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(existingUserEntity));
Mockito.when(authenticationTypeAccessor.getAuthenticationType(Mockito.any())).thenReturn(Optional.of(authenticationType));
Mockito.when(userRepository.save(Mockito.any())).thenReturn(existingUserEntity);
createModelMocks(userRoleRelation, userRoleModel, authenticationType);
DefaultUserAccessor defaultUserAccessor = new DefaultUserAccessor(userRepository, userRoleRepository, defaultPasswordEncoder, roleAccessor, authenticationTypeAccessor);
try {
defaultUserAccessor.updateUser(userModel, false);
fail("External user with ? did not throw expected " + AlertForbiddenOperationException.class.getSimpleName());
} catch (AlertForbiddenOperationException e) {
assertNotNull(e);
} catch (AlertConfigurationException wrongException) {
fail("Wrong exception thrown");
}
}
use of com.synopsys.integration.alert.database.user.UserEntity in project hub-alert by blackducksoftware.
the class DefaultUserAccessorTest method changeUserPasswordTest.
@Test
public void changeUserPasswordTest() {
final String badUsername = "badUsername";
final String newPassword = "newPassword";
UserEntity userEntity = new UserEntity(username, password, emailAddress, 2L);
userEntity.setId(1L);
UserEntity newUserEntity = new UserEntity(username, newPassword, emailAddress, 2L);
userEntity.setId(1L);
Mockito.when(userRepository.findByUserName(Mockito.eq(username))).thenReturn(Optional.of(userEntity));
Mockito.when(userRepository.findByUserName(Mockito.eq(badUsername))).thenReturn(Optional.empty());
Mockito.when(defaultPasswordEncoder.encode(Mockito.any())).thenReturn(newPassword);
Mockito.when(userRepository.save(Mockito.any())).thenReturn(newUserEntity);
DefaultUserAccessor defaultUserAccessor = new DefaultUserAccessor(userRepository, userRoleRepository, defaultPasswordEncoder, roleAccessor, authenticationTypeAccessor);
assertTrue(defaultUserAccessor.changeUserPassword(username, newPassword));
assertFalse(defaultUserAccessor.changeUserPassword(badUsername, newPassword));
}
Aggregations