Tests/Get-ActiveDirectory.Tests.ps1

$projectRoot = Split-Path -Path $PSScriptRoot;
. "$projectRoot\_functionReference.ps1";

Describe "Get-ActiveDirectory" {
    #setup objects
    $applicationsList = New-Object System.Collections.ArrayList;
    $applicationCredentialsList = New-Object System.Collections.ArrayList;

    $application1 = New-Object -TypeName Microsoft.Azure.Graph.RBAC.Version1_6.ActiveDirectory.PSADApplication;
    $application1.ApplicationId = "bc50ae7f-2a7a-4d66-8ed6-fbf17e4950c6";
    $application1.DisplayName = "testName";

    $application2 = New-Object -TypeName Microsoft.Azure.Graph.RBAC.Version1_6.ActiveDirectory.PSADApplication;
    $application2.ApplicationId = "53c5c8b1-2c55-48eb-b0d1-6f5c9eca168a";
    $application2.DisplayName = "testName2";

    $applicationCredential = New-Object -TypeName Microsoft.Azure.Graph.RBAC.Version1_6.ActiveDirectory.PSADCredential;
    $applicationCredential.KeyId = "1";
    $applicationCredential.StartDate = Get-Date;
    $applicationCredential.EndDate = Get-Date;
    $applicationCredential.Type = "Key";

    $applicationsList.Add($application1);
    $applicationsList.Add($application2);

    $applicationCredentialsList.Add($applicationCredential);

    #Mock services
    Mock -CommandName Get-AzureRmADApplication { return $applicationsList };
    Mock -CommandName Get-AzureRmADAppCredential { return $applicationCredentialsList };
    Mock -CommandName Add-Log -MockWith {};
    Mock -CommandName Write-Progress -MockWith {};
    Mock -CommandName Out-Error -MockWith {};
    Mock -CommandName Set-Output -MockWith {};

    Context "Credentials are retrieved for all processed applications" {
        Get-ActiveDirectory;

        It "Calls credential resource with the correct application IDs" {
            Assert-MockCalled -CommandName Get-AzureRmADAppCredential -ParameterFilter { $ApplicationId -eq $application1.ApplicationId } -Times 1 -Exactly;
            Assert-MockCalled -CommandName Get-AzureRmADAppCredential -ParameterFilter { $ApplicationId -eq $application2.ApplicationId } -Times 1 -Exactly;
        }

        It "Sends acquired data to the output pipeline"{
            Assert-MockCalled -CommandName Set-Output -Times 1 -Exactly;
        }
    }
}