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.
[Security GenAI] [ Integration Assistant ] Add unit tests for Integra…
…tion Assistant plugin files (elastic#186512)
- Loading branch information
Showing
6 changed files
with
244 additions
and
0 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
x-pack/plugins/integration_assistant/__jest__/fixtures/build_integration.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,47 @@ | ||
/* | ||
* 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 { Integration } from '../../common/api/model/common_attributes'; | ||
|
||
export const testIntegration: Integration = { | ||
name: 'integration', | ||
title: 'Integration', | ||
description: 'Integration description', | ||
dataStreams: [ | ||
{ | ||
name: 'datastream', | ||
title: 'Datastream', | ||
description: 'Datastream description', | ||
inputTypes: ['filestream', 'tcp', 'udp'], | ||
docs: [ | ||
{ | ||
key: 'value', | ||
anotherKey: 'anotherValue', | ||
}, | ||
], | ||
rawSamples: ['{"test1": "test1"}'], | ||
pipeline: { | ||
processors: [ | ||
{ | ||
set: { | ||
field: 'ecs.version', | ||
value: '8.11.0', | ||
}, | ||
}, | ||
{ | ||
rename: { | ||
field: 'message', | ||
target_field: 'event.original', | ||
ignore_missing: true, | ||
if: 'ctx.event?.original == null', | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
], | ||
}; |
20 changes: 20 additions & 0 deletions
20
x-pack/plugins/integration_assistant/common/api/categorization/categorization_route.test.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,20 @@ | ||
/* | ||
* 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 { expectParseSuccess } from '@kbn/zod-helpers'; | ||
import { CategorizationRequestBody } from './categorization_route'; | ||
import { getCategorizationRequestMock } from '../model/api_test.mock'; | ||
|
||
describe('Categorization request schema', () => { | ||
test('full request validate', () => { | ||
const payload: CategorizationRequestBody = getCategorizationRequestMock(); | ||
|
||
const result = CategorizationRequestBody.safeParse(payload); | ||
expectParseSuccess(result); | ||
expect(result.data).toEqual(payload); | ||
}); | ||
}); |
20 changes: 20 additions & 0 deletions
20
x-pack/plugins/integration_assistant/common/api/ecs/ecs_route.test.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,20 @@ | ||
/* | ||
* 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 { expectParseSuccess } from '@kbn/zod-helpers'; | ||
import { EcsMappingRequestBody } from './ecs_route'; | ||
import { getEcsMappingRequestMock } from '../model/api_test.mock'; | ||
|
||
describe('Ecs Mapping request schema', () => { | ||
test('full request validate', () => { | ||
const payload: EcsMappingRequestBody = getEcsMappingRequestMock(); | ||
|
||
const result = EcsMappingRequestBody.safeParse(payload); | ||
expectParseSuccess(result); | ||
expect(result.data).toEqual(payload); | ||
}); | ||
}); |
82 changes: 82 additions & 0 deletions
82
x-pack/plugins/integration_assistant/common/api/model/api_test.mock.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,82 @@ | ||
/* | ||
* 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 type { BuildIntegrationRequestBody } from '../build_integration/build_integration'; | ||
import type { CategorizationRequestBody } from '../categorization/categorization_route'; | ||
import type { EcsMappingRequestBody } from '../ecs/ecs_route'; | ||
import type { RelatedRequestBody } from '../related/related_route'; | ||
import type { DataStream, Integration, Pipeline } from './common_attributes'; | ||
|
||
const rawSamples = ['{"test1": "test1"}']; | ||
|
||
export const getDataStreamMock = (): DataStream => ({ | ||
description: 'Test description', | ||
name: 'Test name', | ||
inputTypes: ['filestream'], | ||
title: 'Test title', | ||
docs: [ | ||
{ | ||
key: 'value', | ||
anotherKey: 'anotherValue', | ||
}, | ||
], | ||
rawSamples, | ||
pipeline: getPipelineMock(), | ||
}); | ||
|
||
export const getIntegrationMock = (): Integration => ({ | ||
description: 'Test description', | ||
name: 'Test name', | ||
title: 'Test title', | ||
dataStreams: [getDataStreamMock()], | ||
}); | ||
|
||
export const getPipelineMock = (): Pipeline => ({ | ||
processors: [ | ||
{ | ||
set: { | ||
field: 'ecs.version', | ||
value: '8.11.0', | ||
}, | ||
}, | ||
{ | ||
rename: { | ||
field: 'message', | ||
target_field: 'event.original', | ||
ignore_missing: true, | ||
if: 'ctx.event?.original == null', | ||
}, | ||
}, | ||
], | ||
}); | ||
|
||
export const getCategorizationRequestMock = (): CategorizationRequestBody => ({ | ||
connectorId: 'test-connector-id', | ||
currentPipeline: getPipelineMock(), | ||
dataStreamName: 'test-data-stream-name', | ||
packageName: 'test-package-name', | ||
rawSamples, | ||
}); | ||
|
||
export const getBuildIntegrationRequestMock = (): BuildIntegrationRequestBody => ({ | ||
integration: getIntegrationMock(), | ||
}); | ||
|
||
export const getEcsMappingRequestMock = (): EcsMappingRequestBody => ({ | ||
rawSamples, | ||
dataStreamName: 'test-data-stream-name', | ||
packageName: 'test-package-name', | ||
connectorId: 'test-connector-id', | ||
}); | ||
|
||
export const getRelatedRequestMock = (): RelatedRequestBody => ({ | ||
dataStreamName: 'test-data-stream-name', | ||
packageName: 'test-package-name', | ||
rawSamples, | ||
connectorId: 'test-connector-id', | ||
currentPipeline: getPipelineMock(), | ||
}); |
20 changes: 20 additions & 0 deletions
20
x-pack/plugins/integration_assistant/common/api/related/related_route.test.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,20 @@ | ||
/* | ||
* 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 { expectParseSuccess } from '@kbn/zod-helpers'; | ||
import { RelatedRequestBody } from './related_route'; | ||
import { getRelatedRequestMock } from '../model/api_test.mock'; | ||
|
||
describe('Related request schema', () => { | ||
test('full request validate', () => { | ||
const payload: RelatedRequestBody = getRelatedRequestMock(); | ||
|
||
const result = RelatedRequestBody.safeParse(payload); | ||
expectParseSuccess(result); | ||
expect(result.data).toEqual(payload); | ||
}); | ||
}); |
55 changes: 55 additions & 0 deletions
55
x-pack/plugins/integration_assistant/server/graphs/categorization/validate.test.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,55 @@ | ||
/* | ||
* 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 { handleCategorizationValidation } from './validate'; | ||
import type { CategorizationState } from '../../types'; | ||
import { categorizationTestState } from '../../../__jest__/fixtures/categorization'; | ||
|
||
const testState: CategorizationState = categorizationTestState; | ||
|
||
describe('Testing categorization invalid category', () => { | ||
it('handleCategorizationValidation()', async () => { | ||
testState.pipelineResults = [{ test: 'testresult', event: { category: ['foo'] } }]; | ||
const response = handleCategorizationValidation(testState); | ||
expect(response.invalidCategorization).toEqual([ | ||
{ | ||
error: | ||
"field event.category's values (foo) is not one of the allowed values (api, authentication, configuration, database, driver, email, file, host, iam, intrusion_detection, library, malware, network, package, process, registry, session, threat, vulnerability, web)", | ||
}, | ||
]); | ||
expect(response.lastExecutedChain).toBe('handleCategorizationValidation'); | ||
}); | ||
}); | ||
|
||
describe('Testing categorization invalid type', () => { | ||
it('handleCategorizationValidation()', async () => { | ||
testState.pipelineResults = [{ test: 'testresult', event: { type: ['foo'] } }]; | ||
const response = handleCategorizationValidation(testState); | ||
expect(response.invalidCategorization).toEqual([ | ||
{ | ||
error: | ||
"field event.type's values (foo) is not one of the allowed values (access, admin, allowed, change, connection, creation, deletion, denied, end, error, group, indicator, info, installation, protocol, start, user)", | ||
}, | ||
]); | ||
expect(response.lastExecutedChain).toBe('handleCategorizationValidation'); | ||
}); | ||
}); | ||
|
||
describe('Testing categorization invalid compatibility', () => { | ||
it('handleCategorizationValidation()', async () => { | ||
testState.pipelineResults = [ | ||
{ test: 'testresult', event: { category: ['authentication'], type: ['access'] } }, | ||
]; | ||
const response = handleCategorizationValidation(testState); | ||
expect(response.invalidCategorization).toEqual([ | ||
{ | ||
error: 'event.type (access) not compatible with any of the event.category (authentication)', | ||
}, | ||
]); | ||
expect(response.lastExecutedChain).toBe('handleCategorizationValidation'); | ||
}); | ||
}); |