Functions/ConvertFrom-GSuiteEndpoint.Tests.ps1

Describe "GSuite/ConvertFrom-GSuiteEndpoint" -Tag "task", "unit" {

    # Import the function to test
    . "$($PSScriptRoot)\ConvertFrom-GSuiteEndpoint.ps1"

    context "when there are no issues" {
        # Declare the endpoint input
        $GSuiteEndpoint = [PSCustomObject]@{
            Name       = "GoogleCredentials"
            Credential = [PSCredential]::new(
                'id',
                ('ClientSecret:secret User:userRefreshToken Group:groupRefreshToken Domain:domainRefreshToken' | ConvertTo-SecureString -AsPlainText -Force)
            )
        }

        # Construct the expected output hash table
        $expectedHashTable = @{
            ApplicationID = "id"
            Group = "groupRefreshToken"
            User = "userRefreshToken"
            Domain = "domainRefreshToken"
            ClientSecret = "secret"
        }

        it "converts the endpoint into a hash table" {
            # Call the function
            $output = ConvertFrom-GSuiteEndpoint -Endpoint $GSuiteEndpoint

            # Verify the output
            foreach ($keyValuePair in $expectedHashTable.GetEnumerator()){
                $output.($keyValuePair.Name) | Should Be $expectedHashTable.($keyValuePair.Name)
            }
        }
    }

    context "when one of the key-value pair in the password is invalid" {
        # Declare the endpoint input
        $GSuiteEndpoint = [PSCustomObject]@{
            Name       = "GoogleCredentials"
            Credential = [PSCredential]::new(
                'id',
                ('ClientSecret:secret invalid Group:groupRefreshToken Domain:domainRefreshToken' | ConvertTo-SecureString -AsPlainText -Force)
            )
        }

        it "outputs an error, and returns null" {
            # Call the function
            $output = ConvertFrom-GSuiteEndpoint -Endpoint $GSuiteEndpoint -ErrorVariable errorVariable 2>$null

            # Verify the output
            $output | Should Be $null
            $errorVariable | Should Not BeNullOrEmpty
        }
    }
}