Functions/Remove-GSuiteGroup.Tests.ps1

Describe "GSuite/Remove-GSuiteGroup" -Tag "task", "unit" {

    # Import the function to test
    . "$($PSScriptRoot)\Remove-GSuiteGroup.ps1"

    # Declare external functions and mocks
    function Assert-GSuiteConnection { }
    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 = @{
            Group = "token"
        }

        it "deletes a GSuite group, and returns true" {
            # Call the function
            $output = Remove-GSuiteGroup -PrimaryEmailAddress "group@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/groups/group@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 group 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-GSuiteGroup -PrimaryEmailAddress "group@domain.com" -ErrorAction SilentlyContinue

            # Verify the mocks
            Assert-MockCalled Invoke-RestMethod -Times 1 -Exactly -ParameterFilter {
                $Uri -eq "https://www.googleapis.com/admin/directory/v1/groups/group@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
        }
    }


    context "when the group access token cannot be found" {
        # Declare mocks
        mock Assert-GSuiteConnection{
            throw "error"
        }

        it "throws an exception" {
            # Call the function
            { Remove-GSuiteGroup -PrimaryEmailAddress "group@domain.com" -ErrorAction SilentlyContinue } | Should -Throw
        }
    }
}