use of io.gravitee.rest.api.service.MembershipService in project gravitee-management-rest-api by gravitee-io.
the class ApiMembersResourceTest method shouldCreateMember.
@Test
public void shouldCreateMember() {
ApiMembership apiMembership = new ApiMembership();
apiMembership.setId(MEMBER_1);
apiMembership.setRole("my-api-membership-role");
final Response response = envTarget(API).path("members").request().post(Entity.json(apiMembership));
assertEquals(HttpStatusCode.CREATED_201, response.getStatus());
assertNull(response.getHeaders().getFirst(HttpHeaders.LOCATION));
ArgumentCaptor<MembershipService.MembershipReference> memberShipRefCaptor = ArgumentCaptor.forClass(MembershipService.MembershipReference.class);
ArgumentCaptor<MembershipService.MembershipRole> memberShipRoleCaptor = ArgumentCaptor.forClass(MembershipService.MembershipRole.class);
ArgumentCaptor<MembershipService.MembershipMember> memberShipUserCaptor = ArgumentCaptor.forClass(MembershipService.MembershipMember.class);
Mockito.verify(membershipService).addRoleToMemberOnReference(memberShipRefCaptor.capture(), memberShipUserCaptor.capture(), memberShipRoleCaptor.capture());
assertEquals(API, memberShipRefCaptor.getValue().getId());
assertEquals("my-api-membership-role", memberShipRoleCaptor.getValue().getName());
assertEquals(MEMBER_1, memberShipUserCaptor.getValue().getMemberId());
}
use of io.gravitee.rest.api.service.MembershipService in project gravitee-management-rest-api by gravitee-io.
the class ApplicationMembersResourceTest method shouldCreateMember.
@Test
public void shouldCreateMember() {
MemberInput memberInput = new MemberInput().role("USER").user(MEMBER_1);
MemberEntity returnedMemberEntity = mock(MemberEntity.class);
doReturn(MEMBER_1).when(returnedMemberEntity).getId();
doReturn(returnedMemberEntity).when(membershipService).addRoleToMemberOnReference(any(), any(), any());
final Response response = target(APPLICATION).path("members").request().post(Entity.json(memberInput));
assertEquals(HttpStatusCode.CREATED_201, response.getStatus());
assertEquals(target(APPLICATION).path("members").path(MEMBER_1).getUri().toString(), response.getHeaders().getFirst(HttpHeaders.LOCATION));
ArgumentCaptor<MembershipService.MembershipReference> memberShipRefCaptor = ArgumentCaptor.forClass(MembershipService.MembershipReference.class);
ArgumentCaptor<MembershipService.MembershipRole> memberShipRoleCaptor = ArgumentCaptor.forClass(MembershipService.MembershipRole.class);
ArgumentCaptor<MembershipService.MembershipMember> memberShipUserCaptor = ArgumentCaptor.forClass(MembershipService.MembershipMember.class);
Mockito.verify(membershipService).addRoleToMemberOnReference(memberShipRefCaptor.capture(), memberShipUserCaptor.capture(), memberShipRoleCaptor.capture());
assertEquals(APPLICATION, memberShipRefCaptor.getValue().getId());
assertEquals("USER", memberShipRoleCaptor.getValue().getName());
assertEquals(MEMBER_1, memberShipUserCaptor.getValue().getMemberId());
}
use of io.gravitee.rest.api.service.MembershipService in project gravitee-management-rest-api by gravitee-io.
the class ApplicationMembersResourceTest method shouldTransferOwnerShip.
@Test
public void shouldTransferOwnerShip() {
RoleEntity mockRoleEntity = new RoleEntity();
TransferOwnershipInput input = new TransferOwnershipInput().newPrimaryOwnerId(MEMBER_1).primaryOwnerNewrole("OWNER");
doReturn(Optional.of(mockRoleEntity)).when(roleService).findByScopeAndName(any(), any());
final Response response = target(APPLICATION).path("members").path("_transfer_ownership").request().post(Entity.json(input));
assertEquals(HttpStatusCode.NO_CONTENT_204, response.getStatus());
ArgumentCaptor<String> applicationCaptor = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<List<RoleEntity>> roleCaptor = ArgumentCaptor.forClass(List.class);
ArgumentCaptor<MembershipService.MembershipMember> memberShipUserCaptor = ArgumentCaptor.forClass(MembershipService.MembershipMember.class);
Mockito.verify(membershipService).transferApplicationOwnership(applicationCaptor.capture(), memberShipUserCaptor.capture(), roleCaptor.capture());
assertEquals(APPLICATION, applicationCaptor.getValue());
assertEquals(mockRoleEntity, roleCaptor.getValue().get(0));
assertEquals(MEMBER_1, memberShipUserCaptor.getValue().getMemberId());
}
use of io.gravitee.rest.api.service.MembershipService in project gravitee-management-rest-api by gravitee-io.
the class MembershipCommandHandlerTest method handleWithAdminRole.
@Test
public void handleWithAdminRole() {
MembershipPayload membershipPayload = new MembershipPayload();
membershipPayload.setUserId("user#1");
membershipPayload.setOrganizationId("orga#1");
membershipPayload.setReferenceType(MembershipReferenceType.ENVIRONMENT.name());
membershipPayload.setReferenceId("env#1");
membershipPayload.setRole("ENVIRONMENT_PRIMARY_OWNER");
MembershipCommand command = new MembershipCommand(membershipPayload);
UserEntity user = new UserEntity();
user.setId(UUID.random().toString());
RoleEntity role = new RoleEntity();
role.setId(UUID.random().toString());
role.setScope(RoleScope.ENVIRONMENT);
role.setName("ADMIN");
when(userService.findBySource(COCKPIT_SOURCE, membershipPayload.getUserId(), false)).thenReturn(user);
when(roleService.findByScopeAndName(RoleScope.ENVIRONMENT, "ADMIN")).thenReturn(Optional.of(role));
TestObserver<MembershipReply> obs = cut.handle(command).test();
obs.awaitTerminalEvent();
obs.assertNoErrors();
obs.assertValue(reply -> reply.getCommandId().equals(command.getId()) && reply.getCommandStatus().equals(CommandStatus.SUCCEEDED));
ArgumentCaptor<MembershipService.MembershipReference> membershipReference = ArgumentCaptor.forClass(MembershipService.MembershipReference.class);
ArgumentCaptor<MembershipService.MembershipMember> membershipMember = ArgumentCaptor.forClass(MembershipService.MembershipMember.class);
ArgumentCaptor<List<MembershipService.MembershipRole>> membershipRoles = ArgumentCaptor.forClass(List.class);
verify(membershipService).updateRolesToMemberOnReference(membershipReference.capture(), membershipMember.capture(), membershipRoles.capture(), eq(COCKPIT_SOURCE), eq(false));
assertEquals(MembershipReferenceType.ENVIRONMENT, membershipReference.getValue().getType());
assertEquals(membershipPayload.getReferenceId(), membershipReference.getValue().getId());
assertEquals(MembershipMemberType.USER, membershipMember.getValue().getMemberType());
assertEquals(user.getId(), membershipMember.getValue().getMemberId());
assertTrue(membershipRoles.getValue().size() == 1 && membershipRoles.getValue().stream().allMatch(membershipRole -> membershipRole.getScope() == role.getScope() && membershipRole.getName().equals(role.getName())));
}
Aggregations