forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Index Management] Add support for enrich policies (elastic#164080)
- Loading branch information
1 parent
e02c874
commit d1608f0
Showing
65 changed files
with
4,664 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
...nagement/__jest__/client_integration/create_enrich_policy/create_enrich_policy.helpers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { act } from 'react-dom/test-utils'; | ||
|
||
import { registerTestBed, TestBed, AsyncTestBedConfig } from '@kbn/test-jest-helpers'; | ||
import { HttpSetup } from '@kbn/core/public'; | ||
import { EnrichPolicyCreate } from '../../../public/application/sections/enrich_policy_create'; | ||
import { indexManagementStore } from '../../../public/application/store'; | ||
import { WithAppDependencies, services, TestSubjects } from '../helpers'; | ||
|
||
const testBedConfig: AsyncTestBedConfig = { | ||
store: () => indexManagementStore(services as any), | ||
memoryRouter: { | ||
initialEntries: [`/enrich_policies/create`], | ||
componentRoutePath: `/:section(enrich_policies)/create`, | ||
}, | ||
doMountAsync: true, | ||
}; | ||
|
||
export interface CreateEnrichPoliciesTestBed extends TestBed<TestSubjects> { | ||
actions: { | ||
clickNextButton: () => Promise<void>; | ||
clickBackButton: () => Promise<void>; | ||
clickRequestTab: () => Promise<void>; | ||
clickCreatePolicy: () => Promise<void>; | ||
completeConfigurationStep: ({ indices }: { indices?: string }) => Promise<void>; | ||
completeFieldsSelectionStep: () => Promise<void>; | ||
isOnConfigurationStep: () => boolean; | ||
isOnFieldSelectionStep: () => boolean; | ||
isOnCreateStep: () => boolean; | ||
}; | ||
} | ||
|
||
export const setup = async ( | ||
httpSetup: HttpSetup, | ||
overridingDependencies: any = {} | ||
): Promise<CreateEnrichPoliciesTestBed> => { | ||
const initTestBed = registerTestBed( | ||
WithAppDependencies(EnrichPolicyCreate, httpSetup, overridingDependencies), | ||
testBedConfig | ||
); | ||
const testBed = await initTestBed(); | ||
|
||
/** | ||
* User Actions | ||
*/ | ||
const isOnConfigurationStep = () => testBed.exists('configurationForm'); | ||
const isOnFieldSelectionStep = () => testBed.exists('fieldSelectionForm'); | ||
const isOnCreateStep = () => testBed.exists('creationStep'); | ||
const clickNextButton = async () => { | ||
await act(async () => { | ||
testBed.find('nextButton').simulate('click'); | ||
}); | ||
|
||
testBed.component.update(); | ||
}; | ||
const clickBackButton = async () => { | ||
await act(async () => { | ||
testBed.find('backButton').simulate('click'); | ||
}); | ||
|
||
testBed.component.update(); | ||
}; | ||
const clickCreatePolicy = async (executeAfter?: boolean) => { | ||
await act(async () => { | ||
testBed.find(executeAfter ? 'createAndExecuteButton' : 'createButton').simulate('click'); | ||
}); | ||
|
||
testBed.component.update(); | ||
}; | ||
|
||
const clickRequestTab = async () => { | ||
await act(async () => { | ||
testBed.find('requestTab').simulate('click'); | ||
}); | ||
|
||
testBed.component.update(); | ||
}; | ||
|
||
const completeConfigurationStep = async ({ indices }: { indices?: string }) => { | ||
const { form } = testBed; | ||
|
||
form.setInputValue('policyNameField.input', 'test_policy'); | ||
form.setSelectValue('policyTypeField', 'match'); | ||
form.setSelectValue('policySourceIndicesField', indices ?? 'test-1'); | ||
|
||
await clickNextButton(); | ||
}; | ||
|
||
const completeFieldsSelectionStep = async () => { | ||
const { form } = testBed; | ||
|
||
form.setSelectValue('matchField', 'name'); | ||
form.setSelectValue('enrichFields', 'email'); | ||
|
||
await clickNextButton(); | ||
}; | ||
|
||
return { | ||
...testBed, | ||
actions: { | ||
clickNextButton, | ||
clickBackButton, | ||
clickRequestTab, | ||
clickCreatePolicy, | ||
completeConfigurationStep, | ||
completeFieldsSelectionStep, | ||
isOnConfigurationStep, | ||
isOnFieldSelectionStep, | ||
isOnCreateStep, | ||
}, | ||
}; | ||
}; |
213 changes: 213 additions & 0 deletions
213
...management/__jest__/client_integration/create_enrich_policy/create_enrich_policy.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,213 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import React from 'react'; | ||
import { act } from 'react-dom/test-utils'; | ||
|
||
import { setupEnvironment } from '../helpers'; | ||
import { getMatchingIndices, getFieldsFromIndices } from '../helpers/fixtures'; | ||
import { CreateEnrichPoliciesTestBed, setup } from './create_enrich_policy.helpers'; | ||
import { getESPolicyCreationApiCall } from '../../../common/lib'; | ||
|
||
jest.mock('@kbn/kibana-react-plugin/public', () => { | ||
const original = jest.requireActual('@kbn/kibana-react-plugin/public'); | ||
return { | ||
...original, | ||
// Mocking CodeEditor, which uses React Monaco under the hood | ||
CodeEditor: (props: any) => ( | ||
<input | ||
data-test-subj={props['data-test-subj'] || 'mockCodeEditor'} | ||
data-currentvalue={props.value} | ||
onChange={(e: any) => { | ||
props.onChange(e.jsonContent); | ||
}} | ||
/> | ||
), | ||
}; | ||
}); | ||
|
||
jest.mock('@elastic/eui', () => { | ||
const original = jest.requireActual('@elastic/eui'); | ||
return { | ||
...original, | ||
// Mock EuiComboBox as a simple input instead so that its easier to test | ||
EuiComboBox: (props: any) => ( | ||
<input | ||
data-test-subj={props['data-test-subj'] || 'mockEuiCombobox'} | ||
data-currentvalue={props.value} | ||
onChange={(e: any) => { | ||
props.onChange(e.target.value.split(', ')); | ||
}} | ||
/> | ||
), | ||
}; | ||
}); | ||
|
||
describe('Create enrich policy', () => { | ||
const { httpSetup, httpRequestsMockHelpers } = setupEnvironment(); | ||
let testBed: CreateEnrichPoliciesTestBed; | ||
|
||
beforeEach(async () => { | ||
httpRequestsMockHelpers.setGetMatchingIndices(getMatchingIndices()); | ||
|
||
await act(async () => { | ||
testBed = await setup(httpSetup); | ||
}); | ||
|
||
testBed.component.update(); | ||
}); | ||
|
||
test('Has header and docs link', async () => { | ||
const { exists, component } = testBed; | ||
component.update(); | ||
|
||
expect(exists('createEnrichPolicyHeaderContent')).toBe(true); | ||
expect(exists('createEnrichPolicyDocumentationLink')).toBe(true); | ||
}); | ||
|
||
describe('Configuration step', () => { | ||
it('Fields have helpers', async () => { | ||
const { exists } = testBed; | ||
|
||
expect(exists('typePopoverIcon')).toBe(true); | ||
expect(exists('uploadFileLink')).toBe(true); | ||
expect(exists('matchAllQueryLink')).toBe(true); | ||
}); | ||
|
||
it('shows validation errors if form isnt filled', async () => { | ||
await testBed.actions.clickNextButton(); | ||
|
||
expect(testBed.form.getErrorsMessages()).toHaveLength(3); | ||
}); | ||
|
||
it('Allows to submit the form when fields are filled', async () => { | ||
const { actions } = testBed; | ||
|
||
await testBed.actions.completeConfigurationStep({}); | ||
|
||
expect(actions.isOnFieldSelectionStep()).toBe(true); | ||
}); | ||
}); | ||
|
||
describe('Fields selection step', () => { | ||
beforeEach(async () => { | ||
httpRequestsMockHelpers.setGetFieldsFromIndices(getFieldsFromIndices()); | ||
|
||
await act(async () => { | ||
testBed = await setup(httpSetup); | ||
}); | ||
|
||
testBed.component.update(); | ||
|
||
await testBed.actions.completeConfigurationStep({}); | ||
}); | ||
|
||
it('shows validation errors if form isnt filled', async () => { | ||
await testBed.actions.clickNextButton(); | ||
|
||
expect(testBed.form.getErrorsMessages()).toHaveLength(2); | ||
}); | ||
|
||
it('Allows to submit the form when fields are filled', async () => { | ||
const { form, actions } = testBed; | ||
|
||
form.setSelectValue('matchField', 'name'); | ||
form.setSelectValue('enrichFields', 'email'); | ||
|
||
await testBed.actions.clickNextButton(); | ||
|
||
expect(actions.isOnCreateStep()).toBe(true); | ||
}); | ||
|
||
it('When no common fields are returned it shows an error callout', async () => { | ||
httpRequestsMockHelpers.setGetFieldsFromIndices({ | ||
commonFields: [], | ||
indices: [], | ||
}); | ||
|
||
await act(async () => { | ||
testBed = await setup(httpSetup); | ||
}); | ||
|
||
testBed.component.update(); | ||
|
||
await testBed.actions.completeConfigurationStep({ indices: 'test-1, test-2' }); | ||
|
||
expect(testBed.exists('noCommonFieldsError')).toBe(true); | ||
}); | ||
}); | ||
|
||
describe('Creation step', () => { | ||
beforeEach(async () => { | ||
httpRequestsMockHelpers.setGetFieldsFromIndices(getFieldsFromIndices()); | ||
|
||
await act(async () => { | ||
testBed = await setup(httpSetup); | ||
}); | ||
|
||
testBed.component.update(); | ||
|
||
await testBed.actions.completeConfigurationStep({}); | ||
await testBed.actions.completeFieldsSelectionStep(); | ||
}); | ||
|
||
it('Shows CTAs for creating the policy', async () => { | ||
const { exists } = testBed; | ||
|
||
expect(exists('createButton')).toBe(true); | ||
expect(exists('createAndExecuteButton')).toBe(true); | ||
}); | ||
|
||
it('Shows policy summary and request', async () => { | ||
const { find } = testBed; | ||
|
||
expect(find('enrichPolicySummaryList').text()).toContain('test_policy'); | ||
|
||
await testBed.actions.clickRequestTab(); | ||
|
||
expect(find('requestBody').text()).toContain(getESPolicyCreationApiCall('test_policy')); | ||
}); | ||
|
||
it('Shows error message when creating the policy fails', async () => { | ||
const { exists, actions } = testBed; | ||
const error = { | ||
statusCode: 400, | ||
error: 'Bad Request', | ||
message: 'something went wrong...', | ||
}; | ||
|
||
httpRequestsMockHelpers.setCreateEnrichPolicy(undefined, error); | ||
|
||
await actions.clickCreatePolicy(); | ||
|
||
expect(exists('errorWhenCreatingCallout')).toBe(true); | ||
}); | ||
}); | ||
|
||
it('Can navigate back and forth with next/back buttons', async () => { | ||
httpRequestsMockHelpers.setGetFieldsFromIndices(getFieldsFromIndices()); | ||
|
||
await act(async () => { | ||
testBed = await setup(httpSetup); | ||
}); | ||
|
||
const { component, actions } = testBed; | ||
component.update(); | ||
|
||
// Navigate to create step | ||
await actions.completeConfigurationStep({}); | ||
await actions.completeFieldsSelectionStep(); | ||
|
||
// Clicking back button should take us to fields selection step | ||
await actions.clickBackButton(); | ||
expect(actions.isOnFieldSelectionStep()).toBe(true); | ||
|
||
// Clicking back button should take us to configuration step | ||
await actions.clickBackButton(); | ||
expect(actions.isOnConfigurationStep()).toBe(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.