Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[security solutions][lists] Adds end to end tests part 1 #74473

Merged
merged 16 commits into from
Aug 11, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions x-pack/plugins/lists/common/constants.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { EntriesArray } from './schemas/types';
export const DATE_NOW = '2020-04-20T15:25:31.830Z';
export const OLD_DATE_RELATIVE_TO_DATE_NOW = '2020-04-19T15:25:31.830Z';
export const USER = 'some user';
export const ELASTIC_USER = 'elastic';
export const LIST_INDEX = '.lists';
export const LIST_ITEM_INDEX = '.items';
export const NAME = 'some name';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,16 @@ export const getCreateListSchemaMock = (): CreateListSchema => ({
type: TYPE,
version: VERSION,
});

export const getCreateMinimalListSchemaMock = (): CreateListSchema => ({
description: DESCRIPTION,
id: LIST_ID,
name: NAME,
type: TYPE,
});

export const getCreateMinimalListSchemaMockWithoutId = (): CreateListSchema => ({
description: DESCRIPTION,
name: NAME,
type: TYPE,
});
16 changes: 16 additions & 0 deletions x-pack/plugins/lists/common/schemas/response/list_schema.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { ListSchema } from '../../../common/schemas';
import {
DATE_NOW,
DESCRIPTION,
ELASTIC_USER,
IMMUTABLE,
LIST_ID,
META,
Expand Down Expand Up @@ -35,3 +36,18 @@ export const getListResponseMock = (): ListSchema => ({
updated_by: USER,
version: VERSION,
});

/**
* This is useful for end to end tests where we remove the auto generated parts for comparisons
* such as created_at, updated_at, and id.
*/
export const getListResponseMockWithoutAutoGeneratedValues = (): Partial<ListSchema> => ({
_version: 'WzAsMV0=',
created_by: ELASTIC_USER,
description: DESCRIPTION,
immutable: IMMUTABLE,
name: NAME,
type: TYPE,
updated_by: ELASTIC_USER,
version: VERSION,
});
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ export function createTestConfig(name: string, options: CreateTestConfigOptions)
])}`,
`--xpack.actions.enabledActionTypes=${JSON.stringify(enabledActionTypes)}`,
'--xpack.eventLog.logEntries=true',
'--xpack.lists.enabled=true',
...disabledPlugins.map((key) => `--xpack.${key}.enabled=false`),
`--plugin-path=${path.join(__dirname, 'fixtures', 'plugins', 'alerts')}`,
`--plugin-path=${path.join(__dirname, 'fixtures', 'plugins', 'actions')}`,
Expand Down
69 changes: 69 additions & 0 deletions x-pack/test/lists_api_integration/common/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* 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;
}

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since these are defaulted in x-pack/test/lists_api_integration/security_and_spaces/config.ts does it need to be defaulted here too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, from a technical standpoint it does not, it just follows the other patterns where a default is given and then everything still overrides it. I will probably keep it for the sake of the other examples in the other plugins doing it the same way but fwiw.

Later if we introduce tests only for basic license rather than trial which gives us full platinum access then that config.ts would look like this:

export default createTestConfig('security_and_spaces', { license: 'basic' });


return async ({ readConfigFile }: FtrConfigProviderContext) => {
const xPackApiIntegrationTestsConfig = await readConfigFile(
require.resolve('../../api_integration/config.ts')
);
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 Lists 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')}`,
],
},
kbnTestServer: {
...xPackApiIntegrationTestsConfig.get('kbnTestServer'),
serverArgs: [
...xPackApiIntegrationTestsConfig.get('kbnTestServer.serverArgs'),
...disabledPlugins.map((key) => `--xpack.${key}.enabled=false`),
`--plugin-path=${path.join(__dirname, 'fixtures', 'plugins', 'alerts')}`,
`--plugin-path=${path.join(__dirname, 'fixtures', 'plugins', 'actions')}`,
`--plugin-path=${path.join(__dirname, 'fixtures', 'plugins', 'task_manager')}`,
`--plugin-path=${path.join(__dirname, 'fixtures', 'plugins', 'aad')}`,
...(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/lists_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, {}>;
7 changes: 7 additions & 0 deletions x-pack/test/lists_api_integration/common/services.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
* 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.
*/

export { services } from '../../api_integration/services';
14 changes: 14 additions & 0 deletions x-pack/test/lists_api_integration/security_and_spaces/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('security_and_spaces', {
disabledPlugins: [],
license: 'trial',
ssl: true,
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* 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 { LIST_URL } from '../../../../plugins/lists/common/constants';
import { FtrProviderContext } from '../../common/ftr_provider_context';
import {
getCreateMinimalListSchemaMock,
getCreateMinimalListSchemaMockWithoutId,
} from '../../../../plugins/lists/common/schemas/request/create_list_schema.mock';
import { getListResponseMockWithoutAutoGeneratedValues } from '../../../../plugins/lists/common/schemas/response/list_schema.mock';

import { createListsIndex, deleteListsIndex, removeServerGeneratedProperties } from '../../utils';

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

describe('create_lists', () => {
describe('validation errors', () => {
it('should give an error that the index must exist first if it does not exist before creating a rule', async () => {
const { body } = await supertest
.post(LIST_URL)
.set('kbn-xsrf', 'true')
.send(getCreateMinimalListSchemaMock())
.expect(400);

expect(body).to.eql({
message:
'To create a list, the index must exist first. Index ".lists-default" does not exist',
status_code: 400,
});
});
});

describe('creating lists', () => {
beforeEach(async () => {
await createListsIndex(supertest);
});

afterEach(async () => {
await deleteListsIndex(supertest);
});

it('should create a simple list with a list_id', async () => {
const { body } = await supertest
.post(LIST_URL)
.set('kbn-xsrf', 'true')
.send(getCreateMinimalListSchemaMock())
.expect(200);

const bodyToCompare = removeServerGeneratedProperties(body);
expect(bodyToCompare).to.eql(getListResponseMockWithoutAutoGeneratedValues());
});

it('should create a simple list without a list_id', async () => {
const { body } = await supertest
.post(LIST_URL)
.set('kbn-xsrf', 'true')
.send(getCreateMinimalListSchemaMockWithoutId())
.expect(200);

const bodyToCompare = removeServerGeneratedProperties(body);
expect(bodyToCompare).to.eql(getListResponseMockWithoutAutoGeneratedValues());
});

it('should cause a 409 conflict if we attempt to create the same list_id twice', async () => {
await supertest
.post(LIST_URL)
.set('kbn-xsrf', 'true')
.send(getCreateMinimalListSchemaMock())
.expect(200);

const { body } = await supertest
.post(LIST_URL)
.set('kbn-xsrf', 'true')
.send(getCreateMinimalListSchemaMock())
.expect(409);

expect(body).to.eql({
message: 'list id: "some-list-id" already exists',
status_code: 409,
});
});
});
});
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* 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('lists api security and spaces enabled', function () {
this.tags('ciGroup1');

loadTestFile(require.resolve('./create_lists'));
loadTestFile(require.resolve('./read_lists'));
});
};
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;
* 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 { LIST_URL } from '../../../../plugins/lists/common/constants';

import {
getCreateMinimalListSchemaMock,
getCreateMinimalListSchemaMockWithoutId,
} from '../../../../plugins/lists/common/schemas/request/create_list_schema.mock';
import { createListsIndex, deleteListsIndex, removeServerGeneratedProperties } from '../../utils';
import { getListResponseMockWithoutAutoGeneratedValues } from '../../../../plugins/lists/common/schemas/response/list_schema.mock';

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

describe('read_lists', () => {
describe('reading lists', () => {
beforeEach(async () => {
await createListsIndex(supertest);
});

afterEach(async () => {
await deleteListsIndex(supertest);
});

it('should be able to read a single list using id', async () => {
// create a simple list to read
await supertest
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the preference to do this, rather than using fixtures? Ideally we'd keep the scope of this test small and just test reads (this is, in effect, testing both create and read), but on the other hand... maintenance of fixtures can be "not fun." Just curious if there's ever been any discussion as to what the standard should be ... I see a good mix of both across the different plugins. FWIW, we used a bit of a mix for the endpoint artifact packaging code, but any setup calls were done in before*.

Copy link
Contributor Author

@FrankHassanabad FrankHassanabad Aug 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There hasn't been much discussion, I haven't used fixtures before other than other people's.

So far this has been simple to maintain and when things blow up it hasn't been hard to trace it down to the right spot. I'm not against learning fixtures though. Seems like it might be more maintenance like the issues with loading saved objects from esArchiver has been a bit of a pain point for cypress on the front end.

Overall, I have been maintaining the detection engine tests using mostly REST setup, tear downs, and the only maintenance has been two suite skips due to networking issues we solved by using retry loops (which would still have happened with fixtures or dummy data).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree it can make maintenance a little more painful. Carry on. :)

.post(LIST_URL)
.set('kbn-xsrf', 'true')
.send(getCreateMinimalListSchemaMock())
.expect(200);

const { body } = await supertest
.get(`${LIST_URL}?id=some-list-id`)
.set('kbn-xsrf', 'true')
.send(getCreateMinimalListSchemaMock())
.expect(200);

const bodyToCompare = removeServerGeneratedProperties(body);
expect(bodyToCompare).to.eql(getListResponseMockWithoutAutoGeneratedValues());
});

it('should be able to read a single list with an auto-generated list id', async () => {
// create a simple list to read
const { body: createListBody } = await supertest
.post(LIST_URL)
.set('kbn-xsrf', 'true')
.send(getCreateMinimalListSchemaMockWithoutId())
.expect(200);

const { body } = await supertest
.get(`${LIST_URL}?id=${createListBody.id}`)
.set('kbn-xsrf', 'true')
.send(getCreateMinimalListSchemaMock())
.expect(200);

const bodyToCompare = removeServerGeneratedProperties(body);
expect(bodyToCompare).to.eql(getListResponseMockWithoutAutoGeneratedValues());
});

it('should return 404 if given a fake id', async () => {
const { body } = await supertest
.get(`${LIST_URL}?id=c1e1b359-7ac1-4e96-bc81-c683c092436f`)
.set('kbn-xsrf', 'true')
.expect(404);

expect(body).to.eql({
status_code: 404,
message: 'list id: "c1e1b359-7ac1-4e96-bc81-c683c092436f" does not exist',
});
});
});
});
};
Loading