Functions/Remove-GSuiteUser.Tests.ps1
Describe "GSuite/Remove-GSuiteUser" -Tag "task", "unit" { # Import the function to test . "$($PSScriptRoot)\Remove-GSuiteUser.ps1" # Declare external functions and mocks function Invoke-RestMethod { param ($Uri, $Headers, $Method) return "" } context "when there are no issues" { # Declare mocks Mock Invoke-RestMethod { param ($Uri, $Headers, $Method) return "" } # Declared the required global variable $Global:GSuiteAccessTokensHashTable = @{ User = "token" } it "deletes a GSuite user, and returns true" { # Call the function $output = Remove-GSuiteUser -PrimaryEmailAddress "user@domain.com" -ErrorAction SilentlyContinue -ErrorVariable errorVariable # Verify the mocks Assert-MockCalled Invoke-RestMethod -Times 1 -Exactly -ParameterFilter { $Uri -eq "https://www.googleapis.com/admin/directory/v1/users/user@domain.com" -and $Headers.Accept -eq "application/json" -and $Headers.Authorization -eq "Bearer token" -and $Method -eq "DELETE" } -Scope it # Verify the output $output | Should Be $true $errorVariable | Should BeNullOrEmpty } } context "the user is not deleted" { # Declare mocks Mock Invoke-RestMethod { param ($Uri, $Headers, $Method) return "Anything other than an empty string" } it "returns false" { # Call the function $output = Remove-GSuiteUser -PrimaryEmailAddress "user@domain.com" -ErrorAction SilentlyContinue # Verify the mocks Assert-MockCalled Invoke-RestMethod -Times 1 -Exactly -ParameterFilter { $Uri -eq "https://www.googleapis.com/admin/directory/v1/users/user@domain.com" -and $Headers.Accept -eq "application/json" -and $Headers.Authorization -eq "Bearer token" -and $Method -eq "DELETE" } -Scope it # Verify the output $output | Should Be $false } # Reset the global variable to null $Global:GSuiteAccessTokensHashTable = $null } context "when `$Global:GSuiteAccessTokensHashTable is null" { it "throws an exception" { # Call the function { Remove-GSuiteUser -PrimaryEmailAddress "user@domain.com" -ErrorAction SilentlyContinue } | Should -Throw } } context "when `$Global:GSuiteAccessTokensHashTable does not contain the user access token" { # Declared the required global variable $Global:GSuiteAccessTokensHashTable = @{ } it "throws an exception" { # Call the function { Remove-GSuiteUser -PrimaryEmailAddress "user@domain.com" -ErrorAction SilentlyContinue } | Should -Throw } # Reset the global variable to null $Global:GSuiteAccessTokensHashTable = $null } } |