Search in sources :

Example 1 with UserRoleRelation

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);
}
Also used : UserModel(com.synopsys.integration.alert.common.persistence.model.UserModel) UserEntity(com.synopsys.integration.alert.database.user.UserEntity) UserRoleModel(com.synopsys.integration.alert.common.persistence.model.UserRoleModel) UserRoleRelation(com.synopsys.integration.alert.database.user.UserRoleRelation) AuthenticationType(com.synopsys.integration.alert.common.enumeration.AuthenticationType) Test(org.junit.jupiter.api.Test)

Example 2 with UserRoleRelation

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);
}
Also used : UserModel(com.synopsys.integration.alert.common.persistence.model.UserModel) UserEntity(com.synopsys.integration.alert.database.user.UserEntity) UserRoleModel(com.synopsys.integration.alert.common.persistence.model.UserRoleModel) UserRoleRelation(com.synopsys.integration.alert.database.user.UserRoleRelation) AuthenticationType(com.synopsys.integration.alert.common.enumeration.AuthenticationType) Test(org.junit.jupiter.api.Test)

Example 3 with UserRoleRelation

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);
}
Also used : UserModel(com.synopsys.integration.alert.common.persistence.model.UserModel) AuthenticationTypeDetails(com.synopsys.integration.alert.common.persistence.model.AuthenticationTypeDetails) UserEntity(com.synopsys.integration.alert.database.user.UserEntity) UserRoleRelation(com.synopsys.integration.alert.database.user.UserRoleRelation) UserRoleModel(com.synopsys.integration.alert.common.persistence.model.UserRoleModel) Test(org.junit.jupiter.api.Test)

Example 4 with UserRoleRelation

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);
        }
    }
}
Also used : RoleEntity(com.synopsys.integration.alert.database.user.RoleEntity) UserRoleRelation(com.synopsys.integration.alert.database.user.UserRoleRelation) LinkedList(java.util.LinkedList) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with UserRoleRelation

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);
}
Also used : UserModel(com.synopsys.integration.alert.common.persistence.model.UserModel) UserEntity(com.synopsys.integration.alert.database.user.UserEntity) UserRoleRelation(com.synopsys.integration.alert.database.user.UserRoleRelation) UserRoleModel(com.synopsys.integration.alert.common.persistence.model.UserRoleModel) Test(org.junit.jupiter.api.Test)

Aggregations

UserRoleRelation (com.synopsys.integration.alert.database.user.UserRoleRelation)10 UserRoleModel (com.synopsys.integration.alert.common.persistence.model.UserRoleModel)8 Test (org.junit.jupiter.api.Test)8 UserModel (com.synopsys.integration.alert.common.persistence.model.UserModel)7 UserEntity (com.synopsys.integration.alert.database.user.UserEntity)7 AuthenticationType (com.synopsys.integration.alert.common.enumeration.AuthenticationType)4 AlertConfigurationException (com.synopsys.integration.alert.api.common.model.exception.AlertConfigurationException)1 AlertForbiddenOperationException (com.synopsys.integration.alert.common.exception.AlertForbiddenOperationException)1 AuthenticationTypeDetails (com.synopsys.integration.alert.common.persistence.model.AuthenticationTypeDetails)1 RoleEntity (com.synopsys.integration.alert.database.user.RoleEntity)1 LinkedList (java.util.LinkedList)1 Transactional (org.springframework.transaction.annotation.Transactional)1