Skip to content

Commit 489ddc4

Browse files
authored
Migrated last pieces of legacy fixture code (#74470) (#74904)
* Migrated last pieces of legacy fixture code * Implemented own server for webhook simulator * Fixed type checks. Moved slack simulator to own server * close server after tests run * Fixed due to comments * fixed failing tests
1 parent 1c47a5d commit 489ddc4

14 files changed

Lines changed: 269 additions & 282 deletions

File tree

x-pack/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,8 @@
256256
"cronstrue": "^1.51.0",
257257
"cytoscape": "^3.10.0",
258258
"d3": "3.5.17",
259-
"d3-scale": "1.0.7",
260259
"d3-array": "1.2.4",
260+
"d3-scale": "1.0.7",
261261
"dedent": "^0.7.0",
262262
"del": "^5.1.0",
263263
"dragselect": "1.13.1",
@@ -268,7 +268,7 @@
268268
"font-awesome": "4.7.0",
269269
"formsy-react": "^1.1.5",
270270
"fp-ts": "^2.3.1",
271-
"get-port": "4.2.0",
271+
"get-port": "^4.2.0",
272272
"getos": "^3.1.0",
273273
"git-url-parse": "11.1.2",
274274
"github-markdown-css": "^2.10.0",

x-pack/test/alerting_api_integration/basic/tests/actions/builtin_action_types/slack.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,25 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7+
import http from 'http';
8+
import getPort from 'get-port';
79
import { FtrProviderContext } from '../../../../common/ftr_provider_context';
810

9-
import {
10-
getExternalServiceSimulatorPath,
11-
ExternalServiceSimulator,
12-
} from '../../../../common/fixtures/plugins/actions_simulators/server/plugin';
11+
import { getSlackServer } from '../../../../common/fixtures/plugins/actions_simulators/server/plugin';
1312

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

1917
describe('slack action', () => {
20-
let slackSimulatorURL: string = '<could not determine kibana url>';
18+
let slackSimulatorURL: string = '';
19+
let slackServer: http.Server;
2120

22-
// need to wait for kibanaServer to settle ...
23-
before(() => {
24-
slackSimulatorURL = kibanaServer.resolveUrl(
25-
getExternalServiceSimulatorPath(ExternalServiceSimulator.SLACK)
26-
);
21+
before(async () => {
22+
slackServer = await getSlackServer();
23+
const availablePort = await getPort({ port: 9000 });
24+
slackServer.listen(availablePort);
25+
slackSimulatorURL = `http://localhost:${availablePort}`;
2726
});
2827

2928
it('should return 403 when creating a slack action', async () => {
@@ -44,5 +43,9 @@ export default function slackTest({ getService }: FtrProviderContext) {
4443
'Action type .slack is disabled because your basic license does not support it. Please upgrade your license.',
4544
});
4645
});
46+
47+
after(() => {
48+
slackServer.close();
49+
});
4750
});
4851
}

x-pack/test/alerting_api_integration/basic/tests/actions/builtin_action_types/webhook.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,24 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7+
import http from 'http';
8+
import getPort from 'get-port';
79
import { FtrProviderContext } from '../../../../common/ftr_provider_context';
8-
import {
9-
getExternalServiceSimulatorPath,
10-
ExternalServiceSimulator,
11-
} from '../../../../common/fixtures/plugins/actions_simulators/server/plugin';
10+
import { getWebhookServer } from '../../../../common/fixtures/plugins/actions_simulators/server/plugin';
1211

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

1816
describe('webhook action', () => {
19-
let webhookSimulatorURL: string = '<could not determine kibana url>';
20-
17+
let webhookSimulatorURL: string = '';
18+
let webhookServer: http.Server;
2119
// need to wait for kibanaServer to settle ...
22-
before(() => {
23-
webhookSimulatorURL = kibanaServer.resolveUrl(
24-
getExternalServiceSimulatorPath(ExternalServiceSimulator.WEBHOOK)
25-
);
20+
before(async () => {
21+
webhookServer = await getWebhookServer();
22+
const availablePort = await getPort({ port: 9000 });
23+
webhookServer.listen(availablePort);
24+
webhookSimulatorURL = `http://localhost:${availablePort}`;
2625
});
2726

2827
it('should return 403 when creating a webhook action', async () => {
@@ -47,5 +46,9 @@ export default function webhookTest({ getService }: FtrProviderContext) {
4746
'Action type .webhook is disabled because your basic license does not support it. Please upgrade your license.',
4847
});
4948
});
49+
50+
after(() => {
51+
webhookServer.close();
52+
});
5053
});
5154
}

x-pack/test/alerting_api_integration/common/fixtures/plugins/actions_simulators/server/plugin.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7+
import http from 'http';
78
import { Plugin, CoreSetup, IRouter } from 'kibana/server';
89
import { EncryptedSavedObjectsPluginStart } from '../../../../../../../plugins/encrypted_saved_objects/server';
910
import { PluginSetupContract as FeaturesPluginSetup } from '../../../../../../../plugins/features/server';
@@ -13,6 +14,8 @@ import { initPlugin as initPagerduty } from './pagerduty_simulation';
1314
import { initPlugin as initServiceNow } from './servicenow_simulation';
1415
import { initPlugin as initJira } from './jira_simulation';
1516
import { initPlugin as initResilient } from './resilient_simulation';
17+
import { initPlugin as initSlack } from './slack_simulation';
18+
import { initPlugin as initWebhook } from './webhook_simulation';
1619

1720
export const NAME = 'actions-FTS-external-service-simulators';
1821

@@ -39,6 +42,14 @@ export function getAllExternalServiceSimulatorPaths(): string[] {
3942
return allPaths;
4043
}
4144

45+
export async function getWebhookServer(): Promise<http.Server> {
46+
return await initWebhook();
47+
}
48+
49+
export async function getSlackServer(): Promise<http.Server> {
50+
return await initSlack();
51+
}
52+
4253
interface FixtureSetupDeps {
4354
actions: ActionsPluginSetupContract;
4455
features: FeaturesPluginSetup;
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
import http from 'http';
8+
9+
export async function initPlugin() {
10+
return http.createServer((request, response) => {
11+
if (request.method === 'POST') {
12+
let data = '';
13+
request.on('data', (chunk) => {
14+
data += chunk;
15+
});
16+
request.on('end', () => {
17+
const body = JSON.parse(data);
18+
const text = body && body.text;
19+
20+
if (text == null) {
21+
response.statusCode = 400;
22+
response.end('bad request to slack simulator');
23+
return;
24+
}
25+
26+
switch (text) {
27+
case 'success': {
28+
response.statusCode = 200;
29+
response.end('ok');
30+
return;
31+
}
32+
case 'no_text':
33+
response.statusCode = 400;
34+
response.end('no_text');
35+
return;
36+
37+
case 'invalid_payload':
38+
response.statusCode = 400;
39+
response.end('invalid_payload');
40+
return;
41+
42+
case 'invalid_token':
43+
response.statusCode = 403;
44+
response.end('invalid_token');
45+
return;
46+
47+
case 'status_500':
48+
response.statusCode = 500;
49+
response.end('simulated slack 500 response');
50+
return;
51+
52+
case 'rate_limit':
53+
const res = {
54+
retry_after: 1,
55+
ok: false,
56+
error: 'rate_limited',
57+
};
58+
59+
response.writeHead(429, { 'Content-Type': 'application/json', 'Retry-After': '1' });
60+
response.write(JSON.stringify(res));
61+
response.end();
62+
return;
63+
}
64+
response.statusCode = 400;
65+
response.end('unknown request to slack simulator');
66+
});
67+
}
68+
});
69+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
import expect from '@kbn/expect';
7+
import http from 'http';
8+
import { fromNullable, map, filter, getOrElse } from 'fp-ts/lib/Option';
9+
import { pipe } from 'fp-ts/lib/pipeable';
10+
import { constant } from 'fp-ts/lib/function';
11+
12+
export async function initPlugin() {
13+
return http.createServer((request, response) => {
14+
const credentials = pipe(
15+
fromNullable(request.headers.authorization),
16+
map((authorization) => authorization.split(/\s+/)),
17+
filter((parts) => parts.length > 1),
18+
map((parts) => Buffer.from(parts[1], 'base64').toString()),
19+
filter((credentialsPart) => credentialsPart.indexOf(':') !== -1),
20+
map((credentialsPart) => {
21+
const [username, password] = credentialsPart.split(':');
22+
return { username, password };
23+
}),
24+
getOrElse(constant({ username: '', password: '' }))
25+
);
26+
27+
if (request.method === 'POST' || request.method === 'PUT') {
28+
let data = '';
29+
request.on('data', (chunk) => {
30+
data += chunk;
31+
});
32+
request.on('end', () => {
33+
switch (data) {
34+
case 'success':
35+
response.statusCode = 200;
36+
response.end('OK');
37+
return;
38+
case 'authenticate':
39+
return validateAuthentication(credentials, response);
40+
case 'success_post_method':
41+
return validateRequestUsesMethod(request.method ?? '', 'post', response);
42+
case 'success_put_method':
43+
return validateRequestUsesMethod(request.method ?? '', 'put', response);
44+
case 'failure':
45+
response.statusCode = 500;
46+
response.end('Error');
47+
return;
48+
}
49+
response.statusCode = 400;
50+
response.end(
51+
`unknown request to webhook simulator [${data ? `content: ${data}` : `no content`}]`
52+
);
53+
return;
54+
});
55+
} else {
56+
request.on('end', () => {
57+
response.statusCode = 400;
58+
response.end('unknown request to webhook simulator [no content]');
59+
return;
60+
});
61+
}
62+
});
63+
}
64+
65+
function validateAuthentication(credentials: any, res: any) {
66+
try {
67+
expect(credentials).to.eql({
68+
username: 'elastic',
69+
password: 'changeme',
70+
});
71+
res.statusCode = 200;
72+
res.end('OK');
73+
} catch (ex) {
74+
res.statusCode = 403;
75+
res.end(`the validateAuthentication operation failed. ${ex.message}`);
76+
}
77+
}
78+
79+
function validateRequestUsesMethod(requestMethod: string, method: string, res: any) {
80+
try {
81+
expect(requestMethod.toLowerCase()).to.eql(method);
82+
res.statusCode = 200;
83+
res.end('OK');
84+
} catch (ex) {
85+
res.statusCode = 403;
86+
res.end(`the validateAuthentication operation failed. ${ex.message}`);
87+
}
88+
}

x-pack/test/alerting_api_integration/common/fixtures/plugins/actions_simulators_legacy/index.ts

Lines changed: 0 additions & 26 deletions
This file was deleted.

x-pack/test/alerting_api_integration/common/fixtures/plugins/actions_simulators_legacy/package.json

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)