mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-03 23:00:14 -05:00
Used request lib in slack service (#9335)
refs #9178 - tested with slack
This commit is contained in:
parent
50b65bca0c
commit
18e15934fd
2 changed files with 50 additions and 105 deletions
|
@ -1,6 +1,5 @@
|
||||||
var https = require('https'),
|
var common = require('../lib/common'),
|
||||||
url = require('url'),
|
request = require('../lib/request'),
|
||||||
common = require('../lib/common'),
|
|
||||||
urlService = require('../services/url'),
|
urlService = require('../services/url'),
|
||||||
blogIconUtils = require('../utils/blog-icon'),
|
blogIconUtils = require('../utils/blog-icon'),
|
||||||
settingsCache = require('./settings/cache'),
|
settingsCache = require('./settings/cache'),
|
||||||
|
@ -22,26 +21,8 @@ function getSlackSettings() {
|
||||||
return setting ? setting[0] : {};
|
return setting ? setting[0] : {};
|
||||||
}
|
}
|
||||||
|
|
||||||
function makeRequest(reqOptions, reqPayload) {
|
|
||||||
var req = https.request(reqOptions);
|
|
||||||
|
|
||||||
reqPayload = JSON.stringify(reqPayload);
|
|
||||||
|
|
||||||
req.write(reqPayload);
|
|
||||||
req.on('error', function (err) {
|
|
||||||
common.logging.error(new common.errors.GhostError({
|
|
||||||
err: err,
|
|
||||||
context: common.i18n.t('errors.services.ping.requestFailed.error', {service: 'slack'}),
|
|
||||||
help: common.i18n.t('errors.services.ping.requestFailed.help', {url: 'http://docs.ghost.org'})
|
|
||||||
}));
|
|
||||||
});
|
|
||||||
|
|
||||||
req.end();
|
|
||||||
}
|
|
||||||
|
|
||||||
function ping(post) {
|
function ping(post) {
|
||||||
var message,
|
var message,
|
||||||
reqOptions,
|
|
||||||
slackData = {},
|
slackData = {},
|
||||||
slackSettings = getSlackSettings();
|
slackSettings = getSlackSettings();
|
||||||
|
|
||||||
|
@ -73,13 +54,18 @@ function ping(post) {
|
||||||
username: 'Ghost'
|
username: 'Ghost'
|
||||||
};
|
};
|
||||||
|
|
||||||
// fill the options for https request
|
return request(slackSettings.url, {
|
||||||
reqOptions = url.parse(slackSettings.url);
|
body: JSON.stringify(slackData),
|
||||||
reqOptions.method = 'POST';
|
headers: {
|
||||||
reqOptions.headers = {'Content-type': 'application/json'};
|
'Content-type': 'application/json'
|
||||||
|
}
|
||||||
// with all the data we have, we're doing the request now
|
}).catch(function (err) {
|
||||||
makeRequest(reqOptions, slackData);
|
common.logging.error(new common.errors.GhostError({
|
||||||
|
err: err,
|
||||||
|
context: common.i18n.t('errors.services.ping.requestFailed.error', {service: 'slack'}),
|
||||||
|
help: common.i18n.t('errors.services.ping.requestFailed.help', {url: 'http://docs.ghost.org'})
|
||||||
|
}));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -92,68 +92,6 @@ describe('Slack', function () {
|
||||||
resetSlack();
|
resetSlack();
|
||||||
});
|
});
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
// 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'
|
|
||||||
})
|
|
||||||
.replyWithError(404);
|
|
||||||
|
|
||||||
// execute code
|
|
||||||
makeRequest(reqOptions, {
|
|
||||||
text: 'http://myblog.com/mypost',
|
|
||||||
icon_url: 'http://myblog.com/someImageurl.jpg',
|
|
||||||
username: 'Ghost'
|
|
||||||
});
|
|
||||||
|
|
||||||
// assertions
|
|
||||||
pingSlack.isDone().should.be.true();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('ping()', function () {
|
describe('ping()', function () {
|
||||||
var isPostStub,
|
var isPostStub,
|
||||||
urlForSpy,
|
urlForSpy,
|
||||||
|
@ -168,9 +106,11 @@ describe('Slack', function () {
|
||||||
urlForSpy = sandbox.spy(urlService.utils, 'urlFor');
|
urlForSpy = sandbox.spy(urlService.utils, 'urlFor');
|
||||||
|
|
||||||
settingsCacheStub = sandbox.stub(settingsCache, 'get');
|
settingsCacheStub = sandbox.stub(settingsCache, 'get');
|
||||||
|
sandbox.spy(common.logging, 'error');
|
||||||
|
|
||||||
makeRequestStub = sandbox.stub();
|
makeRequestStub = sandbox.stub();
|
||||||
slackReset = slack.__set__('makeRequest', makeRequestStub);
|
slackReset = slack.__set__('request', makeRequestStub);
|
||||||
|
makeRequestStub.resolves();
|
||||||
|
|
||||||
configUtils.set('url', 'http://myblog.com');
|
configUtils.set('url', 'http://myblog.com');
|
||||||
});
|
});
|
||||||
|
@ -180,7 +120,7 @@ describe('Slack', function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('makes a request for a post if url is provided', function () {
|
it('makes a request for a post if url is provided', function () {
|
||||||
var requestOptions, requestData;
|
var requestUrl, requestData;
|
||||||
|
|
||||||
isPostStub.returns(true);
|
isPostStub.returns(true);
|
||||||
settingsCacheStub.withArgs('slack').returns(slackObjWithUrl);
|
settingsCacheStub.withArgs('slack').returns(slackObjWithUrl);
|
||||||
|
@ -194,17 +134,18 @@ describe('Slack', function () {
|
||||||
urlForSpy.calledTwice.should.be.true();
|
urlForSpy.calledTwice.should.be.true();
|
||||||
settingsCacheStub.calledWith('slack').should.be.true();
|
settingsCacheStub.calledWith('slack').should.be.true();
|
||||||
|
|
||||||
requestOptions = makeRequestStub.firstCall.args[0];
|
requestUrl = makeRequestStub.firstCall.args[0];
|
||||||
requestData = makeRequestStub.firstCall.args[1];
|
requestData = JSON.parse(makeRequestStub.firstCall.args[1].body);
|
||||||
|
|
||||||
requestOptions.should.have.property('href').and.be.equal('https://hooks.slack.com/services/a-b-c-d');
|
requestUrl.should.equal(slackObjWithUrl[0].url);
|
||||||
requestData.should.have.property('text').and.be.equal('http://myblog.com/');
|
requestData.text.should.eql('http://myblog.com/');
|
||||||
requestData.should.have.property('icon_url').and.be.equal('http://myblog.com/favicon.ico');
|
requestData.icon_url.should.equal('http://myblog.com/favicon.ico');
|
||||||
requestData.should.have.property('username').and.be.equal('Ghost');
|
requestData.username.should.equal('Ghost');
|
||||||
|
requestData.unfurl_links.should.equal(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('makes a request for a message if url is provided', function () {
|
it('makes a request for a message if url is provided', function () {
|
||||||
var requestOptions, requestData;
|
var requestUrl, requestData;
|
||||||
|
|
||||||
isPostStub.returns(false);
|
isPostStub.returns(false);
|
||||||
settingsCacheStub.withArgs('slack').returns(slackObjWithUrl);
|
settingsCacheStub.withArgs('slack').returns(slackObjWithUrl);
|
||||||
|
@ -220,13 +161,31 @@ describe('Slack', function () {
|
||||||
urlForSpy.calledOnce.should.be.true();
|
urlForSpy.calledOnce.should.be.true();
|
||||||
settingsCacheStub.calledWith('slack').should.be.true();
|
settingsCacheStub.calledWith('slack').should.be.true();
|
||||||
|
|
||||||
requestOptions = makeRequestStub.firstCall.args[0];
|
requestUrl = makeRequestStub.firstCall.args[0];
|
||||||
requestData = makeRequestStub.firstCall.args[1];
|
requestData = JSON.parse(makeRequestStub.firstCall.args[1].body);
|
||||||
|
|
||||||
requestOptions.should.have.property('href').and.be.equal('https://hooks.slack.com/services/a-b-c-d');
|
requestUrl.should.equal(slackObjWithUrl[0].url);
|
||||||
requestData.should.have.property('text').and.be.equal('Hi!');
|
requestData.text.should.eql('Hi!');
|
||||||
requestData.should.have.property('icon_url').and.be.equal('https://myblog.com/favicon.ico');
|
requestData.icon_url.should.equal('https://myblog.com/favicon.ico');
|
||||||
requestData.should.have.property('username').and.be.equal('Ghost');
|
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 () {
|
it('does not make a request if post is a page', function () {
|
||||||
|
|
Loading…
Add table
Reference in a new issue