Search in sources :

Example 11 with UserEntity

use of com.synopsys.integration.alert.database.user.UserEntity in project hub-alert by blackducksoftware.

the class DefaultUserAccessorTest method addUserExistsTest.

@Test
public void addUserExistsTest() throws Exception {
    UserEntity userEntity = new UserEntity(username, password, emailAddress, 2L);
    userEntity.setId(1L);
    Mockito.when(userRepository.findByUserName(Mockito.any())).thenReturn(Optional.of(userEntity));
    DefaultUserAccessor defaultUserAccessor = new DefaultUserAccessor(userRepository, userRoleRepository, defaultPasswordEncoder, roleAccessor, authenticationTypeAccessor);
    try {
        defaultUserAccessor.addUser(username, password, emailAddress);
        fail("User with the same name that already exists in the userRepository did not throw expected " + AlertConfigurationException.class.getSimpleName());
    } catch (AlertConfigurationException e) {
        assertNotNull(e);
    }
}
Also used : UserEntity(com.synopsys.integration.alert.database.user.UserEntity) AlertConfigurationException(com.synopsys.integration.alert.api.common.model.exception.AlertConfigurationException) Test(org.junit.jupiter.api.Test)

Example 12 with UserEntity

use of com.synopsys.integration.alert.database.user.UserEntity in project hub-alert by blackducksoftware.

the class DefaultUserAccessorTest method deleteUserByNameTest.

@Test
public void deleteUserByNameTest() throws Exception {
    // userEntity Id's between 0 and 2 are reserved
    UserEntity userEntity = new UserEntity(username, password, emailAddress, 2L);
    userEntity.setId(5L);
    Mockito.when(userRepository.findByUserName(Mockito.eq(username))).thenReturn(Optional.of(userEntity));
    DefaultUserAccessor defaultUserAccessor = new DefaultUserAccessor(userRepository, userRoleRepository, defaultPasswordEncoder, roleAccessor, authenticationTypeAccessor);
    defaultUserAccessor.deleteUser(username);
    Mockito.verify(roleAccessor).updateUserRoles(Mockito.any(), Mockito.any());
    Mockito.verify(userRepository).deleteById(Mockito.any());
}
Also used : UserEntity(com.synopsys.integration.alert.database.user.UserEntity) Test(org.junit.jupiter.api.Test)

Example 13 with UserEntity

use of com.synopsys.integration.alert.database.user.UserEntity in project hub-alert by blackducksoftware.

the class DefaultUserAccessorTest method assignRolesTest.

@Test
public void assignRolesTest() {
    final String badUsername = "badUsername";
    final Long roleId = 5L;
    UserEntity userEntity = new UserEntity(username, password, 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());
    DefaultUserAccessor defaultUserAccessor = new DefaultUserAccessor(userRepository, userRoleRepository, defaultPasswordEncoder, roleAccessor, authenticationTypeAccessor);
    boolean assignedRoles = defaultUserAccessor.assignRoles(username, Set.of(roleId));
    boolean assignedRolesFalse = defaultUserAccessor.assignRoles(badUsername, Set.of(roleId));
    Mockito.verify(roleAccessor).updateUserRoles(Mockito.eq(userEntity.getId()), Mockito.any());
    Mockito.verify(roleAccessor).getRoles(Mockito.any());
    assertTrue(assignedRoles);
    assertFalse(assignedRolesFalse);
}
Also used : UserEntity(com.synopsys.integration.alert.database.user.UserEntity) Test(org.junit.jupiter.api.Test)

Example 14 with UserEntity

use of com.synopsys.integration.alert.database.user.UserEntity in project hub-alert by blackducksoftware.

the class DefaultUserAccessorTest method deleteUserReservedIdTest.

@Test
public void deleteUserReservedIdTest() throws Exception {
    UserEntity userEntity = new UserEntity(username, password, emailAddress, 2L);
    userEntity.setId(1L);
    Mockito.when(userRepository.findByUserName(Mockito.eq(username))).thenReturn(Optional.of(userEntity));
    Mockito.when(userRepository.findById(Mockito.any())).thenReturn(Optional.of(userEntity));
    DefaultUserAccessor defaultUserAccessor = new DefaultUserAccessor(userRepository, userRoleRepository, defaultPasswordEncoder, roleAccessor, authenticationTypeAccessor);
    try {
        defaultUserAccessor.deleteUser(username);
        fail("A forbidden userEntity id did not throw the expected AlertForbiddenOperationException");
    } catch (AlertForbiddenOperationException e) {
        assertNotNull(e);
    }
}
Also used : UserEntity(com.synopsys.integration.alert.database.user.UserEntity) AlertForbiddenOperationException(com.synopsys.integration.alert.common.exception.AlertForbiddenOperationException) Test(org.junit.jupiter.api.Test)

Example 15 with UserEntity

use of com.synopsys.integration.alert.database.user.UserEntity 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);
}
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

UserEntity (com.synopsys.integration.alert.database.user.UserEntity)20 Test (org.junit.jupiter.api.Test)14 UserModel (com.synopsys.integration.alert.common.persistence.model.UserModel)8 UserRoleModel (com.synopsys.integration.alert.common.persistence.model.UserRoleModel)7 UserRoleRelation (com.synopsys.integration.alert.database.user.UserRoleRelation)7 AlertConfigurationException (com.synopsys.integration.alert.api.common.model.exception.AlertConfigurationException)4 AuthenticationType (com.synopsys.integration.alert.common.enumeration.AuthenticationType)4 Transactional (org.springframework.transaction.annotation.Transactional)4 AlertForbiddenOperationException (com.synopsys.integration.alert.common.exception.AlertForbiddenOperationException)3 AlertRuntimeException (com.synopsys.integration.alert.api.common.model.exception.AlertRuntimeException)2 AuthenticationTypeDetails (com.synopsys.integration.alert.common.persistence.model.AuthenticationTypeDetails)2