Search in sources :

Example 1 with MembershipEntity

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

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

the class AbstractResource method canReadApi.

protected void canReadApi(final String api) {
    if (!isAdmin()) {
        // get memberships of the current user
        List<MembershipEntity> memberships = retrieveApiMembership().collect(Collectors.toList());
        Set<String> groups = memberships.stream().filter(m -> GROUP.equals(m.getReferenceType())).map(m -> m.getReferenceId()).collect(Collectors.toSet());
        Set<String> directMembers = memberships.stream().filter(m -> API.equals(m.getReferenceType())).map(m -> m.getReferenceId()).collect(Collectors.toSet());
        // if the current user is member of the API, continue
        if (directMembers.contains(api)) {
            return;
        }
        // fetch group memberships
        final ApiQuery apiQuery = new ApiQuery();
        apiQuery.setGroups(new ArrayList<>(groups));
        apiQuery.setIds(Collections.singletonList(api));
        final Collection<String> strings = apiService.searchIds(apiQuery);
        final boolean canReadAPI = strings.contains(api);
        if (!canReadAPI) {
            throw new ForbiddenAccessException();
        }
    }
}
Also used : ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) RolePermissionAction(io.gravitee.rest.api.model.permissions.RolePermissionAction) io.gravitee.rest.api.service(io.gravitee.rest.api.service) java.util(java.util) MembershipEntity(io.gravitee.rest.api.model.MembershipEntity) Context(javax.ws.rs.core.Context) USER(io.gravitee.rest.api.model.MembershipMemberType.USER) ForbiddenAccessException(io.gravitee.rest.api.service.exceptions.ForbiddenAccessException) SecurityContext(javax.ws.rs.core.SecurityContext) API(io.gravitee.rest.api.model.MembershipReferenceType.API) RoleScope(io.gravitee.rest.api.model.permissions.RoleScope) UserDetails(io.gravitee.rest.api.idp.api.authentication.UserDetails) Collectors(java.util.stream.Collectors) Inject(javax.inject.Inject) Stream(java.util.stream.Stream) UriBuilder(javax.ws.rs.core.UriBuilder) URI(java.net.URI) UriInfo(javax.ws.rs.core.UriInfo) SecurityContextHolder(org.springframework.security.core.context.SecurityContextHolder) SystemRole(io.gravitee.rest.api.model.permissions.SystemRole) GROUP(io.gravitee.rest.api.model.MembershipReferenceType.GROUP) RolePermission(io.gravitee.rest.api.model.permissions.RolePermission) MembershipEntity(io.gravitee.rest.api.model.MembershipEntity) ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) ForbiddenAccessException(io.gravitee.rest.api.service.exceptions.ForbiddenAccessException)

Example 3 with MembershipEntity

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

the class ApplicationService_UpdateTest method shouldUpdate.

@Test
public void shouldUpdate() throws TechnicalException {
    ApplicationSettings settings = new ApplicationSettings();
    SimpleApplicationSettings clientSettings = new SimpleApplicationSettings();
    clientSettings.setClientId(CLIENT_ID);
    settings.setApp(clientSettings);
    when(existingApplication.getSettings()).thenReturn(settings);
    when(applicationRepository.findById(APPLICATION_ID)).thenReturn(Optional.of(application));
    when(application.getName()).thenReturn(APPLICATION_NAME);
    when(application.getStatus()).thenReturn(ApplicationStatus.ACTIVE);
    when(existingApplication.getName()).thenReturn(APPLICATION_NAME);
    when(existingApplication.getDescription()).thenReturn("My description");
    when(application.getType()).thenReturn(ApplicationType.SIMPLE);
    when(applicationRepository.update(any())).thenReturn(application);
    when(roleService.findPrimaryOwnerRoleByOrganization(any(), any())).thenReturn(mock(RoleEntity.class));
    MembershipEntity po = new MembershipEntity();
    po.setMemberId(USER_NAME);
    po.setMemberType(MembershipMemberType.USER);
    po.setReferenceId(APPLICATION_ID);
    po.setReferenceType(MembershipReferenceType.APPLICATION);
    po.setRoleId("APPLICATION_PRIMARY_OWNER");
    when(membershipService.getMembershipsByReferencesAndRole(any(), any(), any())).thenReturn(Collections.singleton(po));
    final ApplicationEntity applicationEntity = applicationService.update(APPLICATION_ID, existingApplication);
    verify(applicationRepository).update(argThat(application -> APPLICATION_NAME.equals(application.getName()) && application.getUpdatedAt() != null));
    assertNotNull(applicationEntity);
    assertEquals(APPLICATION_NAME, applicationEntity.getName());
}
Also used : RoleEntity(io.gravitee.rest.api.model.RoleEntity) ArgumentMatchers(org.mockito.ArgumentMatchers) Mock(org.mockito.Mock) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) RunWith(org.junit.runner.RunWith) HashMap(java.util.HashMap) ClientRegistrationService(io.gravitee.rest.api.service.configuration.application.ClientRegistrationService) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException) Sets(org.mockito.internal.util.collections.Sets) UserService(io.gravitee.rest.api.service.UserService) UpdateApplicationEntity(io.gravitee.rest.api.model.UpdateApplicationEntity) Map(java.util.Map) ApplicationRepository(io.gravitee.repository.management.api.ApplicationRepository) MembershipMemberType(io.gravitee.rest.api.model.MembershipMemberType) ApplicationSettings(io.gravitee.rest.api.model.application.ApplicationSettings) ParameterService(io.gravitee.rest.api.service.ParameterService) InjectMocks(org.mockito.InjectMocks) MembershipEntity(io.gravitee.rest.api.model.MembershipEntity) Assert.assertNotNull(org.junit.Assert.assertNotNull) AuditService(io.gravitee.rest.api.service.AuditService) ClientIdAlreadyExistsException(io.gravitee.rest.api.service.exceptions.ClientIdAlreadyExistsException) Test(org.junit.Test) SimpleApplicationSettings(io.gravitee.rest.api.model.application.SimpleApplicationSettings) RoleEntity(io.gravitee.rest.api.model.RoleEntity) Mockito(org.mockito.Mockito) MembershipReferenceType(io.gravitee.rest.api.model.MembershipReferenceType) SubscriptionService(io.gravitee.rest.api.service.SubscriptionService) io.gravitee.repository.management.model(io.gravitee.repository.management.model) Optional(java.util.Optional) ApplicationNotFoundException(io.gravitee.rest.api.service.exceptions.ApplicationNotFoundException) Collections(java.util.Collections) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Assert.assertEquals(org.junit.Assert.assertEquals) ApplicationEntity(io.gravitee.rest.api.model.ApplicationEntity) ApplicationServiceImpl(io.gravitee.rest.api.service.impl.ApplicationServiceImpl) ApplicationSettings(io.gravitee.rest.api.model.application.ApplicationSettings) SimpleApplicationSettings(io.gravitee.rest.api.model.application.SimpleApplicationSettings) SimpleApplicationSettings(io.gravitee.rest.api.model.application.SimpleApplicationSettings) MembershipEntity(io.gravitee.rest.api.model.MembershipEntity) UpdateApplicationEntity(io.gravitee.rest.api.model.UpdateApplicationEntity) ApplicationEntity(io.gravitee.rest.api.model.ApplicationEntity) Test(org.junit.Test)

Example 4 with MembershipEntity

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

the class ApiService_FindByIdTest method shouldFindById.

@Test
public void shouldFindById() throws TechnicalException {
    api = new Api();
    api.setId(API_ID);
    api.setEnvironmentId("DEFAULT");
    when(apiRepository.findById(API_ID)).thenReturn(Optional.of(api));
    MembershipEntity po = new MembershipEntity();
    po.setMemberId(USER_NAME);
    when(membershipService.getPrimaryOwner(MembershipReferenceType.API, API_ID)).thenReturn(po);
    when(userService.findById(USER_NAME)).thenReturn(mock(UserEntity.class));
    final ApiEntity apiEntity = apiService.findById(API_ID);
    assertNotNull(apiEntity);
}
Also used : MembershipEntity(io.gravitee.rest.api.model.MembershipEntity) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Api(io.gravitee.repository.management.model.Api) UserEntity(io.gravitee.rest.api.model.UserEntity) Test(org.junit.Test)

Example 5 with MembershipEntity

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

the class TaskServiceTest method shouldFindAll.

@Test
public void shouldFindAll() throws TechnicalException {
    MembershipEntity m1 = new MembershipEntity();
    m1.setId("1");
    m1.setReferenceId("api1");
    m1.setReferenceType(MembershipReferenceType.API);
    m1.setRoleId("API_PO");
    MembershipEntity m2 = new MembershipEntity();
    m2.setId("2");
    m2.setReferenceId("api2");
    m2.setReferenceType(MembershipReferenceType.API);
    m2.setRoleId("API_USER");
    Map<String, char[]> withPerm = new HashMap<>();
    withPerm.put("SUBSCRIPTION", new char[] { 'C', 'R', 'U', 'D' });
    Map<String, char[]> withoutPerm = new HashMap<>();
    withoutPerm.put("SUBSCRIPTION", new char[] { 'C', 'R', 'D' });
    RoleEntity roleEntityWithPerm = new RoleEntity();
    roleEntityWithPerm.setName("PO");
    roleEntityWithPerm.setPermissions(withPerm);
    roleEntityWithPerm.setScope(io.gravitee.rest.api.model.permissions.RoleScope.API);
    RoleEntity roleEntityWithoutPerm = new RoleEntity();
    roleEntityWithoutPerm.setName("USER");
    roleEntityWithoutPerm.setPermissions(withoutPerm);
    roleEntityWithoutPerm.setScope(io.gravitee.rest.api.model.permissions.RoleScope.API);
    when(roleService.findById("API_PO")).thenReturn(roleEntityWithPerm);
    when(roleService.findById("API_USER")).thenReturn(roleEntityWithoutPerm);
    when(promotionTasksService.getPromotionTasks(any())).thenReturn(emptyList());
    Set<MembershipEntity> memberships = new HashSet<>();
    memberships.add(m1);
    memberships.add(m2);
    when(membershipService.getMembershipsByMemberAndReference(any(), any(), any())).thenReturn(memberships);
    when(userService.search(any(UserCriteria.class), any())).thenReturn(new Page<>(emptyList(), 1, 0, 0));
    taskService.findAll("user");
    verify(subscriptionService, times(1)).search(any());
    verify(promotionTasksService, times(1)).getPromotionTasks(any());
}
Also used : RoleEntity(io.gravitee.rest.api.model.RoleEntity) MembershipEntity(io.gravitee.rest.api.model.MembershipEntity) HashMap(java.util.HashMap) UserCriteria(io.gravitee.repository.management.api.search.UserCriteria) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

MembershipEntity (io.gravitee.rest.api.model.MembershipEntity)6 Test (org.junit.Test)5 RoleEntity (io.gravitee.rest.api.model.RoleEntity)4 HashMap (java.util.HashMap)3 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)2 ApplicationRepository (io.gravitee.repository.management.api.ApplicationRepository)2 io.gravitee.repository.management.model (io.gravitee.repository.management.model)2 ApplicationEntity (io.gravitee.rest.api.model.ApplicationEntity)2 MembershipMemberType (io.gravitee.rest.api.model.MembershipMemberType)2 MembershipReferenceType (io.gravitee.rest.api.model.MembershipReferenceType)2 UpdateApplicationEntity (io.gravitee.rest.api.model.UpdateApplicationEntity)2 ApplicationSettings (io.gravitee.rest.api.model.application.ApplicationSettings)2 SimpleApplicationSettings (io.gravitee.rest.api.model.application.SimpleApplicationSettings)2 AuditService (io.gravitee.rest.api.service.AuditService)2 ParameterService (io.gravitee.rest.api.service.ParameterService)2 SubscriptionService (io.gravitee.rest.api.service.SubscriptionService)2 UserService (io.gravitee.rest.api.service.UserService)2 ClientRegistrationService (io.gravitee.rest.api.service.configuration.application.ClientRegistrationService)2 ApplicationNotFoundException (io.gravitee.rest.api.service.exceptions.ApplicationNotFoundException)2 ClientIdAlreadyExistsException (io.gravitee.rest.api.service.exceptions.ClientIdAlreadyExistsException)2