Search in sources :

Example 1 with UserMembership

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

the class MembershipService_FindUserMembershipTest method shouldGetApiWithApiAndGroups.

@Test
public void shouldGetApiWithApiAndGroups() 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");
    Membership mGroup = mock(Membership.class);
    when(mGroup.getReferenceId()).thenReturn("api-id2");
    when(mApi.getRoleId()).thenReturn("role");
    when(mockMembershipRepository.findByMemberIdAndMemberTypeAndReferenceType(eq(USER_ID), eq(MembershipMemberType.USER), eq(MembershipReferenceType.API))).thenReturn(Collections.singleton(mApi));
    when(mockMembershipRepository.findByMemberIdAndMemberTypeAndReferenceType(eq("GROUP"), eq(MembershipMemberType.GROUP), eq(MembershipReferenceType.API))).thenReturn(Collections.singleton(mGroup));
    GroupEntity group1 = mock(GroupEntity.class);
    doReturn("GROUP").when(group1).getId();
    doReturn(new HashSet<>(asList(group1))).when(mockGroupService).findByUser(USER_ID);
    List<UserMembership> references = membershipService.findUserMembership(io.gravitee.rest.api.model.MembershipReferenceType.API, USER_ID);
    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("api-id2"));
    assertTrue(references.get(1).getReference().equals("api-id1") || references.get(1).getReference().equals("api-id2"));
    assertEquals("API", references.get(0).getType());
}
Also used : RoleEntity(io.gravitee.rest.api.model.RoleEntity) GroupEntity(io.gravitee.rest.api.model.GroupEntity) 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 2 with UserMembership

use of io.gravitee.rest.api.model.UserMembership 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 3 with UserMembership

use of io.gravitee.rest.api.model.UserMembership 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 4 with UserMembership

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

the class MembershipService_FindUserMembershipTest method shouldGetApiWithOnlyGroups.

@Test
public void shouldGetApiWithOnlyGroups() throws Exception {
    when(mockRoleService.findByScope(any())).thenReturn(Collections.emptyList());
    Membership mGroup = mock(Membership.class);
    when(mGroup.getReferenceId()).thenReturn("api-id2");
    when(mockMembershipRepository.findByMemberIdAndMemberTypeAndReferenceType(eq(USER_ID), eq(MembershipMemberType.USER), eq(MembershipReferenceType.API))).thenReturn(Collections.emptySet());
    when(mockMembershipRepository.findByMemberIdAndMemberTypeAndReferenceType(eq("GROUP"), eq(MembershipMemberType.GROUP), eq(MembershipReferenceType.API))).thenReturn(Collections.singleton(mGroup));
    GroupEntity group1 = mock(GroupEntity.class);
    doReturn("GROUP").when(group1).getId();
    doReturn(new HashSet<>(asList(group1))).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-id2", references.get(0).getReference());
    assertEquals("API", references.get(0).getType());
}
Also used : GroupEntity(io.gravitee.rest.api.model.GroupEntity) 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)

Aggregations

Membership (io.gravitee.repository.management.model.Membership)4 UserMembership (io.gravitee.rest.api.model.UserMembership)4 Test (org.junit.Test)4 RoleEntity (io.gravitee.rest.api.model.RoleEntity)3 GroupEntity (io.gravitee.rest.api.model.GroupEntity)2