From de72403a31632af640bb2972029af85290104605 Mon Sep 17 00:00:00 2001 From: Kelvin Jin Date: Tue, 1 Nov 2016 13:13:12 -0700 Subject: [PATCH] Added keyFilename/credentials to config object --- lib/debugletapi.js | 5 +- package.json | 2 +- test/fixtures/gcloud-credentials.json | 6 + test/standalone/test-config-credentials.js | 122 +++++++++++++++++++++ 4 files changed, 133 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/gcloud-credentials.json create mode 100644 test/standalone/test-config-credentials.js diff --git a/lib/debugletapi.js b/lib/debugletapi.js index 6736b100a..0877f10ff 100644 --- a/lib/debugletapi.js +++ b/lib/debugletapi.js @@ -45,7 +45,10 @@ function DebugletApi(config) { var config_ = config || {}; /** @private {Object} request style request object */ - this.request_ = utils.authorizedRequestFactory(SCOPES); + this.request_ = utils.authorizedRequestFactory(SCOPES, { + keyFile: config_.keyFilename, + credentials: config_.credentials + }); /** @private {string} numeric project id */ this.project_ = null; diff --git a/package.json b/package.json index b0f0cc816..0481431f8 100644 --- a/package.json +++ b/package.json @@ -59,7 +59,7 @@ "proxyquire": "^1.4.0" }, "dependencies": { - "@google/cloud-diagnostics-common": "0.2.5", + "@google/cloud-diagnostics-common": "0.3.0", "acorn": "^3.3.0", "async": "^2.1.2", "coffee-script": "^1.9.3", diff --git a/test/fixtures/gcloud-credentials.json b/test/fixtures/gcloud-credentials.json new file mode 100644 index 000000000..3499fcc9c --- /dev/null +++ b/test/fixtures/gcloud-credentials.json @@ -0,0 +1,6 @@ +{ + "client_id": "x", + "client_secret": "y", + "refresh_token": "z", + "type": "authorized_user" +} diff --git a/test/standalone/test-config-credentials.js b/test/standalone/test-config-credentials.js new file mode 100644 index 000000000..2e66430f4 --- /dev/null +++ b/test/standalone/test-config-credentials.js @@ -0,0 +1,122 @@ +/** + * Copyright 2015 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +'use strict'; + +var path = require('path'); +var assert = require('assert'); +var nock = require('nock'); + +process.env.GCLOUD_PROJECT = 0; + +describe('test-config-credentials', function() { + it('should use the keyFilename field of the config object', function(done) { + var credentials = require('../fixtures/gcloud-credentials.json'); + var config = { + keyFilename: path.join('test', 'fixtures', 'gcloud-credentials.json') + }; + var agent = require('../..'); + nock.disableNetConnect(); + var scope = nock('https://accounts.google.com') + .post('/o/oauth2/token', function(body) { + assert.equal(body.client_id, credentials.client_id); + assert.equal(body.client_secret, credentials.client_secret); + assert.equal(body.refresh_token, credentials.refresh_token); + return true; + }).reply(200, { + refresh_token: 'hello', + access_token: 'goodbye', + expiry_date: new Date(9999, 1, 1) + }); + // Since we have to get an auth token, this always gets intercepted second + nock('https://clouddebugger.googleapis.com') + .post('/v2/controller/debuggees/register', function() { + scope.done(); + agent.start.wasSuccessful_ = false; + setImmediate(done); + return true; + }).reply(200); + agent.start(config); + }); + + it('should use the credentials field of the config object', function(done) { + var config = { + credentials: require('../fixtures/gcloud-credentials.json') + }; + var agent = require('../..'); + nock.disableNetConnect(); + var scope = nock('https://accounts.google.com') + .post('/o/oauth2/token', function(body) { + assert.equal(body.client_id, config.credentials.client_id); + assert.equal(body.client_secret, config.credentials.client_secret); + assert.equal(body.refresh_token, config.credentials.refresh_token); + return true; + }).reply(200, { + refresh_token: 'hello', + access_token: 'goodbye', + expiry_date: new Date(9999, 1, 1) + }); + // Since we have to get an auth token, this always gets intercepted second + nock('https://clouddebugger.googleapis.com') + .post('/v2/controller/debuggees/register', function() { + scope.done(); + agent.start.wasSuccessful_ = false; + setImmediate(done); + return true; + }).reply(200); + agent.start(config); + }); + + it('should ignore credentials if keyFilename is provided', function(done) { + var correctCredentials = require('../fixtures/gcloud-credentials.json'); + var config = { + keyFilename: path.join('test', 'fixtures', 'gcloud-credentials.json'), + credentials: { + client_id: 'a', + client_secret: 'b', + refresh_token: 'c', + type: 'authorized_user' + } + }; + ['client_id', 'client_secret', 'refresh_token'].forEach(function (field) { + assert(correctCredentials.hasOwnProperty(field)); + assert(config.credentials.hasOwnProperty(field)); + assert.notEqual(config.credentials[field], + correctCredentials[field]); + }); + var agent = require('../..'); + nock.disableNetConnect(); + var scope = nock('https://accounts.google.com') + .post('/o/oauth2/token', function(body) { + assert.equal(body.client_id, correctCredentials.client_id); + assert.equal(body.client_secret, correctCredentials.client_secret); + assert.equal(body.refresh_token, correctCredentials.refresh_token); + return true; + }).reply(200, { + refresh_token: 'hello', + access_token: 'goodbye', + expiry_date: new Date(9999, 1, 1) + }); + // Since we have to get an auth token, this always gets intercepted second + nock('https://clouddebugger.googleapis.com') + .post('/v2/controller/debuggees/register', function() { + scope.done(); + agent.start.wasSuccessful_ = false; + setImmediate(done); + return true; + }).reply(200); + agent.start(config); + }); +});