2018-06-02 22:48:23 +03:00
|
|
|
var should = require('should'),
|
2016-10-06 15:27:35 +03:00
|
|
|
sinon = require('sinon'),
|
2017-03-21 11:24:11 +03:00
|
|
|
_ = require('lodash'),
|
|
|
|
nock = require('nock'),
|
2016-10-06 15:27:35 +03:00
|
|
|
http = require('http'),
|
|
|
|
rewire = require('rewire'),
|
2017-10-25 17:27:56 +03:00
|
|
|
testUtils = require('../../utils'),
|
|
|
|
configUtils = require('../../utils/configUtils'),
|
|
|
|
xmlrpc = rewire('../../../server/services/xmlrpc'),
|
2017-12-12 00:47:46 +03:00
|
|
|
common = require('../../../server/lib/common'),
|
2014-03-15 02:10:50 +04:00
|
|
|
|
2017-03-21 11:24:11 +03:00
|
|
|
sandbox = sinon.sandbox.create();
|
2014-06-05 01:26:03 +04:00
|
|
|
|
2014-03-15 02:10:50 +04:00
|
|
|
describe('XMLRPC', function () {
|
2017-03-21 11:24:11 +03:00
|
|
|
var eventStub;
|
2014-03-15 02:10:50 +04:00
|
|
|
|
|
|
|
beforeEach(function () {
|
2017-12-12 00:47:46 +03:00
|
|
|
eventStub = sandbox.stub(common.events, 'on');
|
2017-02-03 21:25:39 +03:00
|
|
|
configUtils.set('privacy:useRpcPing', true);
|
2014-03-15 02:10:50 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
sandbox.restore();
|
2016-06-05 14:22:11 +03:00
|
|
|
configUtils.restore();
|
|
|
|
nock.cleanAll();
|
2014-03-15 02:10:50 +04:00
|
|
|
});
|
|
|
|
|
2016-06-05 14:22:11 +03:00
|
|
|
it('listen() should initialise event correctly', function () {
|
|
|
|
xmlrpc.listen();
|
|
|
|
eventStub.calledOnce.should.be.true();
|
|
|
|
eventStub.calledWith('post.published', xmlrpc.__get__('listener')).should.be.true();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('listener() calls ping() with toJSONified model', function () {
|
|
|
|
var testPost = _.clone(testUtils.DataGenerator.Content.posts[2]),
|
2016-10-06 15:27:35 +03:00
|
|
|
testModel = {
|
|
|
|
toJSON: function () {
|
|
|
|
return testPost;
|
|
|
|
}
|
|
|
|
},
|
2016-06-05 14:22:11 +03:00
|
|
|
pingStub = sandbox.stub(),
|
|
|
|
resetXmlRpc = xmlrpc.__set__('ping', pingStub),
|
|
|
|
listener = xmlrpc.__get__('listener');
|
|
|
|
|
|
|
|
listener(testModel);
|
|
|
|
|
|
|
|
pingStub.calledOnce.should.be.true();
|
|
|
|
pingStub.calledWith(testPost).should.be.true();
|
|
|
|
|
|
|
|
// Reset xmlrpc ping method
|
|
|
|
resetXmlRpc();
|
|
|
|
});
|
|
|
|
|
2017-10-23 20:30:33 +03:00
|
|
|
it('listener() does not call ping() when importing', function () {
|
|
|
|
var testPost = _.clone(testUtils.DataGenerator.Content.posts[2]),
|
|
|
|
testModel = {
|
|
|
|
toJSON: function () {
|
|
|
|
return testPost;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
pingStub = sandbox.stub(),
|
|
|
|
resetXmlRpc = xmlrpc.__set__('ping', pingStub),
|
|
|
|
listener = xmlrpc.__get__('listener');
|
|
|
|
|
|
|
|
listener(testModel, {importing: true});
|
|
|
|
|
|
|
|
pingStub.calledOnce.should.be.false();
|
|
|
|
|
|
|
|
// Reset xmlrpc ping method
|
|
|
|
resetXmlRpc();
|
|
|
|
});
|
|
|
|
|
2016-06-05 14:22:11 +03:00
|
|
|
describe('ping()', function () {
|
|
|
|
var ping = xmlrpc.__get__('ping');
|
|
|
|
|
2017-12-15 12:50:18 +03:00
|
|
|
it('with a post should execute two pings', function (done) {
|
2018-08-23 16:20:29 +03:00
|
|
|
var ping1 = nock('http://rpc.pingomatic.com').post('/').reply(200),
|
2016-06-05 14:22:11 +03:00
|
|
|
testPost = _.clone(testUtils.DataGenerator.Content.posts[2]);
|
|
|
|
|
|
|
|
ping(testPost);
|
|
|
|
|
2017-12-15 12:50:18 +03:00
|
|
|
(function retry() {
|
2018-08-23 16:20:29 +03:00
|
|
|
if (ping1.isDone()) {
|
2017-12-15 12:50:18 +03:00
|
|
|
return done();
|
|
|
|
}
|
|
|
|
|
|
|
|
setTimeout(retry, 100);
|
|
|
|
}());
|
2016-06-05 14:22:11 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('with default post should not execute pings', function () {
|
2018-08-23 16:20:29 +03:00
|
|
|
var ping1 = nock('http://rpc.pingomatic.com').post('/').reply(200),
|
2016-06-05 14:22:11 +03:00
|
|
|
testPost = _.clone(testUtils.DataGenerator.Content.posts[2]);
|
|
|
|
|
2017-06-08 18:36:14 +03:00
|
|
|
testPost.slug = 'welcome';
|
2016-06-05 14:22:11 +03:00
|
|
|
|
|
|
|
ping(testPost);
|
|
|
|
|
|
|
|
ping1.isDone().should.be.false();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('with a page should not execute pings', function () {
|
2018-08-23 16:20:29 +03:00
|
|
|
var ping1 = nock('http://rpc.pingomatic.com').post('/').reply(200),
|
2016-06-05 14:22:11 +03:00
|
|
|
testPage = _.clone(testUtils.DataGenerator.Content.posts[5]);
|
|
|
|
|
|
|
|
ping(testPage);
|
|
|
|
|
|
|
|
ping1.isDone().should.be.false();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('when privacy.useRpcPing is false should not execute pings', function () {
|
2018-08-23 16:20:29 +03:00
|
|
|
var ping1 = nock('http://rpc.pingomatic.com').post('/').reply(200),
|
2016-06-05 14:22:11 +03:00
|
|
|
testPost = _.clone(testUtils.DataGenerator.Content.posts[2]);
|
|
|
|
|
|
|
|
configUtils.set({privacy: {useRpcPing: false}});
|
|
|
|
|
|
|
|
ping(testPost);
|
|
|
|
|
|
|
|
ping1.isDone().should.be.false();
|
|
|
|
});
|
|
|
|
|
2017-12-15 12:50:18 +03:00
|
|
|
it('captures && logs errors from requests', function (done) {
|
2016-10-06 15:27:35 +03:00
|
|
|
var testPost = _.clone(testUtils.DataGenerator.Content.posts[2]),
|
2018-08-23 16:20:29 +03:00
|
|
|
ping1 = nock('http://rpc.pingomatic.com').post('/').reply(400),
|
2017-12-12 00:47:46 +03:00
|
|
|
loggingStub = sandbox.stub(common.logging, 'error');
|
2016-06-05 14:22:11 +03:00
|
|
|
|
|
|
|
ping(testPost);
|
|
|
|
|
2017-12-15 12:50:18 +03:00
|
|
|
(function retry() {
|
2018-08-23 16:20:29 +03:00
|
|
|
if (ping1.isDone()) {
|
|
|
|
loggingStub.calledOnce.should.eql(true);
|
|
|
|
loggingStub.args[0][0].message.should.containEql('Response code 400');
|
|
|
|
return done();
|
|
|
|
}
|
|
|
|
|
|
|
|
setTimeout(retry, 100);
|
|
|
|
}());
|
|
|
|
});
|
|
|
|
|
|
|
|
it('captures && logs XML errors from requests with newlines between tags', function (done) {
|
|
|
|
var testPost = _.clone(testUtils.DataGenerator.Content.posts[2]),
|
|
|
|
ping1 = nock('http://rpc.pingomatic.com').post('/').reply(200,
|
|
|
|
`<?xml version="1.0"?>
|
|
|
|
<methodResponse>
|
|
|
|
<params>
|
|
|
|
<param>
|
|
|
|
<value>
|
|
|
|
<struct>
|
|
|
|
<member>
|
|
|
|
<name>flerror</name>
|
|
|
|
<value>
|
|
|
|
<boolean>1</boolean>
|
|
|
|
</value>
|
|
|
|
</member>
|
|
|
|
<member>
|
|
|
|
<name>message</name>
|
|
|
|
<value>
|
|
|
|
<string>Uh oh. A wee lil error.</string>
|
|
|
|
</value>
|
|
|
|
</member>
|
|
|
|
</struct>
|
|
|
|
</value>
|
|
|
|
</param>
|
|
|
|
</params>
|
|
|
|
</methodResponse>`),
|
|
|
|
loggingStub = sandbox.stub(common.logging, 'error');
|
|
|
|
|
|
|
|
ping(testPost);
|
|
|
|
|
|
|
|
(function retry() {
|
|
|
|
if (ping1.isDone()) {
|
|
|
|
loggingStub.calledOnce.should.eql(true);
|
|
|
|
loggingStub.args[0][0].message.should.equal('Uh oh. A wee lil error.');
|
|
|
|
return done();
|
|
|
|
}
|
|
|
|
|
|
|
|
setTimeout(retry, 100);
|
|
|
|
}());
|
|
|
|
});
|
|
|
|
|
|
|
|
it('captures && logs XML errors from requests without newlines between tags', function (done) {
|
|
|
|
var testPost = _.clone(testUtils.DataGenerator.Content.posts[2]),
|
|
|
|
ping1 = nock('http://rpc.pingomatic.com').post('/').reply(200,
|
|
|
|
(`<?xml version="1.0"?>
|
|
|
|
<methodResponse>
|
|
|
|
<params>
|
|
|
|
<param>
|
|
|
|
<value>
|
|
|
|
<struct>
|
|
|
|
<member>
|
|
|
|
<name>flerror</name>
|
|
|
|
<value>
|
|
|
|
<boolean>1</boolean>
|
|
|
|
</value>
|
|
|
|
</member>
|
|
|
|
<member>
|
|
|
|
<name>message</name>
|
|
|
|
<value>
|
|
|
|
<string>Uh oh. A wee lil error.</string>
|
|
|
|
</value>
|
|
|
|
</member>
|
|
|
|
</struct>
|
|
|
|
</value>
|
|
|
|
</param>
|
|
|
|
</params>
|
|
|
|
</methodResponse>`).replace('\n', '')),
|
|
|
|
loggingStub = sandbox.stub(common.logging, 'error');
|
|
|
|
|
|
|
|
ping(testPost);
|
|
|
|
|
|
|
|
(function retry() {
|
|
|
|
if (ping1.isDone()) {
|
|
|
|
loggingStub.calledOnce.should.eql(true);
|
|
|
|
loggingStub.args[0][0].message.should.equal('Uh oh. A wee lil error.');
|
|
|
|
return done();
|
|
|
|
}
|
|
|
|
|
|
|
|
setTimeout(retry, 100);
|
|
|
|
}());
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not error with responses that have 0 as flerror value', function (done) {
|
|
|
|
var testPost = _.clone(testUtils.DataGenerator.Content.posts[2]),
|
|
|
|
ping1 = nock('http://rpc.pingomatic.com').post('/').reply(200,
|
|
|
|
`<?xml version="1.0"?>
|
|
|
|
<methodResponse>
|
|
|
|
<params>
|
|
|
|
<param>
|
|
|
|
<value>
|
|
|
|
<struct>
|
|
|
|
<member><name>flerror</name><value><boolean>0</boolean></value></member>
|
|
|
|
<member><name>message</name><value><string>Pings being forwarded to 9 services!</string></value></member>
|
|
|
|
</struct>
|
|
|
|
</value>
|
|
|
|
</param>
|
|
|
|
</params>
|
|
|
|
</methodResponse>`),
|
|
|
|
loggingStub = sandbox.stub(common.logging, 'error');
|
|
|
|
|
|
|
|
ping(testPost);
|
|
|
|
|
|
|
|
(function retry() {
|
|
|
|
if (ping1.isDone()) {
|
|
|
|
loggingStub.calledOnce.should.eql(false);
|
2017-12-15 12:50:18 +03:00
|
|
|
return done();
|
|
|
|
}
|
|
|
|
|
|
|
|
setTimeout(retry, 100);
|
|
|
|
}());
|
2016-06-05 14:22:11 +03:00
|
|
|
});
|
2014-03-15 02:10:50 +04:00
|
|
|
});
|
|
|
|
});
|