Skip to content

Commit

Permalink
connection: add error handling to credentials.
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus committed Aug 27, 2014
1 parent a553164 commit 2fae41a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
18 changes: 18 additions & 0 deletions lib/common/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ var METADATA_TOKEN_URL =
/** @const {object} gcloud-node's package.json file. */
var PKG = require('../../package.json');

/** @const {array} Required properties for a complete `credentials` object. */
var REQUIRED_CREDENTIALS = [
'client_email',
'private_key'
];

/** @const {string} User agent. */
var USER_AGENT = 'gcloud-node/' + PKG.version;

Expand Down Expand Up @@ -95,6 +101,18 @@ function Connection(opts) {
this.isConnecting = false;
this.waitQueue = [];

if (opts.credentials) {
var hasRequiredProperties = REQUIRED_CREDENTIALS.every(function(cred) {
return !!opts.credentials[cred];
});
if (hasRequiredProperties) {
this.credentials = opts.credentials;
} else {
throw new Error('A credentials object must contain the following keys: ' +
REQUIRED_CREDENTIALS.join(' '));
}
}

if (opts.credentials && opts.credentials.private_key &&
opts.credentials.client_email) {
this.credentials = opts.credentials;
Expand Down
19 changes: 15 additions & 4 deletions test/common/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,24 @@ describe('Connection', function() {
});
});

it('should accept and assign credentials object', function() {
var credConnection = new connection.Connection({
credentials: privateKeyFileJson
describe('credentials object', function() {
it('should accept and assign a complete credentials object', function() {
var credConnection = new connection.Connection({
credentials: privateKeyFileJson
});
assert.deepEqual(credConnection.credentials, privateKeyFileJson);
});

it('should reject an incomplete credentials object', function() {
assert.throws(function() {
new connection.Connection({
credentials: {}
});
}, /must contain/);
});
assert.deepEqual(credConnection.credentials, privateKeyFileJson);
});


describe('Token', function() {
var tokenNeverExpires = new connection.Token('token', new Date(3000, 0, 0));
var tokenExpired = new connection.Token('token', new Date(2011, 0, 0));
Expand Down

0 comments on commit 2fae41a

Please sign in to comment.