2018-10-18 21:31:30 +03:00
|
|
|
const _ = require('lodash');
|
|
|
|
const should = require('should');
|
|
|
|
const sinon = require('sinon');
|
|
|
|
const rewire = require('rewire');
|
|
|
|
const testUtils = require('../../utils');
|
|
|
|
const common = require('../../../server/lib/common');
|
2017-11-21 18:43:14 +03:00
|
|
|
// Stuff we test
|
2018-10-18 21:31:30 +03:00
|
|
|
const webhooks = {
|
|
|
|
listen: rewire('../../../server/services/webhooks/listen'),
|
|
|
|
trigger: rewire('../../../server/services/webhooks/trigger')
|
|
|
|
};
|
2017-11-21 18:43:14 +03:00
|
|
|
|
2018-10-18 21:31:30 +03:00
|
|
|
const sandbox = sinon.sandbox.create();
|
2017-11-21 18:43:14 +03:00
|
|
|
|
|
|
|
describe('Webhooks', function () {
|
|
|
|
var eventStub;
|
|
|
|
|
|
|
|
beforeEach(function () {
|
2017-12-12 00:47:46 +03:00
|
|
|
eventStub = sandbox.stub(common.events, 'on');
|
2017-11-21 18:43:14 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
sandbox.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('listen() should initialise events correctly', function () {
|
|
|
|
webhooks.listen();
|
2018-10-18 21:31:30 +03:00
|
|
|
eventStub.calledThrice.should.be.true();
|
2017-11-21 18:43:14 +03:00
|
|
|
});
|
|
|
|
|
2018-10-18 21:31:30 +03:00
|
|
|
it('listener() with "subscriber.added" event calls webhooks.trigger with toJSONified model', function () {
|
2017-11-21 18:43:14 +03:00
|
|
|
var testSubscriber = _.clone(testUtils.DataGenerator.Content.subscribers[0]),
|
|
|
|
testModel = {
|
|
|
|
toJSON: function () {
|
|
|
|
return testSubscriber;
|
|
|
|
}
|
|
|
|
},
|
2018-10-18 21:31:30 +03:00
|
|
|
webhooksStub = {
|
|
|
|
trigger: sandbox.stub()
|
2017-11-21 18:43:14 +03:00
|
|
|
},
|
2018-10-18 21:31:30 +03:00
|
|
|
resetWebhooks = webhooks.listen.__set__('webhooks', webhooksStub),
|
|
|
|
listener = webhooks.listen.__get__('listener'),
|
2017-11-21 18:43:14 +03:00
|
|
|
triggerArgs;
|
|
|
|
|
|
|
|
listener('subscriber.added', testModel);
|
|
|
|
|
2018-10-18 21:31:30 +03:00
|
|
|
webhooksStub.trigger.calledOnce.should.be.true();
|
2017-11-21 18:43:14 +03:00
|
|
|
|
2018-10-18 21:31:30 +03:00
|
|
|
triggerArgs = webhooksStub.trigger.getCall(0).args;
|
2017-11-21 18:43:14 +03:00
|
|
|
triggerArgs[0].should.eql('subscriber.added');
|
|
|
|
triggerArgs[1].should.deepEqual({
|
2018-10-19 16:11:03 +03:00
|
|
|
subscribers: [testSubscriber],
|
|
|
|
event: 'subscriber.added'
|
2017-11-21 18:43:14 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
resetWebhooks();
|
|
|
|
});
|
|
|
|
|
2018-10-18 21:31:30 +03:00
|
|
|
it('listener() with "subscriber.deleted" event calls webhooks.trigger with _previousAttributes values', function () {
|
2017-11-21 18:43:14 +03:00
|
|
|
var testSubscriber = _.clone(testUtils.DataGenerator.Content.subscribers[1]),
|
|
|
|
testModel = {
|
|
|
|
_previousAttributes: testSubscriber
|
|
|
|
},
|
2018-10-18 21:31:30 +03:00
|
|
|
webhooksStub = {
|
|
|
|
trigger: sandbox.stub()
|
2017-11-21 18:43:14 +03:00
|
|
|
},
|
2018-10-18 21:31:30 +03:00
|
|
|
resetWebhooks = webhooks.listen.__set__('webhooks', webhooksStub),
|
|
|
|
listener = webhooks.listen.__get__('listener'),
|
2017-11-21 18:43:14 +03:00
|
|
|
triggerArgs;
|
|
|
|
|
|
|
|
listener('subscriber.deleted', testModel);
|
|
|
|
|
2018-10-18 21:31:30 +03:00
|
|
|
webhooksStub.trigger.calledOnce.should.be.true();
|
2017-11-21 18:43:14 +03:00
|
|
|
|
2018-10-18 21:31:30 +03:00
|
|
|
triggerArgs = webhooksStub.trigger.getCall(0).args;
|
2017-11-21 18:43:14 +03:00
|
|
|
triggerArgs[0].should.eql('subscriber.deleted');
|
|
|
|
triggerArgs[1].should.deepEqual({
|
2018-10-19 16:11:03 +03:00
|
|
|
subscribers: [testSubscriber],
|
|
|
|
event: 'subscriber.deleted'
|
2017-11-21 18:43:14 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
resetWebhooks();
|
|
|
|
});
|
2018-10-18 21:31:30 +03:00
|
|
|
|
|
|
|
it('listener() with "site.changed" event calls webhooks.trigger ', function () {
|
|
|
|
const webhooksStub = {
|
|
|
|
trigger: sandbox.stub()
|
|
|
|
};
|
|
|
|
const resetWebhooks = webhooks.listen.__set__('webhooks', webhooksStub);
|
|
|
|
const listener = webhooks.listen.__get__('listener');
|
|
|
|
let triggerArgs;
|
|
|
|
|
|
|
|
listener('site.changed');
|
|
|
|
|
|
|
|
webhooksStub.trigger.calledOnce.should.be.true();
|
|
|
|
|
|
|
|
triggerArgs = webhooksStub.trigger.getCall(0).args;
|
|
|
|
triggerArgs[0].should.eql('site.changed');
|
2018-10-19 16:11:03 +03:00
|
|
|
triggerArgs[1].should.eql({
|
|
|
|
event: 'site.changed'
|
|
|
|
});
|
2018-10-18 21:31:30 +03:00
|
|
|
|
|
|
|
resetWebhooks();
|
|
|
|
});
|
2017-11-21 18:43:14 +03:00
|
|
|
});
|