Functions/Get-CredentialFromMSPCompleteEndpoint.Tests.ps1

describe "BitTitan.Runbooks.MSPComplete/Get-CredentialFromMSPCompleteEndpoint" -Tag "module", "unit" {

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

    # Declare external functions and mocks
    function Get-MSPCompleteEndpointWithCredential {
        param ($EndpointId)
    }
    mock Get-MSPCompleteEndpointWithCredential {
        return [PSCustomObject]@{
            Credential = [PSCredential]::new("username", ("password" | ConvertTo-SecureString -AsPlainText -Force))
        }
    }

    it "returns the credential property when provided an endpoint with a credential" {
        # Declare the function inputs
        $endpoint = [PSCustomObject]@{
            Name        = "Mock endpoint"
            Id          = [Guid]::Empty.Guid
            Credential  = [PSCredential]::new("username", ("password" | ConvertTo-SecureString -AsPlainText -Force))
        }

        # Call the function
        $output = Get-CredentialFromMSPCompleteEndpoint -Endpoint $endpoint

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

        # Verify the outputs
        $output.Username | Should Be "username"
        $output.GetNetworkCredential().Password | Should Be "password"
    }

    it "returns the credential property from a newly retrieved endpoint when provided an endpoint without a credential" {
        # Declare the function inputs
        $endpoint = [PSCustomObject]@{
            Name        = "Mock endpoint"
            Id          = [Guid]::Empty.Guid
        }

        # Call the function
        $output = Get-CredentialFromMSPCompleteEndpoint -Endpoint $endpoint -Ticket "ticket"

        # Verify the mocks
        Assert-MockCalled Get-MSPCompleteEndpointWithCredential -Times 1 -Exactly -ParameterFilter {
            $EndpointId -eq [Guid]::Empty.Guid
        } -Scope it

        # Verify the outputs
        $output.Username | Should Be "username"
        $output.GetNetworkCredential().Password | Should Be "password"
    }
}