Functions/Connect-PartnerCenterAdminAccount.Tests.ps1

describe "BitTitan.Runbooks.PartnerCenter/Connect-PartnerCenterAdminAccount" -Tags "module", "unit" {

    # Import the function to test
    . "$($PSScriptRoot)\Connect-PartnerCenterAdminAccount.ps1"

    # Declare our own Get-CredentialFromMSPCompleteEndpoint
    # If we don't do this the mock will not work
    function Get-CredentialFromMSPCompleteEndpoint {
        param ($endpoint)
        return [PSCredential]::new("username", ("password" | ConvertTo-SecureString -AsPlainText -Force))
    }

    # Declare our own Connect-PartnerCenter
    # If we don't do this the mock will not work
    function Connect-PartnerCenter {
        param ($ApplicationId, [PSCredential]$Credential)
    }

    context "when there are no issues" {
        # Mock Get-CredentialFromMSPCompleteEndpoint
        mock Get-CredentialFromMSPCompleteEndpoint {
            return [PSCredential]::new("username", ("password" | ConvertTo-SecureString -AsPlainText -Force))
        }

        # Mock Connect-PartnerCenter
        mock Connect-PartnerCenter {}

        it "connects to Partner Center with the provided username and password" {
            # Call the function
            $output = Connect-PartnerCenterAdminAccount -Username "username" -Password ("password" | ConvertTo-SecureString -AsPlainText -Force) -ApplicationId ([Guid]::empty).Guid

            # Verify the mocks
            Assert-MockCalled Get-CredentialFromMSPCompleteEndpoint -Times 0 -Exactly -Scope it
            Assert-MockCalled Connect-PartnerCenter -Times 1 -Exactly -ParameterFilter {
                $Credential.Username -eq "username" -and $Credential.GetNetworkCredential().Password -eq "password" -and $ApplicationId -eq ([Guid]::empty).Guid
            } -Scope it

            # Verify the output
            $output | Should Be $true
        }

        it "connects to Partner Center with the provided endpoint" {
            # Mock the endpoint
            $endpoint = [PSCustomObject]@{
                Name                = "Stub endpoint"
                ExtendedProperties  = @{
                    ApplicationId = ([Guid]::empty).Guid
                }
            }

            # Call the function
            $output = Connect-PartnerCenterAdminAccount -Endpoint $endpoint

            # Verify the mocks
            Assert-MockCalled Get-CredentialFromMSPCompleteEndpoint -Times 1 -Exactly -ParameterFilter {
                $Endpoint.Name -eq "Stub endpoint"
            } -Scope it
            Assert-MockCalled Connect-PartnerCenter -Times 1 -Exactly -ParameterFilter {
                $Credential.Username -eq "username" -and $Credential.GetNetworkCredential().Password -eq "password" -and $ApplicationId -eq ([Guid]::empty).Guid
            } -Scope it

            # Verify the output
            $output | Should Be $true
        }
    }

    context "when there is an exception while connecting to Partner Center" {
        # Mock Get-CredentialFromMSPCompleteEndpoint
        mock Get-CredentialFromMSPCompleteEndpoint {
            return [PSCredential]::new("username", ("password" | ConvertTo-SecureString -AsPlainText -Force))
        }

        # Mock Connect-PartnerCenter
        mock Connect-PartnerCenter {
            throw "throws exception"
        }

        it "fails to connect to Partner Center and outputs an error message" {
            # Call the function
            $output = Connect-PartnerCenterAdminAccount -Username "username" -Password ("password" | ConvertTo-SecureString -AsPlainText -Force) `
                -ApplicationId ([Guid]::empty).Guid -ErrorAction SilentlyContinue -ErrorVariable errorVariable

            # Verify the mocks
            Assert-MockCalled Get-CredentialFromMSPCompleteEndpoint -Times 0 -Exactly -Scope it
            Assert-MockCalled Connect-PartnerCenter -Times 1 -Exactly -ParameterFilter {
                $Credential.Username -eq "username" -and $Credential.GetNetworkCredential().Password -eq "password" -and $ApplicationId -eq ([Guid]::empty).Guid
            } -Scope it

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

    context "when the endpoint does not contain an 'ExtendedProperties' property" {
        # Mock Get-CredentialFromMSPCompleteEndpoint
        mock Get-CredentialFromMSPCompleteEndpoint {
            return [PSCredential]::new("username", ("password" | ConvertTo-SecureString -AsPlainText -Force))
        }

        # Mock Connect-PartnerCenter
        mock Connect-PartnerCenter {}

        it "fails to connect to Partner Center and outputs an error message" {
            # Mock the endpoint
            $endpoint = [PSCustomObject]@{
                Name                = "Stub endpoint"
            }

            # Call the function
            $output = Connect-PartnerCenterAdminAccount -Endpoint $endpoint `
                -ErrorAction SilentlyContinue -ErrorVariable errorVariable

            # Verify the mocks
            Assert-MockCalled Get-CredentialFromMSPCompleteEndpoint -Times 1 -Exactly -Scope it
            Assert-MockCalled Connect-PartnerCenter -Times 0 -Exactly -Scope it

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

    context "when the endpoint does not contain an 'ApplicationId' extended property" {
        # Mock Get-CredentialFromMSPCompleteEndpoint
        mock Get-CredentialFromMSPCompleteEndpoint {
            return [PSCredential]::new("username", ("password" | ConvertTo-SecureString -AsPlainText -Force))
        }

        # Mock Connect-PartnerCenter
        mock Connect-PartnerCenter {}

        it "fails to connect to Partner Center and outputs an error message" {
            # Mock the endpoint
            $endpoint = [PSCustomObject]@{
                Name                = "Stub endpoint"
                ExtendedProperties  = @{}
            }

            # Call the function
            $output = Connect-PartnerCenterAdminAccount -Endpoint $endpoint `
                -ErrorAction SilentlyContinue -ErrorVariable errorVariable

            # Verify the mocks
            Assert-MockCalled Get-CredentialFromMSPCompleteEndpoint -Times 1 -Exactly -Scope it
            Assert-MockCalled Connect-PartnerCenter -Times 0 -Exactly -Scope it

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