Ghost/core/test/unit/services/slack_spec.js

250 lines
8.2 KiB
JavaScript
Raw Normal View History

var should = require('should'), // jshint ignore:line,
sinon = require('sinon'),
_ = require('lodash'),
nock = require('nock'),
rewire = require('rewire'),
url = require('url'),
testUtils = require('../../utils'),
configUtils = require('../../utils/configUtils'),
// Stuff we test
slack = rewire('../../../server/services/slack'),
common = require('../../../server/lib/common'),
urlService = require('../../../server/services/url'),
schema = require('../../../server/data/schema').checks,
settingsCache = require('../../../server/services/settings/cache'),
sandbox = sinon.sandbox.create(),
// Test data
slackObjNoUrl = [{url: ''}],
slackObjWithUrl = [{url: 'https://hooks.slack.com/services/a-b-c-d'}];
describe('Slack', function () {
var eventStub;
beforeEach(function () {
eventStub = sandbox.stub(common.events, 'on');
});
afterEach(function () {
sandbox.restore();
configUtils.restore();
});
it('listen() should initialise event correctly', function () {
slack.listen();
eventStub.calledTwice.should.be.true();
eventStub.firstCall.calledWith('post.published', slack.__get__('listener')).should.be.true();
eventStub.secondCall.calledWith('slack.test', slack.__get__('testPing')).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(),
resetSlack = slack.__set__('ping', pingStub),
listener = slack.__get__('listener');
listener(testModel);
pingStub.calledOnce.should.be.true();
pingStub.calledWith(testPost).should.be.true();
// Reset slack ping method
resetSlack();
});
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(),
resetSlack = slack.__set__('ping', pingStub),
listener = slack.__get__('listener');
listener(testModel, {importing: true});
pingStub.calledOnce.should.be.false();
// Reset slack ping method
resetSlack();
});
it('testPing() calls ping() with default message', function () {
var pingStub = sandbox.stub(),
resetSlack = slack.__set__('ping', pingStub),
testPing = slack.__get__('testPing');
testPing();
pingStub.calledOnce.should.be.true();
pingStub.calledWith(sinon.match.has('message')).should.be.true();
// Reset slack ping method
resetSlack();
});
describe('ping()', function () {
var isPostStub,
urlForSpy,
settingsCacheStub,
slackReset,
makeRequestStub,
ping = slack.__get__('ping');
beforeEach(function () {
isPostStub = sandbox.stub(schema, 'isPost');
urlForSpy = sandbox.spy(urlService.utils, 'urlFor');
settingsCacheStub = sandbox.stub(settingsCache, 'get');
sandbox.spy(common.logging, 'error');
makeRequestStub = sandbox.stub();
slackReset = slack.__set__('request', makeRequestStub);
makeRequestStub.resolves();
configUtils.set('url', 'http://myblog.com');
});
afterEach(function () {
slackReset();
});
it('makes a request for a post if url is provided', function () {
var requestUrl, requestData;
isPostStub.returns(true);
settingsCacheStub.withArgs('slack').returns(slackObjWithUrl);
// execute code
ping({});
// assertions
makeRequestStub.calledOnce.should.be.true();
isPostStub.calledOnce.should.be.true();
urlForSpy.calledTwice.should.be.true();
settingsCacheStub.calledWith('slack').should.be.true();
requestUrl = makeRequestStub.firstCall.args[0];
requestData = JSON.parse(makeRequestStub.firstCall.args[1].body);
requestUrl.should.equal(slackObjWithUrl[0].url);
requestData.text.should.eql('http://myblog.com/');
requestData.icon_url.should.equal('http://myblog.com/favicon.ico');
requestData.username.should.equal('Ghost');
requestData.unfurl_links.should.equal(true);
});
it('makes a request for a message if url is provided', function () {
var requestUrl, requestData;
isPostStub.returns(false);
settingsCacheStub.withArgs('slack').returns(slackObjWithUrl);
🔥✨ remove forceAdminSSL and urlSSL, add admin url (#7937) * 🔥 kill apiUrl helper, use urlFor helper instead More consistency of creating urls. Creates an easier ability to add config changes. Attention: urlFor function is getting a little nesty, BUT that is for now wanted to make easier and centralised changes to the configs. The url util need's refactoring anyway. * 🔥 urlSSL Remove all urlSSL usages. Add TODO's for the next commit to re-add logic for deleted logic. e.g. - cors helper generated an array of url's to allow requests from the defined config url's -> will be replaced by the admin url if available - theme handler prefered the urlSSL in case it was defined -> will be replaced by using the urlFor helper to get the blog url (based on the request secure flag) The changes in this commit doesn't have to be right, but it helped going step by step. The next commit is the more interesting one. * 🔥 ✨ remove forceAdminSSL, add new admin url and adapt logic I wanted to remove the forceAdminSSL as separate commit, but was hard to realise. That's why both changes are in one commit: 1. remove forceAdminSSL 2. add admin.url option - fix TODO's from last commits - rewrite the ssl middleware! - create some private helper functions in the url helper to realise the changes - rename some wordings and functions e.g. base === blog (we have so much different wordings) - i would like to do more, but this would end in a non readable PR - this commit contains the most important changes to offer admin.url option * 🤖 adapt tests IMPORTANT - all changes in the routing tests were needed, because each routing test did not start the ghost server - they just required the ghost application, which resulted in a random server port - having a random server port results in a redirect, caused by the ssl/redirect middleware * 😎 rename check-ssl middleware * 🎨 fix theme-handler because of master rebase
2017-02-03 21:13:22 +03:00
configUtils.set('url', 'https://myblog.com');
// execute code
ping({message: 'Hi!'});
// assertions
makeRequestStub.calledOnce.should.be.true();
isPostStub.calledOnce.should.be.true();
urlForSpy.calledOnce.should.be.true();
settingsCacheStub.calledWith('slack').should.be.true();
requestUrl = makeRequestStub.firstCall.args[0];
requestData = JSON.parse(makeRequestStub.firstCall.args[1].body);
requestUrl.should.equal(slackObjWithUrl[0].url);
requestData.text.should.eql('Hi!');
requestData.icon_url.should.equal('https://myblog.com/favicon.ico');
requestData.username.should.equal('Ghost');
requestData.unfurl_links.should.equal(true);
});
it('makes a request and errors', function (done) {
makeRequestStub.rejects();
settingsCacheStub.withArgs('slack').returns(slackObjWithUrl);
// execute code
ping({});
(function retry() {
if (common.logging.error.calledOnce) {
makeRequestStub.calledOnce.should.be.true();
return done();
}
setTimeout(retry, 50);
}());
});
it('does not make a request if post is a page', function () {
// set up
isPostStub.returns(true);
settingsCacheStub.withArgs('slack').returns(slackObjWithUrl);
// execute code
ping({page: true});
// assertions
makeRequestStub.calledOnce.should.be.false();
isPostStub.calledOnce.should.be.true();
urlForSpy.calledOnce.should.be.true();
settingsCacheStub.calledWith('slack').should.be.true();
});
it('does not make a request if no url is provided', function () {
// set up
isPostStub.returns(true);
settingsCacheStub.withArgs('slack').returns(slackObjNoUrl);
// execute code
ping({});
// assertions
makeRequestStub.calledOnce.should.be.false();
isPostStub.calledOnce.should.be.true();
urlForSpy.calledOnce.should.be.true();
settingsCacheStub.calledWith('slack').should.be.true();
});
it('does not send webhook for \'welcome\' post', function () {
// set up
isPostStub.returns(true);
settingsCacheStub.withArgs('slack').returns(slackObjWithUrl);
// execute code
ping({slug: 'welcome'});
// assertions
makeRequestStub.calledOnce.should.be.false();
isPostStub.calledOnce.should.be.true();
urlForSpy.calledOnce.should.be.true();
settingsCacheStub.calledWith('slack').should.be.true();
});
it('handles broken slack settings', function () {
// set up
settingsCacheStub.withArgs('slack').returns();
// execute code
ping({});
// assertions
makeRequestStub.calledOnce.should.be.false();
isPostStub.calledOnce.should.be.true();
urlForSpy.calledOnce.should.be.false();
settingsCacheStub.calledWith('slack').should.be.true();
});
});
});