From 5decd1d56db4fa4c06cfd79196c2ae62b1d2479d Mon Sep 17 00:00:00 2001 From: Christos Nasikas Date: Sat, 11 Jul 2020 16:38:43 +0300 Subject: [PATCH] Fix unit tests --- .../events_viewer/events_viewer.test.tsx | 3 +- .../components/url_state/index.test.tsx | 5 +- .../url_state/normalize_time_range.test.ts | 36 +++++++---- .../common/containers/source/index.test.tsx | 11 ++++ .../public/common/containers/source/index.tsx | 8 ++- .../public/common/containers/source/mock.ts | 13 +++- .../rules/fetch_index_patterns.test.tsx | 11 ++++ .../components/open_timeline/helpers.test.ts | 4 ++ .../__snapshots__/timeline.test.tsx.snap | 1 + .../timelines/store/timeline/defaults.ts | 2 +- .../timelines/store/timeline/helpers.ts | 2 +- .../utils/build_query/create_options.test.ts | 61 +++++++++++++++++++ 12 files changed, 139 insertions(+), 18 deletions(-) diff --git a/x-pack/plugins/security_solution/public/common/components/events_viewer/events_viewer.test.tsx b/x-pack/plugins/security_solution/public/common/components/events_viewer/events_viewer.test.tsx index 4c0a553fa88807c..965758475d2b302 100644 --- a/x-pack/plugins/security_solution/public/common/components/events_viewer/events_viewer.test.tsx +++ b/x-pack/plugins/security_solution/public/common/components/events_viewer/events_viewer.test.tsx @@ -15,7 +15,7 @@ import { mockEventViewerResponse } from './mock'; import { StatefulEventsViewer } from '.'; import { defaultHeaders } from './default_headers'; import { useFetchIndexPatterns } from '../../../detections/containers/detection_engine/rules/fetch_index_patterns'; -import { mockBrowserFields } from '../../containers/source/mock'; +import { mockBrowserFields, mockDocValueFields } from '../../containers/source/mock'; import { eventsDefaultModel } from './default_model'; import { useMountAppended } from '../../utils/use_mount_appended'; @@ -25,6 +25,7 @@ mockUseFetchIndexPatterns.mockImplementation(() => [ { browserFields: mockBrowserFields, indexPatterns: mockIndexPattern, + docValueFields: mockDocValueFields, }, ]); diff --git a/x-pack/plugins/security_solution/public/common/components/url_state/index.test.tsx b/x-pack/plugins/security_solution/public/common/components/url_state/index.test.tsx index e592b9c3740f244..9d0d9e7b250a053 100644 --- a/x-pack/plugins/security_solution/public/common/components/url_state/index.test.tsx +++ b/x-pack/plugins/security_solution/public/common/components/url_state/index.test.tsx @@ -53,11 +53,14 @@ jest.mock('../../lib/kibana', () => ({ }, }, }), + KibanaServices: { + get: jest.fn(() => ({ uiSettings: { get: () => ({ from: 'now-24h', to: 'now' }) } })), + }, })); describe('UrlStateContainer', () => { afterEach(() => { - jest.resetAllMocks(); + jest.clearAllMocks(); }); describe('handleInitialize', () => { describe('URL state updates redux', () => { diff --git a/x-pack/plugins/security_solution/public/common/components/url_state/normalize_time_range.test.ts b/x-pack/plugins/security_solution/public/common/components/url_state/normalize_time_range.test.ts index 689f3fa76387e23..d0cd9a26850777f 100644 --- a/x-pack/plugins/security_solution/public/common/components/url_state/normalize_time_range.test.ts +++ b/x-pack/plugins/security_solution/public/common/components/url_state/normalize_time_range.test.ts @@ -13,6 +13,11 @@ import { isRelativeTimeRange, } from '../../store/inputs/model'; +import { getTimeRangeSettings } from '../../utils/default_date_settings'; + +const getTimeRangeSettingsMock = getTimeRangeSettings as jest.Mock; + +jest.mock('../../utils/default_date_settings'); jest.mock('@elastic/datemath', () => ({ parse: (date: string) => { if (date === 'now') { @@ -25,8 +30,15 @@ jest.mock('@elastic/datemath', () => ({ }, })); +getTimeRangeSettingsMock.mockImplementation(() => ({ + from: '2020-07-04T08:20:18.966Z', + to: '2020-07-05T08:20:18.966Z', + fromStr: 'now-24h', + toStr: 'now', +})); + describe('#normalizeTimeRange', () => { - test('Absolute time range returns empty strings as empty strings', () => { + test('Absolute time range returns defaults for empty strings', () => { const dateTimeRange: URLTimeRange = { kind: 'absolute', fromStr: undefined, @@ -37,8 +49,8 @@ describe('#normalizeTimeRange', () => { if (isAbsoluteTimeRange(dateTimeRange)) { const expected: AbsoluteTimeRange = { kind: 'absolute', - from: '2020-07-07T08:20:18.966Z', - to: '2020-07-08T08:20:18.966Z', + from: '2020-07-04T08:20:18.966Z', + to: '2020-07-05T08:20:18.966Z', fromStr: undefined, toStr: undefined, }; @@ -120,7 +132,7 @@ describe('#normalizeTimeRange', () => { } }); - test('Absolute time range returns empty string with from and to when garbage is sent in', () => { + test('Absolute time range returns defaults when garbage is sent in', () => { const to = 'garbage'; const from = 'garbage'; const dateTimeRange: URLTimeRange = { @@ -133,8 +145,8 @@ describe('#normalizeTimeRange', () => { if (isAbsoluteTimeRange(dateTimeRange)) { const expected: AbsoluteTimeRange = { kind: 'absolute', - from: '2020-07-07T08:20:18.966Z', - to: '2020-07-08T08:20:18.966Z', + from: '2020-07-04T08:20:18.966Z', + to: '2020-07-05T08:20:18.966Z', fromStr: undefined, toStr: undefined, }; @@ -144,7 +156,7 @@ describe('#normalizeTimeRange', () => { } }); - test('Relative time range returns empty strings as empty strings', () => { + test('Relative time range returns defaults fro empty strings', () => { const dateTimeRange: URLTimeRange = { kind: 'relative', fromStr: '', @@ -155,8 +167,8 @@ describe('#normalizeTimeRange', () => { if (isRelativeTimeRange(dateTimeRange)) { const expected: RelativeTimeRange = { kind: 'relative', - from: '2020-07-07T08:20:18.966Z', - to: '2020-07-08T08:20:18.966Z', + from: '2020-07-04T08:20:18.966Z', + to: '2020-07-05T08:20:18.966Z', fromStr: '', toStr: '', }; @@ -238,7 +250,7 @@ describe('#normalizeTimeRange', () => { } }); - test('Relative time range returns empty string with from and to when garbage is sent in', () => { + test('Relative time range returns defaults when garbage is sent in', () => { const to = 'garbage'; const from = 'garbage'; const dateTimeRange: URLTimeRange = { @@ -251,8 +263,8 @@ describe('#normalizeTimeRange', () => { if (isRelativeTimeRange(dateTimeRange)) { const expected: RelativeTimeRange = { kind: 'relative', - from: '2020-07-07T08:20:18.966Z', - to: '2020-07-08T08:20:18.966Z', + from: '2020-07-04T08:20:18.966Z', + to: '2020-07-05T08:20:18.966Z', fromStr: '', toStr: '', }; diff --git a/x-pack/plugins/security_solution/public/common/containers/source/index.test.tsx b/x-pack/plugins/security_solution/public/common/containers/source/index.test.tsx index b9daba9a409418f..e1adb09441a0cc3 100644 --- a/x-pack/plugins/security_solution/public/common/containers/source/index.test.tsx +++ b/x-pack/plugins/security_solution/public/common/containers/source/index.test.tsx @@ -25,6 +25,7 @@ describe('Index Fields & Browser Fields', () => { return expect(initialResult).toEqual({ browserFields: {}, + docValueFields: [], errorMessage: null, indexPattern: { fields: [], @@ -56,6 +57,16 @@ describe('Index Fields & Browser Fields', () => { current: { indicesExist: true, browserFields: mockBrowserFields, + docValueFields: [ + { + field: '@timestamp', + format: 'date_time', + }, + { + field: 'event.end', + format: 'date_time', + }, + ], indexPattern: { fields: mockIndexFields, title: diff --git a/x-pack/plugins/security_solution/public/common/containers/source/index.tsx b/x-pack/plugins/security_solution/public/common/containers/source/index.tsx index f5c30037b6d92db..60404b57d57597c 100644 --- a/x-pack/plugins/security_solution/public/common/containers/source/index.tsx +++ b/x-pack/plugins/security_solution/public/common/containers/source/index.tsx @@ -85,7 +85,13 @@ export const getdocValueFields = memoizeOne( fields && fields.length > 0 ? fields.reduce((accumulator: DocValueFields[], field: IndexField) => { if (field.type === 'date' && accumulator.length < 100) { - return [...accumulator, { field: field.name, format: field.format ?? 'date_time' }]; + return [ + ...accumulator, + { + field: field.name, + format: isEmpty(field.format) ? 'date_time' : field.format ?? 'date_time', + }, + ]; } return accumulator; }, []) diff --git a/x-pack/plugins/security_solution/public/common/containers/source/mock.ts b/x-pack/plugins/security_solution/public/common/containers/source/mock.ts index 55e8b6ac02b1280..bba6a15d7397090 100644 --- a/x-pack/plugins/security_solution/public/common/containers/source/mock.ts +++ b/x-pack/plugins/security_solution/public/common/containers/source/mock.ts @@ -6,7 +6,7 @@ import { DEFAULT_INDEX_PATTERN } from '../../../../common/constants'; -import { BrowserFields } from '.'; +import { BrowserFields, DocValueFields } from '.'; import { sourceQuery } from './index.gql_query'; export const mocksSource = [ @@ -697,3 +697,14 @@ export const mockBrowserFields: BrowserFields = { }, }, }; + +export const mockDocValueFields: DocValueFields[] = [ + { + field: '@timestamp', + format: 'date_time', + }, + { + field: 'event.end', + format: 'date_time', + }, +]; diff --git a/x-pack/plugins/security_solution/public/detections/containers/detection_engine/rules/fetch_index_patterns.test.tsx b/x-pack/plugins/security_solution/public/detections/containers/detection_engine/rules/fetch_index_patterns.test.tsx index c282a204f19a5d2..62614c66c79db53 100644 --- a/x-pack/plugins/security_solution/public/detections/containers/detection_engine/rules/fetch_index_patterns.test.tsx +++ b/x-pack/plugins/security_solution/public/detections/containers/detection_engine/rules/fetch_index_patterns.test.tsx @@ -374,6 +374,16 @@ describe('useFetchIndexPatterns', () => { 'logs-*', ], indicesExists: true, + docValueFields: [ + { + field: '@timestamp', + format: 'date_time', + }, + { + field: 'event.end', + format: 'date_time', + }, + ], indexPatterns: { fields: [ { name: '@timestamp', searchable: true, type: 'date', aggregatable: true }, @@ -441,6 +451,7 @@ describe('useFetchIndexPatterns', () => { expect(result.current).toEqual([ { browserFields: {}, + docValueFields: [], indexPatterns: { fields: [], title: '', diff --git a/x-pack/plugins/security_solution/public/timelines/components/open_timeline/helpers.test.ts b/x-pack/plugins/security_solution/public/timelines/components/open_timeline/helpers.test.ts index 8f7160be19a6308..556e0e177659986 100644 --- a/x-pack/plugins/security_solution/public/timelines/components/open_timeline/helpers.test.ts +++ b/x-pack/plugins/security_solution/public/timelines/components/open_timeline/helpers.test.ts @@ -262,6 +262,7 @@ describe('helpers', () => { }, ], dataProviders: [], + dateRange: { start: 'now-24h', end: 'now' }, description: '', deletedEventIds: [], eventIdToNoteIds: {}, @@ -356,6 +357,7 @@ describe('helpers', () => { }, ], dataProviders: [], + dateRange: { start: 'now-24h', end: 'now' }, description: '', deletedEventIds: [], eventIdToNoteIds: {}, @@ -490,6 +492,7 @@ describe('helpers', () => { ], version: '1', dataProviders: [], + dateRange: { start: 'now-24h', end: 'now' }, description: '', deletedEventIds: [], eventIdToNoteIds: {}, @@ -611,6 +614,7 @@ describe('helpers', () => { }, ], version: '1', + dateRange: { start: 'now-24h', end: 'now' }, dataProviders: [], description: '', deletedEventIds: [], diff --git a/x-pack/plugins/security_solution/public/timelines/components/timeline/__snapshots__/timeline.test.tsx.snap b/x-pack/plugins/security_solution/public/timelines/components/timeline/__snapshots__/timeline.test.tsx.snap index 23f4781de02c8be..b9526ac1795bf39 100644 --- a/x-pack/plugins/security_solution/public/timelines/components/timeline/__snapshots__/timeline.test.tsx.snap +++ b/x-pack/plugins/security_solution/public/timelines/components/timeline/__snapshots__/timeline.test.tsx.snap @@ -804,6 +804,7 @@ In other use cases the message field can be used to concatenate different values }, ] } + docValueFields={Array []} end="2018-03-24T03:33:52.253Z" eventType="raw" filters={Array []} diff --git a/x-pack/plugins/security_solution/public/timelines/store/timeline/defaults.ts b/x-pack/plugins/security_solution/public/timelines/store/timeline/defaults.ts index d789ec7f127f2fc..cd8aa4d2a6b697c 100644 --- a/x-pack/plugins/security_solution/public/timelines/store/timeline/defaults.ts +++ b/x-pack/plugins/security_solution/public/timelines/store/timeline/defaults.ts @@ -14,7 +14,7 @@ import { SubsetTimelineModel, TimelineModel } from './model'; export const timelineDefaults: SubsetTimelineModel & Pick = { columns: defaultHeaders, dataProviders: [], - dateRange: { start: 'now-24', end: 'now' }, + dateRange: { start: 'now-24h', end: 'now' }, deletedEventIds: [], description: '', eventType: 'all', diff --git a/x-pack/plugins/security_solution/public/timelines/store/timeline/helpers.ts b/x-pack/plugins/security_solution/public/timelines/store/timeline/helpers.ts index a89236a136a3460..667165537ec9f54 100644 --- a/x-pack/plugins/security_solution/public/timelines/store/timeline/helpers.ts +++ b/x-pack/plugins/security_solution/public/timelines/store/timeline/helpers.ts @@ -153,7 +153,7 @@ interface AddNewTimelineParams { export const addNewTimeline = ({ columns, dataProviders = [], - dateRange = { start: 'now-24', end: 'now' }, + dateRange = { start: 'now-24h', end: 'now' }, filters = timelineDefaults.filters, id, itemsPerPage = timelineDefaults.itemsPerPage, diff --git a/x-pack/plugins/security_solution/server/utils/build_query/create_options.test.ts b/x-pack/plugins/security_solution/server/utils/build_query/create_options.test.ts index bec5de7662ecb5a..e83ca7418ad3d45 100644 --- a/x-pack/plugins/security_solution/server/utils/build_query/create_options.test.ts +++ b/x-pack/plugins/security_solution/server/utils/build_query/create_options.test.ts @@ -34,6 +34,16 @@ describe('createOptions', () => { pagination: { limit: 5, }, + docValueFields: [ + { + field: '@timestamp', + format: 'date_time', + }, + { + field: 'event.end', + format: 'date_time', + }, + ], timerange: { from: '2020-07-08T08:00:00.000Z', to: '2020-07-08T20:00:00.000Z', @@ -73,6 +83,16 @@ describe('createOptions', () => { limit: 5, }, filterQuery: {}, + docValueFields: [ + { + field: '@timestamp', + format: 'date_time', + }, + { + field: 'event.end', + format: 'date_time', + }, + ], fields: [], timerange: { from: '2020-07-08T08:00:00.000Z', @@ -102,6 +122,47 @@ describe('createOptions', () => { limit: 5, }, filterQuery: {}, + docValueFields: [ + { + field: '@timestamp', + format: 'date_time', + }, + { + field: 'event.end', + format: 'date_time', + }, + ], + fields: [], + timerange: { + from: '2020-07-08T08:00:00.000Z', + to: '2020-07-08T20:00:00.000Z', + interval: '12 hours ago', + }, + }; + expect(options).toEqual(expected); + }); + + test('should create options given all input except docValueFields', () => { + const argsWithoutSort: Args = omit('docValueFields', args); + const options = createOptions(source, argsWithoutSort, info); + const expected: RequestOptions = { + defaultIndex: DEFAULT_INDEX_PATTERN, + sourceConfiguration: { + fields: { + host: 'host-1', + container: 'container-1', + message: ['message-1'], + pod: 'pod-1', + tiebreaker: 'tiebreaker', + timestamp: 'timestamp-1', + }, + }, + sortField: { sortFieldId: 'sort-1', direction: Direction.asc }, + pagination: { + limit: 5, + }, + filterQuery: {}, + docValueFields: [], fields: [], timerange: { from: '2020-07-08T08:00:00.000Z',