Functions/Connect-MicrosoftTeamsAdminAccount.Tests.ps1
describe "BitTitan.Runbooks.MicrosoftTeams/Connect-MicrosoftTeamsAdminAccount" -Tags "module", "unit" { # Import the function to test . "$($PSScriptRoot)\Connect-MicrosoftTeamsAdminAccount.ps1" # Declare our own Get-CredentialFromMSPCompleteEndpoint # If we don't do this the mock will not work function Get-CredentialFromMSPCompleteEndpoint { param ($Endpoint) return $Endpoint.Credential } # Declare our own Connect-MicrosoftTeams # If we don't do this the mock will not work function Connect-MicrosoftTeams { param ([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-MicrosoftTeams mock Connect-MicrosoftTeams {} it "connects to Microsoft Teams with the provided username and password" { # Call the function $output = Connect-MicrosoftTeamsAdminAccount -Username "username" -Password ("password" | ConvertTo-SecureString -AsPlainText -Force) # Verify the mocks Assert-MockCalled Get-CredentialFromMSPCompleteEndpoint -Times 0 -Exactly -Scope it Assert-MockCalled Connect-MicrosoftTeams -Times 1 -Exactly -ParameterFilter { $Credential.Username -eq "username" -and $Credential.GetNetworkCredential().Password -eq "password" } -Scope it # Verify the output $output | Should Be $true } it "connects to Microsoft Teams with the provided endpoint" { # Mock the endpoint $endpoint = @{ Name = "Unit test mocked endpoint" Credential = @{ Username = "username" Password = "password" | ConvertTo-SecureString -AsPlainText -Force } } # Call the function $output = Connect-MicrosoftTeamsAdminAccount -Endpoint $endpoint # Verify the mocks Assert-MockCalled Get-CredentialFromMSPCompleteEndpoint -Times 1 -Exactly -ParameterFilter { $Endpoint.Name -eq "Unit test mocked endpoint" } -Scope it Assert-MockCalled Connect-MicrosoftTeams -Times 1 -Exactly -ParameterFilter { $Credential.Username -eq "username" -and $Credential.GetNetworkCredential().Password -eq "password" } -Scope it # Verify the output $output | Should Be $true } } context "when there is an exception while connecting to Microsoft Teams" { # Mock Get-CredentialFromMSPCompleteEndpoint mock Get-CredentialFromMSPCompleteEndpoint { return [PSCredential]::new("username", ("password" | ConvertTo-SecureString -AsPlainText -Force)) } # Mock Connect-MicrosoftTeams mock Connect-MicrosoftTeams { throw "throws exception" } it "fails to connect to Microsoft Teams and outputs an error message" { # Call the function $output = Connect-MicrosoftTeamsAdminAccount -Username "username" -Password ("password" | ConvertTo-SecureString -AsPlainText -Force) ` -ErrorAction SilentlyContinue -ErrorVariable errorVariable # Verify the mocks Assert-MockCalled Get-CredentialFromMSPCompleteEndpoint -Times 0 -Exactly -Scope it Assert-MockCalled Connect-MicrosoftTeams -Times 1 -Exactly -ParameterFilter { $Credential.Username -eq "username" -and $Credential.GetNetworkCredential().Password -eq "password" } -Scope it # Verify the output $errorVariable | Should Not BeNullOrEmpty $output | Should Be $false } } } |