2017-12-02 11:19:08 +01:00
|
|
|
import _ from 'lodash';
|
2017-07-01 11:17:46 +02:00
|
|
|
|
2018-04-30 15:41:04 +02:00
|
|
|
import {HEADERS} from '../../../src/lib/constants';
|
2017-12-02 11:19:08 +01:00
|
|
|
import {notify} from '../../../src/lib/notify';
|
2017-07-01 11:17:46 +02:00
|
|
|
|
2017-12-02 11:19:08 +01:00
|
|
|
export default function(express) {
|
2017-07-01 11:17:46 +02:00
|
|
|
const config = {
|
|
|
|
notify: {
|
|
|
|
method: 'POST',
|
|
|
|
headers: [{
|
2018-04-30 15:41:04 +02:00
|
|
|
'Content-Type': HEADERS.JSON
|
2017-07-01 11:17:46 +02:00
|
|
|
}],
|
|
|
|
endpoint: "http://localhost:55550/api/notify",
|
2018-06-21 23:33:20 +02:00
|
|
|
content: `{"color":"green","message":"New package published: * {{ name }}*. Publisher name: * {{ publisher.name }} *.","notify":true,"message_format":"text"}`
|
2017-07-01 11:17:46 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-06-10 05:15:44 +02:00
|
|
|
const publisherInfo = {
|
|
|
|
name: "publisher-name-test"
|
|
|
|
};
|
|
|
|
|
2017-12-02 11:19:08 +01:00
|
|
|
describe('notifications', () => {
|
2017-07-01 11:17:46 +02:00
|
|
|
|
2018-06-14 07:25:09 +02:00
|
|
|
function parseBody(notification) {
|
|
|
|
const jsonBody = JSON.parse(notification);
|
|
|
|
|
|
|
|
return jsonBody;
|
|
|
|
}
|
|
|
|
|
2017-12-02 11:19:08 +01:00
|
|
|
beforeAll(function () {
|
2017-07-01 11:17:46 +02:00
|
|
|
express.post('/api/notify', function (req, res) {
|
|
|
|
res.send(req.body);
|
|
|
|
});
|
|
|
|
express.post('/api/notify/bad', function (req, res) {
|
|
|
|
res.status(400);
|
|
|
|
res.send('bad response');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-12-02 11:19:08 +01:00
|
|
|
test('notification should be send', done => {
|
2017-07-01 11:17:46 +02:00
|
|
|
const metadata = {
|
|
|
|
name: "pkg-test"
|
|
|
|
};
|
|
|
|
|
2018-06-10 05:15:44 +02:00
|
|
|
notify(metadata, config, publisherInfo).then(function (body) {
|
2018-06-14 07:25:09 +02:00
|
|
|
const jsonBody = parseBody(body);
|
2018-06-21 23:33:20 +02:00
|
|
|
expect(
|
|
|
|
`New package published: * ${metadata.name}*. Publisher name: * ${publisherInfo.name} *.`).toBe(jsonBody.message, "Body notify message should be equal");
|
2017-07-01 11:17:46 +02:00
|
|
|
done();
|
|
|
|
}, function (err) {
|
2018-06-21 23:33:20 +02:00
|
|
|
expect(err).toBeDefined();
|
2017-07-01 11:17:46 +02:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-12-02 11:19:08 +01:00
|
|
|
test('notification should be send single header', done => {
|
2017-07-01 11:17:46 +02:00
|
|
|
const metadata = {
|
|
|
|
name: "pkg-test"
|
|
|
|
};
|
|
|
|
|
|
|
|
const configMultipleHeader = _.cloneDeep(config);
|
|
|
|
configMultipleHeader.notify.headers = {
|
2018-04-30 15:41:04 +02:00
|
|
|
'Content-Type': HEADERS.JSON
|
2017-07-01 11:17:46 +02:00
|
|
|
};
|
|
|
|
|
2018-06-10 05:15:44 +02:00
|
|
|
notify(metadata, configMultipleHeader, publisherInfo).then(function (body) {
|
2018-06-14 07:25:09 +02:00
|
|
|
const jsonBody = parseBody(body);
|
2018-06-21 23:33:20 +02:00
|
|
|
expect(
|
|
|
|
`New package published: * ${metadata.name}*. Publisher name: * ${publisherInfo.name} *.`)
|
|
|
|
.toBe(jsonBody.message, "Body notify message should be equal");
|
2017-07-01 11:17:46 +02:00
|
|
|
done();
|
|
|
|
}, function (err) {
|
2018-06-21 23:33:20 +02:00
|
|
|
expect(err).toBeDefined();
|
2017-07-01 11:17:46 +02:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-12-02 11:19:08 +01:00
|
|
|
test(
|
|
|
|
'notification should be send multiple notifications endpoints',
|
|
|
|
done => {
|
|
|
|
const metadata = {
|
|
|
|
name: "pkg-test"
|
|
|
|
};
|
|
|
|
// let notificationsCounter = 0;
|
|
|
|
|
|
|
|
const multipleNotificationsEndpoint = {
|
|
|
|
notify: []
|
|
|
|
};
|
|
|
|
|
|
|
|
for (let i = 0; i < 10; i++) {
|
|
|
|
const notificationSettings = _.cloneDeep(config.notify);
|
|
|
|
// basically we allow al notifications
|
|
|
|
notificationSettings.packagePattern = /^pkg-test$/;
|
|
|
|
// notificationSettings.packagePatternFlags = 'i';
|
|
|
|
multipleNotificationsEndpoint.notify.push(notificationSettings);
|
|
|
|
}
|
2017-07-01 11:17:46 +02:00
|
|
|
|
2018-06-10 05:15:44 +02:00
|
|
|
notify(metadata, multipleNotificationsEndpoint, publisherInfo).then(function (body) {
|
2017-10-12 17:35:34 +02:00
|
|
|
body.forEach(function(notification) {
|
2018-06-14 07:25:09 +02:00
|
|
|
const jsonBody = parseBody(notification);
|
2018-06-21 23:33:20 +02:00
|
|
|
expect(
|
|
|
|
`New package published: * ${metadata.name}*. Publisher name: * ${publisherInfo.name} *.`)
|
|
|
|
.toBe(jsonBody.message, "Body notify message should be equal");
|
2017-10-12 17:35:34 +02:00
|
|
|
});
|
2017-07-01 11:17:46 +02:00
|
|
|
done();
|
|
|
|
}, function (err) {
|
2018-06-21 23:33:20 +02:00
|
|
|
expect(err).toBeDefined();
|
2017-07-01 11:17:46 +02:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-12-02 11:19:08 +01:00
|
|
|
test('notification should fails', done => {
|
2017-07-01 11:17:46 +02:00
|
|
|
const metadata = {
|
|
|
|
name: "pkg-test"
|
|
|
|
};
|
|
|
|
const configFail = _.cloneDeep(config);
|
|
|
|
configFail.notify.endpoint = "http://localhost:55550/api/notify/bad";
|
|
|
|
|
2018-06-10 05:15:44 +02:00
|
|
|
notify(metadata, configFail, publisherInfo).then(function () {
|
2018-06-21 23:33:20 +02:00
|
|
|
expect(false).toBe('This service should fails with status code 400');
|
2017-07-01 11:17:46 +02:00
|
|
|
done();
|
|
|
|
}, function (err) {
|
2018-06-14 07:25:09 +02:00
|
|
|
expect(err).toEqual('bad response');
|
2017-07-01 11:17:46 +02:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-06-10 05:15:44 +02:00
|
|
|
test("publisher property should not be overridden if it exists in metadata", done => {
|
|
|
|
const metadata = {
|
|
|
|
name: "pkg-test",
|
|
|
|
publisher: {
|
|
|
|
name: "existing-publisher-name"
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
notify(metadata, config, publisherInfo).then(
|
|
|
|
function(body) {
|
2018-06-14 07:25:09 +02:00
|
|
|
const jsonBody = parseBody(body);
|
2018-06-21 23:33:20 +02:00
|
|
|
expect(`New package published: * ${metadata.name}*. Publisher name: * ${metadata.publisher.name} *.`)
|
|
|
|
.toBe(jsonBody.message, "Body notify message should be equal");
|
2018-06-10 05:15:44 +02:00
|
|
|
done();
|
|
|
|
},
|
|
|
|
function(err) {
|
2018-06-21 23:33:20 +02:00
|
|
|
expect(err).toBeDefined();
|
2018-06-10 05:15:44 +02:00
|
|
|
done();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2017-07-01 11:17:46 +02:00
|
|
|
});
|
2017-12-02 11:19:08 +01:00
|
|
|
}
|