Skip to content

Commit

Permalink
Remove unnecessary casting of our alerts client mock
Browse files Browse the repository at this point in the history
I think that this was the result of us importing the wrong AlertsClient
type, or perhaps the types were out of sync. Regardless, they work now.
  • Loading branch information
rylnd committed Feb 10, 2020
1 parent dfd81cd commit 11f3581
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

import { alertsClientMock } from '../../../../../alerting/server/alerts_client.mock';
import { AlertsClient } from '../../../../../alerting';
import {
getResult,
getFindResultWithSingleHit,
Expand All @@ -28,10 +27,7 @@ describe('get_existing_prepackaged_rules', () => {
test('should return a single item in a single page', async () => {
const alertsClient = alertsClientMock.create();
alertsClient.find.mockResolvedValue(getFindResultWithSingleHit());
const unsafeCast: AlertsClient = (alertsClient as unknown) as AlertsClient;
const rules = await getExistingPrepackagedRules({
alertsClient: unsafeCast,
});
const rules = await getExistingPrepackagedRules({ alertsClient });
expect(rules).toEqual([getResult()]);
});

Expand Down Expand Up @@ -70,10 +66,7 @@ describe('get_existing_prepackaged_rules', () => {
})
);

const unsafeCast: AlertsClient = (alertsClient as unknown) as AlertsClient;
const rules = await getExistingPrepackagedRules({
alertsClient: unsafeCast,
});
const rules = await getExistingPrepackagedRules({ alertsClient });
expect(rules).toEqual([result1, result2, result3]);
});
});
Expand All @@ -82,10 +75,7 @@ describe('get_existing_prepackaged_rules', () => {
test('should return a single item in a single page', async () => {
const alertsClient = alertsClientMock.create();
alertsClient.find.mockResolvedValue(getFindResultWithSingleHit());
const unsafeCast: AlertsClient = (alertsClient as unknown) as AlertsClient;
const rules = await getNonPackagedRules({
alertsClient: unsafeCast,
});
const rules = await getNonPackagedRules({ alertsClient });
expect(rules).toEqual([getResult()]);
});

Expand Down Expand Up @@ -113,10 +103,7 @@ describe('get_existing_prepackaged_rules', () => {
getFindResultWithMultiHits({ data: [result1, result2], perPage: 2, page: 1, total: 2 })
);

const unsafeCast: AlertsClient = (alertsClient as unknown) as AlertsClient;
const rules = await getNonPackagedRules({
alertsClient: unsafeCast,
});
const rules = await getNonPackagedRules({ alertsClient });
expect(rules).toEqual([result1, result2]);
});

Expand Down Expand Up @@ -152,10 +139,7 @@ describe('get_existing_prepackaged_rules', () => {
})
);

const unsafeCast: AlertsClient = (alertsClient as unknown) as AlertsClient;
const rules = await getNonPackagedRules({
alertsClient: unsafeCast,
});
const rules = await getNonPackagedRules({ alertsClient });
expect(rules).toEqual([result1, result2, result3]);
});
});
Expand All @@ -164,11 +148,7 @@ describe('get_existing_prepackaged_rules', () => {
test('should return a single item in a single page', async () => {
const alertsClient = alertsClientMock.create();
alertsClient.find.mockResolvedValue(getFindResultWithSingleHit());
const unsafeCast: AlertsClient = (alertsClient as unknown) as AlertsClient;
const rules = await getRules({
alertsClient: unsafeCast,
filter: '',
});
const rules = await getRules({ alertsClient, filter: '' });
expect(rules).toEqual([getResult()]);
});

Expand Down Expand Up @@ -196,11 +176,7 @@ describe('get_existing_prepackaged_rules', () => {
getFindResultWithMultiHits({ data: [result1, result2], perPage: 2, page: 1, total: 2 })
);

const unsafeCast: AlertsClient = (alertsClient as unknown) as AlertsClient;
const rules = await getRules({
alertsClient: unsafeCast,
filter: '',
});
const rules = await getRules({ alertsClient, filter: '' });
expect(rules).toEqual([result1, result2]);
});
});
Expand All @@ -209,11 +185,7 @@ describe('get_existing_prepackaged_rules', () => {
test('it returns a count', async () => {
const alertsClient = alertsClientMock.create();
alertsClient.find.mockResolvedValue(getFindResultWithSingleHit());
const unsafeCast: AlertsClient = (alertsClient as unknown) as AlertsClient;
const rules = await getRulesCount({
alertsClient: unsafeCast,
filter: '',
});
const rules = await getRulesCount({ alertsClient, filter: '' });
expect(rules).toEqual(1);
});
});
Expand All @@ -222,10 +194,7 @@ describe('get_existing_prepackaged_rules', () => {
test('it returns a count', async () => {
const alertsClient = alertsClientMock.create();
alertsClient.find.mockResolvedValue(getFindResultWithSingleHit());
const unsafeCast: AlertsClient = (alertsClient as unknown) as AlertsClient;
const rules = await getNonPackagedRulesCount({
alertsClient: unsafeCast,
});
const rules = await getNonPackagedRulesCount({ alertsClient });
expect(rules).toEqual(1);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
getFindResultWithSingleHit,
FindHit,
} from '../routes/__mocks__/request_responses';
import { AlertsClient } from '../../../../../alerting';
import { getExportAll } from './get_export_all';

describe('getExportAll', () => {
Expand All @@ -19,8 +18,7 @@ describe('getExportAll', () => {
alertsClient.get.mockResolvedValue(getResult());
alertsClient.find.mockResolvedValue(getFindResultWithSingleHit());

const unsafeCast: AlertsClient = (alertsClient as unknown) as AlertsClient;
const exports = await getExportAll(unsafeCast);
const exports = await getExportAll(alertsClient);
expect(exports).toEqual({
rulesNdjson:
'{"created_at":"2019-12-13T16:40:33.400Z","updated_at":"2019-12-13T16:40:33.400Z","created_by":"elastic","description":"Detecting root and admin users","enabled":true,"false_positives":[],"filters":[{"query":{"match_phrase":{"host.name":"some-host"}}}],"from":"now-6m","id":"04128c15-0d1b-4716-a4c5-46997ac7f3bd","immutable":false,"index":["auditbeat-*","filebeat-*","packetbeat-*","winlogbeat-*"],"interval":"5m","rule_id":"rule-1","language":"kuery","output_index":".siem-signals","max_signals":100,"risk_score":50,"name":"Detect Root/Admin Users","query":"user.name: root or user.name: admin","references":["http://www.example.com","https://ww.example.com"],"saved_id":"some-id","timeline_id":"some-timeline-id","timeline_title":"some-timeline-title","meta":{"someMeta":"someField"},"severity":"high","updated_by":"elastic","tags":[],"to":"now","type":"query","threat":[{"framework":"MITRE ATT&CK","tactic":{"id":"TA0040","name":"impact","reference":"https://attack.mitre.org/tactics/TA0040/"},"technique":[{"id":"T1499","name":"endpoint denial of service","reference":"https://attack.mitre.org/techniques/T1499/"}]}],"version":1}\n',
Expand All @@ -39,8 +37,7 @@ describe('getExportAll', () => {

alertsClient.find.mockResolvedValue(findResult);

const unsafeCast: AlertsClient = (alertsClient as unknown) as AlertsClient;
const exports = await getExportAll(unsafeCast);
const exports = await getExportAll(alertsClient);
expect(exports).toEqual({
rulesNdjson: '',
exportDetails: '{"exported_count":0,"missing_rules":[],"missing_rules_count":0}\n',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
getFindResultWithSingleHit,
FindHit,
} from '../routes/__mocks__/request_responses';
import { AlertsClient } from '../../../../../alerting';

describe('get_export_by_object_ids', () => {
describe('getExportByObjectIds', () => {
Expand All @@ -20,9 +19,8 @@ describe('get_export_by_object_ids', () => {
alertsClient.get.mockResolvedValue(getResult());
alertsClient.find.mockResolvedValue(getFindResultWithSingleHit());

const unsafeCast: AlertsClient = (alertsClient as unknown) as AlertsClient;
const objects = [{ rule_id: 'rule-1' }];
const exports = await getExportByObjectIds(unsafeCast, objects);
const exports = await getExportByObjectIds(alertsClient, objects);
expect(exports).toEqual({
rulesNdjson:
'{"created_at":"2019-12-13T16:40:33.400Z","updated_at":"2019-12-13T16:40:33.400Z","created_by":"elastic","description":"Detecting root and admin users","enabled":true,"false_positives":[],"filters":[{"query":{"match_phrase":{"host.name":"some-host"}}}],"from":"now-6m","id":"04128c15-0d1b-4716-a4c5-46997ac7f3bd","immutable":false,"index":["auditbeat-*","filebeat-*","packetbeat-*","winlogbeat-*"],"interval":"5m","rule_id":"rule-1","language":"kuery","output_index":".siem-signals","max_signals":100,"risk_score":50,"name":"Detect Root/Admin Users","query":"user.name: root or user.name: admin","references":["http://www.example.com","https://ww.example.com"],"saved_id":"some-id","timeline_id":"some-timeline-id","timeline_title":"some-timeline-title","meta":{"someMeta":"someField"},"severity":"high","updated_by":"elastic","tags":[],"to":"now","type":"query","threat":[{"framework":"MITRE ATT&CK","tactic":{"id":"TA0040","name":"impact","reference":"https://attack.mitre.org/tactics/TA0040/"},"technique":[{"id":"T1499","name":"endpoint denial of service","reference":"https://attack.mitre.org/techniques/T1499/"}]}],"version":1}\n',
Expand All @@ -45,9 +43,8 @@ describe('get_export_by_object_ids', () => {
alertsClient.get.mockResolvedValue(getResult());
alertsClient.find.mockResolvedValue(findResult);

const unsafeCast: AlertsClient = (alertsClient as unknown) as AlertsClient;
const objects = [{ rule_id: 'rule-1' }];
const exports = await getExportByObjectIds(unsafeCast, objects);
const exports = await getExportByObjectIds(alertsClient, objects);
expect(exports).toEqual({
rulesNdjson: '',
exportDetails:
Expand All @@ -62,9 +59,8 @@ describe('get_export_by_object_ids', () => {
alertsClient.get.mockResolvedValue(getResult());
alertsClient.find.mockResolvedValue(getFindResultWithSingleHit());

const unsafeCast: AlertsClient = (alertsClient as unknown) as AlertsClient;
const objects = [{ rule_id: 'rule-1' }];
const exports = await getRulesFromObjects(unsafeCast, objects);
const exports = await getRulesFromObjects(alertsClient, objects);
const expected: RulesErrors = {
exportedCount: 1,
missingRules: [],
Expand Down Expand Up @@ -138,9 +134,8 @@ describe('get_export_by_object_ids', () => {
alertsClient.get.mockResolvedValue(result);
alertsClient.find.mockResolvedValue(findResult);

const unsafeCast: AlertsClient = (alertsClient as unknown) as AlertsClient;
const objects = [{ rule_id: 'rule-1' }];
const exports = await getRulesFromObjects(unsafeCast, objects);
const exports = await getRulesFromObjects(alertsClient, objects);
const expected: RulesErrors = {
exportedCount: 0,
missingRules: [{ rule_id: 'rule-1' }],
Expand All @@ -162,9 +157,8 @@ describe('get_export_by_object_ids', () => {
alertsClient.get.mockRejectedValue({ output: { statusCode: 404 } });
alertsClient.find.mockResolvedValue(findResult);

const unsafeCast: AlertsClient = (alertsClient as unknown) as AlertsClient;
const objects = [{ rule_id: 'rule-1' }];
const exports = await getRulesFromObjects(unsafeCast, objects);
const exports = await getRulesFromObjects(alertsClient, objects);
const expected: RulesErrors = {
exportedCount: 0,
missingRules: [{ rule_id: 'rule-1' }],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import { alertsClientMock } from '../../../../../alerting/server/alerts_client.mock';
import { readRules } from './read_rules';
import { AlertsClient } from '../../../../../alerting';
import { getResult, getFindResultWithSingleHit } from '../routes/__mocks__/request_responses';

describe('read_rules', () => {
Expand All @@ -15,9 +14,8 @@ describe('read_rules', () => {
const alertsClient = alertsClientMock.create();
alertsClient.get.mockResolvedValue(getResult());

const unsafeCast: AlertsClient = (alertsClient as unknown) as AlertsClient;
const rule = await readRules({
alertsClient: unsafeCast,
alertsClient,
id: '04128c15-0d1b-4716-a4c5-46997ac7f3bd',
ruleId: undefined,
});
Expand All @@ -28,9 +26,8 @@ describe('read_rules', () => {
const alertsClient = alertsClientMock.create();
alertsClient.get.mockResolvedValue(getResult());

const unsafeCast: AlertsClient = (alertsClient as unknown) as AlertsClient;
const rule = await readRules({
alertsClient: unsafeCast,
alertsClient,
id: '04128c15-0d1b-4716-a4c5-46997ac7f3bd',
ruleId: null,
});
Expand All @@ -42,9 +39,8 @@ describe('read_rules', () => {
alertsClient.get.mockResolvedValue(getResult());
alertsClient.find.mockResolvedValue(getFindResultWithSingleHit());

const unsafeCast: AlertsClient = (alertsClient as unknown) as AlertsClient;
const rule = await readRules({
alertsClient: unsafeCast,
alertsClient,
id: undefined,
ruleId: 'rule-1',
});
Expand All @@ -56,9 +52,8 @@ describe('read_rules', () => {
alertsClient.get.mockResolvedValue(getResult());
alertsClient.find.mockResolvedValue(getFindResultWithSingleHit());

const unsafeCast: AlertsClient = (alertsClient as unknown) as AlertsClient;
const rule = await readRules({
alertsClient: unsafeCast,
alertsClient,
id: null,
ruleId: 'rule-1',
});
Expand All @@ -70,9 +65,8 @@ describe('read_rules', () => {
alertsClient.get.mockResolvedValue(getResult());
alertsClient.find.mockResolvedValue(getFindResultWithSingleHit());

const unsafeCast: AlertsClient = (alertsClient as unknown) as AlertsClient;
const rule = await readRules({
alertsClient: unsafeCast,
alertsClient,
id: null,
ruleId: null,
});
Expand All @@ -84,9 +78,8 @@ describe('read_rules', () => {
alertsClient.get.mockResolvedValue(getResult());
alertsClient.find.mockResolvedValue(getFindResultWithSingleHit());

const unsafeCast: AlertsClient = (alertsClient as unknown) as AlertsClient;
const rule = await readRules({
alertsClient: unsafeCast,
alertsClient,
id: undefined,
ruleId: undefined,
});
Expand Down
Loading

0 comments on commit 11f3581

Please sign in to comment.