Skip to content

Commit

Permalink
[SIEM][CASE] Api Integration Tests: Configuration (#63948)
Browse files Browse the repository at this point in the history
* Init

* Init get_connectors

* Test post_configuration

* Test patch_configuration

* Rename folder

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
  • Loading branch information
cnasikas and elasticmachine committed Apr 23, 2020
1 parent 44f9cbc commit a145aa9
Show file tree
Hide file tree
Showing 11 changed files with 443 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function initPatchCaseConfigure({ caseConfigureService, caseService, rout

if (myCaseConfigure.saved_objects.length === 0) {
throw Boom.conflict(
'You can not patch this configuration since you did not created first with a post'
'You can not patch this configuration since you did not created first with a post.'
);
}

Expand Down
14 changes: 14 additions & 0 deletions x-pack/test/case_api_integration/basic/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* 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 { createTestConfig } from '../common/config';

// eslint-disable-next-line import/no-default-export
export default createTestConfig('basic', {
disabledPlugins: [],
license: 'basic',
ssl: true,
});
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;
* you may not use this file except in compliance with the Elastic License.
*/

import expect from '@kbn/expect';
import { FtrProviderContext } from '../../../common/ftr_provider_context';

import { CASE_CONFIGURE_URL } from '../../../../../plugins/case/common/constants';
import {
getConfiguration,
removeServerGeneratedPropertiesFromConfigure,
getConfigurationOutput,
deleteConfiguration,
} from '../../../common/lib/utils';

// eslint-disable-next-line import/no-default-export
export default ({ getService }: FtrProviderContext): void => {
const supertest = getService('supertest');
const es = getService('legacyEs');

describe('get_configure', () => {
afterEach(async () => {
await deleteConfiguration(es);
});

it('should return an empty find body correctly if no configuration is loaded', async () => {
const { body } = await supertest
.get(CASE_CONFIGURE_URL)
.set('kbn-xsrf', 'true')
.send()
.expect(200);

expect(body).to.eql({});
});

it('should return a configuration', async () => {
await supertest
.post(CASE_CONFIGURE_URL)
.set('kbn-xsrf', 'true')
.send(getConfiguration())
.expect(200);

const { body } = await supertest
.get(CASE_CONFIGURE_URL)
.set('kbn-xsrf', 'true')
.send()
.expect(200);

const data = removeServerGeneratedPropertiesFromConfigure(body);
expect(data).to.eql(getConfigurationOutput());
});
});
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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 expect from '@kbn/expect';
import { FtrProviderContext } from '../../../common/ftr_provider_context';

import { CASE_CONFIGURE_CONNECTORS_URL } from '../../../../../plugins/case/common/constants';

// eslint-disable-next-line import/no-default-export
export default ({ getService }: FtrProviderContext): void => {
const supertest = getService('supertest');

describe('get_connectors', () => {
it('should return an empty find body correctly if no connectors are loaded', async () => {
const { body } = await supertest
.get(`${CASE_CONFIGURE_CONNECTORS_URL}/_find`)
.set('kbn-xsrf', 'true')
.send()
.expect(200);

expect(body).to.eql([]);
});
});
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* 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 expect from '@kbn/expect';
import { FtrProviderContext } from '../../../common/ftr_provider_context';

import { CASE_CONFIGURE_URL } from '../../../../../plugins/case/common/constants';
import {
getConfiguration,
removeServerGeneratedPropertiesFromConfigure,
getConfigurationOutput,
deleteConfiguration,
} from '../../../common/lib/utils';

// eslint-disable-next-line import/no-default-export
export default ({ getService }: FtrProviderContext): void => {
const supertest = getService('supertest');
const es = getService('legacyEs');

describe('post_configure', () => {
afterEach(async () => {
await deleteConfiguration(es);
});

it('should patch a configuration', async () => {
const res = await supertest
.post(CASE_CONFIGURE_URL)
.set('kbn-xsrf', 'true')
.send(getConfiguration())
.expect(200);

const { body } = await supertest
.patch(CASE_CONFIGURE_URL)
.set('kbn-xsrf', 'true')
.send({ closure_type: 'close-by-pushing', version: res.body.version })
.expect(200);

const data = removeServerGeneratedPropertiesFromConfigure(body);
expect(data).to.eql({ ...getConfigurationOutput(true), closure_type: 'close-by-pushing' });
});

it('should handle patch request when there is no configuration', async () => {
const { body } = await supertest
.patch(CASE_CONFIGURE_URL)
.set('kbn-xsrf', 'true')
.send({ closure_type: 'close-by-pushing', version: 'no-version' })
.expect(409);

expect(body).to.eql({
error: 'Conflict',
message:
'You can not patch this configuration since you did not created first with a post.',
statusCode: 409,
});
});

it('should handle patch request when versions are different', async () => {
await supertest
.post(CASE_CONFIGURE_URL)
.set('kbn-xsrf', 'true')
.send(getConfiguration())
.expect(200);

const { body } = await supertest
.patch(CASE_CONFIGURE_URL)
.set('kbn-xsrf', 'true')
.send({ closure_type: 'close-by-pushing', version: 'no-version' })
.expect(409);

expect(body).to.eql({
error: 'Conflict',
message:
'This configuration has been updated. Please refresh before saving additional updates.',
statusCode: 409,
});
});
});
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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 expect from '@kbn/expect';
import { FtrProviderContext } from '../../../common/ftr_provider_context';

import { CASE_CONFIGURE_URL } from '../../../../../plugins/case/common/constants';
import {
getConfiguration,
removeServerGeneratedPropertiesFromConfigure,
getConfigurationOutput,
deleteConfiguration,
} from '../../../common/lib/utils';

// eslint-disable-next-line import/no-default-export
export default ({ getService }: FtrProviderContext): void => {
const supertest = getService('supertest');
const es = getService('legacyEs');

describe('post_configure', () => {
afterEach(async () => {
await deleteConfiguration(es);
});

it('should create a configuration', async () => {
const { body } = await supertest
.post(CASE_CONFIGURE_URL)
.set('kbn-xsrf', 'true')
.send(getConfiguration())
.expect(200);

const data = removeServerGeneratedPropertiesFromConfigure(body);
expect(data).to.eql(getConfigurationOutput());
});

it('should keep only the latest configuration', async () => {
await supertest
.post(CASE_CONFIGURE_URL)
.set('kbn-xsrf', 'true')
.send(getConfiguration('connector-2'))
.expect(200);

await supertest
.post(CASE_CONFIGURE_URL)
.set('kbn-xsrf', 'true')
.send(getConfiguration())
.expect(200);

const { body } = await supertest
.get(CASE_CONFIGURE_URL)
.set('kbn-xsrf', 'true')
.send()
.expect(200);

const data = removeServerGeneratedPropertiesFromConfigure(body);
expect(data).to.eql(getConfigurationOutput());
});
});
};
20 changes: 20 additions & 0 deletions x-pack/test/case_api_integration/basic/tests/index.ts
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;
* you may not use this file except in compliance with the Elastic License.
*/

import { FtrProviderContext } from '../../common/ftr_provider_context';

// eslint-disable-next-line import/no-default-export
export default ({ loadTestFile }: FtrProviderContext): void => {
describe('case api basic', function() {
// Fastest ciGroup for the moment.
this.tags('ciGroup2');

loadTestFile(require.resolve('./configure/get_configure'));
loadTestFile(require.resolve('./configure/post_configure'));
loadTestFile(require.resolve('./configure/patch_configure'));
loadTestFile(require.resolve('./configure/get_connectors'));
});
};
94 changes: 94 additions & 0 deletions x-pack/test/case_api_integration/common/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* 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 path from 'path';

import { CA_CERT_PATH } from '@kbn/dev-utils';
import { FtrConfigProviderContext } from '@kbn/test/types/ftr';

import { services } from './services';

interface CreateTestConfigOptions {
license: string;
disabledPlugins?: string[];
ssl?: boolean;
}

// test.not-enabled is specifically not enabled
const enabledActionTypes = [
'.email',
'.index',
'.pagerduty',
'.server-log',
'.servicenow',
'.slack',
'.webhook',
'test.authorization',
'test.failing',
'test.index-record',
'test.noop',
'test.rate-limit',
];

export function createTestConfig(name: string, options: CreateTestConfigOptions) {
const { license = 'trial', disabledPlugins = [], ssl = false } = options;

return async ({ readConfigFile }: FtrConfigProviderContext) => {
const xPackApiIntegrationTestsConfig = await readConfigFile(
require.resolve('../../api_integration/config.js')
);

const servers = {
...xPackApiIntegrationTestsConfig.get('servers'),
elasticsearch: {
...xPackApiIntegrationTestsConfig.get('servers.elasticsearch'),
protocol: ssl ? 'https' : 'http',
},
};

return {
testFiles: [require.resolve(`../${name}/tests/`)],
servers,
services,
junit: {
reportName: 'X-Pack Case API Integration Tests',
},
esArchiver: xPackApiIntegrationTestsConfig.get('esArchiver'),
esTestCluster: {
...xPackApiIntegrationTestsConfig.get('esTestCluster'),
license,
ssl,
serverArgs: [
`xpack.license.self_generated.type=${license}`,
`xpack.security.enabled=${!disabledPlugins.includes('security') &&
['trial', 'basic'].includes(license)}`,
],
},
kbnTestServer: {
...xPackApiIntegrationTestsConfig.get('kbnTestServer'),
serverArgs: [
...xPackApiIntegrationTestsConfig.get('kbnTestServer.serverArgs'),
`--xpack.actions.whitelistedHosts=${JSON.stringify([
'localhost',
'some.non.existent.com',
])}`,
`--xpack.actions.enabledActionTypes=${JSON.stringify(enabledActionTypes)}`,
'--xpack.alerting.enabled=true',
'--xpack.eventLog.logEntries=true',
...disabledPlugins.map(key => `--xpack.${key}.enabled=false`),
`--plugin-path=${path.join(__dirname, 'fixtures', 'plugins', 'alerts')}`,
`--plugin-path=${path.join(__dirname, 'fixtures', 'plugins', 'actions')}`,
...(ssl
? [
`--elasticsearch.hosts=${servers.elasticsearch.protocol}://${servers.elasticsearch.hostname}:${servers.elasticsearch.port}`,
`--elasticsearch.ssl.certificateAuthorities=${CA_CERT_PATH}`,
]
: []),
],
},
};
};
}
11 changes: 11 additions & 0 deletions x-pack/test/case_api_integration/common/ftr_provider_context.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* 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 { GenericFtrProviderContext } from '@kbn/test/types/ftr';

import { services } from './services';

export type FtrProviderContext = GenericFtrProviderContext<typeof services, {}>;
Loading

0 comments on commit a145aa9

Please sign in to comment.