import * as assert from 'assert' ; import { describe , it } from 'mocha' ; import * as fs from 'fs' ; import * as nock from 'nock' ; import { UserRefreshClient } from '../src' ; describe ( 'refresh' , ( ) => { function createJSON ( ) { return { client_secret : 'privatekey' , client_id : 'client123' , refresh_token : 'refreshtoken' , type : 'authorized_user' , } ; } it ( 'populates credentials.refresh_token if provided' , ( ) => { const refresh = new UserRefreshClient ( { refreshToken : 'abc123' , } ) ; assert . strictEqual ( refresh . credentials . refresh_token , 'abc123' ) ; } ) ; it ( 'fromJSON should error on null json' , ( ) => { const refresh = new UserRefreshClient ( ) ; assert . throws ( ( ) => { ( refresh as any ) . fromJSON ( null ) ; } ) ; } ) ; it ( 'fromJSON should error on empty json' , ( ) => { const refresh = new UserRefreshClient ( ) ; assert . throws ( ( ) => { refresh . fromJSON ( { } ) ; } ) ; } ) ; it ( 'fromJSON should error on missing client_id' , ( ) => { const json = createJSON ( ) ; delete json . client_id ; const refresh = new UserRefreshClient ( ) ; assert . throws ( ( ) => { refresh . fromJSON ( json ) ; } ) ; } ) ; it ( 'fromJSON should error on missing client_secret' , ( ) => { const json = createJSON ( ) ; delete json . client_secret ; const refresh = new UserRefreshClient ( ) ; assert . throws ( ( ) => { refresh . fromJSON ( json ) ; } ) ; } ) ; it ( 'fromJSON should error on missing refresh_token' , ( ) => { const json = createJSON ( ) ; delete json . refresh_token ; const refresh = new UserRefreshClient ( ) ; assert . throws ( ( ) => { refresh . fromJSON ( json ) ; } ) ; } ) ; it ( 'fromJSON should create UserRefreshClient with clientId_' , ( ) => { const json = createJSON ( ) ; const refresh = new UserRefreshClient ( ) ; refresh . fromJSON ( json ) ; assert . strictEqual ( json . client_id , refresh . _clientId ) ; } ) ; it ( 'fromJSON should create UserRefreshClient with clientSecret_' , ( ) => { const json = createJSON ( ) ; const refresh = new UserRefreshClient ( ) ; refresh . fromJSON ( json ) ; assert . strictEqual ( json . client_secret , refresh . _clientSecret ) ; } ) ; it ( 'fromJSON should create UserRefreshClient with _refreshToken' , ( ) => { const json = createJSON ( ) ; const refresh = new UserRefreshClient ( ) ; refresh . fromJSON ( json ) ; assert . strictEqual ( json . refresh_token , refresh . _refreshToken ) ; } ) ; it ( 'fromStream should error on null stream' , done => { const refresh = new UserRefreshClient ( ) ; ( refresh as any ) . fromStream ( null , ( err ) => { assert . strictEqual ( true , err instanceof Error ) ; done ( ) ; } ) ; } ) ; it ( 'fromStream should read the stream and create a UserRefreshClient' , done => { const fileContents = fs . readFileSync ( './test/fixtures/refresh.json' , 'utf-8' ) ; const json = JSON . parse ( fileContents ) ; const stream = fs . createReadStream ( './test/fixtures/refresh.json' ) ; const refresh = new UserRefreshClient ( ) ; refresh . fromStream ( stream , err => { assert . ifError ( err ) ; assert . strictEqual ( json . client_id , refresh . _clientId ) ; assert . strictEqual ( json . client_secret , refresh . _clientSecret ) ; assert . strictEqual ( json . refresh_token , refresh . _refreshToken ) ; done ( ) ; } ) ; } ) ; it ( 'getRequestHeaders should populate x-goog-user-project header if quota_project_id present' , async ( ) => { const req = nock ( 'https://oauth2.googleapis.com' ) . post ( '/token' ) . reply ( 200 , { } ) ; const stream = fs . createReadStream ( './test/fixtures/config-with-quota/.config/gcloud/application_default_credentials.json' ) ; const refresh = new UserRefreshClient ( ) ; await refresh . fromStream ( stream ) ; const headers = await refresh . getRequestHeaders ( ) ; assert . strictEqual ( headers [ 'x-goog-user-project' ] , 'my-quota-project' ) ; req . done ( ) ; } ) ; it ( 'getRequestHeaders should populate x-goog-user-project header if quota_project_id present and token has not expired' , async ( ) => { const stream = fs . createReadStream ( './test/fixtures/config-with-quota/.config/gcloud/application_default_credentials.json' ) ; const eagerRefreshThresholdMillis = 10 ; const refresh = new UserRefreshClient ( { eagerRefreshThresholdMillis , } ) ; await refresh . fromStream ( stream ) ; refresh . credentials = { access_token : 'woot' , refresh_token : 'jwt-placeholder' , expiry_date : new Date ( ) . getTime ( ) + eagerRefreshThresholdMillis + 1000 , } ; const headers = await refresh . getRequestHeaders ( ) ; assert . strictEqual ( headers [ 'x-goog-user-project' ] , 'my-quota-project' ) ; } ) ; it ( 'getRequestHeaders should populate x-goog-user-project header if quota_project_id present and token has expired' , async ( ) => { const req = nock ( 'https://oauth2.googleapis.com' ) . post ( '/token' ) . reply ( 200 , { } ) ; const stream = fs . createReadStream ( './test/fixtures/config-with-quota/.config/gcloud/application_default_credentials.json' ) ; const refresh = new UserRefreshClient ( ) ; await refresh . fromStream ( stream ) ; refresh . credentials = { access_token : 'woot' , refresh_token : 'jwt-placeholder' , expiry_date : new Date ( ) . getTime ( ) - 1 , } ; const headers = await refresh . getRequestHeaders ( ) ; assert . strictEqual ( headers [ 'x-goog-user-project' ] , 'my-quota-project' ) ; req . done ( ) ; } ) ; } ) ;