2021-10-07 10:22:20 +02:00
|
|
|
import {
|
|
|
|
FEATURE_CREATED,
|
|
|
|
FEATURE_ARCHIVED,
|
|
|
|
FEATURE_ENVIRONMENT_DISABLED,
|
2021-11-12 13:15:51 +01:00
|
|
|
IEvent,
|
2021-10-07 10:22:20 +02:00
|
|
|
} from '../types/events';
|
2021-08-12 15:04:37 +02:00
|
|
|
import { Logger } from '../logger';
|
|
|
|
|
|
|
|
import SlackAddon from './slack';
|
|
|
|
|
|
|
|
import noLogger from '../../test/fixtures/no-logger';
|
|
|
|
|
|
|
|
let fetchRetryCalls: any[] = [];
|
2021-01-19 10:42:45 +01:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
jest.mock(
|
|
|
|
'./addon',
|
|
|
|
() =>
|
|
|
|
class Addon {
|
2021-08-12 15:04:37 +02:00
|
|
|
logger: Logger;
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
constructor(definition, { getLogger }) {
|
|
|
|
this.logger = getLogger('addon/test');
|
2021-08-12 15:04:37 +02:00
|
|
|
fetchRetryCalls = [];
|
2021-05-28 11:10:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async fetchRetry(url, options, retries, backoff) {
|
2021-08-12 15:04:37 +02:00
|
|
|
fetchRetryCalls.push({
|
|
|
|
url,
|
|
|
|
options,
|
|
|
|
retries,
|
|
|
|
backoff,
|
|
|
|
});
|
2021-05-28 11:10:24 +02:00
|
|
|
return Promise.resolve({ status: 200 });
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
test('Should call slack webhook', async () => {
|
2021-02-05 11:30:30 +01:00
|
|
|
const addon = new SlackAddon({
|
|
|
|
getLogger: noLogger,
|
|
|
|
unleashUrl: 'http://some-url.com',
|
|
|
|
});
|
2021-08-12 15:04:37 +02:00
|
|
|
const event: IEvent = {
|
|
|
|
id: 1,
|
|
|
|
createdAt: new Date(),
|
2021-01-19 10:42:45 +01:00
|
|
|
type: FEATURE_CREATED,
|
|
|
|
createdBy: 'some@user.com',
|
2021-10-28 14:09:11 +02:00
|
|
|
project: 'default',
|
2021-11-12 13:15:51 +01:00
|
|
|
featureName: 'some-toggle',
|
2021-01-19 10:42:45 +01:00
|
|
|
data: {
|
|
|
|
name: 'some-toggle',
|
|
|
|
enabled: false,
|
2021-10-28 14:09:11 +02:00
|
|
|
type: 'release',
|
2021-01-19 10:42:45 +01:00
|
|
|
strategies: [{ name: 'default' }],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const parameters = {
|
|
|
|
url: 'http://hooks.slack.com',
|
2023-07-05 09:42:17 +02:00
|
|
|
defaultChannel: 'general',
|
2021-01-19 10:42:45 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
await addon.handleEvent(event, parameters);
|
2021-08-12 15:04:37 +02:00
|
|
|
expect(fetchRetryCalls.length).toBe(1);
|
|
|
|
expect(fetchRetryCalls[0].url).toBe(parameters.url);
|
|
|
|
expect(fetchRetryCalls[0].options.body).toMatchSnapshot();
|
2021-01-19 10:42:45 +01:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('Should call slack webhook for archived toggle', async () => {
|
2021-03-05 12:59:35 +01:00
|
|
|
const addon = new SlackAddon({
|
|
|
|
getLogger: noLogger,
|
|
|
|
unleashUrl: 'http://some-url.com',
|
|
|
|
});
|
2021-08-12 15:04:37 +02:00
|
|
|
const event: IEvent = {
|
|
|
|
id: 2,
|
|
|
|
createdAt: new Date(),
|
2021-03-05 12:59:35 +01:00
|
|
|
type: FEATURE_ARCHIVED,
|
2021-11-12 13:15:51 +01:00
|
|
|
featureName: 'some-toggle',
|
2022-10-05 11:30:51 +02:00
|
|
|
createdBy: 'some@user.com',
|
|
|
|
data: {
|
|
|
|
name: 'some-toggle',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const parameters = {
|
|
|
|
url: 'http://hooks.slack.com',
|
2023-07-05 09:42:17 +02:00
|
|
|
defaultChannel: 'general',
|
2022-10-05 11:30:51 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
await addon.handleEvent(event, parameters);
|
|
|
|
expect(fetchRetryCalls.length).toBe(1);
|
|
|
|
expect(fetchRetryCalls[0].url).toBe(parameters.url);
|
|
|
|
expect(fetchRetryCalls[0].options.body).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Should call slack webhook for archived toggle with project info', async () => {
|
|
|
|
const addon = new SlackAddon({
|
|
|
|
getLogger: noLogger,
|
|
|
|
unleashUrl: 'http://some-url.com',
|
|
|
|
});
|
|
|
|
const event: IEvent = {
|
|
|
|
id: 2,
|
|
|
|
createdAt: new Date(),
|
|
|
|
type: FEATURE_ARCHIVED,
|
|
|
|
featureName: 'some-toggle',
|
|
|
|
project: 'some-project',
|
2021-03-05 12:59:35 +01:00
|
|
|
createdBy: 'some@user.com',
|
|
|
|
data: {
|
|
|
|
name: 'some-toggle',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const parameters = {
|
|
|
|
url: 'http://hooks.slack.com',
|
2023-07-05 09:42:17 +02:00
|
|
|
defaultChannel: 'general',
|
2021-03-05 12:59:35 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
await addon.handleEvent(event, parameters);
|
2021-08-12 15:04:37 +02:00
|
|
|
expect(fetchRetryCalls.length).toBe(1);
|
|
|
|
expect(fetchRetryCalls[0].url).toBe(parameters.url);
|
|
|
|
expect(fetchRetryCalls[0].options.body).toMatchSnapshot();
|
2021-03-05 12:59:35 +01:00
|
|
|
});
|
|
|
|
|
2021-10-07 10:22:20 +02:00
|
|
|
test(`Should call webhook for toggled environment`, async () => {
|
|
|
|
const addon = new SlackAddon({
|
|
|
|
getLogger: noLogger,
|
|
|
|
unleashUrl: 'http://some-url.com',
|
|
|
|
});
|
|
|
|
const event: IEvent = {
|
|
|
|
id: 2,
|
|
|
|
createdAt: new Date(),
|
|
|
|
type: FEATURE_ENVIRONMENT_DISABLED,
|
|
|
|
createdBy: 'some@user.com',
|
|
|
|
environment: 'development',
|
|
|
|
project: 'default',
|
2021-11-12 13:15:51 +01:00
|
|
|
featureName: 'some-toggle',
|
2021-10-07 10:22:20 +02:00
|
|
|
data: {
|
|
|
|
name: 'some-toggle',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const parameters = {
|
|
|
|
url: 'http://hooks.slack.com',
|
2023-07-05 09:42:17 +02:00
|
|
|
defaultChannel: 'general',
|
2021-10-07 10:22:20 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
await addon.handleEvent(event, parameters);
|
|
|
|
expect(fetchRetryCalls).toHaveLength(1);
|
|
|
|
expect(fetchRetryCalls[0].url).toBe(parameters.url);
|
|
|
|
expect(fetchRetryCalls[0].options.body).toMatch(/disabled/);
|
|
|
|
expect(fetchRetryCalls[0].options.body).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('Should use default channel', async () => {
|
2021-02-05 11:30:30 +01:00
|
|
|
const addon = new SlackAddon({
|
|
|
|
getLogger: noLogger,
|
|
|
|
unleashUrl: 'http://some-url.com',
|
|
|
|
});
|
2021-08-12 15:04:37 +02:00
|
|
|
const event: IEvent = {
|
|
|
|
id: 3,
|
|
|
|
createdAt: new Date(),
|
2021-01-19 10:42:45 +01:00
|
|
|
type: FEATURE_CREATED,
|
|
|
|
createdBy: 'some@user.com',
|
2021-11-12 13:15:51 +01:00
|
|
|
featureName: 'some-toggle',
|
2021-01-19 10:42:45 +01:00
|
|
|
data: {
|
|
|
|
name: 'some-toggle',
|
|
|
|
enabled: false,
|
|
|
|
strategies: [{ name: 'default' }],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const parameters = {
|
|
|
|
url: 'http://hooks.slack.com',
|
|
|
|
defaultChannel: 'some-channel',
|
|
|
|
};
|
|
|
|
|
|
|
|
await addon.handleEvent(event, parameters);
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
const req = JSON.parse(fetchRetryCalls[0].options.body);
|
2021-01-19 10:42:45 +01:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(req.channel).toBe('#some-channel');
|
2021-01-19 10:42:45 +01:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('Should override default channel with data from tag', async () => {
|
2021-02-05 11:30:30 +01:00
|
|
|
const addon = new SlackAddon({
|
|
|
|
getLogger: noLogger,
|
|
|
|
unleashUrl: 'http://some-url.com',
|
|
|
|
});
|
2021-08-12 15:04:37 +02:00
|
|
|
const event: IEvent = {
|
|
|
|
id: 4,
|
|
|
|
createdAt: new Date(),
|
2021-01-19 10:42:45 +01:00
|
|
|
type: FEATURE_CREATED,
|
|
|
|
createdBy: 'some@user.com',
|
2021-11-12 13:15:51 +01:00
|
|
|
featureName: 'some-toggle',
|
2021-01-19 10:42:45 +01:00
|
|
|
data: {
|
|
|
|
name: 'some-toggle',
|
|
|
|
enabled: false,
|
|
|
|
strategies: [{ name: 'default' }],
|
|
|
|
},
|
|
|
|
tags: [
|
|
|
|
{
|
|
|
|
type: 'slack',
|
|
|
|
value: 'another-channel',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
const parameters = {
|
|
|
|
url: 'http://hooks.slack.com',
|
|
|
|
defaultChannel: 'some-channel',
|
|
|
|
};
|
|
|
|
|
|
|
|
await addon.handleEvent(event, parameters);
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
const req = JSON.parse(fetchRetryCalls[0].options.body);
|
2021-01-19 10:42:45 +01:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(req.channel).toBe('#another-channel');
|
2021-01-19 10:42:45 +01:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('Should post to all channels in tags', async () => {
|
2021-02-05 11:30:30 +01:00
|
|
|
const addon = new SlackAddon({
|
|
|
|
getLogger: noLogger,
|
|
|
|
unleashUrl: 'http://some-url.com',
|
|
|
|
});
|
2021-08-12 15:04:37 +02:00
|
|
|
const event: IEvent = {
|
|
|
|
id: 5,
|
|
|
|
createdAt: new Date(),
|
2021-01-19 10:42:45 +01:00
|
|
|
type: FEATURE_CREATED,
|
|
|
|
createdBy: 'some@user.com',
|
2021-11-12 13:15:51 +01:00
|
|
|
featureName: 'some-toggle',
|
2021-01-19 10:42:45 +01:00
|
|
|
data: {
|
|
|
|
name: 'some-toggle',
|
|
|
|
enabled: false,
|
|
|
|
strategies: [{ name: 'default' }],
|
|
|
|
},
|
|
|
|
tags: [
|
|
|
|
{
|
|
|
|
type: 'slack',
|
|
|
|
value: 'another-channel-1',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'slack',
|
|
|
|
value: 'another-channel-2',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
const parameters = {
|
|
|
|
url: 'http://hooks.slack.com',
|
|
|
|
defaultChannel: 'some-channel',
|
|
|
|
};
|
|
|
|
|
|
|
|
await addon.handleEvent(event, parameters);
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
const req1 = JSON.parse(fetchRetryCalls[0].options.body);
|
|
|
|
const req2 = JSON.parse(fetchRetryCalls[1].options.body);
|
2021-01-19 10:42:45 +01:00
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
expect(fetchRetryCalls).toHaveLength(2);
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(req1.channel).toBe('#another-channel-1');
|
|
|
|
expect(req2.channel).toBe('#another-channel-2');
|
2021-01-19 10:42:45 +01:00
|
|
|
});
|
2023-07-05 09:42:17 +02:00
|
|
|
|
|
|
|
test('Should include custom headers from parameters in call to service', async () => {
|
|
|
|
const addon = new SlackAddon({
|
|
|
|
getLogger: noLogger,
|
|
|
|
unleashUrl: 'http://some-url.com',
|
|
|
|
});
|
|
|
|
const event: IEvent = {
|
|
|
|
id: 2,
|
|
|
|
createdAt: new Date(),
|
|
|
|
type: FEATURE_ENVIRONMENT_DISABLED,
|
|
|
|
createdBy: 'some@user.com',
|
|
|
|
environment: 'development',
|
|
|
|
project: 'default',
|
|
|
|
featureName: 'some-toggle',
|
|
|
|
data: {
|
|
|
|
name: 'some-toggle',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const parameters = {
|
|
|
|
url: 'http://hooks.slack.com',
|
|
|
|
defaultChannel: 'general',
|
|
|
|
customHeaders: `{ "MY_CUSTOM_HEADER": "MY_CUSTOM_VALUE" }`,
|
|
|
|
};
|
|
|
|
|
|
|
|
await addon.handleEvent(event, parameters);
|
|
|
|
expect(fetchRetryCalls).toHaveLength(1);
|
|
|
|
expect(fetchRetryCalls[0].url).toBe(parameters.url);
|
|
|
|
expect(fetchRetryCalls[0].options.body).toMatch(/disabled/);
|
|
|
|
expect(fetchRetryCalls[0].options.body).toMatchSnapshot();
|
|
|
|
expect(fetchRetryCalls[0].options.headers).toMatchSnapshot();
|
|
|
|
});
|