Tests/Get-Subscription.Tests.ps1

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

Describe "Get-Subscription" {
    #setup objects
    $currentTenantId = "1111"
    $subscriptionsList = New-Object System.Collections.ArrayList;
    $resourceGroupList = New-Object System.Collections.ArrayList;

    $subscription1 = New-Object -TypeName Microsoft.Azure.Commands.Profile.Models.PSAzureSubscription;
    $subscription1.Id = "1234";
    $subscription1.Name = "abcd";

    $subscription2 = New-Object -TypeName Microsoft.Azure.Commands.Profile.Models.PSAzureSubscription;
    $subscription2.Id = "5678";
    $subscription2.Name = "qwerty";
    
    $resourceGroup1 = New-Object -TypeName Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels.PSResourceGroup;
    $resourceGroup1.ResourceGroupName = "resGroup";

    $subscriptionsList.Add($subscription1);
    $subscriptionsList.Add($subscription2);

    $resourceGroupList.Add($resourceGroup1);

    #Mock functions
    Mock -CommandName Get-AzureRmSubscription { return $subscriptionsList }
    Mock -CommandName Get-AzureRmResourceGroup { return $resourceGroupList }
    Mock -CommandName Set-AzureRmContext -MockWith {};
    Mock -CommandName Get-AppService -MockWith {};
    Mock -CommandName Get-KeyVault -MockWith {};
    Mock -CommandName Join-AppSettingActiveDirectory -MockWith {};
    Mock -CommandName Add-Log -MockWith {};
    Mock -CommandName Write-Progress -MockWith {};
    Mock -CommandName Out-Error -MockWith {};

    Context "When subscriptions are specified, only the specified subscriptions are processed"{
        #specify a subscription
        $subscriptions = @("1234");

        Get-Subscription;

        It "Subscription context is changed only for specified subscription" {
            Assert-MockCalled -CommandName Set-AzureRmContext -ParameterFilter { $Subscription -eq $subscription1.Id } -Times 1 -Exactly;
        }

        It "Retrieves resource group only for specified subscription" {
            Assert-MockCalled -CommandName Get-AzureRmResourceGroup -Times 1 -Exactly;
        }
    }

    Context "When subscriptions are not specified, all available subscriptions are processed" {
        Get-Subscription;

        It "Changes context for all subscriptions" {
            Assert-MockCalled -CommandName Set-AzureRmContext -ParameterFilter { $Subscription -eq $subscription1.Id } -Times 1 -Exactly;
            Assert-MockCalled -CommandName Set-AzureRmContext -ParameterFilter { $Subscription -eq $subscription2.Id } -Times 1 -Exactly;
        }

        It "Retrieves all resource groups for all subscriptions" {
            Assert-MockCalled -CommandName Get-AzureRmResourceGroup -Times 2 -Exactly;
        }
    }

    Context "When sections are specified, only the available sections are processed" {
        #specify section
        $sections = @("kv");

        Get-Subscription;

        It "Processes the specified section" {
            Assert-MockCalled -CommandName Get-KeyVault -Times 2;
        }

        It "Does not process other sections" {
            Assert-MockCalled -CommandName Get-AppService -Times 0 -Exactly;
            Assert-MockCalled -CommandName Join-AppSettingActiveDirectory -Times 0 -Exactly;
        }
    }

    Context "When sections are not specified, all available sections are processed"{
        Get-Subscription;

        It "Processes all sections" {
            Assert-MockCalled -CommandName Get-KeyVault -Times 2;
            Assert-MockCalled -CommandName Get-AppService -Times 2;
            Assert-MockCalled -CommandName Join-AppSettingActiveDirectory -Times 2;
        }
    }
}