Search in sources :

Example 26 with RoleEntity

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

the class RoleService_FindByIdTest method test_int_to_CRUD.

private void test_int_to_CRUD(int perm, RolePermissionAction... action) throws TechnicalException {
    Role roleMock = mock(Role.class);
    when(roleMock.getScope()).thenReturn(RoleScope.ENVIRONMENT);
    when(roleMock.getId()).thenReturn("id");
    when(roleMock.getName()).thenReturn("name");
    when(roleMock.getPermissions()).thenReturn(new int[] { perm });
    when(mockRoleRepository.findById("id")).thenReturn(Optional.of(roleMock));
    RoleEntity entity = roleService.findById("id");
    assertNotNull("no entity found", entity);
    assertEquals("invalid id", "id", entity.getId());
    assertEquals("invalid name", "name", 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", action.length, perms.length);
    for (RolePermissionAction rolePermissionAction : action) {
        assertTrue("not the good permission", Arrays.asList(ArrayUtils.toObject(perms)).contains(rolePermissionAction.getId()));
    }
}
Also used : Role(io.gravitee.repository.management.model.Role) RoleEntity(io.gravitee.rest.api.model.RoleEntity) RolePermissionAction(io.gravitee.rest.api.model.permissions.RolePermissionAction)

Example 27 with RoleEntity

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

the class MembershipService_FindUserMembershipTest method shouldGetMembershipForGivenSource.

@Test
public void shouldGetMembershipForGivenSource() throws Exception {
    RoleEntity roleApi = new RoleEntity();
    roleApi.setId("roleApi");
    roleApi.setName("PO");
    roleApi.setScope(RoleScope.API);
    RoleEntity roleApp = new RoleEntity();
    roleApp.setId("roleApp");
    roleApp.setName("PO");
    roleApp.setScope(RoleScope.API);
    when(mockRoleService.findAll()).thenReturn(asList(roleApi, roleApp));
    Membership mApi = mock(Membership.class);
    when(mApi.getReferenceId()).thenReturn("api-id1");
    when(mApi.getRoleId()).thenReturn("roleApi");
    when(mApi.getSource()).thenReturn("oauth2");
    Membership mApp = mock(Membership.class);
    when(mApp.getReferenceId()).thenReturn("app-id1");
    when(mApp.getRoleId()).thenReturn("roleApp");
    when(mApp.getSource()).thenReturn("oauth2");
    when(mockMembershipRepository.findByMemberIdAndMemberTypeAndReferenceTypeAndSource(eq(USER_ID), eq(MembershipMemberType.USER), eq(MembershipReferenceType.GROUP), eq("oauth2"))).thenReturn(Sets.newHashSet(mApi, mApp));
    List<UserMembership> references = membershipService.findUserMembershipBySource(io.gravitee.rest.api.model.MembershipReferenceType.GROUP, USER_ID, "oauth2");
    assertFalse(references.isEmpty());
    assertEquals(2, references.size());
    assertNotEquals(references.get(0).getReference(), references.get(1).getReference());
    assertTrue(references.get(0).getReference().equals("api-id1") || references.get(0).getReference().equals("app-id1"));
    assertTrue(references.get(1).getReference().equals("api-id1") || references.get(1).getReference().equals("app-id1"));
}
Also used : RoleEntity(io.gravitee.rest.api.model.RoleEntity) Membership(io.gravitee.repository.management.model.Membership) UserMembership(io.gravitee.rest.api.model.UserMembership) UserMembership(io.gravitee.rest.api.model.UserMembership) Test(org.junit.Test)

Example 28 with RoleEntity

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

the class MembershipService_FindUserMembershipTest method shouldGetApiWithoutGroups.

@Test
public void shouldGetApiWithoutGroups() throws Exception {
    RoleEntity roleEntity = new RoleEntity();
    roleEntity.setId("role");
    roleEntity.setName("PO");
    roleEntity.setScope(RoleScope.API);
    when(mockRoleService.findByScope(any())).thenReturn(Collections.singletonList(roleEntity));
    Membership mApi = mock(Membership.class);
    when(mApi.getReferenceId()).thenReturn("api-id1");
    when(mApi.getRoleId()).thenReturn("role");
    when(mockMembershipRepository.findByMemberIdAndMemberTypeAndReferenceType(eq(USER_ID), eq(MembershipMemberType.USER), eq(MembershipReferenceType.API))).thenReturn(Collections.singleton(mApi));
    doReturn(Collections.emptySet()).when(mockGroupService).findByUser(USER_ID);
    List<UserMembership> references = membershipService.findUserMembership(io.gravitee.rest.api.model.MembershipReferenceType.API, USER_ID);
    assertFalse(references.isEmpty());
    assertEquals(1, references.size());
    assertEquals("api-id1", references.get(0).getReference());
    assertEquals("API", references.get(0).getType());
}
Also used : RoleEntity(io.gravitee.rest.api.model.RoleEntity) Membership(io.gravitee.repository.management.model.Membership) UserMembership(io.gravitee.rest.api.model.UserMembership) UserMembership(io.gravitee.rest.api.model.UserMembership) Test(org.junit.Test)

Example 29 with RoleEntity

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

the class MembershipService_GetMembersTest method shouldGetMembersWithMembership.

@Test
public void shouldGetMembersWithMembership() throws Exception {
    Membership membership = new Membership();
    membership.setReferenceId(API_ID);
    membership.setCreatedAt(new Date());
    membership.setUpdatedAt(membership.getCreatedAt());
    membership.setReferenceType(MembershipReferenceType.API);
    membership.setRoleId("API_PRIMARY_OWNER");
    membership.setMemberId("user-id");
    membership.setMemberType(MembershipMemberType.USER);
    UserEntity userEntity = new UserEntity();
    userEntity.setId(membership.getMemberId());
    userEntity.setFirstname("John");
    userEntity.setLastname("Doe");
    RoleEntity po = mock(RoleEntity.class);
    po.setName(SystemRole.PRIMARY_OWNER.name());
    List<String> memberIds = Collections.singletonList(membership.getMemberId());
    Set<UserEntity> userEntities = Collections.singleton(userEntity);
    when(membershipRepository.findByReferencesAndRoleId(MembershipReferenceType.API, Collections.singletonList(API_ID), "API_PRIMARY_OWNER")).thenReturn(Collections.singleton(membership));
    when(userService.findByIds(memberIds, false)).thenReturn(userEntities);
    when(roleService.findById("API_PRIMARY_OWNER")).thenReturn(po);
    Set<MemberEntity> members = membershipService.getMembersByReferenceAndRole(io.gravitee.rest.api.model.MembershipReferenceType.API, API_ID, "API_PRIMARY_OWNER");
    Assert.assertNotNull(members);
    Assert.assertFalse("members must not be empty", members.isEmpty());
    verify(membershipRepository, times(1)).findByReferencesAndRoleId(MembershipReferenceType.API, Collections.singletonList(API_ID), "API_PRIMARY_OWNER");
    verify(userService, times(1)).findByIds(memberIds, false);
}
Also used : RoleEntity(io.gravitee.rest.api.model.RoleEntity) Membership(io.gravitee.repository.management.model.Membership) MemberEntity(io.gravitee.rest.api.model.MemberEntity) Date(java.util.Date) UserEntity(io.gravitee.rest.api.model.UserEntity) Test(org.junit.Test)

Aggregations

RoleEntity (io.gravitee.rest.api.model.RoleEntity)29 Test (org.junit.Test)20 UserEntity (io.gravitee.rest.api.model.UserEntity)13 Membership (io.gravitee.repository.management.model.Membership)8 MembershipService (io.gravitee.rest.api.service.MembershipService)7 Role (io.gravitee.repository.management.model.Role)6 MembershipPayload (io.gravitee.cockpit.api.command.membership.MembershipPayload)5 MembershipReply (io.gravitee.cockpit.api.command.membership.MembershipReply)5 NewRoleEntity (io.gravitee.rest.api.model.NewRoleEntity)5 UpdateRoleEntity (io.gravitee.rest.api.model.UpdateRoleEntity)5 Response (javax.ws.rs.core.Response)5 MembershipCommand (io.gravitee.cockpit.api.command.membership.MembershipCommand)4 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)4 MembershipReferenceType (io.gravitee.rest.api.model.MembershipReferenceType)4 Instant (java.time.Instant)4 List (java.util.List)4 JWT (com.auth0.jwt.JWT)3 Algorithm (com.auth0.jwt.algorithms.Algorithm)3 Maps (io.gravitee.common.util.Maps)3 UserDetails (io.gravitee.rest.api.idp.api.authentication.UserDetails)3