1 year ago

#210710

test-img

Zoltaire

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
}

powershell

unit-testing

pester

pester-5

0 Answers

Your Answer

Accepted video resources