2016-06-05 14:22:11 +03:00
|
|
|
var _ = require('lodash'),
|
|
|
|
nock = require('nock'),
|
2016-03-29 11:40:44 +03:00
|
|
|
should = require('should'),
|
|
|
|
sinon = require('sinon'),
|
2016-06-05 14:22:11 +03:00
|
|
|
rewire = require('rewire'),
|
2016-03-29 11:40:44 +03:00
|
|
|
Promise = require('bluebird'),
|
|
|
|
testUtils = require('../utils'),
|
|
|
|
url = require('url'),
|
|
|
|
|
2016-09-09 13:23:47 +03:00
|
|
|
// Stuff we test
|
2017-02-02 21:51:35 +03:00
|
|
|
configUtils = require('../utils/configUtils'),
|
2016-06-05 14:22:11 +03:00
|
|
|
slack = rewire('../../server/data/slack'),
|
2016-03-29 11:40:44 +03:00
|
|
|
events = require('../../server/events'),
|
|
|
|
api = require('../../server/api/settings'),
|
2016-09-09 13:23:47 +03:00
|
|
|
utils = require('../../server/utils'),
|
2016-03-29 11:40:44 +03:00
|
|
|
schema = require('../../server/data/schema').checks,
|
2016-06-05 14:22:11 +03:00
|
|
|
|
2016-03-29 11:40:44 +03:00
|
|
|
sandbox = sinon.sandbox.create(),
|
2016-09-09 13:23:47 +03:00
|
|
|
// Test data
|
2016-03-29 11:40:44 +03:00
|
|
|
slackObjNoUrl =
|
|
|
|
{
|
|
|
|
id: 17,
|
|
|
|
uuid: '50f50671-6e7c-4636-85e0-2962967764fa',
|
|
|
|
key: 'slack',
|
|
|
|
value: '[{"url":""}]',
|
|
|
|
type: 'blog',
|
|
|
|
created_at: '2016-04-06T15:19:19.492Z',
|
|
|
|
created_by: 1,
|
|
|
|
updated_at: '2016-04-06T15:19:19.492Z',
|
|
|
|
updated_by: 1
|
|
|
|
},
|
|
|
|
slackObjWithUrl =
|
|
|
|
{
|
|
|
|
id: 17,
|
|
|
|
uuid: '50f50671-6e7c-4636-85e0-2962967764fa',
|
|
|
|
key: 'slack',
|
|
|
|
value: '[{"url":"https://hooks.slack.com/services/a-b-c-d"}]',
|
|
|
|
type: 'blog',
|
|
|
|
created_at: '2016-04-06T15:19:19.492Z',
|
|
|
|
created_by: 1,
|
|
|
|
updated_at: '2016-04-06T15:19:19.492Z',
|
|
|
|
updated_by: 1
|
|
|
|
};
|
|
|
|
|
|
|
|
// To stop jshint complaining
|
|
|
|
should.equal(true, true);
|
|
|
|
|
|
|
|
describe('Slack', function () {
|
2016-06-05 14:22:11 +03:00
|
|
|
var eventStub;
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
eventStub = sandbox.stub(events, 'on');
|
|
|
|
});
|
2016-03-29 11:40:44 +03:00
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
sandbox.restore();
|
2017-02-02 21:51:35 +03:00
|
|
|
configUtils.restore();
|
2016-03-29 11:40:44 +03:00
|
|
|
});
|
|
|
|
|
2016-06-05 14:22:11 +03:00
|
|
|
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();
|
2016-03-29 11:40:44 +03:00
|
|
|
|
2016-06-05 14:22:11 +03:00
|
|
|
// Reset slack ping method
|
|
|
|
resetSlack();
|
2016-03-29 11:40:44 +03:00
|
|
|
});
|
|
|
|
|
2016-06-05 14:22:11 +03:00
|
|
|
it('testPing() calls ping() with default message', function () {
|
|
|
|
var pingStub = sandbox.stub(),
|
|
|
|
resetSlack = slack.__set__('ping', pingStub),
|
|
|
|
testPing = slack.__get__('testPing');
|
2016-03-29 11:40:44 +03:00
|
|
|
|
2016-06-05 14:22:11 +03:00
|
|
|
testPing();
|
2016-03-29 11:40:44 +03:00
|
|
|
|
2016-06-05 14:22:11 +03:00
|
|
|
pingStub.calledOnce.should.be.true();
|
|
|
|
pingStub.calledWith(sinon.match.has('message')).should.be.true();
|
2016-03-29 11:40:44 +03:00
|
|
|
|
2016-06-05 14:22:11 +03:00
|
|
|
// Reset slack ping method
|
|
|
|
resetSlack();
|
2016-03-29 11:40:44 +03:00
|
|
|
});
|
|
|
|
|
2016-06-05 14:22:11 +03:00
|
|
|
describe('makeRequest()', function () {
|
|
|
|
var makeRequest = slack.__get__('makeRequest');
|
|
|
|
|
|
|
|
it('should make request to slack correctly', function () {
|
|
|
|
// set up
|
|
|
|
var reqOptions,
|
|
|
|
pingSlack;
|
|
|
|
|
|
|
|
// fill the options for https request
|
|
|
|
reqOptions = url.parse('https://hooks.slack.com/services/a-b-c-d');
|
|
|
|
reqOptions.method = 'POST';
|
|
|
|
reqOptions.headers = {'Content-type': 'application/json'};
|
|
|
|
|
|
|
|
pingSlack = nock('https://hooks.slack.com/')
|
|
|
|
.post('/services/a-b-c-d', {text:'http://myblog.com/mypost', icon_url: 'http://myblog.com/someImageurl.jpg', username: 'Ghost'})
|
|
|
|
.reply(200);
|
|
|
|
|
|
|
|
// execute code
|
|
|
|
makeRequest(reqOptions, {text:'http://myblog.com/mypost', icon_url: 'http://myblog.com/someImageurl.jpg', username: 'Ghost'});
|
|
|
|
|
|
|
|
// assertions
|
|
|
|
pingSlack.isDone().should.be.true();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can handle an error response correctly', function () {
|
|
|
|
// set up
|
|
|
|
var reqOptions,
|
|
|
|
pingSlack;
|
2016-03-29 11:40:44 +03:00
|
|
|
|
2016-06-05 14:22:11 +03:00
|
|
|
// fill the options for https request
|
|
|
|
reqOptions = url.parse('https://hooks.slack.com/services/a-b-c-d');
|
|
|
|
reqOptions.method = 'POST';
|
|
|
|
reqOptions.headers = {'Content-type': 'application/json'};
|
2016-03-29 11:40:44 +03:00
|
|
|
|
2016-06-05 14:22:11 +03:00
|
|
|
pingSlack = nock('https://hooks.slack.com/')
|
|
|
|
.post('/services/a-b-c-d', {text:'http://myblog.com/mypost', icon_url: 'http://myblog.com/someImageurl.jpg', username: 'Ghost'})
|
|
|
|
.replyWithError(404);
|
2016-03-29 11:40:44 +03:00
|
|
|
|
2016-06-05 14:22:11 +03:00
|
|
|
// execute code
|
|
|
|
makeRequest(reqOptions, {text:'http://myblog.com/mypost', icon_url: 'http://myblog.com/someImageurl.jpg', username: 'Ghost'});
|
|
|
|
|
|
|
|
// assertions
|
|
|
|
pingSlack.isDone().should.be.true();
|
|
|
|
});
|
2016-03-29 11:40:44 +03:00
|
|
|
});
|
|
|
|
|
2016-06-05 14:22:11 +03:00
|
|
|
describe('ping()', function () {
|
2016-03-29 11:40:44 +03:00
|
|
|
var makeRequestAssertions,
|
2016-06-05 14:22:11 +03:00
|
|
|
isPostStub,
|
2017-02-02 21:51:35 +03:00
|
|
|
urlForSpy,
|
2016-03-29 11:40:44 +03:00
|
|
|
settingsAPIStub,
|
|
|
|
settingsObj,
|
2016-06-05 14:22:11 +03:00
|
|
|
slackReset,
|
|
|
|
makeRequestMock,
|
|
|
|
makeRequestSpy,
|
|
|
|
ping = slack.__get__('ping');
|
2016-03-29 11:40:44 +03:00
|
|
|
|
|
|
|
beforeEach(function () {
|
2016-06-05 14:22:11 +03:00
|
|
|
isPostStub = sandbox.stub(schema, 'isPost');
|
2017-02-02 21:51:35 +03:00
|
|
|
urlForSpy = sandbox.spy(utils.url, 'urlFor');
|
2016-03-29 11:40:44 +03:00
|
|
|
settingsObj = {settings: [], meta: {}};
|
|
|
|
settingsAPIStub = sandbox.stub(api, 'read').returns(Promise.resolve(settingsObj));
|
2016-06-05 14:22:11 +03:00
|
|
|
|
|
|
|
makeRequestMock = function () {
|
2016-03-29 11:40:44 +03:00
|
|
|
makeRequestAssertions.apply(this, arguments);
|
2016-06-05 14:22:11 +03:00
|
|
|
};
|
2017-02-02 21:51:35 +03:00
|
|
|
|
2016-06-05 14:22:11 +03:00
|
|
|
makeRequestSpy = sandbox.spy(makeRequestMock);
|
|
|
|
slackReset = slack.__set__('makeRequest', makeRequestMock);
|
2017-02-02 21:51:35 +03:00
|
|
|
|
|
|
|
configUtils.set('url', 'http://myblog.com');
|
2016-03-29 11:40:44 +03:00
|
|
|
});
|
|
|
|
|
2016-06-05 14:22:11 +03:00
|
|
|
afterEach(function () {
|
|
|
|
slackReset();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('makes a request for a post if url is provided', function (done) {
|
|
|
|
isPostStub.returns(true);
|
|
|
|
settingsObj.settings[0] = slackObjWithUrl;
|
|
|
|
|
|
|
|
// assertions
|
|
|
|
makeRequestAssertions = function (requestOptions, requestData) {
|
|
|
|
isPostStub.calledOnce.should.be.true();
|
2017-02-02 21:51:35 +03:00
|
|
|
urlForSpy.calledTwice.should.be.true();
|
2016-06-05 14:22:11 +03:00
|
|
|
settingsAPIStub.calledOnce.should.be.true();
|
|
|
|
requestOptions.should.have.property('href').and.be.equal('https://hooks.slack.com/services/a-b-c-d');
|
2017-02-02 21:51:35 +03:00
|
|
|
requestData.should.have.property('text').and.be.equal('http://myblog.com/');
|
|
|
|
requestData.should.have.property('icon_url').and.be.equal('http://myblog.com/ghost/img/ghosticon.jpg');
|
2016-06-05 14:22:11 +03:00
|
|
|
requestData.should.have.property('username').and.be.equal('Ghost');
|
|
|
|
done();
|
|
|
|
};
|
|
|
|
|
|
|
|
// execute code
|
|
|
|
ping({}).catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('makes a request for a message if url is provided', function (done) {
|
|
|
|
isPostStub.returns(false);
|
2016-03-29 11:40:44 +03:00
|
|
|
settingsObj.settings[0] = 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');
|
2017-02-02 21:51:35 +03:00
|
|
|
|
2016-03-29 11:40:44 +03:00
|
|
|
// assertions
|
|
|
|
makeRequestAssertions = function (requestOptions, requestData) {
|
2016-06-05 14:22:11 +03:00
|
|
|
isPostStub.calledOnce.should.be.true();
|
2017-02-02 21:51:35 +03:00
|
|
|
urlForSpy.calledOnce.should.be.true();
|
2016-06-05 14:22:11 +03:00
|
|
|
settingsAPIStub.calledOnce.should.be.true();
|
|
|
|
requestOptions.should.have.property('href').and.be.equal('https://hooks.slack.com/services/a-b-c-d');
|
|
|
|
requestData.should.have.property('text').and.be.equal('Hi!');
|
2017-02-02 21:51:35 +03:00
|
|
|
requestData.should.have.property('icon_url').and.be.equal('https://myblog.com/ghost/img/ghosticon.jpg');
|
2016-06-05 14:22:11 +03:00
|
|
|
requestData.should.have.property('username').and.be.equal('Ghost');
|
|
|
|
done();
|
|
|
|
};
|
|
|
|
|
|
|
|
ping({message: 'Hi!'}).catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not make a request if post is a page', function (done) {
|
|
|
|
// set up
|
|
|
|
isPostStub.returns(true);
|
|
|
|
settingsObj.settings[0] = slackObjWithUrl;
|
2016-03-29 11:40:44 +03:00
|
|
|
|
|
|
|
// execute code
|
2016-06-05 14:22:11 +03:00
|
|
|
ping({page: true}).then(function (result) {
|
|
|
|
// assertions
|
|
|
|
isPostStub.calledOnce.should.be.true();
|
2017-02-02 21:51:35 +03:00
|
|
|
urlForSpy.calledOnce.should.be.true();
|
2016-06-05 14:22:11 +03:00
|
|
|
settingsAPIStub.calledOnce.should.be.true();
|
|
|
|
makeRequestSpy.called.should.be.false();
|
|
|
|
should.not.exist(result);
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
2016-03-29 11:40:44 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('does not make a request if no url is provided', function (done) {
|
|
|
|
// set up
|
2016-06-05 14:22:11 +03:00
|
|
|
isPostStub.returns(true);
|
2016-03-29 11:40:44 +03:00
|
|
|
settingsObj.settings[0] = slackObjNoUrl;
|
|
|
|
|
|
|
|
// execute code
|
2016-06-05 14:22:11 +03:00
|
|
|
ping({}).then(function (result) {
|
2016-03-29 11:40:44 +03:00
|
|
|
// assertions
|
2016-06-05 14:22:11 +03:00
|
|
|
isPostStub.calledOnce.should.be.true();
|
2017-02-02 21:51:35 +03:00
|
|
|
urlForSpy.calledOnce.should.be.true();
|
2016-03-29 11:40:44 +03:00
|
|
|
settingsAPIStub.calledOnce.should.be.true();
|
2016-06-05 14:22:11 +03:00
|
|
|
makeRequestSpy.called.should.be.false();
|
2016-03-29 11:40:44 +03:00
|
|
|
should.not.exist(result);
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not send webhook for \'welcome-to-ghost\' post', function (done) {
|
|
|
|
// set up
|
2016-06-05 14:22:11 +03:00
|
|
|
isPostStub.returns(true);
|
2016-03-29 11:40:44 +03:00
|
|
|
settingsObj.settings[0] = slackObjWithUrl;
|
|
|
|
|
|
|
|
// execute code
|
2016-06-05 14:22:11 +03:00
|
|
|
ping({slug: 'welcome-to-ghost'}).then(function (result) {
|
2016-03-29 11:40:44 +03:00
|
|
|
// assertions
|
2016-06-05 14:22:11 +03:00
|
|
|
isPostStub.calledOnce.should.be.true();
|
2017-02-02 21:51:35 +03:00
|
|
|
urlForSpy.calledOnce.should.be.true();
|
2016-03-29 11:40:44 +03:00
|
|
|
settingsAPIStub.calledOnce.should.be.true();
|
2016-06-05 14:22:11 +03:00
|
|
|
makeRequestSpy.called.should.be.false();
|
2016-03-29 11:40:44 +03:00
|
|
|
should.not.exist(result);
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
2016-06-05 14:22:11 +03:00
|
|
|
|
|
|
|
it('handles broken slack settings', function (done) {
|
|
|
|
settingsObj.settings[0] = '';
|
|
|
|
|
|
|
|
ping({}).then(function () {
|
|
|
|
done('This should not get called');
|
|
|
|
}).catch(function () {
|
|
|
|
isPostStub.calledOnce.should.be.true();
|
2017-02-02 21:51:35 +03:00
|
|
|
urlForSpy.calledOnce.should.be.false();
|
2016-06-05 14:22:11 +03:00
|
|
|
settingsAPIStub.calledOnce.should.be.true();
|
|
|
|
makeRequestSpy.called.should.be.false();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2016-03-29 11:40:44 +03:00
|
|
|
});
|
|
|
|
});
|