Search in sources :

Example 21 with UserRoleModel

use of com.synopsys.integration.alert.common.persistence.model.UserRoleModel 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 22 with UserRoleModel

use of com.synopsys.integration.alert.common.persistence.model.UserRoleModel 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)

Example 23 with UserRoleModel

use of com.synopsys.integration.alert.common.persistence.model.UserRoleModel 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);
}
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 24 with UserRoleModel

use of com.synopsys.integration.alert.common.persistence.model.UserRoleModel in project hub-alert by blackducksoftware.

the class UserAccessorTestIT method testExternalUserUpdateRoles.

@Test
public void testExternalUserUpdateRoles() throws AlertForbiddenOperationException, AlertConfigurationException {
    String userName = "testUser";
    String password = "testPassword";
    String email = "testEmail";
    UserModel userModel = UserModel.newUser(userName, password, email, AuthenticationType.LDAP, Collections.emptySet(), true);
    userModel = userAccessor.addUser(userModel, false);
    UserRoleModel userRole = UserRoleModel.of(DefaultUserRole.ALERT_ADMIN.name());
    Set<UserRoleModel> roles = Set.of(userRole);
    UserModel existingUser = UserModel.existingUser(userModel.getId(), userName, null, email, AuthenticationType.LDAP, roles, true);
    UserModel updatedUser = userAccessor.updateUser(existingUser, true);
    assertEquals(roles.stream().map(UserRoleModel::getName).collect(Collectors.toSet()), updatedUser.getRoles().stream().map(UserRoleModel::getName).collect(Collectors.toSet()));
    userAccessor.deleteUser(userName);
}
Also used : UserModel(com.synopsys.integration.alert.common.persistence.model.UserModel) UserRoleModel(com.synopsys.integration.alert.common.persistence.model.UserRoleModel) AlertIntegrationTest(com.synopsys.integration.alert.util.AlertIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 25 with UserRoleModel

use of com.synopsys.integration.alert.common.persistence.model.UserRoleModel in project hub-alert by blackducksoftware.

the class RoleActionsTest method deleteTest.

@Test
public void deleteTest() throws Exception {
    PermissionModel permissionModel = createPermissionModel();
    UserRoleModel userRoleModel = new UserRoleModel(1L, roleName, false, PermissionModelUtil.convertToPermissionMatrixModel(Set.of(permissionModel)));
    Mockito.when(roleAccessor.getRoles(Mockito.anyCollection())).thenReturn(Set.of(userRoleModel));
    RoleActions roleActions = new RoleActions(userManagementDescriptorKey, roleAccessor, authorizationManager, descriptorMap);
    ActionResponse<RolePermissionModel> rolePermissionModelActionResponse = roleActions.delete(1L);
    Mockito.verify(authorizationManager).deleteRole(Mockito.anyLong());
    assertTrue(rolePermissionModelActionResponse.isSuccessful());
    assertFalse(rolePermissionModelActionResponse.hasContent());
    assertEquals(HttpStatus.NO_CONTENT, rolePermissionModelActionResponse.getHttpStatus());
}
Also used : UserRoleModel(com.synopsys.integration.alert.common.persistence.model.UserRoleModel) Test(org.junit.jupiter.api.Test)

Aggregations

UserRoleModel (com.synopsys.integration.alert.common.persistence.model.UserRoleModel)36 Test (org.junit.jupiter.api.Test)24 UserModel (com.synopsys.integration.alert.common.persistence.model.UserModel)17 UserRoleRelation (com.synopsys.integration.alert.database.user.UserRoleRelation)8 UserEntity (com.synopsys.integration.alert.database.user.UserEntity)7 RoleEntity (com.synopsys.integration.alert.database.user.RoleEntity)6 AlertException (com.synopsys.integration.alert.api.common.model.exception.AlertException)4 ActionResponse (com.synopsys.integration.alert.common.action.ActionResponse)4 ValidationActionResponse (com.synopsys.integration.alert.common.action.ValidationActionResponse)4 AuthenticationType (com.synopsys.integration.alert.common.enumeration.AuthenticationType)4 PermissionMatrixModel (com.synopsys.integration.alert.common.persistence.model.PermissionMatrixModel)4 LinkedHashSet (java.util.LinkedHashSet)4 Transactional (org.springframework.transaction.annotation.Transactional)4 AlertConfigurationException (com.synopsys.integration.alert.api.common.model.exception.AlertConfigurationException)3 RoleAccessor (com.synopsys.integration.alert.common.descriptor.accessor.RoleAccessor)3 DefaultUserRole (com.synopsys.integration.alert.common.enumeration.DefaultUserRole)3 AlertForbiddenOperationException (com.synopsys.integration.alert.common.exception.AlertForbiddenOperationException)3 UserAccessor (com.synopsys.integration.alert.common.persistence.accessor.UserAccessor)3 AuthenticationTypeDetails (com.synopsys.integration.alert.common.persistence.model.AuthenticationTypeDetails)3 Collection (java.util.Collection)3