-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrated last pieces of legacy fixture code (#74470)
* 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
- Loading branch information
1 parent
5b64a4c
commit 124bd12
Showing
14 changed files
with
269 additions
and
282 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
...ing_api_integration/common/fixtures/plugins/actions_simulators/server/slack_simulation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* 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 http from 'http'; | ||
|
||
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) { | ||
response.statusCode = 400; | ||
response.end('bad request to slack simulator'); | ||
return; | ||
} | ||
|
||
switch (text) { | ||
case 'success': { | ||
response.statusCode = 200; | ||
response.end('ok'); | ||
return; | ||
} | ||
case 'no_text': | ||
response.statusCode = 400; | ||
response.end('no_text'); | ||
return; | ||
|
||
case 'invalid_payload': | ||
response.statusCode = 400; | ||
response.end('invalid_payload'); | ||
return; | ||
|
||
case 'invalid_token': | ||
response.statusCode = 403; | ||
response.end('invalid_token'); | ||
return; | ||
|
||
case 'status_500': | ||
response.statusCode = 500; | ||
response.end('simulated slack 500 response'); | ||
return; | ||
|
||
case 'rate_limit': | ||
const res = { | ||
retry_after: 1, | ||
ok: false, | ||
error: 'rate_limited', | ||
}; | ||
|
||
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'); | ||
}); | ||
} | ||
}); | ||
} |
88 changes: 88 additions & 0 deletions
88
...g_api_integration/common/fixtures/plugins/actions_simulators/server/webhook_simulation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* 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 expect from '@kbn/expect'; | ||
import http from 'http'; | ||
import { fromNullable, map, filter, getOrElse } from 'fp-ts/lib/Option'; | ||
import { pipe } from 'fp-ts/lib/pipeable'; | ||
import { constant } from 'fp-ts/lib/function'; | ||
|
||
export async function initPlugin() { | ||
return http.createServer((request, response) => { | ||
const credentials = pipe( | ||
fromNullable(request.headers.authorization), | ||
map((authorization) => authorization.split(/\s+/)), | ||
filter((parts) => parts.length > 1), | ||
map((parts) => Buffer.from(parts[1], 'base64').toString()), | ||
filter((credentialsPart) => credentialsPart.indexOf(':') !== -1), | ||
map((credentialsPart) => { | ||
const [username, password] = credentialsPart.split(':'); | ||
return { username, password }; | ||
}), | ||
getOrElse(constant({ username: '', password: '' })) | ||
); | ||
|
||
if (request.method === 'POST' || request.method === 'PUT') { | ||
let data = ''; | ||
request.on('data', (chunk) => { | ||
data += chunk; | ||
}); | ||
request.on('end', () => { | ||
switch (data) { | ||
case 'success': | ||
response.statusCode = 200; | ||
response.end('OK'); | ||
return; | ||
case 'authenticate': | ||
return validateAuthentication(credentials, response); | ||
case 'success_post_method': | ||
return validateRequestUsesMethod(request.method ?? '', 'post', response); | ||
case 'success_put_method': | ||
return validateRequestUsesMethod(request.method ?? '', 'put', response); | ||
case 'failure': | ||
response.statusCode = 500; | ||
response.end('Error'); | ||
return; | ||
} | ||
response.statusCode = 400; | ||
response.end( | ||
`unknown request to webhook simulator [${data ? `content: ${data}` : `no content`}]` | ||
); | ||
return; | ||
}); | ||
} else { | ||
request.on('end', () => { | ||
response.statusCode = 400; | ||
response.end('unknown request to webhook simulator [no content]'); | ||
return; | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
function validateAuthentication(credentials: any, res: any) { | ||
try { | ||
expect(credentials).to.eql({ | ||
username: 'elastic', | ||
password: 'changeme', | ||
}); | ||
res.statusCode = 200; | ||
res.end('OK'); | ||
} catch (ex) { | ||
res.statusCode = 403; | ||
res.end(`the validateAuthentication operation failed. ${ex.message}`); | ||
} | ||
} | ||
|
||
function validateRequestUsesMethod(requestMethod: string, method: string, res: any) { | ||
try { | ||
expect(requestMethod.toLowerCase()).to.eql(method); | ||
res.statusCode = 200; | ||
res.end('OK'); | ||
} catch (ex) { | ||
res.statusCode = 403; | ||
res.end(`the validateAuthentication operation failed. ${ex.message}`); | ||
} | ||
} |
26 changes: 0 additions & 26 deletions
26
.../test/alerting_api_integration/common/fixtures/plugins/actions_simulators_legacy/index.ts
This file was deleted.
Oops, something went wrong.
7 changes: 0 additions & 7 deletions
7
...t/alerting_api_integration/common/fixtures/plugins/actions_simulators_legacy/package.json
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.