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