Search in sources :

Example 1 with MembershipService

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());
}
Also used : Response(javax.ws.rs.core.Response) MembershipService(io.gravitee.rest.api.service.MembershipService) List(java.util.List) Test(org.junit.Test)

Example 2 with MembershipService

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());
}
Also used : Response(javax.ws.rs.core.Response) MembershipService(io.gravitee.rest.api.service.MembershipService) Test(org.junit.Test)

Example 3 with MembershipService

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())));
}
Also used : MembershipReply(io.gravitee.cockpit.api.command.membership.MembershipReply) UserEntity(io.gravitee.rest.api.model.UserEntity) RoleEntity(io.gravitee.rest.api.model.RoleEntity) MembershipService(io.gravitee.rest.api.service.MembershipService) MembershipPayload(io.gravitee.cockpit.api.command.membership.MembershipPayload) List(java.util.List) MembershipCommand(io.gravitee.cockpit.api.command.membership.MembershipCommand) Test(org.junit.Test)

Example 4 with MembershipService

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())));
}
Also used : MembershipReply(io.gravitee.cockpit.api.command.membership.MembershipReply) UserEntity(io.gravitee.rest.api.model.UserEntity) RoleEntity(io.gravitee.rest.api.model.RoleEntity) MembershipService(io.gravitee.rest.api.service.MembershipService) MembershipPayload(io.gravitee.cockpit.api.command.membership.MembershipPayload) List(java.util.List) MembershipCommand(io.gravitee.cockpit.api.command.membership.MembershipCommand) Test(org.junit.Test)

Example 5 with MembershipService

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());
}
Also used : Response(javax.ws.rs.core.Response) MembershipService(io.gravitee.rest.api.service.MembershipService) ApplicationMembership(io.gravitee.rest.api.management.rest.model.ApplicationMembership) Test(org.junit.Test)

Aggregations

MembershipService (io.gravitee.rest.api.service.MembershipService)14 Test (org.junit.Test)14 Response (javax.ws.rs.core.Response)11 List (java.util.List)5 GroupMembership (io.gravitee.rest.api.management.rest.model.GroupMembership)4 RoleEntity (io.gravitee.rest.api.model.RoleEntity)4 MembershipCommand (io.gravitee.cockpit.api.command.membership.MembershipCommand)3 MembershipPayload (io.gravitee.cockpit.api.command.membership.MembershipPayload)3 MembershipReply (io.gravitee.cockpit.api.command.membership.MembershipReply)3 UserEntity (io.gravitee.rest.api.model.UserEntity)3 ApiMembership (io.gravitee.rest.api.management.rest.model.ApiMembership)1 ApplicationMembership (io.gravitee.rest.api.management.rest.model.ApplicationMembership)1 RoleMembership (io.gravitee.rest.api.management.rest.model.RoleMembership)1 MemberEntity (io.gravitee.rest.api.model.MemberEntity)1