1616
1717import { Service } from '@google-cloud/common' ;
1818import * as assert from 'assert' ;
19+ import { GoogleAuth } from 'google-auth-library' ;
20+ import { JWTInput } from 'google-auth-library/build/src/auth/credentials' ;
21+ import { RefreshOptions } from 'google-auth-library/build/src/auth/oauth2client' ;
1922import { OutgoingHttpHeaders } from 'http' ;
2023import * as nock from 'nock' ;
2124import * as os from 'os' ;
25+ import * as path from 'path' ;
2226import { Response } from 'request' ; // Only for type declarations.
2327import * as shimmer from 'shimmer' ;
2428
@@ -30,6 +34,14 @@ import {TestLogger} from './logger';
3034import { hostname , instanceId , oauth2 } from './nocks' ;
3135import { wait } from './utils' ;
3236
37+ interface TestCredentials {
38+ client_id ?: string ;
39+ client_secret ?: string ;
40+ refresh_token ?: string ;
41+ private_key ?: string ;
42+ type ?: string ;
43+ }
44+
3345interface DecorateRequestOptions {
3446 method : string ;
3547 uri : string ;
@@ -116,6 +128,67 @@ describe('Trace Writer', () => {
116128 nock . cleanAll ( ) ;
117129 } ) ;
118130
131+ describe ( 'constructor' , ( ) => {
132+ // Utility method which, for a given config, constructs a new TraceWriter
133+ // instance, and gets the credentials that would be used upon initiating
134+ // a trace batch publish.
135+ const captureCredentialsForConfig =
136+ async ( config : Partial < TraceWriterConfig > ) => {
137+ const writer =
138+ new TraceWriter ( Object . assign ( { } , DEFAULT_CONFIG , config ) , logger ) ;
139+ let capturedJson ;
140+ shimmer . wrap ( writer . authClient , 'fromJSON' , ( fromJSON ) => {
141+ return function (
142+ this : GoogleAuth , json : JWTInput , options ?: RefreshOptions ) {
143+ capturedJson = json ;
144+ return fromJSON . call ( this , json , options ) ;
145+ } ;
146+ } ) ;
147+ await writer . authClient . getClient ( ) ;
148+ shimmer . unwrap ( writer . authClient , 'fromJSON' ) ;
149+ return capturedJson ;
150+ } ;
151+
152+ beforeEach ( ( ) => {
153+ // Just for this scenario, use real metadata endpoints
154+ metadataScopes . cancel ( ) ;
155+ nock . cleanAll ( ) ;
156+ } ) ;
157+
158+ it ( 'should use the keyFilename field of the config object' , async ( ) => {
159+ const expectedCredentials : TestCredentials =
160+ require ( './fixtures/gcloud-credentials.json' ) ;
161+ const actualCredentials = await captureCredentialsForConfig ( {
162+ projectId : 'my-project' ,
163+ keyFilename : path . join ( 'test' , 'fixtures' , 'gcloud-credentials.json' )
164+ } ) ;
165+ assert . deepStrictEqual ( actualCredentials , expectedCredentials ) ;
166+ } ) ;
167+
168+ it ( 'should use the credentials field of the config object' , async ( ) => {
169+ const expectedCredentials : TestCredentials =
170+ require ( './fixtures/gcloud-credentials.json' ) ;
171+ const actualCredentials = await captureCredentialsForConfig (
172+ { projectId : 'my-project' , credentials : expectedCredentials } ) ;
173+ assert . deepStrictEqual ( actualCredentials , expectedCredentials ) ;
174+ } ) ;
175+
176+ it ( 'should ignore keyFilename if credentials is provided' , async ( ) => {
177+ const expectedCredentials : TestCredentials = {
178+ client_id : 'a' ,
179+ client_secret : 'b' ,
180+ refresh_token : 'c' ,
181+ type : 'authorized_user'
182+ } ;
183+ const actualCredentials = await captureCredentialsForConfig ( {
184+ projectId : 'my-project' ,
185+ keyFilename : path . join ( 'test' , 'fixtures' , 'gcloud-credentials.json' ) ,
186+ credentials : expectedCredentials
187+ } ) ;
188+ assert . deepStrictEqual ( actualCredentials , expectedCredentials ) ;
189+ } ) ;
190+ } ) ;
191+
119192 describe ( 'initialization process' , ( ) => {
120193 it ( 'gets the project ID when none is passed in' , ( done ) => {
121194 const writer = new TraceWriter ( DEFAULT_CONFIG , logger ) ;
0 commit comments