Functions/Block-UntilMSOnlineUserAvailable.Tests.ps1

describe "BitTitan.Runbooks.MSOnline/Block-UntilMSOnlineUserAvailable" -Tag "module", "unit" {

    # Import the function to test
    . "$($PSScriptRoot)\Block-UntilMSOnlineUserAvailable.ps1"

    # Declare our own Get-MsolUser
    # If we don't do this the mock will not work
    function Get-MsolUser {
        param ($SearchString)
    }

    # Declare our own Start-Sleep so that the function doesn't delay
    function Start-Sleep {
        param ($Seconds)
    }

    context "when the user is immediately available" {
        # Mock Get-MsolUser
        mock Get-MsolUser {
            return [PSCustomObject]@{
                UserPrincipalName = "user@domain.com"
            }
        }

        # Mock Start-Sleep
        mock Start-Sleep {}

        it "returns the user after trying once" {
            # Call the function
            $output = Block-UntilMSOnlineUserAvailable -UserPrincipalName "user@domain.com"

            # Verify the mocks
            Assert-MockCalled Get-MsolUser -Times 1 -Exactly -ParameterFilter {
                $SearchString -eq "user@domain.com"
            } -Scope it
            Assert-MockCalled Start-Sleep -Times 0 -Exactly -Scope it

            # Verify the output
            $output.UserPrincipalName | Should Be "user@domain.com"
        }
    }

    context "when the user is available after 10 tries" {
        # Mock Get-MsolUser
        $Global:numTries = 0
        mock Get-MsolUser {
            ++$Global:numTries
            if ($Global:numTries -ge 10) {
                return [PSCustomObject]@{
                    UserPrincipalName = "user@domain.com"
                }
            }
        }

        # Mock Start-Sleep
        mock Start-Sleep {}

        it "returns the user after trying 10 times" {
            # Call the function
            $output = Block-UntilMSOnlineUserAvailable -UserPrincipalName "user@domain.com" -WaitLimit 20

            # Verify the mocks
            Assert-MockCalled Get-MsolUser -Times 10 -Exactly -ParameterFilter {
                $SearchString -eq "user@domain.com"
            } -Scope it
            Assert-MockCalled Start-Sleep -Times 9 -Exactly -Scope it

            # Verify the output
            $output.UserPrincipalName | Should Be "user@domain.com"
        }
    }

    context "when the user is not available" {
        # Mock Get-MsolUser
        mock Get-MsolUser {}

        # Mock Start-Sleep
        mock Start-Sleep {}

        it "returns null after trying the maximum number of times and outputs an error" {
            # Call the function
            $output = Block-UntilMSOnlineUserAvailable -UserPrincipalName "user@domain.com" -WaitLimit 20 `
                -ErrorAction SilentlyContinue -ErrorVariable errorVariable

            # Verify the mocks
            Assert-MockCalled Get-MsolUser -Times 20 -Exactly -ParameterFilter {
                $SearchString -eq "user@domain.com"
            } -Scope it
            Assert-MockCalled Start-Sleep -Times 20 -Exactly -Scope it

            # Verify the output
            $errorVariable | Should Not BeNullOrEmpty
            $output | Should Be $null
        }
    }

}