Search in sources :

Example 6 with UserEntity

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);
}
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 7 with UserEntity

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);
}
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 8 with UserEntity

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);
}
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)

Example 9 with UserEntity

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");
    }
}
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) AlertForbiddenOperationException(com.synopsys.integration.alert.common.exception.AlertForbiddenOperationException) AuthenticationType(com.synopsys.integration.alert.common.enumeration.AuthenticationType) AlertConfigurationException(com.synopsys.integration.alert.api.common.model.exception.AlertConfigurationException) Test(org.junit.jupiter.api.Test)

Example 10 with UserEntity

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));
}
Also used : UserEntity(com.synopsys.integration.alert.database.user.UserEntity) 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