2017-12-02 05:19:08 -05:00
|
|
|
import assert from 'assert';
|
|
|
|
import _ from 'lodash';
|
2017-07-01 04:17:46 -05:00
|
|
|
|
2018-04-30 08:41:04 -05:00
|
|
|
import {HEADERS} from '../../../src/lib/constants';
|
2017-12-02 05:19:08 -05:00
|
|
|
import {notify} from '../../../src/lib/notify';
|
2017-07-01 04:17:46 -05:00
|
|
|
|
2017-12-02 05:19:08 -05:00
|
|
|
export default function(express) {
|
2017-07-01 04:17:46 -05:00
|
|
|
const config = {
|
|
|
|
notify: {
|
|
|
|
method: 'POST',
|
|
|
|
headers: [{
|
2018-04-30 08:41:04 -05:00
|
|
|
'Content-Type': HEADERS.JSON
|
2017-07-01 04:17:46 -05:00
|
|
|
}],
|
|
|
|
endpoint: "http://localhost:55550/api/notify",
|
2018-06-09 22:15:44 -05:00
|
|
|
content: '{"color":"green","message":"New package published: * {{ name }}*. Publisher name: * {{ publisher.name }} *.","notify":true,"message_format":"text"}'
|
2017-07-01 04:17:46 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-06-09 22:15:44 -05:00
|
|
|
const publisherInfo = {
|
|
|
|
name: "publisher-name-test"
|
|
|
|
};
|
|
|
|
|
2017-12-02 05:19:08 -05:00
|
|
|
describe('notifications', () => {
|
2017-07-01 04:17:46 -05:00
|
|
|
|
2018-06-14 00:25:09 -05:00
|
|
|
function parseBody(notification) {
|
|
|
|
const jsonBody = JSON.parse(notification);
|
|
|
|
|
|
|
|
return jsonBody;
|
|
|
|
}
|
|
|
|
|
2017-12-02 05:19:08 -05:00
|
|
|
beforeAll(function () {
|
2017-07-01 04:17:46 -05: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 05:19:08 -05:00
|
|
|
test('notification should be send', done => {
|
2017-07-01 04:17:46 -05:00
|
|
|
const metadata = {
|
|
|
|
name: "pkg-test"
|
|
|
|
};
|
|
|
|
|
2018-06-09 22:15:44 -05:00
|
|
|
notify(metadata, config, publisherInfo).then(function (body) {
|
2018-06-14 00:25:09 -05:00
|
|
|
const jsonBody = parseBody(body);
|
2018-06-09 22:15:44 -05:00
|
|
|
assert.ok(
|
|
|
|
`New package published: * ${metadata.name}*. Publisher name: * ${
|
|
|
|
publisherInfo.name
|
|
|
|
} *.` === jsonBody.message,
|
|
|
|
"Body notify message should be equal"
|
|
|
|
);
|
2017-07-01 04:17:46 -05:00
|
|
|
done();
|
|
|
|
}, function (err) {
|
|
|
|
assert.fail(err);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-12-02 05:19:08 -05:00
|
|
|
test('notification should be send single header', done => {
|
2017-07-01 04:17:46 -05:00
|
|
|
const metadata = {
|
|
|
|
name: "pkg-test"
|
|
|
|
};
|
|
|
|
|
|
|
|
const configMultipleHeader = _.cloneDeep(config);
|
|
|
|
configMultipleHeader.notify.headers = {
|
2018-04-30 08:41:04 -05:00
|
|
|
'Content-Type': HEADERS.JSON
|
2017-07-01 04:17:46 -05:00
|
|
|
};
|
|
|
|
|
2018-06-09 22:15:44 -05:00
|
|
|
notify(metadata, configMultipleHeader, publisherInfo).then(function (body) {
|
2018-06-14 00:25:09 -05:00
|
|
|
const jsonBody = parseBody(body);
|
2018-06-09 22:15:44 -05:00
|
|
|
assert.ok(
|
|
|
|
`New package published: * ${metadata.name}*. Publisher name: * ${
|
|
|
|
publisherInfo.name
|
|
|
|
} *.` === jsonBody.message,
|
|
|
|
"Body notify message should be equal"
|
|
|
|
);
|
2017-07-01 04:17:46 -05:00
|
|
|
done();
|
|
|
|
}, function (err) {
|
|
|
|
assert.fail(err);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-12-02 05:19:08 -05: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 04:17:46 -05:00
|
|
|
|
2018-06-09 22:15:44 -05:00
|
|
|
notify(metadata, multipleNotificationsEndpoint, publisherInfo).then(function (body) {
|
2017-10-12 10:35:34 -05:00
|
|
|
body.forEach(function(notification) {
|
2018-06-14 00:25:09 -05:00
|
|
|
const jsonBody = parseBody(notification);
|
2018-06-09 22:15:44 -05:00
|
|
|
assert.ok(
|
|
|
|
`New package published: * ${metadata.name}*. Publisher name: * ${
|
|
|
|
publisherInfo.name
|
|
|
|
} *.` === jsonBody.message,
|
|
|
|
"Body notify message should be equal"
|
|
|
|
);
|
2017-10-12 10:35:34 -05:00
|
|
|
});
|
2017-07-01 04:17:46 -05:00
|
|
|
done();
|
|
|
|
}, function (err) {
|
|
|
|
assert.fail(err);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-12-02 05:19:08 -05:00
|
|
|
test('notification should fails', done => {
|
2017-07-01 04:17:46 -05:00
|
|
|
const metadata = {
|
|
|
|
name: "pkg-test"
|
|
|
|
};
|
|
|
|
const configFail = _.cloneDeep(config);
|
|
|
|
configFail.notify.endpoint = "http://localhost:55550/api/notify/bad";
|
|
|
|
|
2018-06-09 22:15:44 -05:00
|
|
|
notify(metadata, configFail, publisherInfo).then(function () {
|
2017-07-01 04:17:46 -05:00
|
|
|
assert.equal(false, 'This service should fails with status code 400');
|
|
|
|
done();
|
|
|
|
}, function (err) {
|
2018-06-14 00:25:09 -05:00
|
|
|
expect(err).toEqual('bad response');
|
2017-07-01 04:17:46 -05:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-06-09 22:15:44 -05: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 00:25:09 -05:00
|
|
|
const jsonBody = parseBody(body);
|
2018-06-09 22:15:44 -05:00
|
|
|
assert.ok(
|
|
|
|
`New package published: * ${metadata.name}*. Publisher name: * ${
|
|
|
|
metadata.publisher.name
|
|
|
|
} *.` === jsonBody.message,
|
|
|
|
"Body notify message should be equal"
|
|
|
|
);
|
|
|
|
done();
|
|
|
|
},
|
|
|
|
function(err) {
|
|
|
|
assert.fail(err);
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2017-07-01 04:17:46 -05:00
|
|
|
});
|
2017-12-02 05:19:08 -05:00
|
|
|
}
|