2021-04-29 10:21:29 +02:00
|
|
|
const { FEATURE_CREATED } = require('../types/events');
|
2021-01-19 10:42:45 +01:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
jest.mock(
|
|
|
|
'./addon',
|
|
|
|
() =>
|
|
|
|
class Addon {
|
|
|
|
constructor(definition, { getLogger }) {
|
|
|
|
this.logger = getLogger('addon/test');
|
|
|
|
this.fetchRetryCalls = [];
|
|
|
|
}
|
2021-01-19 10:42:45 +01:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
async fetchRetry(url, options, retries, backoff) {
|
|
|
|
this.fetchRetryCalls.push({ url, options, retries, backoff });
|
|
|
|
return Promise.resolve({ status: 200 });
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
const WebhookAddon = require('./webhook');
|
2021-01-19 10:42:45 +01:00
|
|
|
|
|
|
|
const noLogger = require('../../test/fixtures/no-logger');
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('Should handle event without "bodyTemplate"', () => {
|
2021-01-19 10:42:45 +01:00
|
|
|
const addon = new WebhookAddon({ getLogger: noLogger });
|
|
|
|
const event = {
|
|
|
|
type: FEATURE_CREATED,
|
|
|
|
createdBy: 'some@user.com',
|
|
|
|
data: {
|
|
|
|
name: 'some-toggle',
|
|
|
|
enabled: false,
|
|
|
|
strategies: [{ name: 'default' }],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const parameters = {
|
|
|
|
url: 'http://test.webhook.com',
|
|
|
|
};
|
|
|
|
|
|
|
|
addon.handleEvent(event, parameters);
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(addon.fetchRetryCalls.length).toBe(1);
|
|
|
|
expect(addon.fetchRetryCalls[0].url).toBe(parameters.url);
|
|
|
|
expect(addon.fetchRetryCalls[0].options.body).toBe(JSON.stringify(event));
|
2021-01-19 10:42:45 +01:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('Should format event with "bodyTemplate"', () => {
|
2021-01-19 10:42:45 +01:00
|
|
|
const addon = new WebhookAddon({ getLogger: noLogger });
|
|
|
|
const event = {
|
|
|
|
type: FEATURE_CREATED,
|
|
|
|
createdBy: 'some@user.com',
|
|
|
|
data: {
|
|
|
|
name: 'some-toggle',
|
|
|
|
enabled: false,
|
|
|
|
strategies: [{ name: 'default' }],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const parameters = {
|
|
|
|
url: 'http://test.webhook.com/plain',
|
|
|
|
bodyTemplate: '{{event.type}} on toggle {{event.data.name}}',
|
|
|
|
contentType: 'text/plain',
|
|
|
|
};
|
|
|
|
|
|
|
|
addon.handleEvent(event, parameters);
|
|
|
|
const call = addon.fetchRetryCalls[0];
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(addon.fetchRetryCalls.length).toBe(1);
|
|
|
|
expect(call.url).toBe(parameters.url);
|
|
|
|
expect(call.options.headers['Content-Type']).toBe('text/plain');
|
|
|
|
expect(call.options.body).toBe('feature-created on toggle some-toggle');
|
2021-01-19 10:42:45 +01:00
|
|
|
});
|