Skip to content

Commit

Permalink
fix jest tests to use mocked SO client
Browse files Browse the repository at this point in the history
  • Loading branch information
dhurley14 committed Dec 17, 2021
1 parent 50a2b40 commit 9e0be05
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe.each([
['Legacy', false],
['RAC', true],
])('utils - %s', (_, isRuleRegistryEnabled) => {
const { clients } = requestContextMock.createTools();
const { clients, context } = requestContextMock.createTools();

describe('transformAlertToRule', () => {
test('should work with a full data set', () => {
Expand Down Expand Up @@ -661,70 +661,58 @@ describe.each([
action_type_id: '.slack',
params: {},
};
const soClient = clients.core.savedObjects.getClient();
beforeEach(() => {
clients.core.elasticsearch.client.asInternalUser.search.mockReset();
clients.core.elasticsearch.client.asInternalUser.search.mockClear();
soClient.find.mockReset();
soClient.find.mockClear();
});

test('returns original action if Elasticsearch query fails', async () => {
clients.core.elasticsearch.client.asInternalUser.search.mockRejectedValueOnce(
new Error('failed to query')
);
const result = await swapActionIds(
mockAction,
clients.core.elasticsearch.client.asInternalUser
);
clients.core.savedObjects
.getClient()
.find.mockRejectedValueOnce(new Error('failed to query'));
const result = await swapActionIds(mockAction, soClient);
expect(result).toEqual(mockAction);
});

test('returns original action if Elasticsearch query returns no hits', async () => {
// @ts-expect-error
clients.core.elasticsearch.client.asInternalUser.search.mockImplementationOnce(async () => ({
body: {
hits: { total: 0, hits: [] },
},
soClient.find.mockImplementationOnce(async () => ({
total: 0,
per_page: 0,
page: 1,
saved_objects: [],
}));
const result = await swapActionIds(
mockAction,
clients.core.elasticsearch.client.asInternalUser
);
const result = await swapActionIds(mockAction, soClient);
expect(result).toEqual(mockAction);
});

test('returns error if conflicting action connectors are found -> two hits found with same originId', async () => {
// @ts-expect-error
clients.core.elasticsearch.client.asInternalUser.search.mockImplementationOnce(async () => ({
body: {
hits: {
total: 2,
hits: [{ fakeActionKey: 'fakeActionValue' }, { fakeActionKey: 'fakeActionValue2' }],
},
},
soClient.find.mockImplementationOnce(async () => ({
total: 0,
per_page: 0,
page: 1,
saved_objects: [
{ score: 0, id: 'fake id 1', type: 'action', attributes: {}, references: [] },
{ score: 0, id: 'fake id 2', type: 'action', attributes: {}, references: [] },
],
}));
const result = await swapActionIds(
mockAction,
clients.core.elasticsearch.client.asInternalUser
);
const result = await swapActionIds(mockAction, soClient);
expect(result instanceof Error).toBeTruthy();
expect((result as unknown as Error).message).toEqual(
'action connector with originId: some-7.x-id had conflicts. Please resolve these conflicts either in the file you are attempting to upload or resolve the conflicting action connector in Kibana.'
);
});

test('returns action with new migrated _id if a single hit is found when querying by action connector originId', async () => {
// @ts-expect-error
clients.core.elasticsearch.client.asInternalUser.search.mockImplementationOnce(async () => ({
body: {
hits: {
total: 1,
hits: [{ _id: 'action:new-post-8.0-id' }],
},
},
soClient.find.mockImplementationOnce(async () => ({
total: 0,
per_page: 0,
page: 1,
saved_objects: [
{ score: 0, id: 'new-post-8.0-id', type: 'action', attributes: {}, references: [] },
],
}));
const result = await swapActionIds(
mockAction,
clients.core.elasticsearch.client.asInternalUser
);
const result = await swapActionIds(mockAction, soClient);
expect(result).toEqual({ ...mockAction, id: 'new-post-8.0-id' });
});
});
Expand All @@ -736,25 +724,29 @@ describe.each([
action_type_id: '.slack',
params: {},
};
const soClient = clients.core.savedObjects.getClient();
beforeEach(() => {
soClient.find.mockReset();
soClient.find.mockClear();
});
test('returns import rules schemas + migrated action', async () => {
const rule: ReturnType<typeof getCreateRulesSchemaMock> = {
...getCreateRulesSchemaMock('rule-1'),
actions: [mockAction],
};
// @ts-expect-error
clients.core.elasticsearch.client.asInternalUser.search.mockImplementationOnce(async () => ({
body: {
hits: {
total: 1,
hits: [{ _id: 'action:new-post-8.0-id' }],
},
},
soClient.find.mockImplementationOnce(async () => ({
total: 0,
per_page: 0,
page: 1,
saved_objects: [
{ score: 0, id: 'new-post-8.0-id', type: 'action', attributes: {}, references: [] },
],
}));

const res = await migrateLegacyActionsIds(
// @ts-expect-error
[rule],
clients.core.elasticsearch.client.asInternalUser
soClient
);
expect(res).toEqual([{ ...rule, actions: [{ ...mockAction, id: 'new-post-8.0-id' }] }]);
});
Expand All @@ -764,20 +756,19 @@ describe.each([
...getCreateRulesSchemaMock('rule-1'),
actions: [mockAction, { ...mockAction, id: 'different-id' }],
};
// @ts-expect-error
clients.core.elasticsearch.client.asInternalUser.search.mockImplementation(async () => ({
body: {
hits: {
total: 1,
hits: [{ _id: 'action:new-post-8.0-id' }],
},
},
soClient.find.mockImplementation(async () => ({
total: 0,
per_page: 0,
page: 1,
saved_objects: [
{ score: 0, id: 'new-post-8.0-id', type: 'action', attributes: {}, references: [] },
],
}));

const res = await migrateLegacyActionsIds(
// @ts-expect-error
[rule],
clients.core.elasticsearch.client.asInternalUser
soClient
);
expect(res).toEqual([
{
Expand All @@ -795,20 +786,20 @@ describe.each([
...getCreateRulesSchemaMock('rule-1'),
actions: [mockAction],
};
// @ts-expect-error
clients.core.elasticsearch.client.asInternalUser.search.mockImplementationOnce(async () => ({
body: {
hits: {
total: 2,
hits: [{ fakeActionKey: 'fakeActionValue' }, { fakeActionKey: 'fakeActionValue2' }],
},
},
soClient.find.mockImplementationOnce(async () => ({
total: 0,
per_page: 0,
page: 1,
saved_objects: [
{ score: 0, id: 'new-post-8.0-id', type: 'action', attributes: {}, references: [] },
{ score: 0, id: 'new-post-8.0-id-2', type: 'action', attributes: {}, references: [] },
],
}));

const res = await migrateLegacyActionsIds(
// @ts-expect-error
[rule],
clients.core.elasticsearch.client.asInternalUser
soClient
);
expect(res[0] instanceof Error).toBeTruthy();
expect((res[0] as unknown as Error).message).toEqual(
Expand All @@ -828,29 +819,28 @@ describe.each([
actions: [mockAction],
};

// @ts-expect-error
clients.core.elasticsearch.client.asInternalUser.search.mockImplementationOnce(async () => ({
body: {
hits: {
total: 1,
hits: [{ _id: 'action:new-post-8.0-id' }],
},
},
soClient.find.mockImplementationOnce(async () => ({
total: 0,
per_page: 0,
page: 1,
saved_objects: [
{ score: 0, id: 'new-post-8.0-id', type: 'action', attributes: {}, references: [] },
],
}));
// @ts-expect-error
clients.core.elasticsearch.client.asInternalUser.search.mockImplementationOnce(async () => ({
body: {
hits: {
total: 2,
hits: [{ fakeActionKey: 'fakeActionValue' }, { fakeActionKey: 'fakeActionValue2' }],
},
},
soClient.find.mockImplementationOnce(async () => ({
total: 0,
per_page: 0,
page: 1,
saved_objects: [
{ score: 0, id: 'new-post-8.0-id', type: 'action', attributes: {}, references: [] },
{ score: 0, id: 'new-post-8.0-id-2', type: 'action', attributes: {}, references: [] },
],
}));

const res = await migrateLegacyActionsIds(
// @ts-expect-error
[rule, rule],
clients.core.elasticsearch.client.asInternalUser
soClient
);
expect(res[0]).toEqual({ ...rule, actions: [{ ...mockAction, id: 'new-post-8.0-id' }] });
expect(res[1] instanceof Error).toBeTruthy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { countBy } from 'lodash/fp';
import uuid from 'uuid';
import { Action } from '@kbn/securitysolution-io-ts-alerting-types';
import { ElasticsearchClient, SavedObjectsClientContract } from 'kibana/server';
import { SavedObjectsClientContract } from 'kibana/server';

import { RulesSchema } from '../../../../../common/detection_engine/schemas/response/rules_schema';
import { ImportRulesSchemaDecoded } from '../../../../../common/detection_engine/schemas/request/import_rules_schema';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ export default ({ getService }: FtrProviderContext): void => {
});
});

it.only('importing a non-default-space 7.16 rule with a connector made in the non-default space should result in a 200', async () => {
it('importing a non-default-space 7.16 rule with a connector made in the non-default space should result in a 200', async () => {
await esArchiver.load(
'x-pack/test/functional/es_archives/security_solution/import_rule_connector'
);
Expand Down Expand Up @@ -560,7 +560,6 @@ export default ({ getService }: FtrProviderContext): void => {
.set('kbn-xsrf', 'true')
.attach('file', buffer, 'rules.ndjson')
.expect(200);
console.error('WHAT HAPPENED', JSON.stringify(body, null, 2));
expect(body.success).to.eql(true);
expect(body.success_count).to.eql(1);
expect(body.errors.length).to.eql(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,64 +13,64 @@ export default ({ loadTestFile }: FtrProviderContext): void => {
describe('', function () {
this.tags('ciGroup11');

// loadTestFile(require.resolve('./aliases'));
// loadTestFile(require.resolve('./create_endpoint_exceptions'));
// loadTestFile(require.resolve('./add_actions'));
// loadTestFile(require.resolve('./update_actions'));
// loadTestFile(require.resolve('./add_prepackaged_rules'));
// loadTestFile(require.resolve('./check_privileges'));
// loadTestFile(require.resolve('./create_index'));
// loadTestFile(require.resolve('./create_rules'));
// loadTestFile(require.resolve('./preview_rules'));
// loadTestFile(require.resolve('./create_rules_bulk'));
// loadTestFile(require.resolve('./create_ml'));
// loadTestFile(require.resolve('./create_threat_matching'));
// loadTestFile(require.resolve('./create_exceptions'));
// loadTestFile(require.resolve('./delete_rules'));
// loadTestFile(require.resolve('./delete_rules_bulk'));
// loadTestFile(require.resolve('./export_rules'));
// loadTestFile(require.resolve('./find_rules'));
// loadTestFile(require.resolve('./generating_signals'));
// loadTestFile(require.resolve('./get_prepackaged_rules_status'));
loadTestFile(require.resolve('./aliases'));
loadTestFile(require.resolve('./create_endpoint_exceptions'));
loadTestFile(require.resolve('./add_actions'));
loadTestFile(require.resolve('./update_actions'));
loadTestFile(require.resolve('./add_prepackaged_rules'));
loadTestFile(require.resolve('./check_privileges'));
loadTestFile(require.resolve('./create_index'));
loadTestFile(require.resolve('./create_rules'));
loadTestFile(require.resolve('./preview_rules'));
loadTestFile(require.resolve('./create_rules_bulk'));
loadTestFile(require.resolve('./create_ml'));
loadTestFile(require.resolve('./create_threat_matching'));
loadTestFile(require.resolve('./create_exceptions'));
loadTestFile(require.resolve('./delete_rules'));
loadTestFile(require.resolve('./delete_rules_bulk'));
loadTestFile(require.resolve('./export_rules'));
loadTestFile(require.resolve('./find_rules'));
loadTestFile(require.resolve('./generating_signals'));
loadTestFile(require.resolve('./get_prepackaged_rules_status'));
loadTestFile(require.resolve('./import_rules'));
// loadTestFile(require.resolve('./read_rules'));
// loadTestFile(require.resolve('./resolve_read_rules'));
// loadTestFile(require.resolve('./update_rules'));
// loadTestFile(require.resolve('./update_rules_bulk'));
// loadTestFile(require.resolve('./patch_rules_bulk'));
// loadTestFile(require.resolve('./perform_bulk_action'));
// loadTestFile(require.resolve('./patch_rules'));
// loadTestFile(require.resolve('./read_privileges'));
// loadTestFile(require.resolve('./open_close_signals'));
// loadTestFile(require.resolve('./get_signals_migration_status'));
// loadTestFile(require.resolve('./create_signals_migrations'));
// loadTestFile(require.resolve('./finalize_signals_migrations'));
// loadTestFile(require.resolve('./delete_signals_migrations'));
// loadTestFile(require.resolve('./timestamps'));
// loadTestFile(require.resolve('./runtime'));
// loadTestFile(require.resolve('./throttle'));
// loadTestFile(require.resolve('./ignore_fields'));
// loadTestFile(require.resolve('./migrations'));
loadTestFile(require.resolve('./read_rules'));
loadTestFile(require.resolve('./resolve_read_rules'));
loadTestFile(require.resolve('./update_rules'));
loadTestFile(require.resolve('./update_rules_bulk'));
loadTestFile(require.resolve('./patch_rules_bulk'));
loadTestFile(require.resolve('./perform_bulk_action'));
loadTestFile(require.resolve('./patch_rules'));
loadTestFile(require.resolve('./read_privileges'));
loadTestFile(require.resolve('./open_close_signals'));
loadTestFile(require.resolve('./get_signals_migration_status'));
loadTestFile(require.resolve('./create_signals_migrations'));
loadTestFile(require.resolve('./finalize_signals_migrations'));
loadTestFile(require.resolve('./delete_signals_migrations'));
loadTestFile(require.resolve('./timestamps'));
loadTestFile(require.resolve('./runtime'));
loadTestFile(require.resolve('./throttle'));
loadTestFile(require.resolve('./ignore_fields'));
loadTestFile(require.resolve('./migrations'));
});

// That split here enable us on using a different ciGroup to run the tests
// listed on ./exception_operators_data_types/index
// describe('', function () {
// loadTestFile(require.resolve('./exception_operators_data_types/index'));
// });
describe('', function () {
loadTestFile(require.resolve('./exception_operators_data_types/index'));
});

// That split here enable us on using a different ciGroup to run the tests
// listed on ./keyword_family/index
// describe('', function () {
// loadTestFile(require.resolve('./keyword_family/index'));
// });
describe('', function () {
loadTestFile(require.resolve('./keyword_family/index'));
});

// describe('', function () {
// loadTestFile(require.resolve('./alerts/index'));
// });
describe('', function () {
loadTestFile(require.resolve('./alerts/index'));
});

// describe('', function () {
// loadTestFile(require.resolve('./telemetry/index'));
// });
describe('', function () {
loadTestFile(require.resolve('./telemetry/index'));
});
});
};

0 comments on commit 9e0be05

Please sign in to comment.