Functions/Add-ManagerEmailAddressProperty.Tests.ps1

describe "BitTitan.Runbooks.ExchangeOnline/Add-ManagerEmailAddressProperty" -Tag "module", "unit" {

    # Import the function to test
    . "$($PSScriptRoot)\Add-ManagerEmailAddressProperty.ps1"

    # Declare our own Get-User
    function Get-User {
        param ($Identity)
    }

    context "when the user has a manager" {
        # Mock Get-User
        mock Get-User {
            return [PSCustomObject]@{
                UserPrincipalName = "manager@domain.com"
            }
        }

        it "returns the user object with the manager email address" {
            # Mock the user object
            $user = [PSCustomObject]@{
                Manager = "Manager Name"
            }

            # Call the function
            $output = Add-ManagerEmailAddressProperty -User $user

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

            # Verify the output
            $output.ManagerEmailAddress | Should Be "manager@domain.com"
        }
    }

    context "when the user does not have a manager" {
        # Mock Get-User
        mock Get-User {
            return [PSCustomObject]@{
                UserPrincipalName = "manager@domain.com"
            }
        }

        it "returns the user object without the manager email address" {
            # Mock the user object
            $user = [PSCustomObject]@{}

            # Call the function
            $output = Add-ManagerEmailAddressProperty -User $user

            # Verify the mocks
            Assert-MockCalled Get-User -Times 0 -Exactly -Scope it

            # Verify the output
            $output.ManagerEmailAddress | Should Be $null
        }
    }
}