Search in sources :

Example 11 with ValidationResult

use of org.graylog2.plugin.database.validators.ValidationResult in project graylog2-server by Graylog2.

the class EntitySharesServiceTest method validateLastOwnerCannotBeRemoved.

// TODO Test more EntitySharesService functionality
@DisplayName("Validates we cannot remove the last owner")
@Test
void validateLastOwnerCannotBeRemoved() {
    final GRN entity = grnRegistry.newGRN(GRNTypes.STREAM, "54e3deadbeefdeadbeefaffe");
    final EntityShareRequest shareRequest = EntityShareRequest.create(ImmutableMap.of());
    // This test can also see the "invisible user"
    final Set<GRN> allGrantees = dbGrantService.getAll().stream().map(GrantDTO::grantee).collect(Collectors.toSet());
    lenient().when(granteeService.getAvailableGrantees(any())).thenReturn(allGrantees.stream().map(g -> Grantee.createUser(g, g.entity())).collect(Collectors.toSet()));
    final User user = createMockUser("hans");
    final Subject subject = mock(Subject.class);
    final EntityShareResponse entityShareResponse = entitySharesService.prepareShare(entity, shareRequest, user, subject);
    assertThat(entityShareResponse.validationResult()).satisfies(validationResult -> {
        assertThat(validationResult.failed()).isTrue();
        assertThat(validationResult.getErrors()).isNotEmpty();
        assertThat(validationResult.getErrors().get(EntityShareRequest.SELECTED_GRANTEE_CAPABILITIES).toString()).contains("Removing the following owners").contains("grn::::user:jane").contains("grn::::user:invisible");
    });
}
Also used : GRN(org.graylog.grn.GRN) User(org.graylog2.plugin.database.users.User) Subject(org.apache.shiro.subject.Subject) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 12 with ValidationResult

use of org.graylog2.plugin.database.validators.ValidationResult in project graylog2-server by Graylog2.

the class EntitySharesServiceTest method validateOwnerSwitch.

@DisplayName("Validates we can switch owners")
@Test
void validateOwnerSwitch() {
    final GRN entity = grnRegistry.newGRN(GRNTypes.STREAM, "54e3deadbeefdeadbeefaffe");
    final GRN horst = grnRegistry.newGRN(GRNTypes.USER, "horst");
    final EntityShareRequest shareRequest = EntityShareRequest.create(ImmutableMap.of(horst, Capability.OWN));
    final User user = createMockUser("hans");
    final Subject subject = mock(Subject.class);
    final EntityShareResponse entityShareResponse = entitySharesService.prepareShare(entity, shareRequest, user, subject);
    assertThat(entityShareResponse.validationResult()).satisfies(validationResult -> {
        assertThat(validationResult.failed()).isFalse();
        assertThat(validationResult.getErrors()).isEmpty();
    });
}
Also used : GRN(org.graylog.grn.GRN) User(org.graylog2.plugin.database.users.User) Subject(org.apache.shiro.subject.Subject) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 13 with ValidationResult

use of org.graylog2.plugin.database.validators.ValidationResult in project graylog2-server by Graylog2.

the class NotificationTestData method getDummyContext.

public static EventNotificationContext getDummyContext(NotificationDto notificationDto, String userName) {
    final EventDto eventDto = EventDto.builder().alert(true).eventDefinitionId("EventDefinitionTestId").eventDefinitionType("notification-test-v1").eventTimestamp(Tools.nowUTC()).processingTimestamp(Tools.nowUTC()).id("TEST_NOTIFICATION_ID").streams(ImmutableSet.of(Stream.DEFAULT_EVENTS_STREAM_ID)).message("Notification test message triggered from user <" + userName + ">").source(Stream.DEFAULT_STREAM_ID).keyTuple(ImmutableList.of("testkey")).key("testkey").originContext(EventOriginContext.elasticsearchMessage("testIndex_42", "b5e53442-12bb-4374-90ed-0deadbeefbaz")).priority(2).fields(ImmutableMap.of("field1", "value1", "field2", "value2")).build();
    final EventDefinitionDto eventDefinitionDto = EventDefinitionDto.builder().alert(true).id(TEST_NOTIFICATION_ID).title("Event Definition Test Title").description("Event Definition Test Description").config(new EventProcessorConfig() {

        @Override
        public String type() {
            return "test-dummy-v1";
        }

        @Override
        public ValidationResult validate() {
            return null;
        }

        @Override
        public EventProcessorConfigEntity toContentPackEntity(EntityDescriptorIds entityDescriptorIds) {
            return null;
        }
    }).fieldSpec(ImmutableMap.of()).priority(2).keySpec(ImmutableList.of()).notificationSettings(new EventNotificationSettings() {

        @Override
        public long gracePeriodMs() {
            return 0;
        }

        @Override
        public // disable to avoid errors in getBacklogForEvent()
        long backlogSize() {
            return 0;
        }

        @Override
        public Builder toBuilder() {
            return null;
        }
    }).build();
    return EventNotificationContext.builder().notificationId(TEST_NOTIFICATION_ID).notificationConfig(notificationDto.config()).event(eventDto).eventDefinition(eventDefinitionDto).build();
}
Also used : EventDefinitionDto(org.graylog.events.processor.EventDefinitionDto) EntityDescriptorIds(org.graylog2.contentpacks.EntityDescriptorIds) EventDto(org.graylog.events.event.EventDto) EventProcessorConfig(org.graylog.events.processor.EventProcessorConfig)

Example 14 with ValidationResult

use of org.graylog2.plugin.database.validators.ValidationResult in project graylog2-server by Graylog2.

the class EventDefinitionDtoTest method testValidateWithEmptyTitle.

@Test
public void testValidateWithEmptyTitle() {
    final EventDefinitionDto invalidEventDefinition = testSubject.toBuilder().title("").build();
    final ValidationResult validationResult = invalidEventDefinition.validate();
    assertThat(validationResult.failed()).isTrue();
    assertThat(validationResult.getErrors()).containsOnlyKeys("title");
}
Also used : ValidationResult(org.graylog2.plugin.rest.ValidationResult) Test(org.junit.Test)

Example 15 with ValidationResult

use of org.graylog2.plugin.database.validators.ValidationResult in project graylog2-server by Graylog2.

the class EventDefinitionDtoTest method setUp.

@Before
public void setUp() throws Exception {
    final AggregationEventProcessorConfig configMock = mock(AggregationEventProcessorConfig.class);
    when(configMock.validate()).thenReturn(new ValidationResult());
    testSubject = EventDefinitionDto.builder().title("foo").description("bar").priority(1).alert(false).config(configMock).keySpec(ImmutableList.<String>builder().build()).notificationSettings(EventNotificationSettings.withGracePeriod(0)).build();
}
Also used : AggregationEventProcessorConfig(org.graylog.events.processor.aggregation.AggregationEventProcessorConfig) ValidationResult(org.graylog2.plugin.rest.ValidationResult) Before(org.junit.Before)

Aggregations

ValidationResult (org.graylog2.plugin.rest.ValidationResult)43 Test (org.junit.Test)24 ApiOperation (io.swagger.annotations.ApiOperation)16 NoAuditEvent (org.graylog2.audit.jersey.NoAuditEvent)16 RequiresPermissions (org.apache.shiro.authz.annotation.RequiresPermissions)14 Path (javax.ws.rs.Path)12 AuditEvent (org.graylog2.audit.jersey.AuditEvent)12 POST (javax.ws.rs.POST)11 List (java.util.List)10 Subject (org.apache.shiro.subject.Subject)8 GRN (org.graylog.grn.GRN)8 User (org.graylog2.plugin.database.users.User)8 ArrayList (java.util.ArrayList)7 Produces (javax.ws.rs.Produces)7 ValidationException (org.graylog2.plugin.database.ValidationException)6 DisplayName (org.junit.jupiter.api.DisplayName)6 Test (org.junit.jupiter.api.Test)6 BasicDBObject (com.mongodb.BasicDBObject)5 Collectors (java.util.stream.Collectors)5 NotFoundException (javax.ws.rs.NotFoundException)5