Tests/Get-KeyVault.Tests.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
$projectRoot = Split-Path -Path $PSScriptRoot; . "$projectRoot\_functionReference.ps1"; Describe "Get-KeyVault" { $resourceGroupName = "testGroup"; $keyVaultList = New-Object System.Collections.ArrayList; $keyVaultKeyList = New-Object System.Collections.ArrayList; $keyVaultSecretList = New-Object System.Collections.ArrayList; $keyVaultCertList = New-Object System.Collections.ArrayList; #The Azure Resource Manager DLL has some of the properties of these classes set as read-only #So a custom powershell object is created to mimick the .Net objects $keyVault1 = [PsCustomObject]@{VaultName = "someVault"} $keyVaultKey1 = [PsCustomObject]@{ Name = "newKey"; Id = "1234"; Created = Get-Date; Expires = Get-Date; Enabled = $true; } $keyVaultSecret1 = [PsCustomObject]@{ Name = "newSecret"; Id = "2244"; Created = Get-Date; Expires = Get-Date; Enabled = $true; } $keyVaultCert1 = [PsCustomObject]@{ Name = "newCert"; Id = "3355"; Created = Get-Date; Expires = Get-Date; Enabled = $true; } $keyVaultList.Add($keyVault1); $keyVaultKeyList.Add($keyVaultKey1); $keyVaultSecretList.Add($keyVaultSecret1); $keyVaultCertList.Add($keyVaultCert1); #Mock services Mock -CommandName Get-AzureRmKeyVault { return $keyVaultList }; Mock -CommandName Get-AzureKeyVaultKey { return $keyVaultKeyList }; Mock -CommandName Get-AzureKeyVaultSecret { return $keyVaultSecretList }; Mock -CommandName Get-AzureKeyVaultCertificate { return $keyVaultCertList }; Mock -CommandName Add-Log -MockWith {}; Mock -CommandName Out-Error -MockWith {}; Mock -CommandName Set-Output -MockWith {}; Context "Retrieves all keyvaults in a given resource group name" { Get-KeyVault; It "Calls the Key Vault service with the correct resource group name" { Assert-MockCalled -CommandName Get-AzureRmKeyVault -ParameterFilter { $ResourceGroupName -eq $resourceGroupName } -Times 1 -Exactly } } Context "All items inside a key vault are retrieved" { Get-KeyVault; It "Calls service to retieve keys with the correct vault name" { Assert-MockCalled -CommandName Get-AzureKeyVaultKey -ParameterFilter { $VaultName -eq $keyVault1.VaultName } -Times 1 -Exactly } It "Calls service to retieve secrets with the correct vault name" { Assert-MockCalled -CommandName Get-AzureKeyVaultSecret -ParameterFilter { $VaultName -eq $keyVault1.VaultName } -Times 1 -Exactly } It "Calls service to retieve certificates with the correct vault name" { Assert-MockCalled -CommandName Get-AzureKeyVaultCertificate -ParameterFilter { $VaultName -eq $keyVault1.VaultName } -Times 1 -Exactly } It "Sends acquired data to the output pipeline" { Assert-MockCalled -CommandName Set-Output -Times 1 -Exactly; } } } |