use of io.gravitee.rest.api.service.MembershipService in project gravitee-management-rest-api by gravitee-io.
the class ApplicationMembersResourceTest method shouldTransferOwnerShipWithWrongRole.
@Test
public void shouldTransferOwnerShipWithWrongRole() {
doReturn(Optional.empty()).when(roleService).findByScopeAndName(any(), any());
TransferOwnershipInput input = new TransferOwnershipInput().newPrimaryOwnerId(MEMBER_1).primaryOwnerNewrole("OWNER");
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());
assertNotNull(roleCaptor.getValue());
assertTrue(roleCaptor.getValue().isEmpty());
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 shouldUpdateMember.
@Test
public void shouldUpdateMember() {
MemberInput memberInput = new MemberInput().role("USER");
final Response response = target(APPLICATION).path("members").path(MEMBER_2).request().put(Entity.json(memberInput));
assertEquals(HttpStatusCode.OK_200, response.getStatus());
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).updateRoleToMemberOnReference(memberShipRefCaptor.capture(), memberShipUserCaptor.capture(), memberShipRoleCaptor.capture());
assertEquals(APPLICATION, memberShipRefCaptor.getValue().getId());
assertEquals("USER", memberShipRoleCaptor.getValue().getName());
assertEquals(MEMBER_2, memberShipUserCaptor.getValue().getMemberId());
}
use of io.gravitee.rest.api.service.MembershipService in project gravitee-management-rest-api by gravitee-io.
the class MembershipCommandHandlerTest method handleWithRole.
@Test
public void handleWithRole() {
MembershipPayload membershipPayload = new MembershipPayload();
membershipPayload.setUserId("user#1");
membershipPayload.setOrganizationId("orga#1");
membershipPayload.setReferenceType(MembershipReferenceType.ORGANIZATION.name());
membershipPayload.setReferenceId("orga#1");
membershipPayload.setRole("ORGANIZATION_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.ORGANIZATION);
role.setName("ADMIN");
when(userService.findBySource(COCKPIT_SOURCE, membershipPayload.getUserId(), false)).thenReturn(user);
when(roleService.findByScopeAndName(RoleScope.ORGANIZATION, "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.ORGANIZATION, 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())));
}
use of io.gravitee.rest.api.service.MembershipService in project gravitee-management-rest-api by gravitee-io.
the class MembershipCommandHandlerTest method handleWithUserRole.
@Test
public void handleWithUserRole() {
MembershipPayload membershipPayload = new MembershipPayload();
membershipPayload.setUserId("user#1");
membershipPayload.setOrganizationId("orga#1");
membershipPayload.setReferenceType(MembershipReferenceType.ENVIRONMENT.name());
membershipPayload.setReferenceId("env#1");
membershipPayload.setRole("ENVIRONMENT_USER");
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("USER");
when(userService.findBySource(COCKPIT_SOURCE, membershipPayload.getUserId(), false)).thenReturn(user);
when(roleService.findByScopeAndName(RoleScope.ENVIRONMENT, "USER")).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())));
}
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() {
Mockito.reset(membershipService);
ApplicationMembership applicationMembership = new ApplicationMembership();
applicationMembership.setId(MEMBER_1);
applicationMembership.setRole("my-application-membership-role");
final Response response = envTarget(APPLICATION).path("members").request().post(Entity.json(applicationMembership));
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(APPLICATION, memberShipRefCaptor.getValue().getId());
assertEquals("my-application-membership-role", memberShipRoleCaptor.getValue().getName());
assertEquals(MEMBER_1, memberShipUserCaptor.getValue().getMemberId());
}
Aggregations