|
| 1 | +/** |
| 2 | + * Copyright 2016 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +'use strict'; |
| 17 | + |
| 18 | +var path = require('path'); |
| 19 | +var assert = require('assert'); |
| 20 | +var nock = require('nock'); |
| 21 | + |
| 22 | +process.env.GCLOUD_PROJECT = 0; |
| 23 | + |
| 24 | +describe('test-config-credentials', function() { |
| 25 | + it('should use the keyFilename field of the config object', function(done) { |
| 26 | + var credentials = require('../fixtures/gcloud-credentials.json'); |
| 27 | + var config = { |
| 28 | + keyFilename: path.join('test', 'fixtures', 'gcloud-credentials.json') |
| 29 | + }; |
| 30 | + var agent = require('../..'); |
| 31 | + nock.disableNetConnect(); |
| 32 | + var scope = nock('https://accounts.google.com') |
| 33 | + .post('/o/oauth2/token', function(body) { |
| 34 | + assert.equal(body.client_id, credentials.client_id); |
| 35 | + assert.equal(body.client_secret, credentials.client_secret); |
| 36 | + assert.equal(body.refresh_token, credentials.refresh_token); |
| 37 | + return true; |
| 38 | + }).reply(200, { |
| 39 | + refresh_token: 'hello', |
| 40 | + access_token: 'goodbye', |
| 41 | + expiry_date: new Date(9999, 1, 1) |
| 42 | + }); |
| 43 | + // Since we have to get an auth token, this always gets intercepted second |
| 44 | + nock('https://clouddebugger.googleapis.com') |
| 45 | + .post('/v2/controller/debuggees/register', function() { |
| 46 | + scope.done(); |
| 47 | + agent.start.wasSuccessful_ = false; |
| 48 | + setImmediate(done); |
| 49 | + return true; |
| 50 | + }).reply(200); |
| 51 | + agent.start(config); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should use the credentials field of the config object', function(done) { |
| 55 | + var config = { |
| 56 | + credentials: require('../fixtures/gcloud-credentials.json') |
| 57 | + }; |
| 58 | + var agent = require('../..'); |
| 59 | + nock.disableNetConnect(); |
| 60 | + var scope = nock('https://accounts.google.com') |
| 61 | + .post('/o/oauth2/token', function(body) { |
| 62 | + assert.equal(body.client_id, config.credentials.client_id); |
| 63 | + assert.equal(body.client_secret, config.credentials.client_secret); |
| 64 | + assert.equal(body.refresh_token, config.credentials.refresh_token); |
| 65 | + return true; |
| 66 | + }).reply(200, { |
| 67 | + refresh_token: 'hello', |
| 68 | + access_token: 'goodbye', |
| 69 | + expiry_date: new Date(9999, 1, 1) |
| 70 | + }); |
| 71 | + // Since we have to get an auth token, this always gets intercepted second |
| 72 | + nock('https://clouddebugger.googleapis.com') |
| 73 | + .post('/v2/controller/debuggees/register', function() { |
| 74 | + scope.done(); |
| 75 | + agent.start.wasSuccessful_ = false; |
| 76 | + setImmediate(done); |
| 77 | + return true; |
| 78 | + }).reply(200); |
| 79 | + agent.start(config); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should ignore credentials if keyFilename is provided', function(done) { |
| 83 | + var correctCredentials = require('../fixtures/gcloud-credentials.json'); |
| 84 | + var config = { |
| 85 | + keyFilename: path.join('test', 'fixtures', 'gcloud-credentials.json'), |
| 86 | + credentials: { |
| 87 | + client_id: 'a', |
| 88 | + client_secret: 'b', |
| 89 | + refresh_token: 'c', |
| 90 | + type: 'authorized_user' |
| 91 | + } |
| 92 | + }; |
| 93 | + ['client_id', 'client_secret', 'refresh_token'].forEach(function (field) { |
| 94 | + assert(correctCredentials.hasOwnProperty(field)); |
| 95 | + assert(config.credentials.hasOwnProperty(field)); |
| 96 | + assert.notEqual(config.credentials[field], |
| 97 | + correctCredentials[field]); |
| 98 | + }); |
| 99 | + var agent = require('../..'); |
| 100 | + nock.disableNetConnect(); |
| 101 | + var scope = nock('https://accounts.google.com') |
| 102 | + .post('/o/oauth2/token', function(body) { |
| 103 | + assert.equal(body.client_id, correctCredentials.client_id); |
| 104 | + assert.equal(body.client_secret, correctCredentials.client_secret); |
| 105 | + assert.equal(body.refresh_token, correctCredentials.refresh_token); |
| 106 | + return true; |
| 107 | + }).reply(200, { |
| 108 | + refresh_token: 'hello', |
| 109 | + access_token: 'goodbye', |
| 110 | + expiry_date: new Date(9999, 1, 1) |
| 111 | + }); |
| 112 | + // Since we have to get an auth token, this always gets intercepted second |
| 113 | + nock('https://clouddebugger.googleapis.com') |
| 114 | + .post('/v2/controller/debuggees/register', function() { |
| 115 | + scope.done(); |
| 116 | + agent.start.wasSuccessful_ = false; |
| 117 | + setImmediate(done); |
| 118 | + return true; |
| 119 | + }).reply(200); |
| 120 | + agent.start(config); |
| 121 | + }); |
| 122 | +}); |
0 commit comments