From 996d7ce8d7301e4516fbb1da3c438c7a44c76571 Mon Sep 17 00:00:00 2001 From: Chris Charabaruk <chris.charabaruk@outlook.com> Date: Wed, 13 Apr 2016 12:42:02 -0400 Subject: [PATCH] Fix missing `subaccounts` property in SparkPost class (#137) * Fix missing `subaccounts` property in SparkPost class * Only set key_valid_ips if input actually includes it Fixes a state that causes the server to 500 out when creating or updating a subaccount. * Add specs for handling `options.keyValidIps` on subaccount create * Ensure keyValidIps is handled properly for API formatting --- lib/sparkpost.js | 1 + lib/subaccounts.js | 11 ++++++- test/spec/subaccounts.spec.js | 55 +++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 1 deletion(-) diff --git a/lib/sparkpost.js b/lib/sparkpost.js index e873def..3f59e1a 100644 --- a/lib/sparkpost.js +++ b/lib/sparkpost.js @@ -60,6 +60,7 @@ var SparkPost = function(apiKey, options) { this.recipientLists = require('./recipientLists')(this); this.relayWebhooks = require('./relayWebhooks')(this); this.sendingDomains = require('./sendingDomains')(this); + this.subaccounts = require('./subaccounts')(this); this.suppressionList = require('./suppressionList')(this); this.templates = require('./templates')(this); this.transmissions = require('./transmissions')(this); diff --git a/lib/subaccounts.js b/lib/subaccounts.js index 888a2a6..85de232 100644 --- a/lib/subaccounts.js +++ b/lib/subaccounts.js @@ -11,7 +11,16 @@ var toApiFormat = function(input) { model.status = input.status; model.key_grants = Array.isArray(input.keyGrants) ? input.keyGrants : [input.keyGrants]; - model.key_valid_ips = Array.isArray(input.keyValidIps) ? input.keyValidIps : [input.keyValidIps]; + + // server returns 500 if key_valid_ips is empty array + if (input.keyValidIps) { + var keyValidIpsIsArray = Array.isArray(input.keyValidIps); + if (keyValidIpsIsArray && input.keyValidIps.length > 0) { + model.key_valid_ips = input.keyValidIps; + } else if (!keyValidIpsIsArray) { + model.key_valid_ips = [input.keyValidIps]; + } + } return model; }; diff --git a/test/spec/subaccounts.spec.js b/test/spec/subaccounts.spec.js index a69f804..d36d873 100644 --- a/test/spec/subaccounts.spec.js +++ b/test/spec/subaccounts.spec.js @@ -122,6 +122,61 @@ describe('Subaccounts Library', function () { done(); }); }); + + it('should not set key_valid_ips in request if keyValidIps is missing from options', function(done) { + var options = { + name: 'test', + keyLabel: 'test', + keyGrants: [] + }; + + subaccounts.create(options, function(err, data) { + expect(client.post.firstCall.args[0].json.key_valid_ips).to.be.undefined; + done(); + }) + }); + + it('should not set key_valid_ips in request if keyValidIps is empty array', function(done) { + var options = { + name: 'test', + keyLabel: 'test', + keyGrants: [], + keyValidIps: [] + }; + + subaccounts.create(options, function(err, data) { + expect(client.post.firstCall.args[0].json.key_valid_ips).to.be.undefined; + done(); + }) + }); + + it('should set key_valid_ips in request if keyValidIps is in options and is a non-empty array', function(done) { + var options = { + name: 'test', + keyLabel: 'test', + keyGrants: [], + keyValidIps: ['127.0.0.1'] + }; + + subaccounts.create(options, function(err, data) { + expect(client.post.firstCall.args[0].json.key_valid_ips).to.eql(['127.0.0.1']); + done(); + }) + }); + + it('should set key_valid_ips in request if keyValidIps is in options and is not an array', function(done) { + var options = { + name: 'test', + keyLabel: 'test', + keyGrants: [], + keyValidIps: '127.0.0.1' + }; + + subaccounts.create(options, function(err, data) { + expect(client.post.firstCall.args[0].json.key_valid_ips).to.eql(['127.0.0.1']); + done(); + }) + }); }); describe('update Method', function() {