Search in sources :

Example 6 with UpdateRole

use of io.gravitee.am.service.model.UpdateRole in project gravitee-access-management by gravitee-io.

the class RoleServiceTest method shouldUpdate_defaultRolePermissions.

@Test
public void shouldUpdate_defaultRolePermissions() {
    UpdateRole updateRole = new UpdateRole();
    updateRole.setName(DefaultRole.DOMAIN_USER.name());
    updateRole.setPermissions(Permission.flatten(Collections.singletonMap(Permission.DOMAIN, Collections.singleton(Acl.READ))));
    Role role = new Role();
    role.setName(DefaultRole.DOMAIN_USER.name());
    // should be able to update a default role.
    role.setDefaultRole(true);
    role.setReferenceType(ReferenceType.ORGANIZATION);
    role.setReferenceId(ORGANIZATION_ID);
    when(roleRepository.findById(ReferenceType.ORGANIZATION, ORGANIZATION_ID, "my-role")).thenReturn(Maybe.just(role));
    when(roleRepository.findAll(ReferenceType.ORGANIZATION, ORGANIZATION_ID)).thenReturn(Flowable.empty());
    when(roleRepository.update(argThat(r -> r.getPermissionAcls().equals(Permission.unflatten(updateRole.getPermissions()))))).thenReturn(Single.just(role));
    when(eventService.create(any())).thenReturn(Single.just(new Event()));
    TestObserver testObserver = roleService.update(ReferenceType.ORGANIZATION, ORGANIZATION_ID, "my-role", updateRole, null).test();
    testObserver.awaitTerminalEvent();
    testObserver.assertComplete();
    testObserver.assertNoErrors();
    verify(roleRepository, times(1)).findById(ReferenceType.ORGANIZATION, ORGANIZATION_ID, "my-role");
    verify(roleRepository, times(1)).findAll(ReferenceType.ORGANIZATION, ORGANIZATION_ID);
    verify(roleRepository, times(1)).update(any(Role.class));
}
Also used : Role(io.gravitee.am.model.Role) NewRole(io.gravitee.am.service.model.NewRole) DefaultRole(io.gravitee.am.model.permissions.DefaultRole) UpdateRole(io.gravitee.am.service.model.UpdateRole) Arrays(java.util.Arrays) Mock(org.mockito.Mock) Completable(io.reactivex.Completable) Role(io.gravitee.am.model.Role) Permission(io.gravitee.am.model.permissions.Permission) Maybe(io.reactivex.Maybe) RunWith(org.junit.runner.RunWith) TechnicalException(io.gravitee.am.repository.exceptions.TechnicalException) Single(io.reactivex.Single) Event(io.gravitee.am.model.common.event.Event) Matchers.anyString(org.mockito.Matchers.anyString) io.gravitee.am.service.exception(io.gravitee.am.service.exception) Acl(io.gravitee.am.model.Acl) Flowable(io.reactivex.Flowable) NewRole(io.gravitee.am.service.model.NewRole) ReferenceType(io.gravitee.am.model.ReferenceType) RoleServiceImpl(io.gravitee.am.service.impl.RoleServiceImpl) InjectMocks(org.mockito.InjectMocks) Set(java.util.Set) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test) DefaultRole(io.gravitee.am.model.permissions.DefaultRole) RoleRepository(io.gravitee.am.repository.management.api.RoleRepository) UpdateRole(io.gravitee.am.service.model.UpdateRole) Mockito(org.mockito.Mockito) Matchers.anyList(org.mockito.Matchers.anyList) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Collections(java.util.Collections) UpdateRole(io.gravitee.am.service.model.UpdateRole) Event(io.gravitee.am.model.common.event.Event) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test)

Example 7 with UpdateRole

use of io.gravitee.am.service.model.UpdateRole in project gravitee-access-management by gravitee-io.

the class RoleServiceTest method shouldUpdate_uniquenessException.

@Test
public void shouldUpdate_uniquenessException() {
    UpdateRole updateRole = Mockito.mock(UpdateRole.class);
    when(updateRole.getName()).thenReturn("existing-role-name");
    Role role = new Role();
    role.setId("existing-role-id");
    role.setName("existing-role-name");
    role.setReferenceType(ReferenceType.DOMAIN);
    role.setReferenceId("domain#1");
    when(roleRepository.findById(ReferenceType.DOMAIN, DOMAIN, "my-role")).thenReturn(Maybe.just(new Role()));
    when(roleRepository.findAll(ReferenceType.DOMAIN, DOMAIN)).thenReturn(Flowable.just(role));
    TestObserver testObserver = new TestObserver();
    roleService.update(DOMAIN, "my-role", updateRole).subscribe(testObserver);
    testObserver.assertError(RoleAlreadyExistsException.class);
    testObserver.assertNotComplete();
    verify(roleRepository, never()).create(any(Role.class));
}
Also used : Role(io.gravitee.am.model.Role) NewRole(io.gravitee.am.service.model.NewRole) DefaultRole(io.gravitee.am.model.permissions.DefaultRole) UpdateRole(io.gravitee.am.service.model.UpdateRole) UpdateRole(io.gravitee.am.service.model.UpdateRole) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test)

Example 8 with UpdateRole

use of io.gravitee.am.service.model.UpdateRole in project gravitee-access-management by gravitee-io.

the class RoleServiceTest method shouldNotUpdate_defaultRoleName.

@Test
public void shouldNotUpdate_defaultRoleName() {
    UpdateRole updateRole = new UpdateRole();
    updateRole.setName("new name");
    Role role = new Role();
    role.setId("my-role");
    role.setName(DefaultRole.DOMAIN_USER.name());
    role.setDefaultRole(true);
    role.setReferenceType(ReferenceType.ORGANIZATION);
    role.setReferenceId(ORGANIZATION_ID);
    when(roleRepository.findById(ReferenceType.ORGANIZATION, ORGANIZATION_ID, "my-role")).thenReturn(Maybe.just(role));
    TestObserver testObserver = roleService.update(ReferenceType.ORGANIZATION, ORGANIZATION_ID, "my-role", updateRole, null).test();
    testObserver.awaitTerminalEvent();
    testObserver.assertNotComplete();
    testObserver.assertError(DefaultRoleUpdateException.class);
    verify(roleRepository, times(1)).findById(ReferenceType.ORGANIZATION, ORGANIZATION_ID, "my-role");
    verify(roleRepository, never()).findAll(ReferenceType.ORGANIZATION, ORGANIZATION_ID);
    verify(roleRepository, never()).update(any(Role.class));
}
Also used : Role(io.gravitee.am.model.Role) NewRole(io.gravitee.am.service.model.NewRole) DefaultRole(io.gravitee.am.model.permissions.DefaultRole) UpdateRole(io.gravitee.am.service.model.UpdateRole) UpdateRole(io.gravitee.am.service.model.UpdateRole) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test)

Example 9 with UpdateRole

use of io.gravitee.am.service.model.UpdateRole in project gravitee-access-management by gravitee-io.

the class RoleResource method convert.

/**
 * Starting from v3, AM role permissions and domain role permissions start to work differently.
 * AM permission are now structured using Permission and Acls while domain role permission are stil simple strings (oauth scopes).
 * Internaly, role structure has now 2 distinct attributes:
 * <ul>
 *     <li>permissions: holds the AM role permissions</li>
 *     <li>oauthScopes: holds the domain role permissions</li>
 * </ul>
 * This will be removed when we deal with this issue: https://github.com/gravitee-io/issues/issues/3323
 */
private UpdateRole convert(UpdateRole updateDomainRole) {
    UpdateRole updateRole = new UpdateRole();
    updateRole.setDescription(updateDomainRole.getDescription());
    updateRole.setName(updateDomainRole.getName());
    updateRole.setOauthScopes(updateDomainRole.getPermissions());
    return updateRole;
}
Also used : UpdateRole(io.gravitee.am.service.model.UpdateRole)

Aggregations

UpdateRole (io.gravitee.am.service.model.UpdateRole)9 Role (io.gravitee.am.model.Role)8 DefaultRole (io.gravitee.am.model.permissions.DefaultRole)7 NewRole (io.gravitee.am.service.model.NewRole)7 TestObserver (io.reactivex.observers.TestObserver)7 Test (org.junit.Test)7 Acl (io.gravitee.am.model.Acl)2 ReferenceType (io.gravitee.am.model.ReferenceType)2 Event (io.gravitee.am.model.common.event.Event)2 Permission (io.gravitee.am.model.permissions.Permission)2 Maybe (io.reactivex.Maybe)2 User (io.gravitee.am.identityprovider.api.User)1 RoleEntity (io.gravitee.am.management.handlers.management.api.model.RoleEntity)1 AbstractResource (io.gravitee.am.management.handlers.management.api.resources.AbstractResource)1 TechnicalException (io.gravitee.am.repository.exceptions.TechnicalException)1 RoleRepository (io.gravitee.am.repository.management.api.RoleRepository)1 DomainService (io.gravitee.am.service.DomainService)1 RoleService (io.gravitee.am.service.RoleService)1 io.gravitee.am.service.exception (io.gravitee.am.service.exception)1 DomainNotFoundException (io.gravitee.am.service.exception.DomainNotFoundException)1