Search in sources :

Example 1 with NewRoleEntity

use of io.gravitee.rest.api.model.NewRoleEntity in project gravitee-management-rest-api by gravitee-io.

the class RoleService_CreateTest method shouldNotCreateBecauseOfReservedRoleName.

@Test(expected = RoleReservedNameException.class)
public void shouldNotCreateBecauseOfReservedRoleName() throws TechnicalException {
    NewRoleEntity newRoleEntityMock = mock(NewRoleEntity.class);
    when(newRoleEntityMock.getName()).thenReturn("admin");
    roleService.create(newRoleEntityMock);
    fail("should fail earlier");
}
Also used : NewRoleEntity(io.gravitee.rest.api.model.NewRoleEntity) Test(org.junit.Test)

Example 2 with NewRoleEntity

use of io.gravitee.rest.api.model.NewRoleEntity in project gravitee-management-rest-api by gravitee-io.

the class RoleService_CreateTest method shouldCreate.

@Test
public void shouldCreate() throws TechnicalException {
    NewRoleEntity newRoleEntityMock = mock(NewRoleEntity.class);
    when(newRoleEntityMock.getName()).thenReturn("new mock role");
    when(newRoleEntityMock.getScope()).thenReturn(io.gravitee.rest.api.model.permissions.RoleScope.ENVIRONMENT);
    when(newRoleEntityMock.getPermissions()).thenReturn(Collections.singletonMap(DOCUMENTATION.getName(), new char[] { RolePermissionAction.CREATE.getId() }));
    Role roleMock = mock(Role.class);
    when(roleMock.getId()).thenReturn("new_mock_role");
    when(roleMock.getName()).thenReturn("new mock role");
    when(roleMock.getScope()).thenReturn(RoleScope.ENVIRONMENT);
    when(roleMock.getPermissions()).thenReturn(new int[] { 3008 });
    when(mockRoleRepository.create(any())).thenReturn(roleMock);
    RoleEntity entity = roleService.create(newRoleEntityMock);
    assertNotNull("no entoty created", entity);
    assertEquals("invalid id", "new_mock_role", entity.getId());
    assertEquals("invalid name", "new mock role", entity.getName());
    assertEquals("invalid scope", io.gravitee.rest.api.model.permissions.RoleScope.ENVIRONMENT, entity.getScope());
    assertFalse("no permissions found", entity.getPermissions().isEmpty());
    assertTrue("invalid Permission name", entity.getPermissions().containsKey(DOCUMENTATION.getName()));
    char[] perms = entity.getPermissions().get(DOCUMENTATION.getName());
    assertEquals("not enough permissions", 1, perms.length);
    assertEquals("not the good permission", RolePermissionAction.CREATE.getId(), perms[0]);
}
Also used : Role(io.gravitee.repository.management.model.Role) RoleEntity(io.gravitee.rest.api.model.RoleEntity) NewRoleEntity(io.gravitee.rest.api.model.NewRoleEntity) NewRoleEntity(io.gravitee.rest.api.model.NewRoleEntity) Test(org.junit.Test)

Example 3 with NewRoleEntity

use of io.gravitee.rest.api.model.NewRoleEntity in project gravitee-management-rest-api by gravitee-io.

the class RoleServiceImpl method create.

private RoleEntity create(final NewRoleEntity roleEntity, String organizationId) {
    try {
        Role role = convert(roleEntity);
        if (roleRepository.findByScopeAndNameAndReferenceIdAndReferenceType(role.getScope(), role.getName(), organizationId, RoleReferenceType.ORGANIZATION).isPresent()) {
            throw new RoleAlreadyExistsException(role.getScope(), role.getName());
        }
        role.setId(UuidString.generateRandom());
        role.setCreatedAt(new Date());
        role.setUpdatedAt(role.getCreatedAt());
        role.setReferenceId(organizationId);
        role.setReferenceType(RoleReferenceType.ORGANIZATION);
        RoleEntity entity = convert(roleRepository.create(role));
        auditService.createOrganizationAuditLog(Collections.singletonMap(ROLE, role.getScope() + ":" + role.getName()), ROLE_CREATED, role.getCreatedAt(), null, role);
        if (entity.isDefaultRole()) {
            toggleDefaultRole(roleEntity.getScope(), entity.getName());
        }
        return entity;
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to create role {}", roleEntity.getName(), ex);
        throw new TechnicalManagementException("An error occurs while trying to create role " + roleEntity.getName(), ex);
    }
}
Also used : Role(io.gravitee.repository.management.model.Role) UpdateRoleEntity(io.gravitee.rest.api.model.UpdateRoleEntity) NewRoleEntity(io.gravitee.rest.api.model.NewRoleEntity) RoleEntity(io.gravitee.rest.api.model.RoleEntity) TechnicalException(io.gravitee.repository.exceptions.TechnicalException)

Example 4 with NewRoleEntity

use of io.gravitee.rest.api.model.NewRoleEntity in project gravitee-management-rest-api by gravitee-io.

the class RoleService_CreateTest method shouldNotCreateBecauseOfInvalidPermissionAction.

@Test(expected = IllegalArgumentException.class)
public void shouldNotCreateBecauseOfInvalidPermissionAction() throws TechnicalException {
    NewRoleEntity newRoleEntityMock = mock(NewRoleEntity.class);
    when(newRoleEntityMock.getName()).thenReturn("new mock role");
    when(newRoleEntityMock.getScope()).thenReturn(io.gravitee.rest.api.model.permissions.RoleScope.ENVIRONMENT);
    when(newRoleEntityMock.getPermissions()).thenReturn(Collections.singletonMap(DOCUMENTATION.getName(), new char[] { 'X' }));
    roleService.create(newRoleEntityMock);
    fail("should fail earlier");
}
Also used : NewRoleEntity(io.gravitee.rest.api.model.NewRoleEntity) Test(org.junit.Test)

Aggregations

NewRoleEntity (io.gravitee.rest.api.model.NewRoleEntity)4 Test (org.junit.Test)3 Role (io.gravitee.repository.management.model.Role)2 RoleEntity (io.gravitee.rest.api.model.RoleEntity)2 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)1 UpdateRoleEntity (io.gravitee.rest.api.model.UpdateRoleEntity)1