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());
}
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);
}
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);
}
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);
}
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);
}
Aggregations