Functions/Connect-ExchangeOnlineAdminAccount.Tests.ps1
describe "BitTitan.Runbooks.ExchangeOnline/Connect-ExchangeOnlineAdminAccount" -Tag "module", "unit" { # Import the function to test . "$($PSScriptRoot)\Connect-ExchangeOnlineAdminAccount.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-ExchangeOnlineAdminAccount -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://outlook.office365.com/powershell-liveid/" -and $Credential.Username -eq "username" -and $Credential.GetNetworkCredential().Password -eq "password" -and $Name -eq "ExchangeOnline" } -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-ExchangeOnlineAdminAccount -Endpoint $endpoint # Verify the mocks Assert-MockCalled New-PSSession -Times 1 -Exactly -ParameterFilter { $ConfigurationName -eq "Microsoft.Exchange" -and $ConnectionUri -eq "https://outlook.office365.com/powershell-liveid/" -and $Credential.Username -eq "username" -and $Credential.GetNetworkCredential().Password -eq "password" -and $Name -eq "ExchangeOnline" } -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-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-ExchangeOnlineAdminAccount -Endpoint $endpoint ` -ErrorAction SilentlyContinue -ErrorVariable errorVariable # Verify the output $errorVariable | Should Not BeNullOrEmpty $output | Should Be $false } } } context "when runspace creation is being throttled" { # Declare mocks mock New-PSSession { throw "Fail to create runspace because you have exceeded your budget to create runspace" } mock Import-PSSession { return New-MockObject -Type PSModuleInfo } mock Import-Module {} mock Start-Sleep {} it "waits and tries again" { # Mock the endpoint $endpoint = [PSCustomObject]@{ Credential = [PSCredential]::new("username", ("password" | ConvertTo-SecureString -AsPlainText -Force)) } # Call the function $numberOfAttempts = 4 $output = Connect-ExchangeOnlineAdminAccount -Endpoint $endpoint -MaximumAttempts $numberOfAttempts # Verify the mocks Assert-MockCalled New-PSSession -Times $numberOfAttempts -Exactly -ParameterFilter { $ConfigurationName -eq "Microsoft.Exchange" -and $ConnectionUri -eq "https://outlook.office365.com/powershell-liveid/" -and $Credential.Username -eq "username" -and $Credential.GetNetworkCredential().Password -eq "password" -and $Name -eq "ExchangeOnline" } -Scope it Assert-MockCalled Import-PSSession -Times 0 -Exactly -Scope it Assert-MockCalled Import-Module -Times 0 -Exactly -Scope it Assert-MockCalled Start-Sleep -Times ($numberOfAttempts - 1) -Exactly -ParameterFilter { $Seconds -eq 60 } -Scope it # Verify the output $output | Should Be $false } } context "when the remote shell has encountered an error" { # Declare mocks mock New-PSSession { throw "failed because the shell was not found on the server." } mock Import-PSSession { return New-MockObject -Type PSModuleInfo } mock Import-Module {} mock Start-Sleep {} it "waits and tries again" { # Mock the endpoint $endpoint = [PSCustomObject]@{ Credential = [PSCredential]::new("username", ("password" | ConvertTo-SecureString -AsPlainText -Force)) } # Call the function $numberOfAttempts = 4 $output = Connect-ExchangeOnlineAdminAccount -Endpoint $endpoint -MaximumAttempts $numberOfAttempts # Verify the mocks Assert-MockCalled New-PSSession -Times $numberOfAttempts -Exactly -ParameterFilter { $ConfigurationName -eq "Microsoft.Exchange" -and $ConnectionUri -eq "https://outlook.office365.com/powershell-liveid/" -and $Credential.Username -eq "username" -and $Credential.GetNetworkCredential().Password -eq "password" -and $Name -eq "ExchangeOnline" } -Scope it Assert-MockCalled Import-PSSession -Times 0 -Exactly -Scope it Assert-MockCalled Import-Module -Times 0 -Exactly -Scope it Assert-MockCalled Start-Sleep -Times ($numberOfAttempts - 1) -Exactly -ParameterFilter { $Seconds -eq 10 } -Scope it # Verify the output $output | Should Be $false } } context "when the remote server outputs any other error" { # Declare mocks mock New-PSSession { Write-Error "Some unknown error message" return New-MockObject -Type System.Management.Automation.Runspaces.PSSession } mock Import-PSSession { return New-MockObject -Type PSModuleInfo } mock Import-Module {} 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-ExchangeOnlineAdminAccount -Endpoint $endpoint -ErrorAction SilentlyContinue ` -ErrorVariable errorVariable # Verify the mocks Assert-MockCalled New-PSSession -Times 1 -Exactly -ParameterFilter { $ConfigurationName -eq "Microsoft.Exchange" -and $ConnectionUri -eq "https://outlook.office365.com/powershell-liveid/" -and $Credential.Username -eq "username" -and $Credential.GetNetworkCredential().Password -eq "password" -and $Name -eq "ExchangeOnline" } -Scope it Assert-MockCalled Import-PSSession -Times 1 -Exactly -Scope it Assert-MockCalled Import-Module -Times 1 -Exactly -Scope it # Verify the output $errorVariable | Should Not BeNullOrEmpty $output | Should Be $false } } } |