'use strict' ; import * as sinon from 'sinon' ; import { ExtensionsInteractionUtil } from '../../extension/util/ExtensionsInteractionUtil' ; import Axios from 'axios' ; export class ExtensionsInteractionUtilHelper { mySandBox : sinon . SinonSandbox ; accessToken : string ; cloudAccountGetAccessTokenStub : sinon . SinonStub ; cloudAccountIsLoggedInStub : sinon . SinonStub ; cloudAccountHasSelectedAccountStub : sinon . SinonStub ; cloudAccountGetIbpResourcesStub : sinon . SinonStub ; constructor ( sandbox ) { this . mySandBox = sandbox ; this . accessToken = undefined ; this . cloudAccountGetAccessTokenStub = this . mySandBox . stub ( ExtensionsInteractionUtil , 'cloudAccountGetAccessToken' ) . callsFake ( this . getTokenDirectly ) ; this . cloudAccountIsLoggedInStub = this . mySandBox . stub ( ExtensionsInteractionUtil , 'cloudAccountIsLoggedIn' ) . callsFake ( this . resolveFalse ) ; this . cloudAccountHasSelectedAccountStub = this . mySandBox . stub ( ExtensionsInteractionUtil , 'cloudAccountHasSelectedAccount' ) . callsFake ( this . resolveFalse ) ; this . cloudAccountGetIbpResourcesStub = this . mySandBox . stub ( ExtensionsInteractionUtil , 'cloudAccountGetIbpResources' ) . callThrough ( ) ; } private async resolveFalse ( ) { return false ; } private async getTokenDirectly ( ) { if ( ! this . accessToken ) { const options = { method : 'post' , url : 'https://iam.cloud.ibm.com/identity/token' , headers : { 'Accept' : 'application/json' , 'Content-Type' : 'application/x-www-form-urlencoded' } , params : { grant_type : 'urn:ibm:params:oauth:grant-type:apikey' , apikey : `${ process . env . MAP_OPSTOOLS_SAAS_API_KEY }` } , } ; const result = await Axios ( options ) ; this . accessToken = result . data . access_token ; } return this . accessToken ; } }