Search in sources :

Example 6 with RoleEntity

use of io.gravitee.rest.api.model.RoleEntity in project gravitee-management-rest-api by gravitee-io.

the class RoleService_UpdateTest method shouldUpdate.

@Test
public void shouldUpdate() throws TechnicalException {
    UpdateRoleEntity updateRoleEntityMock = mock(UpdateRoleEntity.class);
    when(updateRoleEntityMock.getId()).thenReturn("updated_mock_role");
    when(updateRoleEntityMock.getName()).thenReturn("update mock role");
    when(updateRoleEntityMock.getScope()).thenReturn(io.gravitee.rest.api.model.permissions.RoleScope.ENVIRONMENT);
    when(updateRoleEntityMock.getPermissions()).thenReturn(Collections.singletonMap(DOCUMENTATION.getName(), new char[] { RolePermissionAction.CREATE.getId() }));
    Role roleMock = mock(Role.class);
    when(roleMock.getId()).thenReturn("updated_mock_role");
    when(roleMock.getName()).thenReturn("new mock role");
    when(roleMock.getScope()).thenReturn(RoleScope.ENVIRONMENT);
    when(roleMock.getPermissions()).thenReturn(new int[] { 3008 });
    when(roleMock.getReferenceType()).thenReturn(RoleReferenceType.ORGANIZATION);
    when(roleMock.getReferenceId()).thenReturn("orga#1");
    when(mockRoleRepository.update(argThat(role -> role.getReferenceId().equals("orga#1") && role.getReferenceType() == RoleReferenceType.ORGANIZATION))).thenReturn(roleMock);
    when(mockRoleRepository.findById("updated_mock_role")).thenReturn(Optional.of(roleMock));
    RoleEntity entity = roleService.update(updateRoleEntityMock);
    assertNotNull("no entoty created", entity);
    assertEquals("invalid id", "updated_mock_role", entity.getId());
    assertEquals("invalid name", "new mock role", entity.getName());
    assertEquals("invalid scope", io.gravitee.rest.api.model.permissions.RoleScope.ENVIRONMENT, entity.getScope());
    assertFalse("no permissions found", entity.getPermissions().isEmpty());
    assertTrue("invalid Permission name", entity.getPermissions().containsKey(DOCUMENTATION.getName()));
    char[] perms = entity.getPermissions().get(DOCUMENTATION.getName());
    assertEquals("not enough permissions", 1, perms.length);
    assertEquals("not the good permission", RolePermissionAction.CREATE.getId(), perms[0]);
}
Also used : Role(io.gravitee.repository.management.model.Role) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) InjectMocks(org.mockito.InjectMocks) RolePermissionAction(io.gravitee.rest.api.model.permissions.RolePermissionAction) RoleScope(io.gravitee.repository.management.model.RoleScope) RoleRepository(io.gravitee.repository.management.api.RoleRepository) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) Mock(org.mockito.Mock) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) UpdateRoleEntity(io.gravitee.rest.api.model.UpdateRoleEntity) RunWith(org.junit.runner.RunWith) AuditService(io.gravitee.rest.api.service.AuditService) RoleNotFoundException(io.gravitee.rest.api.service.exceptions.RoleNotFoundException) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) DOCUMENTATION(io.gravitee.rest.api.model.permissions.EnvironmentPermission.DOCUMENTATION) RoleEntity(io.gravitee.rest.api.model.RoleEntity) RoleServiceImpl(io.gravitee.rest.api.service.impl.RoleServiceImpl) Optional(java.util.Optional) Assert(org.junit.Assert) Collections(java.util.Collections) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Role(io.gravitee.repository.management.model.Role) RoleReferenceType(io.gravitee.repository.management.model.RoleReferenceType) Mockito.mock(org.mockito.Mockito.mock) UpdateRoleEntity(io.gravitee.rest.api.model.UpdateRoleEntity) RoleEntity(io.gravitee.rest.api.model.RoleEntity) UpdateRoleEntity(io.gravitee.rest.api.model.UpdateRoleEntity) Test(org.junit.Test)

Example 7 with RoleEntity

use of io.gravitee.rest.api.model.RoleEntity in project gravitee-management-rest-api by gravitee-io.

the class MemberMapperTest method testConvert.

@Test
public void testConvert() {
    Instant now = Instant.now();
    Date nowDate = Date.from(now);
    // init
    RoleEntity ownerRoleEntity = new RoleEntity();
    ownerRoleEntity.setName("OWNER");
    memberEntity = new MemberEntity();
    memberEntity.setCreatedAt(nowDate);
    memberEntity.setDisplayName(MEMBER_DISPLAYNAME);
    memberEntity.setEmail(MEMBER_EMAIL);
    memberEntity.setId(MEMBER_ID);
    memberEntity.setRoles(Arrays.asList(ownerRoleEntity));
    memberEntity.setUpdatedAt(nowDate);
    UserEntity userEntity = Mockito.mock(UserEntity.class);
    when(userEntity.getDisplayName()).thenReturn(MEMBER_DISPLAYNAME);
    when(userEntity.getEmail()).thenReturn(MEMBER_EMAIL);
    when(userEntity.getId()).thenReturn(MEMBER_ID);
    when(uriInfo.getBaseUriBuilder()).thenReturn(UriBuilder.fromPath(""));
    when(userService.findById(MEMBER_ID)).thenReturn(userEntity);
    when(userMapper.convert(userEntity)).thenCallRealMethod();
    when(userMapper.computeUserLinks(anyString(), any())).thenCallRealMethod();
    // Test
    Member responseMember = memberMapper.convert(memberEntity, uriInfo);
    assertNotNull(responseMember);
    assertEquals(now.toEpochMilli(), responseMember.getCreatedAt().toInstant().toEpochMilli());
    assertNull(responseMember.getId());
    assertEquals("OWNER", responseMember.getRole());
    assertEquals(now.toEpochMilli(), responseMember.getUpdatedAt().toInstant().toEpochMilli());
    User user = responseMember.getUser();
    assertNotNull(user);
    assertEquals(MEMBER_DISPLAYNAME, user.getDisplayName());
    assertEquals(MEMBER_EMAIL, user.getEmail());
    assertEquals(MEMBER_ID, user.getId());
    assertEquals("environments/DEFAULT/users/" + MEMBER_ID + "/avatar?", user.getLinks().getAvatar());
}
Also used : RoleEntity(io.gravitee.rest.api.model.RoleEntity) User(io.gravitee.rest.api.portal.rest.model.User) Instant(java.time.Instant) MemberEntity(io.gravitee.rest.api.model.MemberEntity) Member(io.gravitee.rest.api.portal.rest.model.Member) Date(java.util.Date) UserEntity(io.gravitee.rest.api.model.UserEntity) Test(org.junit.Test)

Example 8 with RoleEntity

use of io.gravitee.rest.api.model.RoleEntity in project gravitee-management-rest-api by gravitee-io.

the class ApplicationService_FindAllTest method shouldTryFindAll.

@Test
public void shouldTryFindAll() throws Exception {
    Application application = new Application();
    application.setId("appId");
    application.setType(ApplicationType.SIMPLE);
    application.setStatus(ApplicationStatus.ACTIVE);
    when(applicationRepository.findAllByEnvironment(eq("DEFAULT"), eq(ApplicationStatus.ACTIVE))).thenReturn(new HashSet<>(Collections.singletonList(application)));
    when(roleService.findPrimaryOwnerRoleByOrganization(any(), any())).thenReturn(new RoleEntity());
    when(membershipService.getMembershipsByReferencesAndRole(any(), any(), any())).thenReturn(new HashSet<>(Collections.singletonList(new MembershipEntity())));
    when(userService.findByIds(any())).thenReturn(Collections.emptySet());
    Set<ApplicationListItem> set = applicationService.findAll();
    assertThat(set).hasSize(1);
    verify(applicationRepository, times(1)).findAllByEnvironment("DEFAULT", ApplicationStatus.ACTIVE);
}
Also used : RoleEntity(io.gravitee.rest.api.model.RoleEntity) MembershipEntity(io.gravitee.rest.api.model.MembershipEntity) ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) Application(io.gravitee.repository.management.model.Application) Test(org.junit.Test)

Example 9 with RoleEntity

use of io.gravitee.rest.api.model.RoleEntity 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 10 with RoleEntity

use of io.gravitee.rest.api.model.RoleEntity 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)

Aggregations

RoleEntity (io.gravitee.rest.api.model.RoleEntity)29 Test (org.junit.Test)20 UserEntity (io.gravitee.rest.api.model.UserEntity)13 Membership (io.gravitee.repository.management.model.Membership)8 MembershipService (io.gravitee.rest.api.service.MembershipService)7 Role (io.gravitee.repository.management.model.Role)6 MembershipPayload (io.gravitee.cockpit.api.command.membership.MembershipPayload)5 MembershipReply (io.gravitee.cockpit.api.command.membership.MembershipReply)5 NewRoleEntity (io.gravitee.rest.api.model.NewRoleEntity)5 UpdateRoleEntity (io.gravitee.rest.api.model.UpdateRoleEntity)5 Response (javax.ws.rs.core.Response)5 MembershipCommand (io.gravitee.cockpit.api.command.membership.MembershipCommand)4 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)4 MembershipReferenceType (io.gravitee.rest.api.model.MembershipReferenceType)4 Instant (java.time.Instant)4 List (java.util.List)4 JWT (com.auth0.jwt.JWT)3 Algorithm (com.auth0.jwt.algorithms.Algorithm)3 Maps (io.gravitee.common.util.Maps)3 UserDetails (io.gravitee.rest.api.idp.api.authentication.UserDetails)3