Tests/Get-KeyVault.Tests.ps1

$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;
        }
    }
}