Skip to content

Commit

Permalink
[Security GenAI] [ Integration Assistant ] Add unit tests for Integra…
Browse files Browse the repository at this point in the history
…tion Assistant plugin files (elastic#186512)
  • Loading branch information
bhapas authored Jun 25, 2024
1 parent 53de504 commit 4ffd530
Show file tree
Hide file tree
Showing 6 changed files with 244 additions and 0 deletions.
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',
},
},
],
},
},
],
};
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);
});
});
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);
});
});
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(),
});
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);
});
});
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');
});
});

0 comments on commit 4ffd530

Please sign in to comment.