Search in sources :

Example 16 with Membership

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

the class MembershipService_AddOrUpdateMemberTest method shouldAddApiGroupMembership.

@Test
public void shouldAddApiGroupMembership() throws Exception {
    UserEntity userEntity = new UserEntity();
    userEntity.setId("my name");
    userEntity.setUsername("my name");
    userEntity.setEmail("me@mail.com");
    RoleEntity role = mock(RoleEntity.class);
    Membership newMembership = new Membership();
    newMembership.setReferenceType(MembershipReferenceType.GROUP);
    newMembership.setRoles(Collections.singletonMap(RoleScope.API.getId(), "OWNER"));
    newMembership.setReferenceId(GROUP_ID);
    newMembership.setUserId(userEntity.getUsername());
    GroupEntity groupEntityMock = mock(GroupEntity.class);
    when(groupEntityMock.getName()).thenReturn("foo");
    when(role.getScope()).thenReturn(io.gravitee.management.model.permissions.RoleScope.API);
    when(roleService.findById(any(), any())).thenReturn(role);
    when(userService.findById(userEntity.getId())).thenReturn(userEntity);
    when(groupService.findById(GROUP_ID)).thenReturn(groupEntityMock);
    when(membershipRepository.findById(userEntity.getId(), MembershipReferenceType.GROUP, GROUP_ID)).thenReturn(empty(), of(newMembership));
    when(membershipRepository.create(any())).thenReturn(newMembership);
    membershipService.addOrUpdateMember(new MembershipService.MembershipReference(MembershipReferenceType.GROUP, GROUP_ID), new MembershipService.MembershipUser(userEntity.getId(), null), new MembershipService.MembershipRole(RoleScope.API, "OWNER"));
    verify(userService, times(2)).findById(userEntity.getId());
    verify(membershipRepository, times(2)).findById(userEntity.getUsername(), MembershipReferenceType.GROUP, GROUP_ID);
    verify(membershipRepository, times(1)).create(any());
    verify(membershipRepository, never()).update(any());
    verify(emailService, times(1)).sendAsyncEmailNotification(any());
}
Also used : RoleEntity(io.gravitee.management.model.RoleEntity) GroupEntity(io.gravitee.management.model.GroupEntity) Membership(io.gravitee.repository.management.model.Membership) UserEntity(io.gravitee.management.model.UserEntity) Test(org.junit.Test)

Example 17 with Membership

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

the class MembershipService_GetMemberPermissionsTest method shouldGetPermissionsIfMemberOfApiGroup.

@Test
public void shouldGetPermissionsIfMemberOfApiGroup() throws Exception {
    ApiEntity api = mock(ApiEntity.class);
    doReturn(API_ID).when(api).getId();
    when(api.getGroups()).thenReturn(Collections.singleton(GROUP_ID1));
    Membership membership = mock(Membership.class);
    doReturn(Collections.singletonMap(RoleScope.API.getId(), ROLENAME)).when(membership).getRoles();
    doReturn(MembershipReferenceType.GROUP).when(membership).getReferenceType();
    doReturn(GROUP_ID1).when(membership).getReferenceId();
    doReturn(USERNAME).when(membership).getUserId();
    doReturn(empty()).when(membershipRepository).findById(USERNAME, MembershipReferenceType.API, API_ID);
    doReturn(of(membership)).when(membershipRepository).findById(USERNAME, MembershipReferenceType.GROUP, GROUP_ID1);
    UserEntity userEntity = mock(UserEntity.class);
    doReturn(userEntity).when(userService).findById(USERNAME);
    RoleEntity roleEntity = mock(RoleEntity.class);
    Map<String, char[]> rolePerms = new HashMap<>();
    rolePerms.put(ApiPermission.DOCUMENTATION.getName(), new char[] { RolePermissionAction.UPDATE.getId(), RolePermissionAction.CREATE.getId() });
    doReturn(rolePerms).when(roleEntity).getPermissions();
    doReturn(roleEntity).when(roleService).findById(RoleScope.API, ROLENAME);
    Map<String, char[]> permissions = membershipService.getMemberPermissions(api, USERNAME);
    assertNotNull(permissions);
    assertPermissions(rolePerms, permissions);
    verify(membershipRepository, times(1)).findById(USERNAME, MembershipReferenceType.API, API_ID);
    verify(membershipRepository, times(2)).findById(eq(USERNAME), eq(MembershipReferenceType.GROUP), anyString());
    verify(userService, times(1)).findById(USERNAME);
}
Also used : Membership(io.gravitee.repository.management.model.Membership) Test(org.junit.Test)

Example 18 with Membership

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

the class MembershipService_GetMemberPermissionsTest method shouldGetPermissionsIfMemberOfApi.

@Test
public void shouldGetPermissionsIfMemberOfApi() throws Exception {
    ApiEntity api = mock(ApiEntity.class);
    doReturn(API_ID).when(api).getId();
    Membership membership = mock(Membership.class);
    doReturn(Collections.singletonMap(RoleScope.API.getId(), ROLENAME)).when(membership).getRoles();
    doReturn(MembershipReferenceType.API).when(membership).getReferenceType();
    doReturn(API_ID).when(membership).getReferenceId();
    doReturn(USERNAME).when(membership).getUserId();
    GroupEntity group = mock(GroupEntity.class);
    doReturn(GROUP_ID1).when(group).getId();
    doReturn(Collections.singleton(group)).when(api).getGroups();
    doReturn(of(membership)).when(membershipRepository).findById(USERNAME, MembershipReferenceType.API, API_ID);
    UserEntity userEntity = mock(UserEntity.class);
    doReturn(userEntity).when(userService).findById(USERNAME);
    RoleEntity roleEntity = mock(RoleEntity.class);
    Map<String, char[]> rolePerms = new HashMap<>();
    rolePerms.put(ApiPermission.DOCUMENTATION.getName(), new char[] { RolePermissionAction.UPDATE.getId(), RolePermissionAction.CREATE.getId() });
    doReturn(rolePerms).when(roleEntity).getPermissions();
    doReturn(roleEntity).when(roleService).findById(RoleScope.API, ROLENAME);
    Map<String, char[]> permissions = membershipService.getMemberPermissions(api, USERNAME);
    assertNotNull(permissions);
    assertPermissions(rolePerms, permissions);
    verify(membershipRepository, times(2)).findById(USERNAME, MembershipReferenceType.API, API_ID);
    verify(membershipRepository, never()).findById(eq(USERNAME), eq(MembershipReferenceType.GROUP), anyString());
    verify(userService, times(1)).findById(USERNAME);
}
Also used : Membership(io.gravitee.repository.management.model.Membership) Test(org.junit.Test)

Example 19 with Membership

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

the class MembershipService_GetMemberPermissionsTest method shouldGetMergedPermissionsIfMemberOfMultipleApiGroups.

@Test
public void shouldGetMergedPermissionsIfMemberOfMultipleApiGroups() throws Exception {
    ApiEntity api = mock(ApiEntity.class);
    doReturn(API_ID).when(api).getId();
    when(api.getGroups()).thenReturn(new HashSet<>(Arrays.asList(GROUP_ID1, GROUP_ID2)));
    Membership membership1 = mock(Membership.class);
    doReturn(Collections.singletonMap(RoleScope.API.getId(), ROLENAME)).when(membership1).getRoles();
    doReturn(MembershipReferenceType.GROUP).when(membership1).getReferenceType();
    doReturn(GROUP_ID1).when(membership1).getReferenceId();
    doReturn(USERNAME).when(membership1).getUserId();
    doReturn(empty()).when(membershipRepository).findById(USERNAME, MembershipReferenceType.API, API_ID);
    doReturn(of(membership1)).when(membershipRepository).findById(USERNAME, MembershipReferenceType.GROUP, GROUP_ID1);
    Membership membership2 = mock(Membership.class);
    doReturn(Collections.singletonMap(RoleScope.API.getId(), ROLENAME2)).when(membership2).getRoles();
    doReturn(MembershipReferenceType.GROUP).when(membership2).getReferenceType();
    doReturn(GROUP_ID2).when(membership2).getReferenceId();
    doReturn(USERNAME).when(membership2).getUserId();
    doReturn(empty()).when(membershipRepository).findById(USERNAME, MembershipReferenceType.API, API_ID);
    doReturn(of(membership2)).when(membershipRepository).findById(USERNAME, MembershipReferenceType.GROUP, GROUP_ID2);
    UserEntity userEntity = mock(UserEntity.class);
    doReturn(userEntity).when(userService).findById(USERNAME);
    RoleEntity roleEntity = mock(RoleEntity.class);
    Map<String, char[]> rolePerms = new HashMap<>();
    rolePerms.put(ApiPermission.DOCUMENTATION.getName(), new char[] { RolePermissionAction.UPDATE.getId(), RolePermissionAction.CREATE.getId() });
    doReturn(rolePerms).when(roleEntity).getPermissions();
    doReturn(roleEntity).when(roleService).findById(RoleScope.API, ROLENAME);
    RoleEntity roleEntity2 = mock(RoleEntity.class);
    Map<String, char[]> rolePerms2 = new HashMap<>();
    rolePerms2.put(ApiPermission.DOCUMENTATION.getName(), new char[] { RolePermissionAction.READ.getId(), RolePermissionAction.DELETE.getId() });
    rolePerms2.put(ApiPermission.PLAN.getName(), new char[] { RolePermissionAction.READ.getId() });
    doReturn(rolePerms2).when(roleEntity2).getPermissions();
    doReturn(roleEntity2).when(roleService).findById(RoleScope.API, ROLENAME2);
    Map<String, char[]> permissions = membershipService.getMemberPermissions(api, USERNAME);
    assertNotNull(permissions);
    Map<String, char[]> expectedPermissions = new HashMap<>();
    expectedPermissions.put(ApiPermission.DOCUMENTATION.getName(), new char[] { RolePermissionAction.CREATE.getId(), RolePermissionAction.READ.getId(), RolePermissionAction.UPDATE.getId(), RolePermissionAction.DELETE.getId() });
    expectedPermissions.put(ApiPermission.PLAN.getName(), new char[] { RolePermissionAction.READ.getId() });
    assertPermissions(expectedPermissions, permissions);
    verify(membershipRepository, times(1)).findById(USERNAME, MembershipReferenceType.API, API_ID);
    verify(membershipRepository, times(4)).findById(eq(USERNAME), eq(MembershipReferenceType.GROUP), anyString());
    verify(userService, times(2)).findById(USERNAME);
}
Also used : Membership(io.gravitee.repository.management.model.Membership) Test(org.junit.Test)

Example 20 with Membership

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

the class ApiService_FindByIdTest method shouldFindById.

@Test
public void shouldFindById() throws TechnicalException {
    when(apiRepository.findById(API_ID)).thenReturn(Optional.of(api));
    Membership po = new Membership(USER_NAME, API_ID, MembershipReferenceType.API);
    po.setRoles(Collections.singletonMap(RoleScope.API.getId(), SystemRole.PRIMARY_OWNER.name()));
    when(membershipRepository.findByReferenceAndRole(any(), any(), any(), any())).thenReturn(Collections.singleton(po));
    final ApiEntity apiEntity = apiService.findById(API_ID);
    assertNotNull(apiEntity);
}
Also used : Membership(io.gravitee.repository.management.model.Membership) ApiEntity(io.gravitee.management.model.ApiEntity) Test(org.junit.Test)

Aggregations

Membership (io.gravitee.repository.management.model.Membership)20 Test (org.junit.Test)15 UserEntity (io.gravitee.management.model.UserEntity)8 ApplicationEntity (io.gravitee.management.model.ApplicationEntity)6 RoleEntity (io.gravitee.management.model.RoleEntity)4 MemberEntity (io.gravitee.management.model.MemberEntity)3 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)3 Proxy (io.gravitee.definition.model.Proxy)2 io.gravitee.management.model (io.gravitee.management.model)2 ApiEntity (io.gravitee.management.model.ApiEntity)2 GroupEntity (io.gravitee.management.model.GroupEntity)2 UpdateApplicationEntity (io.gravitee.management.model.UpdateApplicationEntity)2 Application (io.gravitee.repository.management.model.Application)2 Date (java.util.Date)2 ArgumentMatcher (org.mockito.ArgumentMatcher)2 PropertyFilter (com.fasterxml.jackson.databind.ser.PropertyFilter)1 SimpleFilterProvider (com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider)1 Path (io.gravitee.definition.model.Path)1 Policy (io.gravitee.definition.model.Policy)1 UpdateApiEntity (io.gravitee.management.model.UpdateApiEntity)1