Ghost/core/test/unit/services/xmlrpc_spec.js
Katharina Irrgang fb044e6d88
Bumped sinon from 4.4.6 to 7.3.2 (#10400)
refs #9389

- https://github.com/sinonjs/sinon/blob/master/CHANGELOG.md

Breaking changes for Ghost:

- no need to create a sandbox anymore, each file get's it's own sandbox
- just require sinon and use this sandbox
- you can still create separate sandboxes with .createSandbox
- reset single stubs: use .resetHistory instead of .reset

This is a global replace for any sandbox creation.

---

From https://sinonjs.org/releases/v7.2.3/sandbox/

> Default sandbox
> Since sinon@5.0.0, the sinon object is a default sandbox. Unless you have a very advanced setup or need a special configuration, you probably want to just use that one.
2019-01-21 17:53:44 +01:00

251 lines
8.6 KiB
JavaScript

var should = require('should'),
sinon = require('sinon'),
_ = require('lodash'),
nock = require('nock'),
http = require('http'),
rewire = require('rewire'),
testUtils = require('../../utils'),
configUtils = require('../../utils/configUtils'),
xmlrpc = rewire('../../../server/services/xmlrpc'),
common = require('../../../server/lib/common');
describe('XMLRPC', function () {
var eventStub;
beforeEach(function () {
eventStub = sinon.stub(common.events, 'on');
configUtils.set('privacy:useRpcPing', true);
});
afterEach(function () {
sinon.restore();
configUtils.restore();
nock.cleanAll();
});
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 = sinon.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();
});
it('listener() does not call ping() when importing', function () {
var testPost = _.clone(testUtils.DataGenerator.Content.posts[2]),
testModel = {
toJSON: function () {
return testPost;
}
},
pingStub = sinon.stub(),
resetXmlRpc = xmlrpc.__set__('ping', pingStub),
listener = xmlrpc.__get__('listener');
listener(testModel, {importing: true});
pingStub.calledOnce.should.be.false();
// Reset xmlrpc ping method
resetXmlRpc();
});
describe('ping()', function () {
var ping = xmlrpc.__get__('ping');
it('with a post should execute two pings', function (done) {
var ping1 = nock('http://rpc.pingomatic.com').post('/').reply(200),
testPost = _.clone(testUtils.DataGenerator.Content.posts[2]);
ping(testPost);
(function retry() {
if (ping1.isDone()) {
return done();
}
setTimeout(retry, 100);
}());
});
it('with default post should not execute pings', function () {
var ping1 = nock('http://rpc.pingomatic.com').post('/').reply(200),
testPost = _.clone(testUtils.DataGenerator.Content.posts[2]);
testPost.slug = 'welcome';
ping(testPost);
ping1.isDone().should.be.false();
});
it('with a page should not execute pings', function () {
var ping1 = nock('http://rpc.pingomatic.com').post('/').reply(200),
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 () {
var ping1 = 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();
});
it('captures && logs errors from requests', function (done) {
var testPost = _.clone(testUtils.DataGenerator.Content.posts[2]),
ping1 = nock('http://rpc.pingomatic.com').post('/').reply(400),
loggingStub = sinon.stub(common.logging, 'error');
ping(testPost);
(function retry() {
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 = sinon.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 = sinon.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 = sinon.stub(common.logging, 'error');
ping(testPost);
(function retry() {
if (ping1.isDone()) {
loggingStub.calledOnce.should.eql(false);
return done();
}
setTimeout(retry, 100);
}());
});
});
});