Functions/Get-GSuiteUser.Tests.ps1

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

    # Import the function to test
    . "$($PSScriptRoot)\Get-GSuiteUser.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/users?") -ne -1){
                return @{
                    users = "users"
                }
            }
            return "user"
        }

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

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

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

        it "retrieves the user with the specified domain" {
            # Call the function
            $output = Get-GSuiteUser -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/users?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 "users"
            $errorVariable | Should BeNullOrEmpty
        }

        it "retrieves all the users" {
            # Call the function
            $output = Get-GSuiteUser -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/users?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 "users"
            $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-GSuiteUser -All -ErrorAction SilentlyContinue -ErrorVariable errorVariable } | 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
            { Get-GSuiteUser -All -ErrorAction SilentlyContinue -ErrorVariable errorVariable } | Should -Throw
        }

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