@@ -43,7 +43,6 @@ chai.use(chaiAsPromised);
4343
4444const  expect  =  chai . expect ; 
4545
46- let  TEST_GCLOUD_CREDENTIALS : any ; 
4746const  GCLOUD_CREDENTIAL_SUFFIX  =  'gcloud/application_default_credentials.json' ; 
4847const  GCLOUD_CREDENTIAL_PATH  =  path . resolve ( process . env . HOME ,  '.config' ,  GCLOUD_CREDENTIAL_SUFFIX ) ; 
4948const  MOCK_REFRESH_TOKEN_CONFIG  =  { 
@@ -52,38 +51,6 @@ const MOCK_REFRESH_TOKEN_CONFIG = {
5251  type : 'authorized_user' , 
5352  refresh_token : 'test_token' , 
5453} ; 
55- try  { 
56-   TEST_GCLOUD_CREDENTIALS  =  JSON . parse ( fs . readFileSync ( GCLOUD_CREDENTIAL_PATH ) . toString ( ) ) ; 
57- }  catch  ( error )  { 
58-   // tslint:disable-next-line:no-console 
59-   console . log ( 
60-     'WARNING: gcloud credentials not found. Run `gcloud beta auth application-default login`. '  + 
61-     'Relevant tests will be skipped.' , 
62-   ) ; 
63- } 
64- 
65- /** 
66-  * Logs a warning and returns true if no gcloud credentials are found, meaning the test which calls 
67-  * this will be skipped. 
68-  * 
69-  * The only thing that should ever skip these tests is continuous integration. When developing 
70-  * locally, these tests should be run. 
71-  * 
72-  * @return  {boolean } Whether or not the caller should skip the current test. 
73-  */ 
74- const  skipAndLogWarningIfNoGcloud  =  ( )  =>  { 
75-   if  ( typeof  TEST_GCLOUD_CREDENTIALS  ===  'undefined' )  { 
76-     // tslint:disable-next-line:no-console 
77-     console . log ( 
78-       'WARNING: Test being skipped because gcloud credentials not found. Run `gcloud beta auth '  + 
79-       'application-default login`.' , 
80-     ) ; 
81- 
82-     return  true ; 
83-   } 
84- 
85-   return  false ; 
86- } ; 
8754
8855const  ONE_HOUR_IN_SECONDS  =  60  *  60 ; 
8956const  FIVE_MINUTES_IN_SECONDS  =  5  *  60 ; 
@@ -92,17 +59,32 @@ const FIVE_MINUTES_IN_SECONDS = 5 * 60;
9259describe ( 'Credential' ,  ( )  =>  { 
9360  let  mockCertificateObject : any ; 
9461  let  oldProcessEnv : NodeJS . ProcessEnv ; 
62+   let  getTokenScope : nock . Scope ; 
63+   let  mockedRequests : nock . Scope [ ]  =  [ ] ; 
64+ 
65+   before ( ( )  =>  { 
66+     getTokenScope  =  nock ( 'https://accounts.google.com' ) 
67+       . persist ( ) 
68+       . post ( '/o/oauth2/token' ) 
69+       . reply ( 200 ,  { 
70+         access_token : utils . generateRandomAccessToken ( ) , 
71+         token_type : 'Bearer' , 
72+         expires_in : 3600 , 
73+       } ,  { 
74+         'cache-control' : 'no-cache, no-store, max-age=0, must-revalidate' , 
75+       } ) ; 
76+   } ) ; 
9577
96-   before ( ( )  =>  utils . mockFetchAccessTokenRequests ( ) ) ; 
97- 
98-   after ( ( )  =>  nock . cleanAll ( ) ) ; 
78+   after ( ( )  =>  getTokenScope . done ( ) ) ; 
9979
10080  beforeEach ( ( )  =>  { 
10181    mockCertificateObject  =  _ . clone ( mocks . certificateObject ) ; 
10282    oldProcessEnv  =  process . env ; 
10383  } ) ; 
10484
10585  afterEach ( ( )  =>  { 
86+     _ . forEach ( mockedRequests ,  ( mockedRequest )  =>  mockedRequest . done ( ) ) ; 
87+     mockedRequests  =  [ ] ; 
10688    process . env  =  oldProcessEnv ; 
10789  } ) ; 
10890
@@ -289,11 +271,18 @@ describe('Credential', () => {
289271    } ) ; 
290272
291273    it ( 'should create access tokens' ,  ( )  =>  { 
292-       if  ( skipAndLogWarningIfNoGcloud ( ) )  { 
293-         return ; 
294-       } 
274+       const  scope  =  nock ( 'https://www.googleapis.com' ) 
275+         . post ( '/oauth2/v4/token' ) 
276+         . reply ( 200 ,  { 
277+           access_token : 'token' , 
278+           token_type : 'Bearer' , 
279+           expires_in : 60  *  60 , 
280+         } ,  { 
281+           'cache-control' : 'no-cache, no-store, max-age=0, must-revalidate' , 
282+         } ) ; 
283+       mockedRequests . push ( scope ) ; 
295284
296-       const  c  =  new  RefreshTokenCredential ( TEST_GCLOUD_CREDENTIALS ) ; 
285+       const  c  =  new  RefreshTokenCredential ( mocks . refreshToken ) ; 
297286      return  c . getAccessToken ( ) . then ( ( token )  =>  { 
298287        expect ( token . access_token ) . to . be . a ( 'string' ) . and . to . not . be . empty ; 
299288        expect ( token . expires_in ) . to . greaterThan ( FIVE_MINUTES_IN_SECONDS ) ; 
@@ -328,16 +317,12 @@ describe('Credential', () => {
328317  } ) ; 
329318
330319  describe ( 'ApplicationDefaultCredential' ,  ( )  =>  { 
331-     let  credPath : string ; 
332320    let  fsStub : sinon . SinonStub ; 
333321
334-     beforeEach ( ( )   =>  credPath  =  process . env . GOOGLE_APPLICATION_CREDENTIALS ) ; 
335- 
336322    afterEach ( ( )  =>  { 
337323      if  ( fsStub )  { 
338324        fsStub . restore ( ) ; 
339325      } 
340-       process . env . GOOGLE_APPLICATION_CREDENTIALS  =  credPath ; 
341326    } ) ; 
342327
343328    it ( 'should return a CertCredential with GOOGLE_APPLICATION_CREDENTIALS set' ,  ( )  =>  { 
@@ -370,10 +355,13 @@ describe('Credential', () => {
370355    } ) ; 
371356
372357    it ( 'should return a RefreshTokenCredential with gcloud login' ,  ( )  =>  { 
373-       if  ( skipAndLogWarningIfNoGcloud ( ) )  { 
358+       if  ( ! fs . existsSync ( GCLOUD_CREDENTIAL_PATH ) )  { 
359+         // tslint:disable-next-line:no-console 
360+         console . log ( 
361+           'WARNING: Test being skipped because gcloud credentials not found. Run `gcloud beta auth '  + 
362+           'application-default login`.' ) ; 
374363        return ; 
375364      } 
376- 
377365      delete  process . env . GOOGLE_APPLICATION_CREDENTIALS ; 
378366      expect ( ( new  ApplicationDefaultCredential ( ) ) . getCredential ( ) ) . to . be . an . instanceof ( RefreshTokenCredential ) ; 
379367    } ) ; 
0 commit comments