From 0b7d4127b02aadf8abcb9bf0aec25085f5d77914 Mon Sep 17 00:00:00 2001 From: Kelvin Jin Date: Thu, 27 Sep 2018 14:15:28 -0700 Subject: [PATCH] fix: update @google-cloud/common dependency to 0.25.3 --- package.json | 2 +- test/test-config-credentials.ts | 139 -------------------------------- test/test-trace-writer.ts | 73 +++++++++++++++++ tsconfig.json | 1 - 4 files changed, 74 insertions(+), 141 deletions(-) delete mode 100644 test/test-config-credentials.ts diff --git a/package.json b/package.json index 039acfffa..8a6d794be 100644 --- a/package.json +++ b/package.json @@ -94,7 +94,7 @@ "typescript": "~3.0.0" }, "dependencies": { - "@google-cloud/common": "^0.23.0", + "@google-cloud/common": "^0.25.3", "builtin-modules": "^3.0.0", "console-log-level": "^1.4.0", "continuation-local-storage": "^3.2.1", diff --git a/test/test-config-credentials.ts b/test/test-config-credentials.ts deleted file mode 100644 index 3d1edf932..000000000 --- a/test/test-config-credentials.ts +++ /dev/null @@ -1,139 +0,0 @@ -/** - * 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. - */ - -import * as assert from 'assert'; -import * as nock from 'nock'; -import {disableNetConnect, enableNetConnect} from 'nock'; -import * as path from 'path'; - -import {FORCE_NEW} from '../src/util'; -import {oauth2, patchTraces} from './nocks'; -import * as testTraceModule from './trace'; -import {plan} from './utils'; - -interface TestCredentials { - client_id?: string; - client_secret?: string; - refresh_token?: string; - private_key?: string; - type?: string; -} - -function queueSpans(n: number) { - const traceApi = testTraceModule.get(); - for (let i = 0; i < n; i++) { - traceApi.runInRootSpan({name: `trace-${i}`}, (rootSpan) => { - assert.ok(rootSpan); - rootSpan!.endSpan(); - }); - } -} - -describe('Credentials Configuration', () => { - let savedProject: string|undefined; - - before(() => { - savedProject = process.env.GCLOUD_PROJECT; - process.env.GCLOUD_PROJECT = '0'; - testTraceModule.setTraceWriterForTest(); - disableNetConnect(); - }); - - after(() => { - process.env.GCLOUD_PROJECT = savedProject; - testTraceModule.setTraceWriterForTest(testTraceModule.TestTraceWriter); - enableNetConnect(); - }); - - it('should use the keyFilename field of the config object', (done) => { - const progress = plan(done, 2); - const credentials: TestCredentials = - require('./fixtures/gcloud-credentials.json'); - const config = { - bufferSize: 2, - keyFilename: path.join('test', 'fixtures', 'gcloud-credentials.json'), - [FORCE_NEW]: true - }; - const agent = testTraceModule.start(config); - const scope = oauth2((body) => { - assert.strictEqual(body.client_id, credentials.client_id); - assert.strictEqual(body.client_secret, credentials.client_secret); - assert.strictEqual(body.refresh_token, credentials.refresh_token); - progress(); - return true; - }); - // Since we have to get an auth token, this always gets intercepted second - patchTraces('0', () => { - scope.done(); - progress(); - return true; - }); - queueSpans(2); - }); - - it('should use the credentials field of the config object', (done) => { - const progress = plan(done, 2); - const credentials: TestCredentials = - require('./fixtures/gcloud-credentials.json'); - const config = {bufferSize: 2, credentials, [FORCE_NEW]: true}; - const agent = testTraceModule.start(config); - const scope = oauth2((body) => { - assert.strictEqual(body.client_id, credentials.client_id); - assert.strictEqual(body.client_secret, credentials.client_secret); - assert.strictEqual(body.refresh_token, credentials.refresh_token); - progress(); - return true; - }); - // Since we have to get an auth token, this always gets intercepted second - patchTraces('0', () => { - scope.done(); - progress(); - return true; - }); - queueSpans(2); - }); - - it('should ignore keyFilename if credentials is provided', (done) => { - const progress = plan(done, 2); - const correctCredentials: TestCredentials = { - client_id: 'a', - client_secret: 'b', - refresh_token: 'c', - type: 'authorized_user' - }; - const config = { - bufferSize: 2, - credentials: correctCredentials, - keyFilename: path.join('test', 'fixtures', 'gcloud-credentials.json'), - [FORCE_NEW]: true - }; - const agent = testTraceModule.start(config); - const scope = oauth2((body) => { - assert.strictEqual(body.client_id, correctCredentials.client_id); - assert.strictEqual(body.client_secret, correctCredentials.client_secret); - assert.strictEqual(body.refresh_token, correctCredentials.refresh_token); - progress(); - return true; - }); - // Since we have to get an auth token, this always gets intercepted second - patchTraces('0', () => { - scope.done(); - progress(); - return true; - }); - queueSpans(2); - }); -}); diff --git a/test/test-trace-writer.ts b/test/test-trace-writer.ts index 289014f88..48d365a09 100644 --- a/test/test-trace-writer.ts +++ b/test/test-trace-writer.ts @@ -16,9 +16,13 @@ import {Service} from '@google-cloud/common'; import * as assert from 'assert'; +import {GoogleAuth} from 'google-auth-library'; +import {JWTInput} from 'google-auth-library/build/src/auth/credentials'; +import {RefreshOptions} from 'google-auth-library/build/src/auth/oauth2client'; import {OutgoingHttpHeaders} from 'http'; import * as nock from 'nock'; import * as os from 'os'; +import * as path from 'path'; import {Response} from 'request'; // Only for type declarations. import * as shimmer from 'shimmer'; @@ -30,6 +34,14 @@ import {TestLogger} from './logger'; import {hostname, instanceId, oauth2} from './nocks'; import {wait} from './utils'; +interface TestCredentials { + client_id?: string; + client_secret?: string; + refresh_token?: string; + private_key?: string; + type?: string; +} + interface DecorateRequestOptions { method: string; uri: string; @@ -116,6 +128,67 @@ describe('Trace Writer', () => { nock.cleanAll(); }); + describe('constructor', () => { + // Utility method which, for a given config, constructs a new TraceWriter + // instance, and gets the credentials that would be used upon initiating + // a trace batch publish. + const captureCredentialsForConfig = + async (config: Partial) => { + const writer = + new TraceWriter(Object.assign({}, DEFAULT_CONFIG, config), logger); + let capturedJson; + shimmer.wrap(writer.authClient, 'fromJSON', (fromJSON) => { + return function( + this: GoogleAuth, json: JWTInput, options?: RefreshOptions) { + capturedJson = json; + return fromJSON.call(this, json, options); + }; + }); + await writer.authClient.getClient(); + shimmer.unwrap(writer.authClient, 'fromJSON'); + return capturedJson; + }; + + beforeEach(() => { + // Just for this scenario, use real metadata endpoints + metadataScopes.cancel(); + nock.cleanAll(); + }); + + it('should use the keyFilename field of the config object', async () => { + const expectedCredentials: TestCredentials = + require('./fixtures/gcloud-credentials.json'); + const actualCredentials = await captureCredentialsForConfig({ + projectId: 'my-project', + keyFilename: path.join('test', 'fixtures', 'gcloud-credentials.json') + }); + assert.deepStrictEqual(actualCredentials, expectedCredentials); + }); + + it('should use the credentials field of the config object', async () => { + const expectedCredentials: TestCredentials = + require('./fixtures/gcloud-credentials.json'); + const actualCredentials = await captureCredentialsForConfig( + {projectId: 'my-project', credentials: expectedCredentials}); + assert.deepStrictEqual(actualCredentials, expectedCredentials); + }); + + it('should ignore keyFilename if credentials is provided', async () => { + const expectedCredentials: TestCredentials = { + client_id: 'a', + client_secret: 'b', + refresh_token: 'c', + type: 'authorized_user' + }; + const actualCredentials = await captureCredentialsForConfig({ + projectId: 'my-project', + keyFilename: path.join('test', 'fixtures', 'gcloud-credentials.json'), + credentials: expectedCredentials + }); + assert.deepStrictEqual(actualCredentials, expectedCredentials); + }); + }); + describe('initialization process', () => { it('gets the project ID when none is passed in', (done) => { const writer = new TraceWriter(DEFAULT_CONFIG, logger); diff --git a/tsconfig.json b/tsconfig.json index 142a638a5..ae43a2d15 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -27,7 +27,6 @@ "test/test-cls.ts", "test/test-cls-ah.ts", "test/test-config-cls.ts", - "test/test-config-credentials.ts", "test/test-config-plugins.ts", "test/test-default-ignore-ah-health.ts", "test/test-env-log-level.ts",