Functions/Get-MicrosoftGraphAuthenticationToken.Tests.ps1

describe "BitTitan.Runbooks.MicrosoftGraph/Get-MicrosoftGraphAuthenticationToken" -Tag "module", "unit" {

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

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

    context "when there are no issues" {
        # Declare mocks
        mock Invoke-RestMethod {
            return @{
                expires_in      = 3600
                access_token    = "token"
            }
        }

        it "retrieves the authentication token given the input values" {
            # Call the function
            $output = Get-MicrosoftGraphAuthenticationToken -ApplicationId "id" -ClientSecret "secret" `
                -OrganizationName "contoso" -ErrorAction SilentlyContinue -ErrorVariable errorVariable

            # Verify the mocks
            Assert-MockCalled Invoke-RestMethod -Times 1 -Exactly -ParameterFilter {
                $Uri -eq "https://login.microsoftonline.com/contoso.onmicrosoft.com/oauth2/v2.0/token" -and
                $ContentType -eq "application/x-www-form-urlencoded" -and
                $Body.client_id -eq "id" -and
                $Body.grant_type -eq "client_credentials" -and
                $Body.client_secret -eq "secret" -and
                $Body.scope -eq "https://graph.microsoft.com/.default" -and
                $Method -eq "POST"
            } -Scope it

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

        it "retrieves the authentication token given the endpoint" {
            # Call the function
            $output = Get-MicrosoftGraphAuthenticationToken -Endpoint @{
                Credential = [PSCredential]::new(
                    "id",
                    ("secret" | ConvertTo-SecureString -AsPlainText -Force)
                )
                ExtendedProperties = @{
                    OrganizationName    = "contoso"
                }
            } -ErrorAction SilentlyContinue -ErrorVariable errorVariable

            # Verify the mocks
            Assert-MockCalled Invoke-RestMethod -Times 1 -Exactly -ParameterFilter {
                $Uri -eq "https://login.microsoftonline.com/contoso.onmicrosoft.com/oauth2/v2.0/token" -and
                $ContentType -eq "application/x-www-form-urlencoded" -and
                $Body.client_id -eq "id" -and
                $Body.grant_type -eq "client_credentials" -and
                $Body.client_secret -eq "secret" -and
                $Body.scope -eq "https://graph.microsoft.com/.default" -and
                $Method -eq "POST"
            } -Scope it

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

    # Declare the endpoint properties to be empty
    context "when OrganizationName is not provided in the endpoint extended properties" {
        # Declare mocks
        mock Invoke-RestMethod {
            return @{
                expires_in      = 3600
                access_token    = "token"
            }
        }

        it "outputs an error" {
            # Declare the endpoint
            $endpoint = @{
                Credential = [PSCredential]::new(
                    "id",
                    ("secret" | ConvertTo-SecureString -AsPlainText -Force)
                )
                ExtendedProperties = @{
                }
            }

            # Call the function
            $output = Get-MicrosoftGraphAuthenticationToken -Endpoint $endpoint `
                -ErrorAction SilentlyContinue -ErrorVariable errorVariable

            # Verify the mocks
            Assert-MockCalled Invoke-RestMethod -Times 0 -Exactly -Scope it

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

    context "when an exception occurs when calling the rest method" {
        # Declare mocks
        mock Invoke-RestMethod {
            throw "up"
        }

        it "outputs an error" {
            # Call the function
            $output = Get-MicrosoftGraphAuthenticationToken -Endpoint @{
                Credential = [PSCredential]::new(
                    "id",
                    ("secret" | ConvertTo-SecureString -AsPlainText -Force)
                )
                ExtendedProperties = @{
                    OrganizationName    = "contoso"
                }
            } -ErrorAction SilentlyContinue -ErrorVariable errorVariable

            # Verify the mocks
            Assert-MockCalled Invoke-RestMethod -Times 1 -Exactly -ParameterFilter {
                $Uri -eq "https://login.microsoftonline.com/contoso.onmicrosoft.com/oauth2/v2.0/token" -and
                $ContentType -eq "application/x-www-form-urlencoded" -and
                $Body.client_id -eq "id" -and
                $Body.grant_type -eq "client_credentials" -and
                $Body.client_secret -eq "secret" -and
                $Body.scope -eq "https://graph.microsoft.com/.default" -and
                $Method -eq "POST"
            } -Scope it

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

    context "when no response is received from the authentication server" {
        # Declare mocks
        mock Invoke-RestMethod {}

        it "outputs an error" {
            # Call the function
            $output = Get-MicrosoftGraphAuthenticationToken -Endpoint @{
                Credential = [PSCredential]::new(
                    "id",
                    ("secret" | ConvertTo-SecureString -AsPlainText -Force)
                )
                ExtendedProperties = @{
                    OrganizationName    = "contoso"
                }
            } -ErrorAction SilentlyContinue -ErrorVariable errorVariable

            # Verify the mocks
            Assert-MockCalled Invoke-RestMethod -Times 1 -Exactly -ParameterFilter {
                $Uri -eq "https://login.microsoftonline.com/contoso.onmicrosoft.com/oauth2/v2.0/token" -and
                $ContentType -eq "application/x-www-form-urlencoded" -and
                $Body.client_id -eq "id" -and
                $Body.grant_type -eq "client_credentials" -and
                $Body.client_secret -eq "secret" -and
                $Body.scope -eq "https://graph.microsoft.com/.default" -and
                $Method -eq "POST"
            } -Scope it

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

    context "when the token expiry time is invalid" {
        # Declare mocks
        mock Invoke-RestMethod {
            return @{
                expires_in = -1
                access_token = "token"
            }
        }

        it "outputs an error" {
            # Call the function
            $output = Get-MicrosoftGraphAuthenticationToken -Endpoint @{
                Credential = [PSCredential]::new(
                    "id",
                    ("secret" | ConvertTo-SecureString -AsPlainText -Force)
                )
                ExtendedProperties = @{
                    OrganizationName    = "contoso"
                }
            } -ErrorAction SilentlyContinue -ErrorVariable errorVariable

            # Verify the mocks
            Assert-MockCalled Invoke-RestMethod -Times 1 -Exactly -ParameterFilter {
                $Uri -eq "https://login.microsoftonline.com/contoso.onmicrosoft.com/oauth2/v2.0/token" -and
                $ContentType -eq "application/x-www-form-urlencoded" -and
                $Body.client_id -eq "id" -and
                $Body.grant_type -eq "client_credentials" -and
                $Body.client_secret -eq "secret" -and
                $Body.scope -eq "https://graph.microsoft.com/.default" -and
                $Method -eq "POST"
            } -Scope it

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

    context "when the token is invalid" {
        # Declare mocks
        mock Invoke-RestMethod {
            return @{
                expires_in = 3600
                access_token = ""
            }
        }

        it "outputs an error" {
            # Call the function
            $output = Get-MicrosoftGraphAuthenticationToken -Endpoint @{
                Credential = [PSCredential]::new(
                    "id",
                    ("secret" | ConvertTo-SecureString -AsPlainText -Force)
                )
                ExtendedProperties = @{
                    OrganizationName    = "contoso"
                }
            } -ErrorAction SilentlyContinue -ErrorVariable errorVariable

            # Verify the mocks
            Assert-MockCalled Invoke-RestMethod -Times 1 -Exactly -ParameterFilter {
                $Uri -eq "https://login.microsoftonline.com/contoso.onmicrosoft.com/oauth2/v2.0/token" -and
                $ContentType -eq "application/x-www-form-urlencoded" -and
                $Body.client_id -eq "id" -and
                $Body.grant_type -eq "client_credentials" -and
                $Body.client_secret -eq "secret" -and
                $Body.scope -eq "https://graph.microsoft.com/.default" -and
                $Method -eq "POST"
            } -Scope it

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