Skip to content

Commit

Permalink
Merge branch 'master' of github.com:elastic/kibana into newplatform/p…
Browse files Browse the repository at this point in the history
…lugin-gen-paths
  • Loading branch information
Liza K committed Feb 19, 2020
2 parents c679c66 + ca3f27a commit 8ef6f05
Show file tree
Hide file tree
Showing 9 changed files with 695 additions and 29 deletions.
6 changes: 4 additions & 2 deletions src/plugins/expressions/common/execution/execution.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,16 +336,18 @@ describe('Execution', () => {
});

test('execution state is "pending" while execution is in progress', async () => {
jest.useFakeTimers();
const execution = createExecution('sleep 20');
execution.start(null);
await new Promise(r => setTimeout(r, 5));
jest.advanceTimersByTime(5);
expect(execution.state.get().state).toBe('pending');
jest.useRealTimers();
});

test('execution state is "result" when execution successfully completes', async () => {
const execution = createExecution('sleep 1');
execution.start(null);
await new Promise(r => setTimeout(r, 30));
await execution.result;
expect(execution.state.get().state).toBe('result');
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
import { ShardsResponse } from '../../../types';
import { RuleAlertType, IRuleSavedAttributesSavedObjectAttributes } from '../../rules/types';
import { RuleAlertParamsRest, PrepackagedRules } from '../../types';
import { TEST_BOUNDARY } from './utils';

export const mockPrepackagedRule = (): PrepackagedRules => ({
rule_id: 'rule-1',
Expand Down Expand Up @@ -224,6 +225,24 @@ export const getFindResultWithMultiHits = ({
};
};

export const getImportRulesRequest = (payload?: Buffer): ServerInjectOptions => ({
method: 'POST',
url: `${DETECTION_ENGINE_RULES_URL}/_import`,
headers: {
'Content-Type': `multipart/form-data; boundary=${TEST_BOUNDARY}`,
},
payload,
});

export const getImportRulesRequestOverwriteTrue = (payload?: Buffer): ServerInjectOptions => ({
method: 'POST',
url: `${DETECTION_ENGINE_RULES_URL}/_import?overwrite=true`,
headers: {
'Content-Type': `multipart/form-data; boundary=${TEST_BOUNDARY}`,
},
payload,
});

export const getDeleteRequest = (): ServerInjectOptions => ({
method: 'DELETE',
url: `${DETECTION_ENGINE_RULES_URL}?rule_id=rule-1`,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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 { OutputRuleAlertRest } from '../../types';

export const TEST_BOUNDARY = 'test_multipart_boundary';

// Not parsable due to extra colon following `name` property - name::
export const UNPARSABLE_LINE =
'{"name"::"Simple Rule Query","description":"Simple Rule Query","risk_score":1,"rule_id":"rule-1","severity":"high","type":"query","query":"user.name: root or user.name: admin"}';

/**
* This is a typical simple rule for testing that is easy for most basic testing
* @param ruleId
*/
export const getSimpleRule = (ruleId = 'rule-1'): Partial<OutputRuleAlertRest> => ({
name: 'Simple Rule Query',
description: 'Simple Rule Query',
risk_score: 1,
rule_id: ruleId,
severity: 'high',
type: 'query',
query: 'user.name: root or user.name: admin',
});

/**
* Given an array of rule_id strings this will return a ndjson buffer which is useful
* for testing uploads.
* @param ruleIds Array of strings of rule_ids
* @param isNdjson Boolean to determine file extension
*/
export const getSimpleRuleAsMultipartContent = (ruleIds: string[], isNdjson = true): Buffer => {
const arrayOfRules = ruleIds.map(ruleId => {
const simpleRule = getSimpleRule(ruleId);
return JSON.stringify(simpleRule);
});
const stringOfRules = arrayOfRules.join('\r\n');

const resultingPayload =
`--${TEST_BOUNDARY}\r\n` +
`Content-Disposition: form-data; name="file"; filename="rules.${
isNdjson ? 'ndjson' : 'json'
}\r\n` +
'Content-Type: application/octet-stream\r\n' +
'\r\n' +
`${stringOfRules}\r\n` +
`--${TEST_BOUNDARY}--\r\n`;

return Buffer.from(resultingPayload);
};
Loading

0 comments on commit 8ef6f05

Please sign in to comment.