@@ -32,7 +32,7 @@ import * as mocks from '../../resources/mocks';
32
32
33
33
import {
34
34
GoogleOAuthAccessToken , RefreshTokenCredential , ServiceAccountCredential ,
35
- ComputeEngineCredential , getApplicationDefault ,
35
+ ComputeEngineCredential , getApplicationDefault , isApplicationDefault , Credential ,
36
36
} from '../../../src/auth/credential' ;
37
37
import { HttpClient } from '../../../src/utils/api-request' ;
38
38
import { Agent } from 'https' ;
@@ -183,15 +183,17 @@ describe('Credential', () => {
183
183
projectId : mockCertificateObject . project_id ,
184
184
clientEmail : mockCertificateObject . client_email ,
185
185
privateKey : mockCertificateObject . private_key ,
186
+ implicit : false ,
186
187
} ) ;
187
188
} ) ;
188
189
189
- it ( 'should return a certificate ' , ( ) => {
190
- const c = new ServiceAccountCredential ( mockCertificateObject ) ;
190
+ it ( 'should return an implicit Credential ' , ( ) => {
191
+ const c = new ServiceAccountCredential ( mockCertificateObject , undefined , true ) ;
191
192
expect ( c ) . to . deep . include ( {
192
193
projectId : mockCertificateObject . project_id ,
193
194
clientEmail : mockCertificateObject . client_email ,
194
195
privateKey : mockCertificateObject . private_key ,
196
+ implicit : true ,
195
197
} ) ;
196
198
} ) ;
197
199
@@ -267,6 +269,20 @@ describe('Credential', () => {
267
269
. to . throw ( 'Refresh token must contain a "type" property' ) ;
268
270
} ) ;
269
271
272
+ it ( 'should return a Credential' , ( ) => {
273
+ const c = new RefreshTokenCredential ( mocks . refreshToken ) ;
274
+ expect ( c ) . to . deep . include ( {
275
+ implicit : false ,
276
+ } ) ;
277
+ } ) ;
278
+
279
+ it ( 'should return an implicit Credential' , ( ) => {
280
+ const c = new RefreshTokenCredential ( mocks . refreshToken , undefined , true ) ;
281
+ expect ( c ) . to . deep . include ( {
282
+ implicit : true ,
283
+ } ) ;
284
+ } ) ;
285
+
270
286
it ( 'should create access tokens' , ( ) => {
271
287
const scope = nock ( 'https://www.googleapis.com' )
272
288
. post ( '/oauth2/v4/token' )
@@ -477,6 +493,72 @@ describe('Credential', () => {
477
493
} ) ;
478
494
} ) ;
479
495
496
+ describe ( 'isApplicationDefault()' , ( ) => {
497
+ let fsStub : sinon . SinonStub ;
498
+
499
+ afterEach ( ( ) => {
500
+ if ( fsStub ) {
501
+ fsStub . restore ( ) ;
502
+ }
503
+ } ) ;
504
+
505
+ it ( 'should return true for ServiceAccountCredential loaded from GOOGLE_APPLICATION_CREDENTIALS' , ( ) => {
506
+ process . env . GOOGLE_APPLICATION_CREDENTIALS = path . resolve ( __dirname , '../../resources/mock.key.json' ) ;
507
+ const c = getApplicationDefault ( ) ;
508
+ expect ( c ) . to . be . an . instanceof ( ServiceAccountCredential ) ;
509
+ expect ( isApplicationDefault ( c ) ) . to . be . true ;
510
+ } ) ;
511
+
512
+ it ( 'should return true for RefreshTokenCredential loaded from GOOGLE_APPLICATION_CREDENTIALS' , ( ) => {
513
+ process . env . GOOGLE_APPLICATION_CREDENTIALS = GCLOUD_CREDENTIAL_PATH ;
514
+ fsStub = sinon . stub ( fs , 'readFileSync' ) . returns ( JSON . stringify ( MOCK_REFRESH_TOKEN_CONFIG ) ) ;
515
+ const c = getApplicationDefault ( ) ;
516
+ expect ( c ) . is . instanceOf ( RefreshTokenCredential ) ;
517
+ expect ( isApplicationDefault ( c ) ) . to . be . true ;
518
+ } ) ;
519
+
520
+ it ( 'should return true for credential loaded from gcloud SDK' , ( ) => {
521
+ if ( ! fs . existsSync ( GCLOUD_CREDENTIAL_PATH ) ) {
522
+ // tslint:disable-next-line:no-console
523
+ console . log (
524
+ 'WARNING: Test being skipped because gcloud credentials not found. Run `gcloud beta auth ' +
525
+ 'application-default login`.' ) ;
526
+ return ;
527
+ }
528
+ delete process . env . GOOGLE_APPLICATION_CREDENTIALS ;
529
+ const c = getApplicationDefault ( ) ;
530
+ expect ( c ) . to . be . an . instanceof ( RefreshTokenCredential ) ;
531
+ expect ( isApplicationDefault ( c ) ) . to . be . true ;
532
+ } ) ;
533
+
534
+ it ( 'should return true for ComputeEngineCredential' , ( ) => {
535
+ delete process . env . GOOGLE_APPLICATION_CREDENTIALS ;
536
+ fsStub = sinon . stub ( fs , 'readFileSync' ) . throws ( new Error ( 'no gcloud credential file' ) ) ;
537
+ const c = getApplicationDefault ( ) ;
538
+ expect ( c ) . to . be . an . instanceof ( ComputeEngineCredential ) ;
539
+ expect ( isApplicationDefault ( c ) ) . to . be . true ;
540
+ } ) ;
541
+
542
+ it ( 'should return false for explicitly loaded ServiceAccountCredential' , ( ) => {
543
+ const c = new ServiceAccountCredential ( mockCertificateObject ) ;
544
+ expect ( isApplicationDefault ( c ) ) . to . be . false ;
545
+ } ) ;
546
+
547
+ it ( 'should return false for explicitly loaded RefreshTokenCredential' , ( ) => {
548
+ const c = new RefreshTokenCredential ( mocks . refreshToken ) ;
549
+ expect ( isApplicationDefault ( c ) ) . to . be . false ;
550
+ } ) ;
551
+
552
+ it ( 'should return false for custom credential' , ( ) => {
553
+ const c : Credential = {
554
+ getAccessToken : ( ) => {
555
+ throw new Error ( ) ;
556
+ } ,
557
+ } ;
558
+ expect ( isApplicationDefault ( c ) ) . to . be . false ;
559
+ } ) ;
560
+ } ) ;
561
+
480
562
describe ( 'HTTP Agent' , ( ) => {
481
563
const expectedToken = utils . generateRandomAccessToken ( ) ;
482
564
let stub : sinon . SinonStub ;
0 commit comments