Zoltaire
Zoltaire

Reputation: 21

Mocking Auth Token function in Pester

I have written a PowerShell function that generates an auth token in azure using REST API call. I am using the tenant id, Service principal client id and client secret as input param to generate this token. The SP client id and client secret are being read from the azure keyvault.

I wish to write a unit test to:

  1. Read secret from keyvault
  2. Mock the REST API call to generate dummy auth token

I am planning to use PESTER for unit testing and need some assistance here.

Here is my function:

function string GetAuthToken() {       
        $AuthTokenUri = "https://login.microsoftonline.com/$TenantId/oauth2/token"
        $Body = @{
            'resource'      = $resourcename
            'client_id'     = $ID
            'grant_type'    = 'client_credentials'
            'client_secret' = $Secret
        }
       $Token = Invoke-RestMethod -Method Post -Uri $AuthTokenUri -Body $Body -ContentType application/x-www-form-urlencoded'
       $AuthToken = ($Token.access_token).ToString()
       return $AuthToken
}

Upvotes: 0

Views: 275

Answers (0)

Related Questions