Skip to content

Commit 2fae41a

Browse files
connection: add error handling to credentials.
1 parent a553164 commit 2fae41a

2 files changed

Lines changed: 33 additions & 4 deletions

File tree

lib/common/connection.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ var METADATA_TOKEN_URL =
3636
/** @const {object} gcloud-node's package.json file. */
3737
var PKG = require('../../package.json');
3838

39+
/** @const {array} Required properties for a complete `credentials` object. */
40+
var REQUIRED_CREDENTIALS = [
41+
'client_email',
42+
'private_key'
43+
];
44+
3945
/** @const {string} User agent. */
4046
var USER_AGENT = 'gcloud-node/' + PKG.version;
4147

@@ -95,6 +101,18 @@ function Connection(opts) {
95101
this.isConnecting = false;
96102
this.waitQueue = [];
97103

104+
if (opts.credentials) {
105+
var hasRequiredProperties = REQUIRED_CREDENTIALS.every(function(cred) {
106+
return !!opts.credentials[cred];
107+
});
108+
if (hasRequiredProperties) {
109+
this.credentials = opts.credentials;
110+
} else {
111+
throw new Error('A credentials object must contain the following keys: ' +
112+
REQUIRED_CREDENTIALS.join(' '));
113+
}
114+
}
115+
98116
if (opts.credentials && opts.credentials.private_key &&
99117
opts.credentials.client_email) {
100118
this.credentials = opts.credentials;

test/common/connection.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,24 @@ describe('Connection', function() {
4545
});
4646
});
4747

48-
it('should accept and assign credentials object', function() {
49-
var credConnection = new connection.Connection({
50-
credentials: privateKeyFileJson
48+
describe('credentials object', function() {
49+
it('should accept and assign a complete credentials object', function() {
50+
var credConnection = new connection.Connection({
51+
credentials: privateKeyFileJson
52+
});
53+
assert.deepEqual(credConnection.credentials, privateKeyFileJson);
54+
});
55+
56+
it('should reject an incomplete credentials object', function() {
57+
assert.throws(function() {
58+
new connection.Connection({
59+
credentials: {}
60+
});
61+
}, /must contain/);
5162
});
52-
assert.deepEqual(credConnection.credentials, privateKeyFileJson);
5363
});
5464

65+
5566
describe('Token', function() {
5667
var tokenNeverExpires = new connection.Token('token', new Date(3000, 0, 0));
5768
var tokenExpired = new connection.Token('token', new Date(2011, 0, 0));

0 commit comments

Comments
 (0)