Functions/Get-GSuiteGroup.Tests.ps1

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

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

    # Declare external functions and mocks
    function Invoke-RestMethod {
        param ($Uri, $Headers, $Method)
    }

    context "when there are no issues" {
        # Declare mocks
        mock Invoke-RestMethod {
            param ($Uri, $Headers, $Method)
            if($Uri.IndexOf("https://www.googleapis.com/admin/directory/v1/groups?") -ne -1){
                return @{
                    groups = "groups"
                }
            }
            return "group"
        }

        # Declared the required global variable
        $Global:GSuiteAccessTokensHashTable = @{
            Group = "token"
        }

        it "retrieves the group with the specified email address" {
            # Call the function
            $output = Get-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 "GET"
            } -Scope it

            # Verify the output
            $output | Should Be "group"
            $errorVariable | Should BeNullOrEmpty
        }

        it "retrieves the groups with the specified domain" {
            # Call the function
            $output = Get-GSuiteGroup -Domain "domain" -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?domain=domain" -and
                $Headers.'Accept' -eq "application/json" -and
                $Headers.'Authorization' -eq "Bearer token" -and
                $Method -eq "GET"
            } -Scope it

            # Verify the output
            $output | Should Be "groups"
            $errorVariable | Should BeNullOrEmpty
        }

        it "retrieves all the groups" {
            # Call the function
            $output = Get-GSuiteGroup -All -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?customer=my_customer" -and
                $Headers.'Accept' -eq "application/json" -and
                $Headers.'Authorization' -eq "Bearer token" -and
                $Method -eq "GET"
            } -Scope it

            # Verify the output
            $output | Should Be "groups"
            $errorVariable | Should BeNullOrEmpty
        }

        # Reset the global variable to null
        $Global:GSuiteAccessTokensHashTable = $null
    }

    context "when `$Global:GSuiteAccessTokensHashTable is null" {

        it "throws an exception" {
            # Call the function
            { Get-GSuiteGroup -All -ErrorAction SilentlyContinue -ErrorVariable errorVariable } | Should -Throw
        }
    }

    context "when `$Global:GSuiteAccessTokensHashTable does not contain the group access token" {
        # Declared the required global variable
        $Global:GSuiteAccessTokensHashTable = @{ }

        it "throws an exception" {
            # Call the function
            { Get-GSuiteGroup -All -ErrorAction SilentlyContinue -ErrorVariable errorVariable } | Should -Throw
        }

        # Reset the global variable to null
        $Global:GSuiteAccessTokensHashTable = $null
    }
}