Functions/Connect-Office365SecurityAndComplianceAdminAccount.Tests.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
describe "BitTitan.Runbooks.Office365SecurityAndCompliance/Connect-Office365SecurityAndComplianceAdminAccount" -Tag "module", "unit" { # Import the function to test . "$($PSScriptRoot)\Connect-Office365SecurityAndComplianceAdminAccount.ps1" # Declare our own Get-CredentialFromMSPCompleteEndpoint # If we don't do this the mock will not work function Get-CredentialFromMSPCompleteEndpoint { param ($endpoint) } context "when there are no issues" { # Mock Get-CredentialFromMSPCompleteEndpoint mock Get-CredentialFromMSPCompleteEndpoint { return [PSCredential]::new("username", ("password" | ConvertTo-SecureString -AsPlainText -Force)) } # Mock New-PSSession mock New-PSSession { return New-MockObject -Type System.Management.Automation.Runspaces.PSSession } # Mock Import-PSSession mock Import-PSSession { return New-MockObject -Type PSModuleInfo } # Mock Import-Module 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 Get-CredentialFromMSPCompleteEndpoint -Times 0 -Exactly -Scope it 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 = "Mock endpoint" # Call the function $output = Connect-Office365SecurityAndComplianceAdminAccount -Endpoint $endpoint # Verify the mocks Assert-MockCalled Get-CredentialFromMSPCompleteEndpoint -Times 1 -Exactly -ParameterFilter { $Endpoint -eq "Mock endpoint" } -Scope it 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" { # Mock Get-CredentialFromMSPCompleteEndpoint mock Get-CredentialFromMSPCompleteEndpoint { return [PSCredential]::new("username", ("password" | ConvertTo-SecureString -AsPlainText -Force)) } # Mock New-PSSession mock New-PSSession { return New-MockObject -Type System.Management.Automation.Runspaces.PSSession } # Mock Import-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 = "Mock endpoint" # Call the function $output = Connect-Office365SecurityAndComplianceAdminAccount -Endpoint $endpoint ` -ErrorAction SilentlyContinue -ErrorVariable errorVariable # Verify the output $errorVariable | Should Not BeNullOrEmpty $output | Should Be $false } } } } |