Functions/New-GSuiteGroup.Tests.ps1
Describe "GSuite/New-GSuiteGroup" -Tag "task", "unit" { # Import the function to test . "$($PSScriptRoot)\New-GSuiteGroup.ps1" # Declare external functions and mocks function Assert-GSuiteConnection { } function Invoke-RestMethod { param ($Uri, $Headers, $Method, $Body) return "group" } context "when there are no issues" { # Declare mocks Mock Invoke-RestMethod { param ($Uri, $Headers, $Method, $Body) return "group" } # Declared the required global variable $Global:GSuiteAccessTokensHashTable = @{ Group = "token" } it "creates a new GSuite group" { # Call the function $output = New-GSuiteGroup -PrimaryEmailAddress "group@domain.com" -Name "groupName" ` -ErrorAction SilentlyContinue -ErrorVariable errorVariable # Construct the expected request body $NewGroupBody = @{ name = "groupName" email = "group@domain.com" } | ConvertTo-Json # Verify the mocks Assert-MockCalled Invoke-RestMethod -Times 1 -Exactly -ParameterFilter { $Uri -eq "https://www.googleapis.com/admin/directory/v1/groups" -and $Headers.'Content-Type' -eq "application/json" -and $Headers.Accept -eq "application/json" -and $Headers.Authorization -eq "Bearer token" -and $Method -eq "POST" -and $Body -eq $NewGroupBody } -Scope it # Verify the output $output | Should Be "group" $errorVariable | Should BeNullOrEmpty } # Reset the global variable to null $Global:GSuiteAccessTokensHashTable = $null } context "when the group access token cannot be found" { # Declare mocks mock Assert-GSuiteConnection{ throw "error" } it "throws an exception" { # Call the function { New-GSuiteGroup -PrimaryEmailAddress "group@domain.com" -Name "groupName" ` -ErrorAction SilentlyContinue -ErrorVariable errorVariable } | Should -Throw } } } |