Skip to content

Commit

Permalink
Updated webhook update method to not send id in request (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
aydrian authored Aug 26, 2016
1 parent ff39f79 commit 57684c8
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 40 deletions.
73 changes: 41 additions & 32 deletions lib/webhooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var api = 'webhooks'

module.exports = function(client) {
var webhooks = {
all: function(options, callback) {
all: function (options, callback) {
var reqOpts = {
uri: api
};
Expand All @@ -22,10 +22,10 @@ module.exports = function(client) {

client.get(reqOpts, callback);
},
describe: function(options, callback) {
describe: function (options, callback) {
options = options || {};

if(!options.id) {
if (!options.id) {
callback(new Error('id is required'));
return;
}
Expand All @@ -40,13 +40,13 @@ module.exports = function(client) {

client.get(reqOpts, callback);
},
create: function(webhook, callback) {
if(typeof webhook === 'function') {
create: function (webhook, callback) {
if (typeof webhook === 'function') {
callback = webhook;
webhook = null;
}

if(!webhook) {
if (!webhook) {
callback(new Error('webhook object is required'));
return;
}
Expand All @@ -57,25 +57,52 @@ module.exports = function(client) {

client.post(options, callback);
},
update: function(webhook, callback) {
if(typeof webhook === 'function') {
update: function (webhook, callback) {
var object, options, id;

if (typeof webhook === 'function') {
callback = webhook;
webhook = null;
}

if(!webhook) {
if (!webhook) {
callback(new Error('webhook object is required'));
return;
}

var object = toApiFormat(webhook)
, options = {
uri: api + '/' + webhook.id
, json: object
};
if (!webhook.id) {
callback(new Error('webhook.id is required'));
return;
}

id = webhook.id;
delete webhook.id;

object = toApiFormat(webhook);
options = {
uri: api + '/' + id
, json: object
};

client.put(options, callback);
},
delete: function (id, callback) {
if (typeof id === 'function') {
callback = id;
id = null;
}

if (!id) {
callback(new Error('id is required'));
return;
}

var options = {
uri: api + '/' + id
};

client.delete(options, callback);
},
validate: function(options, callback) {
options = options || {};

Expand Down Expand Up @@ -142,23 +169,5 @@ module.exports = function(client) {
}
};

webhooks['delete'] = function(id, callback) {
if (typeof id === 'function') {
callback = id;
id = null;
}

if (!id) {
callback(new Error('id is required'));
return;
}

var options = {
uri: api + '/' + id
};

client['delete'](options, callback);
};

return webhooks;
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"url": "https://github.com/SparkPost/node-sparkpost"
},
"author": "Message Systems, Inc.",
"license": "Apache 2.0",
"license": "Apache-2.0",
"bugs": {
"url": "https://github.com/SparkPost/node-sparkpost/issues"
},
Expand Down
22 changes: 15 additions & 7 deletions test/spec/webhooks.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('Webhooks Library', function() {
get: sinon.stub().yields(),
post: sinon.stub().yields(),
put: sinon.stub().yields(),
'delete': sinon.stub().yields()
delete: sinon.stub().yields()
};

webhooks = require('../../lib/webhooks')(client);
Expand Down Expand Up @@ -124,28 +124,36 @@ describe('Webhooks Library', function() {
done();
});
});

it('should throw an error if webhook.id is missing', function(done) {
webhooks.update({}, function (err) {
expect(err.message).to.equal('webhook.id is required');
expect(client.post).not.to.have.been.called;
done();
});
});
});

describe('delete Method', function() {
it('should call client delete method with the appropriate uri', function(done) {
webhooks['delete']('test', function(err, data) {
expect(client['delete'].firstCall.args[0].uri).to.equal('webhooks/test');
webhooks.delete('test', function(err, data) {
expect(client.delete.firstCall.args[0].uri).to.equal('webhooks/test');
done();
});
});

it('should throw an error if id is null', function(done) {
webhooks['delete'](null, function(err) {
webhooks.delete(null, function(err) {
expect(err.message).to.equal('id is required');
expect(client['delete']).not.to.have.been.called;
expect(client.delete).not.to.have.been.called;
done();
});
});

it('should throw an error if id is missing', function(done) {
webhooks['delete'](function(err) {
webhooks.delete(function(err) {
expect(err.message).to.equal('id is required');
expect(client['delete']).not.to.have.been.called;
expect(client.delete).not.to.have.been.called;
done();
});
});
Expand Down

0 comments on commit 57684c8

Please sign in to comment.