mirror of
https://github.com/Unleash/unleash.git
synced 2025-10-13 11:17:26 +02:00
###What Adds an optional sensitive parameter for customHeaders to all current addons. It is sensitive because the user might be including api key headers.
149 lines
4.5 KiB
TypeScript
149 lines
4.5 KiB
TypeScript
import { Logger } from '../logger';
|
|
|
|
import { FEATURE_CREATED, IEvent } from '../types/events';
|
|
|
|
import WebhookAddon from './webhook';
|
|
|
|
import noLogger from '../../test/fixtures/no-logger';
|
|
|
|
let fetchRetryCalls: any[] = [];
|
|
|
|
jest.mock(
|
|
'./addon',
|
|
() =>
|
|
class Addon {
|
|
logger: Logger;
|
|
|
|
constructor(definition, { getLogger }) {
|
|
this.logger = getLogger('addon/test');
|
|
fetchRetryCalls = [];
|
|
}
|
|
|
|
async fetchRetry(url, options, retries, backoff) {
|
|
fetchRetryCalls.push({
|
|
url,
|
|
options,
|
|
retries,
|
|
backoff,
|
|
});
|
|
return Promise.resolve({ status: 200 });
|
|
}
|
|
},
|
|
);
|
|
|
|
test('Should handle event without "bodyTemplate"', () => {
|
|
const addon = new WebhookAddon({ getLogger: noLogger });
|
|
const event: IEvent = {
|
|
id: 1,
|
|
createdAt: new Date(),
|
|
type: FEATURE_CREATED,
|
|
createdBy: 'some@user.com',
|
|
featureName: 'some-toggle',
|
|
data: {
|
|
name: 'some-toggle',
|
|
enabled: false,
|
|
strategies: [{ name: 'default' }],
|
|
},
|
|
};
|
|
|
|
const parameters = {
|
|
url: 'http://test.webhook.com',
|
|
};
|
|
|
|
addon.handleEvent(event, parameters);
|
|
expect(fetchRetryCalls.length).toBe(1);
|
|
expect(fetchRetryCalls[0].url).toBe(parameters.url);
|
|
expect(fetchRetryCalls[0].options.body).toBe(JSON.stringify(event));
|
|
});
|
|
|
|
test('Should format event with "bodyTemplate"', () => {
|
|
const addon = new WebhookAddon({ getLogger: noLogger });
|
|
const event: IEvent = {
|
|
id: 1,
|
|
createdAt: new Date(),
|
|
type: FEATURE_CREATED,
|
|
createdBy: 'some@user.com',
|
|
featureName: 'some-toggle',
|
|
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 = fetchRetryCalls[0];
|
|
expect(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');
|
|
});
|
|
|
|
test('Should format event with "authorization"', () => {
|
|
const addon = new WebhookAddon({ getLogger: noLogger });
|
|
const event: IEvent = {
|
|
id: 1,
|
|
createdAt: new Date(),
|
|
type: FEATURE_CREATED,
|
|
createdBy: 'some@user.com',
|
|
featureName: 'some-toggle',
|
|
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',
|
|
authorization: 'API KEY 123abc',
|
|
};
|
|
|
|
addon.handleEvent(event, parameters);
|
|
const call = fetchRetryCalls[0];
|
|
expect(fetchRetryCalls.length).toBe(1);
|
|
expect(call.url).toBe(parameters.url);
|
|
expect(call.options.headers.Authorization).toBe(parameters.authorization);
|
|
expect(call.options.body).toBe('feature-created on toggle some-toggle');
|
|
});
|
|
|
|
test('Should handle custom headers', async () => {
|
|
const addon = new WebhookAddon({ getLogger: noLogger });
|
|
const event: IEvent = {
|
|
id: 1,
|
|
createdAt: new Date(),
|
|
type: FEATURE_CREATED,
|
|
createdBy: 'some@user.com',
|
|
featureName: 'some-toggle',
|
|
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',
|
|
authorization: 'API KEY 123abc',
|
|
customHeaders: `{ "MY_CUSTOM_HEADER": "MY_CUSTOM_VALUE" }`,
|
|
};
|
|
|
|
addon.handleEvent(event, parameters);
|
|
const call = fetchRetryCalls[0];
|
|
expect(fetchRetryCalls.length).toBe(1);
|
|
expect(call.url).toBe(parameters.url);
|
|
expect(call.options.headers.Authorization).toBe(parameters.authorization);
|
|
expect(call.options.headers.MY_CUSTOM_HEADER).toBe('MY_CUSTOM_VALUE');
|
|
expect(call.options.body).toBe('feature-created on toggle some-toggle');
|
|
});
|