Tests/Get-OriAzBopKeyVaultSecret.tests.ps1
. $PSScriptRoot\..\Public\Get-OriAzBopKeyVaultSecret.ps1 Describe 'OriAzBopBoostrapOriPsgallery\Get-OriAzBopKeyVaultSecret' { Context 'Succesfull excution ' { $Script:VaultIdentityUri = "http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01" $Script:VaultResourceUri = "https://vault.azure.net" $Script:VaultIdentityId = "c5026693-9d1f-4131-99f6-17a42edc9e4a" $Script:VaultName = "mykeyvault" $Script:VaultSecretUri1 = 'https://{0}.vault.azure.net/secrets/{1}?api-version=2016-10-01' -f $Script:VaultName, "MyUserName" $Script:VaultSecretUri2 = 'https://{0}.vault.azure.net/secrets/{1}?api-version=2016-10-01' -f $Script:VaultName, "MyPassword" $expectedAuthorzationHeader = "Bearer blablaToken" Mock -CommandName Invoke-RestMethod -ParameterFilter { $Uri -eq $Script:VaultIdentityUri -and ` $Body.resource -eq $Script:VaultResourceUri -and ` $Body.object_id -eq $Script:VaultIdentityId -and ` $Headers.Metadata -eq 'true' } -MockWith { @{access_token = 'blablaToken' } } Mock -CommandName Invoke-RestMethod -ParameterFilter { $Method -eq 'GET' -and ` $Uri -eq $Script:VaultSecretUri1 -and ` $Headers.Authorization -eq $expectedAuthorzationHeader } -MockWith { @{value = 'SecretUserName' } } Mock -CommandName Invoke-RestMethod -ParameterFilter { $Method -eq 'GET' -and ` $Uri -eq $Script:VaultSecretUri2 -and ` $Headers.Authorization -eq $expectedAuthorzationHeader } -MockWith { @{value = 'P@ssword' } } Mock -CommandName Write-Verbose -Verifiable it 'Should be succesfully processed' { $SecretUsername = Get-OriAzBopKeyVaultSecret ` -VaultName $Script:VaultName ` -SecretName "MyUserName" ` -IdentityObjectId $Script:VaultIdentityId ` -Verbose $SecretUsername | Should -BeExactly 'SecretUserName' $SecretCredential = Get-OriAzBopKeyVaultSecret ` -VaultName $Script:VaultName ` -UserName $SecretUsername ` -SecretName "MyPassword" ` -IdentityObjectId $Script:VaultIdentityId ` -Verbose $SecretCredential.UserName | Should -BeExactly 'SecretUserName' $SecretCredential.GetNetworkCredential().Password | Should -BeExactly 'P@ssword' } Assert-MockCalled Invoke-RestMethod -Times 2 -Exactly -ParameterFilter { $Uri -ilike "*metadata/identity/oauth2/token*" } Assert-MockCalled Invoke-RestMethod -Times 1 -Exactly -ParameterFilter { $Uri -ilike "*vault.azure.net/secrets/MyUserName*" } Assert-MockCalled Invoke-RestMethod -Times 1 -Exactly -ParameterFilter { $Uri -ilike "*vault.azure.net/secrets/MyPassword*" } Assert-MockCalled Write-Verbose -Times 2 -Exactly -ParameterFilter { $Message -eq "-- End of Get-OriAzBopKeyVaultSecret --" } Assert-VerifiableMock } } |