Search in sources :

Example 1 with MappedFieldTypesService

use of org.graylog2.indexer.fieldtypes.MappedFieldTypesService in project graylog2-server by Graylog2.

the class FieldTypesResourceTest method allFieldTypesChecksPermissionsForStream.

@Test
public void allFieldTypesChecksPermissionsForStream() {
    final SearchUser searchUser = TestSearchUser.builder().allowStream("4242").denyStream("2323").build();
    final MappedFieldTypesService dependency = Mockito.mock(MappedFieldTypesService.class);
    Mockito.when(dependency.fieldTypesByStreamIds(Mockito.any(), Mockito.any())).thenAnswer(invocation -> {
        final Collection<String> streamIDs = invocation.getArgument(0);
        // for each streamID return a field that's called exactly like the streamID
        return streamIDs.stream().map(streamID -> MappedFieldTypeDTO.create(streamID, FieldTypes.Type.builder().type("text").build())).collect(Collectors.toSet());
    });
    final FieldTypesResource resource = new FieldTypesResource(dependency);
    final Set<MappedFieldTypeDTO> fields = resource.allFieldTypes(searchUser);
    // field for allowed stream has to be present
    assertThat(fields.stream().anyMatch(f -> f.name().equals("4242"))).isTrue();
    // field for denied stream must not be present
    assertThat(fields.stream().anyMatch(f -> f.name().equals("2323"))).isFalse();
}
Also used : MappedFieldTypesService(org.graylog2.indexer.fieldtypes.MappedFieldTypesService) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Mock(org.mockito.Mock) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RelativeRange(org.graylog2.plugin.indexer.searches.timeranges.RelativeRange) Captor(org.mockito.Captor) ArgumentCaptor(org.mockito.ArgumentCaptor) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) MockitoJUnit(org.mockito.junit.MockitoJUnit) TimeRange(org.graylog2.plugin.indexer.searches.timeranges.TimeRange) Before(org.junit.Before) ImmutableSet(com.google.common.collect.ImmutableSet) Collection(java.util.Collection) Set(java.util.Set) MappedFieldTypesService(org.graylog2.indexer.fieldtypes.MappedFieldTypesService) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Collectors(java.util.stream.Collectors) Mockito.verify(org.mockito.Mockito.verify) FieldTypes(org.graylog2.indexer.fieldtypes.FieldTypes) Mockito(org.mockito.Mockito) Rule(org.junit.Rule) MissingStreamPermissionException(org.graylog2.shared.rest.exceptions.MissingStreamPermissionException) SearchUser(org.graylog.plugins.views.search.permissions.SearchUser) MockitoRule(org.mockito.junit.MockitoRule) Collections(java.util.Collections) SearchUser(org.graylog.plugins.views.search.permissions.SearchUser) Test(org.junit.Test)

Example 2 with MappedFieldTypesService

use of org.graylog2.indexer.fieldtypes.MappedFieldTypesService in project graylog2-server by Graylog2.

the class FieldTypesResourceTest method byStreamChecksPermissionsForStream.

@Test
public void byStreamChecksPermissionsForStream() {
    final SearchUser searchUser = TestSearchUser.builder().allowStream("2323").allowStream("4242").build();
    final MappedFieldTypesService dependency = Mockito.mock(MappedFieldTypesService.class);
    Mockito.when(dependency.fieldTypesByStreamIds(Mockito.any(), Mockito.any())).thenAnswer(invocation -> {
        final Collection<String> streamIDs = invocation.getArgument(0);
        // for each streamID return a field that's called exactly like the streamID
        return streamIDs.stream().map(streamID -> MappedFieldTypeDTO.create(streamID, FieldTypes.Type.builder().type("text").build())).collect(Collectors.toSet());
    });
    final FieldTypesResource resource = new FieldTypesResource(dependency);
    final Set<MappedFieldTypeDTO> fields = resource.byStreams(FieldTypesForStreamsRequest.Builder.builder().streams(ImmutableSet.of("2323", "4242")).build(), searchUser);
    // field for allowed stream has to be present
    assertThat(fields.stream().anyMatch(f -> f.name().equals("2323"))).isTrue();
    assertThat(fields.stream().anyMatch(f -> f.name().equals("4242"))).isTrue();
}
Also used : MappedFieldTypesService(org.graylog2.indexer.fieldtypes.MappedFieldTypesService) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Mock(org.mockito.Mock) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RelativeRange(org.graylog2.plugin.indexer.searches.timeranges.RelativeRange) Captor(org.mockito.Captor) ArgumentCaptor(org.mockito.ArgumentCaptor) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) MockitoJUnit(org.mockito.junit.MockitoJUnit) TimeRange(org.graylog2.plugin.indexer.searches.timeranges.TimeRange) Before(org.junit.Before) ImmutableSet(com.google.common.collect.ImmutableSet) Collection(java.util.Collection) Set(java.util.Set) MappedFieldTypesService(org.graylog2.indexer.fieldtypes.MappedFieldTypesService) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Collectors(java.util.stream.Collectors) Mockito.verify(org.mockito.Mockito.verify) FieldTypes(org.graylog2.indexer.fieldtypes.FieldTypes) Mockito(org.mockito.Mockito) Rule(org.junit.Rule) MissingStreamPermissionException(org.graylog2.shared.rest.exceptions.MissingStreamPermissionException) SearchUser(org.graylog.plugins.views.search.permissions.SearchUser) MockitoRule(org.mockito.junit.MockitoRule) Collections(java.util.Collections) SearchUser(org.graylog.plugins.views.search.permissions.SearchUser) Test(org.junit.Test)

Aggregations

ImmutableSet (com.google.common.collect.ImmutableSet)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)2 SearchUser (org.graylog.plugins.views.search.permissions.SearchUser)2 FieldTypes (org.graylog2.indexer.fieldtypes.FieldTypes)2 MappedFieldTypesService (org.graylog2.indexer.fieldtypes.MappedFieldTypesService)2 RelativeRange (org.graylog2.plugin.indexer.searches.timeranges.RelativeRange)2 TimeRange (org.graylog2.plugin.indexer.searches.timeranges.TimeRange)2 MissingStreamPermissionException (org.graylog2.shared.rest.exceptions.MissingStreamPermissionException)2 Before (org.junit.Before)2 Rule (org.junit.Rule)2 Test (org.junit.Test)2 ArgumentCaptor (org.mockito.ArgumentCaptor)2 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)2 ArgumentMatchers.eq (org.mockito.ArgumentMatchers.eq)2 Captor (org.mockito.Captor)2