1
0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-11-13 03:35:52 +01:00
verdaccio/test/functional/notifications/notify.js

161 lines
4.5 KiB
JavaScript
Raw Normal View History

2017-12-02 11:19:08 +01:00
import assert from 'assert';
import _ from 'lodash';
import {HEADERS} from '../../../src/lib/constants';
2017-12-02 11:19:08 +01:00
import {notify} from '../../../src/lib/notify';
2017-12-02 11:19:08 +01:00
export default function(express) {
const config = {
notify: {
method: 'POST',
headers: [{
'Content-Type': HEADERS.JSON
}],
endpoint: "http://localhost:55550/api/notify",
content: '{"color":"green","message":"New package published: * {{ name }}*. Publisher name: * {{ publisher.name }} *.","notify":true,"message_format":"text"}'
}
};
const publisherInfo = {
name: "publisher-name-test"
};
2017-12-02 11:19:08 +01:00
describe('notifications', () => {
2017-12-02 11:19:08 +01:00
beforeAll(function () {
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 => {
const metadata = {
name: "pkg-test"
};
notify(metadata, config, publisherInfo).then(function (body) {
const jsonBody = JSON.parse(body);
assert.ok(
`New package published: * ${metadata.name}*. Publisher name: * ${
publisherInfo.name
} *.` === jsonBody.message,
"Body notify message should be equal"
);
done();
}, function (err) {
assert.fail(err);
done();
});
});
2017-12-02 11:19:08 +01:00
test('notification should be send single header', done => {
const metadata = {
name: "pkg-test"
};
const configMultipleHeader = _.cloneDeep(config);
configMultipleHeader.notify.headers = {
'Content-Type': HEADERS.JSON
};
notify(metadata, configMultipleHeader, publisherInfo).then(function (body) {
const jsonBody = JSON.parse(body);
assert.ok(
`New package published: * ${metadata.name}*. Publisher name: * ${
publisherInfo.name
} *.` === jsonBody.message,
"Body notify message should be equal"
);
done();
}, function (err) {
assert.fail(err);
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);
}
notify(metadata, multipleNotificationsEndpoint, publisherInfo).then(function (body) {
body.forEach(function(notification) {
const jsonBody = JSON.parse(notification);
assert.ok(
`New package published: * ${metadata.name}*. Publisher name: * ${
publisherInfo.name
} *.` === jsonBody.message,
"Body notify message should be equal"
);
});
done();
}, function (err) {
assert.fail(err);
done();
});
});
2017-12-02 11:19:08 +01:00
test('notification should fails', done => {
const metadata = {
name: "pkg-test"
};
const configFail = _.cloneDeep(config);
configFail.notify.endpoint = "http://localhost:55550/api/notify/bad";
notify(metadata, configFail, publisherInfo).then(function () {
assert.equal(false, 'This service should fails with status code 400');
done();
}, function (err) {
assert.ok('Bad Request' === err, 'The error message should be "Bad Request');
done();
});
});
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) {
const jsonBody = JSON.parse(body);
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-12-02 11:19:08 +01:00
}