Skip to content

Commit

Permalink
tests for detection engine get/put utils
Browse files Browse the repository at this point in the history
  • Loading branch information
dhurley14 committed Nov 11, 2019
1 parent d86b6c7 commit c555d0f
Show file tree
Hide file tree
Showing 5 changed files with 527 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { SignalSourceHit, SignalSearchResponse, SignalAlertParams, BulkResponse } from '../types';

export const sampleSignalAlertParams = (): SignalAlertParams => ({
id: 'rule-1',
description: 'Detecting root and admin users',
index: ['auditbeat-*', 'filebeat-*', 'packetbeat-*', 'winlogbeat-*'],
interval: '5m',
name: 'Detect Root/Admin Users',
type: 'query',
from: 'now-6m',
to: 'now',
severity: 'high',
query: 'user.name: root or user.name: admin',
language: 'kuery',
references: ['http://google.com'],
maxSignals: 100,
enabled: true,
filter: undefined,
filters: undefined,
savedId: undefined,
size: undefined,
});

export const sampleDocNoSortId: SignalSourceHit = {
_index: 'myFakeSignalIndex',
_type: 'doc',
_score: 100,
_version: 1,
_id: 'someFakeId',
_source: {
someKey: 'someValue',
'@timestamp': 'someTimeStamp',
},
};

export const sampleDocWithSortId: SignalSourceHit = {
_index: 'myFakeSignalIndex',
_type: 'doc',
_score: 100,
_version: 1,
_id: 'someFakeId',
_source: {
someKey: 'someValue',
'@timestamp': 'someTimeStamp',
},
sort: ['1234567891111'],
};

export const sampleDocSearchResultsNoSortId: SignalSearchResponse = {
took: 10,
timed_out: false,
_shards: {
total: 10,
successful: 10,
failed: 0,
skipped: 0,
},
hits: {
total: 100,
max_score: 100,
hits: [
{
...sampleDocNoSortId,
},
],
},
};

export const sampleDocSearchResultsWithSortId: SignalSearchResponse = {
took: 10,
timed_out: false,
_shards: {
total: 10,
successful: 10,
failed: 0,
skipped: 0,
},
hits: {
total: 1,
max_score: 100,
hits: [
{
...sampleDocWithSortId,
},
],
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,148 @@ describe('create_signals', () => {
},
});
});
test('if searchAfterSortId is a valid sortId string', () => {
const fakeSortId = '123456789012';
const query = buildEventsSearchQuery({
index: ['auditbeat-*'],
from: 'now-5m',
to: 'today',
filter: {},
size: 100,
searchAfterSortId: fakeSortId,
});
expect(query).toEqual({
allowNoIndices: true,
index: ['auditbeat-*'],
size: 100,
ignoreUnavailable: true,
body: {
query: {
bool: {
filter: [
{},
{
bool: {
filter: [
{
bool: {
should: [
{
range: {
'@timestamp': {
gte: 'now-5m',
},
},
},
],
minimum_should_match: 1,
},
},
{
bool: {
should: [
{
range: {
'@timestamp': {
lte: 'today',
},
},
},
],
minimum_should_match: 1,
},
},
],
},
},
{
match_all: {},
},
],
},
},
track_total_hits: true,
sort: [
{
'@timestamp': {
order: 'asc',
},
},
],
search_after: [fakeSortId],
},
});
});
test('if searchAfterSortId is a valid sortId number', () => {
const fakeSortIdNumber = 123456789012;
const query = buildEventsSearchQuery({
index: ['auditbeat-*'],
from: 'now-5m',
to: 'today',
filter: {},
size: 100,
searchAfterSortId: fakeSortIdNumber,
});
expect(query).toEqual({
allowNoIndices: true,
index: ['auditbeat-*'],
size: 100,
ignoreUnavailable: true,
body: {
query: {
bool: {
filter: [
{},
{
bool: {
filter: [
{
bool: {
should: [
{
range: {
'@timestamp': {
gte: 'now-5m',
},
},
},
],
minimum_should_match: 1,
},
},
{
bool: {
should: [
{
range: {
'@timestamp': {
lte: 'today',
},
},
},
],
minimum_should_match: 1,
},
},
],
},
},
{
match_all: {},
},
],
},
},
track_total_hits: true,
sort: [
{
'@timestamp': {
order: 'asc',
},
},
],
search_after: [fakeSortIdNumber],
},
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface BuildEventsSearchQuery {
to: string;
filter: unknown;
size: number;
searchAfterSortId?: string;
searchAfterSortId?: string | number;
}

export const buildEventsSearchQuery = ({
Expand Down
Loading

0 comments on commit c555d0f

Please sign in to comment.