Skip to content

Commit

Permalink
add function tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pmuellr committed Aug 22, 2019
1 parent 4b16d0a commit 047fef7
Show file tree
Hide file tree
Showing 6 changed files with 328 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,22 @@
*/

import axios, { AxiosResponse } from 'axios';
import { Services } from '../../types';

interface SendPagerdutyOptions {
apiUrl: string;
data: any;
headers: Record<string, string>;
services: Services;
}

// post an event to pagerduty
export async function sendPagerduty(options: SendPagerdutyOptions): Promise<AxiosResponse> {
const { apiUrl, data, headers } = options;
const axiosOptions = {
headers,
validateStatus: () => true,
};

return axios.post(apiUrl, data, { headers });
return axios.post(apiUrl, data, axiosOptions);
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,32 +79,18 @@ describe('validateConfig()', () => {
}).toThrowErrorMatchingInlineSnapshot(
`"error validating action type config: [shouldNotBeHere]: definition for this key is missing"`
);
expect(() => {
validateConfig(actionType, { apiUrl: false });
}).toThrowErrorMatchingInlineSnapshot(`
"error validating action type config: [apiUrl]: types that failed validation:
- [apiUrl.0]: expected value of type [string] but got [boolean]
- [apiUrl.1]: expected value to equal [null] but got [false]"
`);
});
});

describe('validateSecrets()', () => {
test('should validate and pass when secrets is valid', () => {
const routingKey = '0123456789ABCDEF0123456789ABCDEF';
const routingKey = 'super-secret';
expect(validateSecrets(actionType, { routingKey })).toEqual({
routingKey,
});
});

test('should validate and throw error when secrets is invalid', () => {
const routingKey = '123456789ABCDEF0123456789ABCDEF'; // 31 chars!
expect(() => {
validateSecrets(actionType, { routingKey });
}).toThrowErrorMatchingInlineSnapshot(
`"error validating action type secrets: [routingKey]: value is [123456789ABCDEF0123456789ABCDEF] but it must have a minimum length of [32]."`
);

expect(() => {
validateSecrets(actionType, { routingKey: false });
}).toThrowErrorMatchingInlineSnapshot(
Expand Down Expand Up @@ -166,35 +152,38 @@ describe('execute()', () => {
const id = 'some-action-id';
const executorOptions: ActionTypeExecutorOptions = { id, config, params, secrets, services };
const actionResponse = await actionType.executor(executorOptions);
expect(sendPagerdutyMock.mock.calls[0][0]).toMatchInlineSnapshot(`
Object {
"apiUrl": "https://events.pagerduty.com/v2/enqueue",
"data": Object {
"dedup_key": "action:some-action-id",
"event_action": "trigger",
"payload": Object {
"severity": "info",
"source": "Kibana Action some-action-id",
"summary": "No summary provided.",
},
},
"headers": Object {
"Content-Type": "application/json",
"X-Routing-Key": "super-secret",
},
}
`);
const { apiUrl, data, headers } = sendPagerdutyMock.mock.calls[0][0];
expect({ apiUrl, data, headers }).toMatchInlineSnapshot(`
Object {
"apiUrl": "https://events.pagerduty.com/v2/enqueue",
"data": Object {
"dedup_key": "action:some-action-id",
"event_action": "trigger",
"payload": Object {
"severity": "info",
"source": "Kibana Action some-action-id",
"summary": "No summary provided.",
},
},
"headers": Object {
"Content-Type": "application/json",
"X-Routing-Key": "super-secret",
},
}
`);
expect(actionResponse).toMatchInlineSnapshot(`
Object {
"data": "data-here",
"status": "ok",
}
`);
Object {
"data": "data-here",
"status": "ok",
}
`);
});

test('should succeed with maximal valid params for trigger', async () => {
const randoDate = new Date('1963-09-23T01:23:45Z').toISOString();
const secrets = { routingKey: 'super-secret' };
const secrets = {
routingKey: 'super-secret',
};
const config = {
apiUrl: 'the-api-url',
};
Expand All @@ -217,39 +206,42 @@ describe('execute()', () => {
const id = 'some-action-id';
const executorOptions: ActionTypeExecutorOptions = { id, config, params, secrets, services };
const actionResponse = await actionType.executor(executorOptions);
expect(sendPagerdutyMock.mock.calls[0][0]).toMatchInlineSnapshot(`
Object {
"apiUrl": "the-api-url",
"data": Object {
"dedup_key": "a-dedup-key",
"event_action": "trigger",
"payload": Object {
"class": "the-class",
"component": "the-component",
"group": "the-group",
"severity": "critical",
"source": "the-source",
"summary": "the summary",
"timestamp": "1963-09-23T01:23:45.000Z",
},
},
"headers": Object {
"Content-Type": "application/json",
"X-Routing-Key": "super-secret",
},
}
`);
const { apiUrl, data, headers } = sendPagerdutyMock.mock.calls[0][0];
expect({ apiUrl, data, headers }).toMatchInlineSnapshot(`
Object {
"apiUrl": "the-api-url",
"data": Object {
"dedup_key": "a-dedup-key",
"event_action": "trigger",
"payload": Object {
"class": "the-class",
"component": "the-component",
"group": "the-group",
"severity": "critical",
"source": "the-source",
"summary": "the summary",
"timestamp": "1963-09-23T01:23:45.000Z",
},
},
"headers": Object {
"Content-Type": "application/json",
"X-Routing-Key": "super-secret",
},
}
`);
expect(actionResponse).toMatchInlineSnapshot(`
Object {
"data": "data-here",
"status": "ok",
}
`);
Object {
"data": "data-here",
"status": "ok",
}
`);
});

test('should succeed with maximal valid params for acknowledge', async () => {
const randoDate = new Date('1963-09-23T01:23:45Z').toISOString();
const secrets = { routingKey: 'super-secret' };
const secrets = {
routingKey: 'super-secret',
};
const config = {
apiUrl: 'the-api-url',
};
Expand All @@ -272,30 +264,33 @@ describe('execute()', () => {
const id = 'some-action-id';
const executorOptions: ActionTypeExecutorOptions = { id, config, params, secrets, services };
const actionResponse = await actionType.executor(executorOptions);
expect(sendPagerdutyMock.mock.calls[0][0]).toMatchInlineSnapshot(`
Object {
"apiUrl": "the-api-url",
"data": Object {
"dedup_key": "a-dedup-key",
"event_action": "acknowledge",
},
"headers": Object {
"Content-Type": "application/json",
"X-Routing-Key": "super-secret",
},
}
`);
const { apiUrl, data, headers } = sendPagerdutyMock.mock.calls[0][0];
expect({ apiUrl, data, headers }).toMatchInlineSnapshot(`
Object {
"apiUrl": "the-api-url",
"data": Object {
"dedup_key": "a-dedup-key",
"event_action": "acknowledge",
},
"headers": Object {
"Content-Type": "application/json",
"X-Routing-Key": "super-secret",
},
}
`);
expect(actionResponse).toMatchInlineSnapshot(`
Object {
"data": "data-here",
"status": "ok",
}
`);
Object {
"data": "data-here",
"status": "ok",
}
`);
});

test('should succeed with maximal valid params for resolve', async () => {
const randoDate = new Date('1963-09-23T01:23:45Z').toISOString();
const secrets = { routingKey: 'super-secret' };
const secrets = {
routingKey: 'super-secret',
};
const config = {
apiUrl: 'the-api-url',
};
Expand All @@ -318,25 +313,26 @@ describe('execute()', () => {
const id = 'some-action-id';
const executorOptions: ActionTypeExecutorOptions = { id, config, params, secrets, services };
const actionResponse = await actionType.executor(executorOptions);
expect(sendPagerdutyMock.mock.calls[0][0]).toMatchInlineSnapshot(`
Object {
"apiUrl": "the-api-url",
"data": Object {
"dedup_key": "a-dedup-key",
"event_action": "resolve",
},
"headers": Object {
"Content-Type": "application/json",
"X-Routing-Key": "super-secret",
},
}
`);
const { apiUrl, data, headers } = sendPagerdutyMock.mock.calls[0][0];
expect({ apiUrl, data, headers }).toMatchInlineSnapshot(`
Object {
"apiUrl": "the-api-url",
"data": Object {
"dedup_key": "a-dedup-key",
"event_action": "resolve",
},
"headers": Object {
"Content-Type": "application/json",
"X-Routing-Key": "super-secret",
},
}
`);
expect(actionResponse).toMatchInlineSnapshot(`
Object {
"data": "data-here",
"status": "ok",
}
`);
Object {
"data": "data-here",
"status": "ok",
}
`);
});

test('should fail when sendPagerdury throws', async () => {
Expand All @@ -352,11 +348,11 @@ describe('execute()', () => {
const executorOptions: ActionTypeExecutorOptions = { id, config, params, secrets, services };
const actionResponse = await actionType.executor(executorOptions);
expect(actionResponse).toMatchInlineSnapshot(`
Object {
"message": "error in pagerduty action \\"some-action-id\\" posting event: doing some testing",
"status": "error",
}
`);
Object {
"message": "error in pagerduty action \\"some-action-id\\" posting event: doing some testing",
"status": "error",
}
`);
});

test('should fail when sendPagerdury returns 429', async () => {
Expand All @@ -372,12 +368,12 @@ describe('execute()', () => {
const executorOptions: ActionTypeExecutorOptions = { id, config, params, secrets, services };
const actionResponse = await actionType.executor(executorOptions);
expect(actionResponse).toMatchInlineSnapshot(`
Object {
"message": "error in pagerduty action \\"some-action-id\\" posting event: status 429, retry later",
"retry": true,
"status": "error",
}
`);
Object {
"message": "error in pagerduty action \\"some-action-id\\" posting event: status 429, retry later",
"retry": true,
"status": "error",
}
`);
});

test('should fail when sendPagerdury returns 501', async () => {
Expand All @@ -393,12 +389,12 @@ describe('execute()', () => {
const executorOptions: ActionTypeExecutorOptions = { id, config, params, secrets, services };
const actionResponse = await actionType.executor(executorOptions);
expect(actionResponse).toMatchInlineSnapshot(`
Object {
"message": "error in pagerduty action \\"some-action-id\\" posting event: status 501, retry later",
"retry": true,
"status": "error",
}
`);
Object {
"message": "error in pagerduty action \\"some-action-id\\" posting event: status 501, retry later",
"retry": true,
"status": "error",
}
`);
});

test('should fail when sendPagerdury returns 418', async () => {
Expand All @@ -414,10 +410,10 @@ describe('execute()', () => {
const executorOptions: ActionTypeExecutorOptions = { id, config, params, secrets, services };
const actionResponse = await actionType.executor(executorOptions);
expect(actionResponse).toMatchInlineSnapshot(`
Object {
"message": "error in pagerduty action \\"some-action-id\\" posting event: unexpected status 418",
"status": "error",
}
`);
Object {
"message": "error in pagerduty action \\"some-action-id\\" posting event: unexpected status 418",
"status": "error",
}
`);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const ConfigSchema = schema.object({
export type ActionTypeSecretsType = TypeOf<typeof SecretsSchema>;

const SecretsSchema = schema.object({
routingKey: schema.string({ minLength: 32, maxLength: 32 }),
routingKey: schema.string(),
});

// params definition
Expand Down Expand Up @@ -114,7 +114,7 @@ async function executor(execOptions: ActionTypeExecutorOptions): Promise<ActionT

let response;
try {
response = await sendPagerduty({ apiUrl, data, headers });
response = await sendPagerduty({ apiUrl, data, headers, services });
} catch (err) {
const message = i18n.translate('xpack.actions.builtin.pagerduty.postingErrorMessage', {
defaultMessage: 'error in pagerduty action "{id}" posting event: {errorMessage}',
Expand Down
Loading

0 comments on commit 047fef7

Please sign in to comment.