Skip to content

Commit

Permalink
add rule actions unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maximpn committed Oct 11, 2022
1 parent 2f873ee commit fc207d2
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* 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 { performBulkAction } from '../../../../containers/detection_engine/rules';
import { METRIC_TYPE, TELEMETRY_EVENT, track } from '../../../../../common/lib/telemetry';

import { performTrackableBulkAction } from './actions';

jest.mock('../../../../containers/detection_engine/rules');
jest.mock('../../../../../common/lib/telemetry');

describe('Rule Actions', () => {
describe('performTrackableBulkAction', () => {
describe('for an immutable rule', () => {
beforeEach(() => {
(performBulkAction as jest.Mock).mockResolvedValue({
attributes: {
results: {
updated: [
{
immutable: true,
},
],
},
},
});
});

test('sends enable action telemetry', async () => {
await performTrackableBulkAction('enable', 'some query');

expect(track).toHaveBeenCalledWith(METRIC_TYPE.COUNT, TELEMETRY_EVENT.SIEM_RULE_ENABLED);
});

test('sends disable action telemetry', async () => {
await performTrackableBulkAction('disable', 'some query');

expect(track).toHaveBeenCalledWith(METRIC_TYPE.COUNT, TELEMETRY_EVENT.SIEM_RULE_DISABLED);
});
});

describe('for a mutable rule', () => {
beforeEach(() => {
(performBulkAction as jest.Mock).mockResolvedValue({
attributes: {
results: {
updated: [
{
immutable: false,
},
],
},
},
});
});

test('sends enable action telemetry', async () => {
await performTrackableBulkAction('enable', 'some query');

expect(track).toHaveBeenCalledWith(METRIC_TYPE.COUNT, TELEMETRY_EVENT.CUSTOM_RULE_ENABLED);
});

test('sends disable action telemetry', async () => {
await performTrackableBulkAction('disable', 'some query');

expect(track).toHaveBeenCalledWith(METRIC_TYPE.COUNT, TELEMETRY_EVENT.CUSTOM_RULE_DISABLED);
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,8 @@ function sendTelemetry(action: Omit<BulkAction, BulkAction.export>, response: Bu
track(
METRIC_TYPE.COUNT,
action === BulkAction.disable
? TELEMETRY_EVENT.CUSTOM_RULE_ENABLED
: TELEMETRY_EVENT.CUSTOM_RULE_DISABLED
? TELEMETRY_EVENT.CUSTOM_RULE_DISABLED
: TELEMETRY_EVENT.CUSTOM_RULE_ENABLED
);
}
}

0 comments on commit fc207d2

Please sign in to comment.