use of com.synopsys.integration.alert.database.user.UserRoleRelation 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.UserRoleRelation in project hub-alert by blackducksoftware.
the class DefaultUserAccessorTest method getUserByUsernameTest.
@Test
public void getUserByUsernameTest() {
final String emptyUsername = "";
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.findByUserName(username)).thenReturn(Optional.of(userEntity));
Mockito.when(userRepository.findByUserName(emptyUsername)).thenReturn(Optional.empty());
createModelMocks(userRoleRelation, userRoleModel, AuthenticationType.DATABASE);
DefaultUserAccessor defaultUserAccessor = new DefaultUserAccessor(userRepository, userRoleRepository, defaultPasswordEncoder, roleAccessor, authenticationTypeAccessor);
Optional<UserModel> userModelOptional = defaultUserAccessor.getUser(username);
Optional<UserModel> userModelOptionalEmpty = defaultUserAccessor.getUser(emptyUsername);
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.UserRoleRelation in project hub-alert by blackducksoftware.
the class DefaultUserAccessorTest method getUsersTest.
@Test
public void getUsersTest() {
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.findAll()).thenReturn(List.of(userEntity));
createModelMocks(userRoleRelation, userRoleModel, AuthenticationType.DATABASE);
DefaultUserAccessor defaultUserAccessor = new DefaultUserAccessor(userRepository, userRoleRepository, defaultPasswordEncoder, roleAccessor, authenticationTypeAccessor);
List<UserModel> userModelList = defaultUserAccessor.getUsers();
assertEquals(1, userModelList.size());
UserModel userModel = userModelList.get(0);
testUserModel(userEntity.getId(), username, emailAddress, roleName, userModel);
}
use of com.synopsys.integration.alert.database.user.UserRoleRelation in project hub-alert by blackducksoftware.
the class UserRoleRelationTest method testRelation.
@Test
public void testRelation() {
final Long expectedUserId = 99L;
final Long expectedRoleId = 100L;
final UserRoleRelation relation = new UserRoleRelation(expectedUserId, expectedRoleId);
assertEquals(expectedUserId, relation.getUserId());
assertEquals(expectedRoleId, relation.getRoleId());
}
use of com.synopsys.integration.alert.database.user.UserRoleRelation in project hub-alert by blackducksoftware.
the class DefaultUserAccessor method createModel.
private UserModel createModel(UserEntity user) {
List<UserRoleRelation> roleRelations = userRoleRepository.findAllByUserId(user.getId());
List<Long> roleIdsForUser = roleRelations.stream().map(UserRoleRelation::getRoleId).collect(Collectors.toList());
Set<UserRoleModel> roles = roleAccessor.getRoles(roleIdsForUser);
AuthenticationType authenticationType = authenticationTypeAccessor.getAuthenticationType(user.getAuthenticationType()).orElse(null);
return UserModel.existingUser(user.getId(), user.getUserName(), user.getPassword(), user.getEmailAddress(), authenticationType, roles, user.isEnabled());
}
Aggregations