Skip to content

Commit

Permalink
address PR comment with better function test
Browse files Browse the repository at this point in the history
  • Loading branch information
pmuellr committed Dec 10, 2020
1 parent 4081c28 commit 651eefc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ import {
AlertInstanceContext,
} from '../../../../../../../plugins/alerts/server';

export const EscapableStrings = {
escapableBold: '*bold*',
escapableBacktic: 'back`tic',
escapableBackticBold: '`*bold*`',
escapableHtml: '<&>',
escapableDoubleQuote: '"double quote"',
escapableLineFeed: 'line\x0afeed',
};

function getAlwaysFiringAlertType() {
const paramsSchema = schema.object({
index: schema.string(),
Expand Down Expand Up @@ -376,7 +385,7 @@ function getPatternFiringAlertType() {
// fire if pattern says to
for (const [instanceId, instancePattern] of Object.entries(pattern)) {
if (instancePattern[patternIndex]) {
services.alertInstanceFactory(instanceId).scheduleActions('default');
services.alertInstanceFactory(instanceId).scheduleActions('default', EscapableStrings);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,15 @@ export default function executionStatusAlertTests({ getService }: FtrProviderCon
const createdAction = actionResponse.body;
objectRemover.add(Spaces.space1.id, createdAction.id, 'action', 'actions');

// from x-pack/test/alerting_api_integration/common/fixtures/plugins/alerts/server/alert_types.ts
const varsTemplate = '{{context.escapableDoubleQuote}} -- {{context.escapableLineFeed}}';

const alertResponse = await supertest
.post(`${getUrlPrefix(Spaces.space1.id)}/api/alerts/alert`)
.set('kbn-xsrf', 'foo')
.send(
getTestAlertData({
name: 'contains "double quote"',
name: 'testing variable escapes for webhook',
alertTypeId: 'test.patternFiring',
params: {
pattern: { instance: [true] },
Expand All @@ -94,7 +97,7 @@ export default function executionStatusAlertTests({ getService }: FtrProviderCon
id: createdAction.id,
group: 'default',
params: {
body: 'payload {{alertId}} - {{alertName}}',
body: `payload {{alertId}} - ${varsTemplate}`,
},
},
],
Expand All @@ -107,10 +110,10 @@ export default function executionStatusAlertTests({ getService }: FtrProviderCon
const body = await retry.try(async () =>
waitForActionBody(webhookSimulatorURL, createdAlert.id)
);
expect(body).to.be(`contains \\"double quote\\"`);
expect(body).to.be(`\\"double quote\\" -- line\\nfeed`);
});

it('should handle bold and italic escapes in slack', async () => {
it('should handle escapes in slack', async () => {
const actionResponse = await supertest
.post(`${getUrlPrefix(Spaces.space1.id)}/api/actions/action`)
.set('kbn-xsrf', 'test')
Expand All @@ -125,58 +128,16 @@ export default function executionStatusAlertTests({ getService }: FtrProviderCon
const createdAction = actionResponse.body;
objectRemover.add(Spaces.space1.id, createdAction.id, 'action', 'actions');

const alertResponse = await supertest
.post(`${getUrlPrefix(Spaces.space1.id)}/api/alerts/alert`)
.set('kbn-xsrf', 'foo')
.send(
getTestAlertData({
name: 'contains *bold* and _italic_ and back`tic and htmlish <&> things',
alertTypeId: 'test.patternFiring',
params: {
pattern: { instance: [true] },
},
actions: [
{
id: createdAction.id,
group: 'default',
params: {
message: 'message {{alertId}} - {{alertName}}',
},
},
],
})
);
expect(alertResponse.status).to.eql(200);
const createdAlert = alertResponse.body;
objectRemover.add(Spaces.space1.id, createdAlert.id, 'alert', 'alerts');

const body = await retry.try(async () =>
waitForActionBody(slackSimulatorURL, createdAlert.id)
);
expect(body).to.be("`contains *bold* and _italic_ and back'tic and htmlish <&> things`");
});

it('should handle single escapes in slack', async () => {
const actionResponse = await supertest
.post(`${getUrlPrefix(Spaces.space1.id)}/api/actions/action`)
.set('kbn-xsrf', 'test')
.send({
name: 'testing single mustache escapes for slack',
actionTypeId: '.slack',
secrets: {
webhookUrl: slackSimulatorURL,
},
});
expect(actionResponse.status).to.eql(200);
const createdAction = actionResponse.body;
objectRemover.add(Spaces.space1.id, createdAction.id, 'action', 'actions');
// from x-pack/test/alerting_api_integration/common/fixtures/plugins/alerts/server/alert_types.ts
const varsTemplate =
'{{context.escapableBacktic}} -- {{context.escapableBold}} -- {{context.escapableBackticBold}} -- {{context.escapableHtml}}';

const alertResponse = await supertest
.post(`${getUrlPrefix(Spaces.space1.id)}/api/alerts/alert`)
.set('kbn-xsrf', 'foo')
.send(
getTestAlertData({
name: 'contains back`tic and htmlish <&> things',
name: 'testing variable escapes for slack',
alertTypeId: 'test.patternFiring',
params: {
pattern: { instance: [true] },
Expand All @@ -186,7 +147,7 @@ export default function executionStatusAlertTests({ getService }: FtrProviderCon
id: createdAction.id,
group: 'default',
params: {
message: 'message {{alertId}} - {{alertName}}',
message: `message {{alertId}} - ${varsTemplate}`,
},
},
],
Expand All @@ -199,7 +160,7 @@ export default function executionStatusAlertTests({ getService }: FtrProviderCon
const body = await retry.try(async () =>
waitForActionBody(slackSimulatorURL, createdAlert.id)
);
expect(body).to.be("contains back'tic and htmlish &lt;&amp;&gt; things");
expect(body).to.be("back'tic -- `*bold*` -- `'*bold*'` -- &lt;&amp;&gt;");
});
});

Expand Down

0 comments on commit 651eefc

Please sign in to comment.