Functions/Add-MembersToMSPCompleteGroupObject.Tests.ps1

describe "MSPComplete/Add-MembersToMSPCompleteGroupObject" -Tag "module", "unit" {

    # Set the BT runbook environment
    Set-BT_RunbookEnvironment -Environment Beta -IsRunningOnLocalMachine $true -IsTestEnvironment $true

    # Import the function under test
    . "$($PSScriptRoot)/Add-MembersToMSPCompleteGroupObject.ps1"

    # Declare external functions and mocks
    function Get-BT_GroupMembership {
        param ($Ticket, $GroupId, $IsDeleted, [Switch]$RetrieveAll)
        return [PSCustomObject]@{
            EndUserId = "0"
        }
    }
    function Get-BT_CustomerEndUser {
        param ($Ticket, $Id)
        return [PSCustomObject]@{
            PrimaryEmailAddress = "member1@domain.com"
        }
    }

    context "when there are no issues" {

        it "adds a 'Members' property to a group object" {
            # Declare the function inputs
            $group = [PSCustomObject]@{
                Name = "name"
                Id   = [Guid]::Empty.Guid
            }

            # Call the function
            $output = Add-MembersToMSPCompleteGroupObject -Group $group

            # Verify the output
            $output.Members[0] | Should Be "member1@domain.com"
        }
    }

    context "when an exception is thrown while retrieving the members" {
        # Declare mocks
        mock Get-BT_GroupMembership {
            throw "up"
        }
        mock Write-OutputMessage {}

        it "outputs an error" {
            # Declare the function inputs
            $group = [PSCustomObject]@{
                Name = "name"
                Id   = [Guid]::Empty.Guid
            }

            # Call the function
            $output = Add-MembersToMSPCompleteGroupObject -Group $group

            # Verify the mocks
            Assert-MockCalled Write-OutputMessage -Times 1 -Exactly -ParameterFilter {
                $OutputStream -eq "Error"
            } -Scope it

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