Functions/Connect-Office365SecurityAndComplianceAdminAccount.Tests.ps1

describe "BitTitan.Runbooks.Office365SecurityAndCompliance/Connect-Office365SecurityAndComplianceAdminAccount" -Tag "module", "unit" {

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

    context "when there are no issues" {
        # Declare mocks
        mock New-PSSession {
            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-Office365SecurityAndComplianceAdminAccount -Username "username" `
                -Password ("password" | ConvertTo-SecureString -AsPlainText -Force)

            # Verify the mocks
            Assert-MockCalled New-PSSession -Times 1 -Exactly -ParameterFilter {
                $ConfigurationName -eq "Microsoft.Exchange" -and
                $ConnectionUri -eq "https://ps.compliance.protection.outlook.com/powershell-liveid/" -and
                $Credential.Username -eq "username" -and
                $Credential.GetNetworkCredential().Password -eq "password" -and
                $Name -eq "Office365SecurityAndCompliance"
            } -Scope it
            Assert-MockCalled Import-PSSession -Times 1 -Exactly -ParameterFilter {
                $Prefix -eq "SC_"
            } -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-Office365SecurityAndComplianceAdminAccount -Endpoint $endpoint

            # Verify the mocks
            Assert-MockCalled New-PSSession -Times 1 -Exactly -ParameterFilter {
                $ConfigurationName -eq "Microsoft.Exchange" -and
                $ConnectionUri -eq "https://ps.compliance.protection.outlook.com/powershell-liveid/" -and
                $Credential.Username -eq "username" -and
                $Credential.GetNetworkCredential().Password -eq "password" -and
                $Name -eq "Office365SecurityAndCompliance"
            } -Scope it
            Assert-MockCalled Import-PSSession -Times 1 -Exactly -ParameterFilter {
                $Prefix -eq "SC_"
            } -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-PSSession",
        "Import-PSSession",
        "Import-Module"
    )
    foreach ($function in $functionsToThrowExceptions) {
        context "when $($function) throws an exception" {
            # Declare mocks
            mock New-PSSession {
                return New-MockObject -Type System.Management.Automation.Runspaces.PSSession
            }
            mock Import-PSSession {
                return New-MockObject -Type PSModuleInfo
            }

            # 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-Office365SecurityAndComplianceAdminAccount -Endpoint $endpoint `
                    -ErrorAction SilentlyContinue -ErrorVariable errorVariable

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