use of io.gravitee.rest.api.model.RoleEntity in project gravitee-management-rest-api by gravitee-io.
the class RoleService_FindByIdTest method test_int_to_CRUD.
private void test_int_to_CRUD(int perm, RolePermissionAction... action) throws TechnicalException {
Role roleMock = mock(Role.class);
when(roleMock.getScope()).thenReturn(RoleScope.ENVIRONMENT);
when(roleMock.getId()).thenReturn("id");
when(roleMock.getName()).thenReturn("name");
when(roleMock.getPermissions()).thenReturn(new int[] { perm });
when(mockRoleRepository.findById("id")).thenReturn(Optional.of(roleMock));
RoleEntity entity = roleService.findById("id");
assertNotNull("no entity found", entity);
assertEquals("invalid id", "id", entity.getId());
assertEquals("invalid name", "name", 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", action.length, perms.length);
for (RolePermissionAction rolePermissionAction : action) {
assertTrue("not the good permission", Arrays.asList(ArrayUtils.toObject(perms)).contains(rolePermissionAction.getId()));
}
}
use of io.gravitee.rest.api.model.RoleEntity in project gravitee-management-rest-api by gravitee-io.
the class MembershipService_FindUserMembershipTest method shouldGetMembershipForGivenSource.
@Test
public void shouldGetMembershipForGivenSource() throws Exception {
RoleEntity roleApi = new RoleEntity();
roleApi.setId("roleApi");
roleApi.setName("PO");
roleApi.setScope(RoleScope.API);
RoleEntity roleApp = new RoleEntity();
roleApp.setId("roleApp");
roleApp.setName("PO");
roleApp.setScope(RoleScope.API);
when(mockRoleService.findAll()).thenReturn(asList(roleApi, roleApp));
Membership mApi = mock(Membership.class);
when(mApi.getReferenceId()).thenReturn("api-id1");
when(mApi.getRoleId()).thenReturn("roleApi");
when(mApi.getSource()).thenReturn("oauth2");
Membership mApp = mock(Membership.class);
when(mApp.getReferenceId()).thenReturn("app-id1");
when(mApp.getRoleId()).thenReturn("roleApp");
when(mApp.getSource()).thenReturn("oauth2");
when(mockMembershipRepository.findByMemberIdAndMemberTypeAndReferenceTypeAndSource(eq(USER_ID), eq(MembershipMemberType.USER), eq(MembershipReferenceType.GROUP), eq("oauth2"))).thenReturn(Sets.newHashSet(mApi, mApp));
List<UserMembership> references = membershipService.findUserMembershipBySource(io.gravitee.rest.api.model.MembershipReferenceType.GROUP, USER_ID, "oauth2");
assertFalse(references.isEmpty());
assertEquals(2, references.size());
assertNotEquals(references.get(0).getReference(), references.get(1).getReference());
assertTrue(references.get(0).getReference().equals("api-id1") || references.get(0).getReference().equals("app-id1"));
assertTrue(references.get(1).getReference().equals("api-id1") || references.get(1).getReference().equals("app-id1"));
}
use of io.gravitee.rest.api.model.RoleEntity in project gravitee-management-rest-api by gravitee-io.
the class MembershipService_FindUserMembershipTest method shouldGetApiWithoutGroups.
@Test
public void shouldGetApiWithoutGroups() throws Exception {
RoleEntity roleEntity = new RoleEntity();
roleEntity.setId("role");
roleEntity.setName("PO");
roleEntity.setScope(RoleScope.API);
when(mockRoleService.findByScope(any())).thenReturn(Collections.singletonList(roleEntity));
Membership mApi = mock(Membership.class);
when(mApi.getReferenceId()).thenReturn("api-id1");
when(mApi.getRoleId()).thenReturn("role");
when(mockMembershipRepository.findByMemberIdAndMemberTypeAndReferenceType(eq(USER_ID), eq(MembershipMemberType.USER), eq(MembershipReferenceType.API))).thenReturn(Collections.singleton(mApi));
doReturn(Collections.emptySet()).when(mockGroupService).findByUser(USER_ID);
List<UserMembership> references = membershipService.findUserMembership(io.gravitee.rest.api.model.MembershipReferenceType.API, USER_ID);
assertFalse(references.isEmpty());
assertEquals(1, references.size());
assertEquals("api-id1", references.get(0).getReference());
assertEquals("API", references.get(0).getType());
}
use of io.gravitee.rest.api.model.RoleEntity in project gravitee-management-rest-api by gravitee-io.
the class MembershipService_GetMembersTest method shouldGetMembersWithMembership.
@Test
public void shouldGetMembersWithMembership() throws Exception {
Membership membership = new Membership();
membership.setReferenceId(API_ID);
membership.setCreatedAt(new Date());
membership.setUpdatedAt(membership.getCreatedAt());
membership.setReferenceType(MembershipReferenceType.API);
membership.setRoleId("API_PRIMARY_OWNER");
membership.setMemberId("user-id");
membership.setMemberType(MembershipMemberType.USER);
UserEntity userEntity = new UserEntity();
userEntity.setId(membership.getMemberId());
userEntity.setFirstname("John");
userEntity.setLastname("Doe");
RoleEntity po = mock(RoleEntity.class);
po.setName(SystemRole.PRIMARY_OWNER.name());
List<String> memberIds = Collections.singletonList(membership.getMemberId());
Set<UserEntity> userEntities = Collections.singleton(userEntity);
when(membershipRepository.findByReferencesAndRoleId(MembershipReferenceType.API, Collections.singletonList(API_ID), "API_PRIMARY_OWNER")).thenReturn(Collections.singleton(membership));
when(userService.findByIds(memberIds, false)).thenReturn(userEntities);
when(roleService.findById("API_PRIMARY_OWNER")).thenReturn(po);
Set<MemberEntity> members = membershipService.getMembersByReferenceAndRole(io.gravitee.rest.api.model.MembershipReferenceType.API, API_ID, "API_PRIMARY_OWNER");
Assert.assertNotNull(members);
Assert.assertFalse("members must not be empty", members.isEmpty());
verify(membershipRepository, times(1)).findByReferencesAndRoleId(MembershipReferenceType.API, Collections.singletonList(API_ID), "API_PRIMARY_OWNER");
verify(userService, times(1)).findByIds(memberIds, false);
}
Aggregations