Functions/Connect-SkypeForBusinessOnlineAdminAccount.Tests.ps1

describe "BitTitan.Runbooks.SkypeForBusinessOnline/Connect-SkypeForBusinessOnlineAdminAccount" -Tag "module", "unit" {

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

    # Declare external functions and mocks
    function New-CSOnlineSession {
        param ($credential)
    }

    context "when there are no issues" {
        # Declare mocks
        mock New-CSOnlineSession {
            return New-MockObject -Type System.Management.Automation.Runspaces.PSSession
        }
        mock Import-PSSession {
            return New-MockObject -Type PSModuleInfo
        }
        mock Import-Module {}

        it "connects using the username and password" {
            # Call the function
            $output = Connect-SkypeForBusinessOnlineAdminAccount -Username "username" `
                -Password ("password" | ConvertTo-SecureString -AsPlainText -Force)

            # Verify the mocks
            Assert-MockCalled New-CSOnlineSession -Times 1 -Exactly -ParameterFilter {
                $Credential.Username -eq "username" -and
                $Credential.GetNetworkCredential().Password -eq "password"
            } -Scope it
            Assert-MockCalled Import-PSSession -Times 1 -Exactly -Scope it
            Assert-MockCalled Import-Module -Times 1 -Exactly -Scope it

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

        it "connects using an endpoint" {
            # Mock the endpoint
            $endpoint = [PSCustomObject]@{
                Credential = [PSCredential]::new("username", ("password" | ConvertTo-SecureString -AsPlainText -Force))
            }

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

            # Verify the mocks
            Assert-MockCalled New-CSOnlineSession -Times 1 -Exactly -ParameterFilter {
                $Credential.Username -eq "username" -and
                $Credential.GetNetworkCredential().Password -eq "password"
            } -Scope it
            Assert-MockCalled Import-PSSession -Times 1 -Exactly -Scope it
            Assert-MockCalled Import-Module -Times 1 -Exactly -Scope it

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

    # Declare the functions to throw exceptions
    $functionsToThrowExceptions = @(
        "New-CSOnlineSession",
        "Import-PSSession",
        "Import-Module"
    )
    foreach ($function in $functionsToThrowExceptions) {
        context "when $($function) throws an exception" {
            # Declare mocks
            mock New-CSOnlineSession {
                return New-MockObject -Type System.Management.Automation.Runspaces.PSSession
            }
            mock Import-PSSession {
                return New-MockObject -Type PSModuleInfo
            }
            mock Import-Module {}

            # Mock the function to throw an exception
            mock $function {
                throw "throws exception"
            }

            it "fails to connect and outputs an error message" {
                # Mock the endpoint
                $endpoint = [PSCustomObject]@{
                    Credential = [PSCredential]::new("username", ("password" | ConvertTo-SecureString -AsPlainText -Force))
                }

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

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