Search in sources :

Example 1 with RoleEntity

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

the class DefaultRoleAccessorTest method updatePermissionsForRole.

@Test
public void updatePermissionsForRole() throws Exception {
    final String roleName = "roleName";
    final String contextString = "context-test";
    final String descriptorName = "descriptorName";
    RoleEntity roleEntity = new RoleEntity(roleName, true);
    roleEntity.setId(1L);
    ConfigContextEntity configContextEntity = new ConfigContextEntity(contextString);
    configContextEntity.setId(1L);
    RegisteredDescriptorEntity registeredDescriptorEntity = new RegisteredDescriptorEntity(descriptorName, 1L);
    registeredDescriptorEntity.setId(1L);
    PermissionKey permissionKey = new PermissionKey(contextString, descriptorName);
    PermissionMatrixModel permissionMatrix = new PermissionMatrixModel(Map.of(permissionKey, AccessOperation.READ.getBit() + AccessOperation.WRITE.getBit()));
    PermissionMatrixRelation permissionMatrixRelation = new PermissionMatrixRelation(roleEntity.getId(), configContextEntity.getId(), registeredDescriptorEntity.getId(), AccessOperation.READ.getBit() + AccessOperation.WRITE.getBit());
    Mockito.when(roleRepository.findByRoleName(Mockito.any())).thenReturn(Optional.of(roleEntity));
    mockUpdateRoleOperations(permissionMatrixRelation, configContextEntity, registeredDescriptorEntity);
    mockCreateModelFromPermission(configContextEntity, registeredDescriptorEntity);
    DefaultRoleAccessor authorizationUtility = new DefaultRoleAccessor(roleRepository, userRoleRepository, permissionMatrixRepository, registeredDescriptorRepository, configContextRepository);
    PermissionMatrixModel permissionMatrixModel = authorizationUtility.updatePermissionsForRole(roleName, permissionMatrix);
    Mockito.verify(permissionMatrixRepository).saveAll(Mockito.any());
    assertFalse(permissionMatrixModel.isEmpty());
    assertEquals(permissionMatrix, permissionMatrixModel);
}
Also used : RoleEntity(com.synopsys.integration.alert.database.user.RoleEntity) PermissionMatrixModel(com.synopsys.integration.alert.common.persistence.model.PermissionMatrixModel) PermissionKey(com.synopsys.integration.alert.common.persistence.model.PermissionKey) RegisteredDescriptorEntity(com.synopsys.integration.alert.database.configuration.RegisteredDescriptorEntity) PermissionMatrixRelation(com.synopsys.integration.alert.database.authorization.PermissionMatrixRelation) ConfigContextEntity(com.synopsys.integration.alert.database.configuration.ConfigContextEntity) Test(org.junit.jupiter.api.Test)

Example 2 with RoleEntity

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

the class DefaultRoleAccessorTest method createRoleWithPermissions.

@Test
public void createRoleWithPermissions() {
    final String roleName = "roleName";
    final String contextString = "context-test";
    final String descriptorName = "descriptorName";
    RoleEntity roleEntity = new RoleEntity(roleName, true);
    roleEntity.setId(1L);
    ConfigContextEntity configContextEntity = new ConfigContextEntity(contextString);
    configContextEntity.setId(1L);
    RegisteredDescriptorEntity registeredDescriptorEntity = new RegisteredDescriptorEntity(descriptorName, 1L);
    registeredDescriptorEntity.setId(1L);
    PermissionKey permissionKey = new PermissionKey(contextString, descriptorName);
    PermissionMatrixModel permissionMatrixModel = new PermissionMatrixModel(Map.of(permissionKey, AccessOperation.READ.getBit() + AccessOperation.WRITE.getBit()));
    PermissionMatrixRelation permissionMatrixRelation = new PermissionMatrixRelation(roleEntity.getId(), configContextEntity.getId(), registeredDescriptorEntity.getId(), AccessOperation.READ.getBit() + AccessOperation.WRITE.getBit());
    Mockito.when(roleRepository.save(Mockito.any())).thenReturn(new RoleEntity(roleName, true));
    mockUpdateRoleOperations(permissionMatrixRelation, configContextEntity, registeredDescriptorEntity);
    mockCreateModelFromPermission(configContextEntity, registeredDescriptorEntity);
    DefaultRoleAccessor authorizationUtility = new DefaultRoleAccessor(roleRepository, userRoleRepository, permissionMatrixRepository, registeredDescriptorRepository, configContextRepository);
    UserRoleModel userRoleModel = authorizationUtility.createRoleWithPermissions(roleName, permissionMatrixModel);
    Mockito.verify(permissionMatrixRepository).deleteAll(Mockito.any());
    assertEquals(roleName, userRoleModel.getName());
    assertTrue(userRoleModel.isCustom());
    assertEquals(permissionMatrixModel, userRoleModel.getPermissions());
}
Also used : RoleEntity(com.synopsys.integration.alert.database.user.RoleEntity) PermissionMatrixModel(com.synopsys.integration.alert.common.persistence.model.PermissionMatrixModel) PermissionKey(com.synopsys.integration.alert.common.persistence.model.PermissionKey) RegisteredDescriptorEntity(com.synopsys.integration.alert.database.configuration.RegisteredDescriptorEntity) PermissionMatrixRelation(com.synopsys.integration.alert.database.authorization.PermissionMatrixRelation) UserRoleModel(com.synopsys.integration.alert.common.persistence.model.UserRoleModel) ConfigContextEntity(com.synopsys.integration.alert.database.configuration.ConfigContextEntity) Test(org.junit.jupiter.api.Test)

Example 3 with RoleEntity

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

the class DefaultRoleAccessorTest method updateRoleNameTest.

@Test
public void updateRoleNameTest() throws Exception {
    final String roleName = "roleName";
    final Long roleId = 1L;
    RoleEntity roleEntity = new RoleEntity(DefaultUserRole.ALERT_USER.name(), true);
    roleEntity.setId(1L);
    Mockito.when(roleRepository.findById(Mockito.any())).thenReturn(Optional.of(roleEntity));
    DefaultRoleAccessor authorizationUtility = new DefaultRoleAccessor(roleRepository, userRoleRepository, permissionMatrixRepository, registeredDescriptorRepository, configContextRepository);
    authorizationUtility.updateRoleName(roleId, roleName);
    Mockito.verify(roleRepository).save(Mockito.any());
}
Also used : RoleEntity(com.synopsys.integration.alert.database.user.RoleEntity) Test(org.junit.jupiter.api.Test)

Example 4 with RoleEntity

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

the class DefaultRoleAccessorTest method getRolesTest.

@Test
public void getRolesTest() {
    RoleEntity roleEntity = new RoleEntity(DefaultUserRole.ALERT_USER.name(), true);
    roleEntity.setId(1L);
    Mockito.when(roleRepository.findAll()).thenReturn(List.of(roleEntity));
    DefaultRoleAccessor authorizationUtility = new DefaultRoleAccessor(roleRepository, userRoleRepository, permissionMatrixRepository, registeredDescriptorRepository, configContextRepository);
    Set<UserRoleModel> userRoleModelsSet = authorizationUtility.getRoles();
    UserRoleModel expectedUserRoleModel = createUserRoleModel(1L, DefaultUserRole.ALERT_USER.name(), true);
    assertEquals(1, userRoleModelsSet.size());
    assertTrue(userRoleModelsSet.contains(expectedUserRoleModel));
}
Also used : RoleEntity(com.synopsys.integration.alert.database.user.RoleEntity) UserRoleModel(com.synopsys.integration.alert.common.persistence.model.UserRoleModel) Test(org.junit.jupiter.api.Test)

Example 5 with RoleEntity

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

the class DefaultRoleAccessorTest method getRolesByRoleIdsTest.

@Test
public void getRolesByRoleIdsTest() {
    RoleEntity roleEntity = new RoleEntity(DefaultUserRole.ALERT_USER.name(), true);
    roleEntity.setId(1L);
    Mockito.when(roleRepository.findById(Mockito.any())).thenReturn(Optional.of(roleEntity));
    DefaultRoleAccessor authorizationUtility = new DefaultRoleAccessor(roleRepository, userRoleRepository, permissionMatrixRepository, registeredDescriptorRepository, configContextRepository);
    Set<UserRoleModel> userRoleModelsSet = authorizationUtility.getRoles(List.of(1L));
    UserRoleModel expectedUserRoleModel = createUserRoleModel(1L, DefaultUserRole.ALERT_USER.name(), true);
    assertEquals(1, userRoleModelsSet.size());
    assertTrue(userRoleModelsSet.contains(expectedUserRoleModel));
}
Also used : RoleEntity(com.synopsys.integration.alert.database.user.RoleEntity) UserRoleModel(com.synopsys.integration.alert.common.persistence.model.UserRoleModel) Test(org.junit.jupiter.api.Test)

Aggregations

RoleEntity (com.synopsys.integration.alert.database.user.RoleEntity)15 Test (org.junit.jupiter.api.Test)9 UserRoleModel (com.synopsys.integration.alert.common.persistence.model.UserRoleModel)6 Transactional (org.springframework.transaction.annotation.Transactional)6 PermissionMatrixRelation (com.synopsys.integration.alert.database.authorization.PermissionMatrixRelation)5 AlertForbiddenOperationException (com.synopsys.integration.alert.common.exception.AlertForbiddenOperationException)3 PermissionKey (com.synopsys.integration.alert.common.persistence.model.PermissionKey)3 PermissionMatrixModel (com.synopsys.integration.alert.common.persistence.model.PermissionMatrixModel)3 ConfigContextEntity (com.synopsys.integration.alert.database.configuration.ConfigContextEntity)3 RegisteredDescriptorEntity (com.synopsys.integration.alert.database.configuration.RegisteredDescriptorEntity)3 AlertConfigurationException (com.synopsys.integration.alert.api.common.model.exception.AlertConfigurationException)1 PermissionMatrixRepository (com.synopsys.integration.alert.database.authorization.PermissionMatrixRepository)1 ConfigContextRepository (com.synopsys.integration.alert.database.configuration.repository.ConfigContextRepository)1 RegisteredDescriptorRepository (com.synopsys.integration.alert.database.configuration.repository.RegisteredDescriptorRepository)1 RoleRepository (com.synopsys.integration.alert.database.user.RoleRepository)1 UserRoleRelation (com.synopsys.integration.alert.database.user.UserRoleRelation)1 UserRoleRepository (com.synopsys.integration.alert.database.user.UserRoleRepository)1 LinkedHashSet (java.util.LinkedHashSet)1 LinkedList (java.util.LinkedList)1