Skip to content

Commit

Permalink
Fix missing subaccounts property in SparkPost class (#137)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
coldacid authored and aydrian committed Apr 13, 2016
1 parent c65cbb2 commit 996d7ce
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/sparkpost.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 10 additions & 1 deletion lib/subaccounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down
55 changes: 55 additions & 0 deletions test/spec/subaccounts.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 996d7ce

Please sign in to comment.