Functions/Get-AllExchangeOnlineUsers.Tests.ps1

describe "BitTitan.Runbooks.ExchangeOnline/Get-AllExchangeOnlineUsers" -Tag "module", "unit" {

    # Import the function to test
    . "$($PSScriptRoot)\Get-AllExchangeOnlineUsers.ps1"

    # Declare external functions and mocks
    function Get-BT_RunbookEnvironment {}
    function Get-Office365TestEnvironmentAvailableUsers {
        param ($Domain, [Switch]$ExchangeOnline)
    }
    function Get-User {
        param ($ResultSize)
    }
    $Global:Office365TestRunID = "office365TestRunID"

    context "when the environment is set to testing" {
        # Declare mocks
        mock Get-BT_RunbookEnvironment {
            return @{
                IsTestEnvironment = $true
            }
        }
        mock Get-Office365TestEnvironmentAvailableUsers {}
        $Global:ExchangeOnlineUsername = "admin@domain.com"

        it "returns the test environment users" {
            # Call the function
            Get-AllExchangeOnlineUsers

            # Verify the mocks
            Assert-MockCalled Get-Office365TestEnvironmentAvailableUsers -Times 1 -Exactly -ParameterFilter {
                $Office365TestRunID -eq "office365TestRunID" -and $ExchangeOnline
            } -Scope it
        }
    }

    context "when the environment is not set to testing" {
        # Declare mocks
        mock Get-BT_RunbookEnvironment {
            return @{
                IsTestEnvironment = $false
            }
        }
        mock Get-User {}
        $Global:ExchangeOnlineUsername = "admin@domain.com"

        it "returns the Exchange Online users" {
            # Call the function
            Get-AllExchangeOnlineUsers

            # Verify the mocks
            Assert-MockCalled Get-User -Times 1 -Exactly -ParameterFilter {
                $ResultSize -eq "unlimited"
            } -Scope it
        }
    }
}