2016-06-05 14:22:11 +03:00
|
|
|
var _ = require('lodash'),
|
|
|
|
nock = require('nock'),
|
2014-03-15 02:10:50 +04:00
|
|
|
should = require('should'),
|
|
|
|
sinon = require('sinon'),
|
2016-06-05 14:22:11 +03:00
|
|
|
rewire = require('rewire'),
|
2014-03-15 02:10:50 +04:00
|
|
|
testUtils = require('../utils'),
|
2016-06-05 14:22:11 +03:00
|
|
|
configUtils = require('../utils/configUtils'),
|
|
|
|
xmlrpc = rewire('../../server/data/xml/xmlrpc'),
|
2015-03-24 23:23:23 +03:00
|
|
|
events = require('../../server/events'),
|
2014-03-15 02:10:50 +04:00
|
|
|
// storing current environment
|
|
|
|
currentEnv = process.env.NODE_ENV;
|
|
|
|
|
2014-06-05 01:26:03 +04:00
|
|
|
// To stop jshint complaining
|
|
|
|
should.equal(true, true);
|
|
|
|
|
2014-03-15 02:10:50 +04:00
|
|
|
describe('XMLRPC', function () {
|
2016-06-05 14:22:11 +03:00
|
|
|
var sandbox, eventStub;
|
2014-03-15 02:10:50 +04:00
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
sandbox = sinon.sandbox.create();
|
2016-06-05 14:22:11 +03:00
|
|
|
eventStub = sandbox.stub(events, 'on');
|
2014-03-15 02:10:50 +04:00
|
|
|
// give environment a value that will ping
|
2014-06-05 01:26:03 +04:00
|
|
|
process.env.NODE_ENV = 'production';
|
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
|
|
|
// reset the environment
|
|
|
|
process.env.NODE_ENV = currentEnv;
|
|
|
|
});
|
|
|
|
|
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]),
|
|
|
|
testModel = {toJSON: function () {return testPost; }},
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('ping()', function () {
|
|
|
|
var ping = xmlrpc.__get__('ping');
|
|
|
|
|
|
|
|
it('with a post should execute two pings', function () {
|
|
|
|
var ping1 = nock('http://blogsearch.google.com').post('/ping/RPC2').reply(200),
|
|
|
|
ping2 = nock('http://rpc.pingomatic.com').post('/').reply(200),
|
|
|
|
testPost = _.clone(testUtils.DataGenerator.Content.posts[2]);
|
|
|
|
|
|
|
|
ping(testPost);
|
|
|
|
|
|
|
|
ping1.isDone().should.be.true();
|
|
|
|
ping2.isDone().should.be.true();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('with default post should not execute pings', function () {
|
|
|
|
var ping1 = nock('http://blogsearch.google.com').post('/ping/RPC2').reply(200),
|
|
|
|
ping2 = nock('http://rpc.pingomatic.com').post('/').reply(200),
|
|
|
|
testPost = _.clone(testUtils.DataGenerator.Content.posts[2]);
|
|
|
|
|
|
|
|
testPost.slug = 'welcome-to-ghost';
|
|
|
|
|
|
|
|
ping(testPost);
|
|
|
|
|
|
|
|
ping1.isDone().should.be.false();
|
|
|
|
ping2.isDone().should.be.false();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('with a page should not execute pings', function () {
|
|
|
|
var ping1 = nock('http://blogsearch.google.com').post('/ping/RPC2').reply(200),
|
|
|
|
ping2 = nock('http://rpc.pingomatic.com').post('/').reply(200),
|
|
|
|
testPage = _.clone(testUtils.DataGenerator.Content.posts[5]);
|
|
|
|
|
|
|
|
ping(testPage);
|
|
|
|
|
|
|
|
ping1.isDone().should.be.false();
|
|
|
|
ping2.isDone().should.be.false();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('when privacy.useRpcPing is false should not execute pings', function () {
|
|
|
|
var ping1 = nock('http://blogsearch.google.com').post('/ping/RPC2').reply(200),
|
|
|
|
ping2 = nock('http://rpc.pingomatic.com').post('/').reply(200),
|
|
|
|
testPost = _.clone(testUtils.DataGenerator.Content.posts[2]);
|
|
|
|
|
|
|
|
configUtils.set({privacy: {useRpcPing: false}});
|
|
|
|
|
|
|
|
ping(testPost);
|
|
|
|
|
|
|
|
ping1.isDone().should.be.false();
|
|
|
|
ping2.isDone().should.be.false();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('captures errors from requests', function (done) {
|
|
|
|
var ping1 = nock('http://blogsearch.google.com').post('/ping/RPC2').reply(200),
|
|
|
|
ping2 = nock('http://rpc.pingomatic.com').post('/').replyWithError('ping site is down'),
|
|
|
|
testPost = _.clone(testUtils.DataGenerator.Content.posts[2]),
|
|
|
|
errorMock, resetXmlRpc;
|
|
|
|
|
|
|
|
errorMock = {
|
|
|
|
logError: function logError(error) {
|
|
|
|
should.exist(error);
|
|
|
|
error.message.should.eql('ping site is down');
|
|
|
|
|
|
|
|
// Reset xmlrpc handleError method and exit test
|
|
|
|
resetXmlRpc();
|
|
|
|
done();
|
2015-03-24 23:23:23 +03:00
|
|
|
}
|
|
|
|
};
|
2014-03-15 02:10:50 +04:00
|
|
|
|
2016-06-05 14:22:11 +03:00
|
|
|
resetXmlRpc = xmlrpc.__set__('errors', errorMock);
|
|
|
|
|
|
|
|
ping(testPost);
|
|
|
|
|
|
|
|
ping1.isDone().should.be.true();
|
|
|
|
ping2.isDone().should.be.true();
|
|
|
|
});
|
2014-03-15 02:10:50 +04:00
|
|
|
});
|
|
|
|
});
|