use of com.synopsys.integration.alert.database.user.UserRoleRelation 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.database.user.UserRoleRelation 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.UserRoleRelation 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.UserRoleRelation in project hub-alert by blackducksoftware.
the class DefaultRoleAccessor method updateUserRoles.
@Override
@Transactional(propagation = Propagation.REQUIRED)
public void updateUserRoles(Long userId, Collection<UserRoleModel> roles) {
if (null != userId) {
userRoleRepository.bulkDeleteAllByUserId(userId);
if (null != roles && !roles.isEmpty()) {
Collection<String> roleNames = roles.stream().map(UserRoleModel::getName).collect(Collectors.toSet());
List<RoleEntity> roleEntities = roleRepository.findRoleEntitiesByRoleNames(roleNames);
List<UserRoleRelation> roleRelations = new LinkedList<>();
for (RoleEntity role : roleEntities) {
roleRelations.add(new UserRoleRelation(userId, role.getId()));
}
userRoleRepository.saveAll(roleRelations);
}
}
}
use of com.synopsys.integration.alert.database.user.UserRoleRelation 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);
}
Aggregations