Search in sources :

Example 11 with MembershipService

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

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

Example 13 with MembershipService

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

Example 14 with MembershipService

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

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