Skip to content
This repository has been archived by the owner on Apr 17, 2020. It is now read-only.

Commit

Permalink
fix deploy not failing with critical option (#20)
Browse files Browse the repository at this point in the history
* add 'critical' service  option

* with the critical option true it does not fail with bad request code eg 401 or 500

* add spec
  • Loading branch information
eredi93 authored and LevelbossMike committed Sep 28, 2016
1 parent 3b29bea commit 29eaac2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
4 changes: 4 additions & 0 deletions lib/notify.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ module.exports = CoreObject.extend({
body = JSON.stringify(body);
}

if (critical && !(300 > response.statusCode && response.statusCode >= 200)) {
return Promise.reject(response.statusCode);
}

plugin.log(serviceKey + ' => ' + body);
}.bind(this))
.catch(function(error) {
Expand Down
49 changes: 39 additions & 10 deletions tests/unit/lib/notify-nodetest.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var nock = require('nock');


describe('Notify', function() {
var Notify, subject, scope, mockUi, plugin, url, serviceKey;
var Notify, subject, mock_request, mock_request_bad_url, mockUi, plugin, url, serviceKey;

before(function() {
Notify = require('../../../lib/notify');
Expand Down Expand Up @@ -36,7 +36,7 @@ describe('Notify', function() {
}
};

scope = nock('http://notify.bugsnag.com')
mock_request = nock('http://notify.bugsnag.com')
.post('/deploy', {
apiKey: '1234'
})
Expand All @@ -45,13 +45,21 @@ describe('Notify', function() {
apiKey: '4321'
})
.reply(200, { status: 'OK' })
.post('/deploy', {})
.replyWithError(401, 'Bad Request');
.post('/deploy', {
apiKey: '4321',
bar: 'foo'
})
.reply(500, { status: 'Internal Server Error' });

mock_request_bad_url = nock('http://notify.bugsnag.comm')
.post('/deploy', {})
.replyWithError('Timeout');
});

describe('#send', function() {
beforeEach(function() {
url = 'http://notify.bugsnag.com/deploy';
bad_url = 'http://notify.bugsnag.comm/deploy';
subject = new Notify({
plugin: plugin
});
Expand Down Expand Up @@ -188,10 +196,28 @@ describe('Notify', function() {
});
});

it('logs when a request was successful and critical is true', function() {
var opts = {
url: url,
body: { apiKey: '4321' },
critical: true
}

var promise = subject.send(serviceKey, opts);

return assert.isFulfilled(promise)
.then(function() {
var messages = mockUi.messages;

assert.isAbove(messages.length, 0);
assert.equal(messages[0], '- '+serviceKey+' => {"status":"OK"}');
});
});

describe('when request fails', function() {
it('resolves when the request fails', function() {
var promise = subject.send(serviceKey, {
url: url,
url: bad_url,
body: {}
});

Expand All @@ -200,7 +226,7 @@ describe('Notify', function() {

it('logs to the console', function() {
var promise = subject.send(serviceKey, {
url: url,
url: bad_url,
body: {}
});

Expand All @@ -209,25 +235,28 @@ describe('Notify', function() {
var messages = mockUi.messages;

assert.isAbove(messages.length, 0);
assert.equal(messages[0], '- '+serviceKey+' => Error: 401');
assert.equal(messages[0], '- '+serviceKey+' => Error: Timeout');
});
});
});

describe('when request fails and critical is true', function() {
it('reject when the request fails', function() {
var promise = subject.send(serviceKey, {
url: url,
url: bad_url,
body: {},
critical: true
});
return assert.isRejected(promise);
});

it('reject and logs to the console', function() {
it('reject when the status code is not 2xx', function() {
var promise = subject.send(serviceKey, {
url: url,
body: {},
body: {
apiKey: '4321',
bar: 'foo'
},
critical: true
});

Expand Down

0 comments on commit 29eaac2

Please sign in to comment.