Skip to content
This repository has been archived by the owner on Apr 3, 2024. It is now read-only.

Commit

Permalink
Added keyFilename/credentials to config object
Browse files Browse the repository at this point in the history
  • Loading branch information
kjin committed Nov 1, 2016
1 parent a131faf commit de72403
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 2 deletions.
5 changes: 4 additions & 1 deletion lib/debugletapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/gcloud-credentials.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"client_id": "x",
"client_secret": "y",
"refresh_token": "z",
"type": "authorized_user"
}
122 changes: 122 additions & 0 deletions test/standalone/test-config-credentials.js
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit de72403

Please sign in to comment.