Skip to content

Commit

Permalink
Fixed type checks. Moved slack simulator to own server
Browse files Browse the repository at this point in the history
  • Loading branch information
YulNaumenko committed Aug 6, 2020
1 parent 1b84ffe commit 71dce90
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,23 @@
* you may not use this file except in compliance with the Elastic License.
*/

import http from 'http';
import { FtrProviderContext } from '../../../../common/ftr_provider_context';

import {
getExternalServiceSimulatorPath,
ExternalServiceSimulator,
} from '../../../../common/fixtures/plugins/actions_simulators/server/plugin';
import { getSlackServer } from '../../../../common/fixtures/plugins/actions_simulators/server/plugin';

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

describe('slack action', () => {
let slackSimulatorURL: string = '<could not determine kibana url>';
let slackSimulatorURL: string = '';
let slackServer: http.Server;

// need to wait for kibanaServer to settle ...
before(() => {
slackSimulatorURL = kibanaServer.resolveUrl(
getExternalServiceSimulatorPath(ExternalServiceSimulator.SLACK)
);
before(async () => {
slackServer = await getSlackServer();
slackServer.listen(9005);
slackSimulatorURL = 'http://localhost:9005';
});

it('should return 403 when creating a slack action', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,11 @@

import http from 'http';
import { FtrProviderContext } from '../../../../common/ftr_provider_context';
import {
getExternalServiceSimulatorPath,
ExternalServiceSimulator,
getWebhookServer,
} from '../../../../common/fixtures/plugins/actions_simulators/server/plugin';
import { getWebhookServer } from '../../../../common/fixtures/plugins/actions_simulators/server/plugin';

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

describe('webhook action', () => {
let webhookSimulatorURL: string = '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,14 @@ export function getAllExternalServiceSimulatorPaths(): string[] {
return allPaths;
}

export async function getWebhookServer(): http.Server {
export async function getWebhookServer(): Promise<http.Server> {
return await initWebhook();
}

export async function getSlackServer(): Promise<http.Server> {
return await initSlack();
}

interface FixtureSetupDeps {
actions: ActionsPluginSetupContract;
features: FeaturesPluginSetup;
Expand All @@ -56,8 +60,6 @@ interface FixtureStartDeps {
}

export class FixturePlugin implements Plugin<void, void, FixtureSetupDeps, FixtureStartDeps> {
private webhookHttpServer: http.Server | undefined = undefined;

public setup(core: CoreSetup<FixtureStartDeps>, { features, actions }: FixtureSetupDeps) {
// this action is specifically NOT enabled in ../../config.ts
const notEnabledActionType: ActionType = {
Expand Down Expand Up @@ -101,7 +103,6 @@ export class FixturePlugin implements Plugin<void, void, FixtureSetupDeps, Fixtu
initServiceNow(router, getExternalServiceSimulatorPath(ExternalServiceSimulator.SERVICENOW));
initJira(router, getExternalServiceSimulatorPath(ExternalServiceSimulator.JIRA));
initResilient(router, getExternalServiceSimulatorPath(ExternalServiceSimulator.RESILIENT));
initSlack(router, getExternalServiceSimulatorPath(ExternalServiceSimulator.SLACK));
}

public start() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,76 +4,66 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { schema } from '@kbn/config-schema';
import {
RequestHandlerContext,
KibanaRequest,
KibanaResponseFactory,
IKibanaResponse,
IRouter,
} from 'kibana/server';
import http from 'http';

export function initPlugin(router: IRouter, path: string) {
router.post(
{
path,
options: {
authRequired: false,
},
validate: {
body: schema.object({
text: schema.string(),
}),
},
},
// ServiceNow simulator: create a servicenow action pointing here, and you can get
// different responses based on the message posted. See the README.md for
// more info.
async function (
context: RequestHandlerContext,
req: KibanaRequest<any, any, any, any>,
res: KibanaResponseFactory
): Promise<IKibanaResponse<any>> {
const body = req.body;
const text = body && body.text;
export async function initPlugin() {
return http.createServer((request, response) => {
if (request.method === 'POST') {
let data = '';
request.on('data', (chunk) => {
data += chunk;
});
request.on('end', () => {
const body = JSON.parse(data);
const text = body && body.text;

if (text == null) {
return res.badRequest({ body: 'bad request to slack simulator' });
}
if (text == null) {
response.statusCode = 400;
response.end('bad request to slack simulator');
return;
}

switch (text) {
case 'success':
return res.ok({ body: 'ok' });
switch (text) {
case 'success': {
response.statusCode = 200;
response.end('ok');
return;
}
case 'no_text':
response.statusCode = 400;
response.end('no_text');
return;

case 'no_text':
return res.badRequest({ body: 'no_text' });
case 'invalid_payload':
response.statusCode = 400;
response.end('invalid_payload');
return;

case 'invalid_payload':
return res.badRequest({ body: 'invalid_payload' });
case 'invalid_token':
response.statusCode = 403;
response.end('invalid_token');
return;

case 'invalid_token':
return res.forbidden({ body: 'invalid_token' });
case 'status_500':
response.statusCode = 500;
response.end('simulated slack 500 response');
return;

case 'status_500':
return res.internalError({ body: 'simulated slack 500 response' });
case 'rate_limit':
const res = {
retry_after: 1,
ok: false,
error: 'rate_limited',
};

case 'rate_limit':
const response = {
retry_after: 1,
ok: false,
error: 'rate_limited',
};

return res.custom({
body: Buffer.from('ok'),
statusCode: 429,
headers: {
'retry-after': '1',
},
});
}

return res.badRequest({ body: 'unknown request to slack simulator' });
response.writeHead(429, { 'Content-Type': 'application/json', 'Retry-After': '1' });
response.write(JSON.stringify(res));
response.end();
return;
}
response.statusCode = 400;
response.end('unknown request to slack simulator');
});
}
);
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export async function initPlugin() {
}
response.statusCode = 400;
response.end(
`unknown request to webhook simulator [${body ? `content: ${body}` : `no content`}]`
`unknown request to webhook simulator [${data ? `content: ${data}` : `no content`}]`
);
return;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,25 @@
*/

import expect from '@kbn/expect';

import http from 'http';
import { FtrProviderContext } from '../../../../common/ftr_provider_context';

import {
getExternalServiceSimulatorPath,
ExternalServiceSimulator,
} from '../../../../common/fixtures/plugins/actions_simulators/server/plugin';
import { getSlackServer } from '../../../../common/fixtures/plugins/actions_simulators/server/plugin';

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

describe('slack action', () => {
let simulatedActionId = '';
let slackSimulatorURL: string = '<could not determine kibana url>';

let slackSimulatorURL: string = '';
let slackServer: http.Server;
// need to wait for kibanaServer to settle ...
before(() => {
slackSimulatorURL = kibanaServer.resolveUrl(
getExternalServiceSimulatorPath(ExternalServiceSimulator.SLACK)
);
before(async () => {
slackServer = await getSlackServer();
slackServer.listen(9004);
slackSimulatorURL = 'http://localhost:9004';
});

it('should return 200 when creating a slack action successfully', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { getWebhookServer } from '../../../../common/fixtures/plugins/actions_si
// eslint-disable-next-line import/no-default-export
export default function webhookTest({ getService }: FtrProviderContext) {
const supertest = getService('supertest');
const kibanaServer = getService('kibanaServer');

async function createWebhookAction(
webhookSimulatorURL: string,
Expand Down Expand Up @@ -45,7 +44,6 @@ export default function webhookTest({ getService }: FtrProviderContext) {
describe('webhook action', () => {
let webhookSimulatorURL: string = '';
let webhookServer: http.Server;
// need to wait for kibanaServer to settle ...
before(async () => {
webhookServer = await getWebhookServer();
webhookServer.listen(9003);
Expand Down

0 comments on commit 71dce90

Please sign in to comment.