Functions/Get-SharePointOnlineFileDownloadLink.Tests.ps1

describe "BitTitan.Runbooks.SharePointOnline/Get-SharePointOnlineFileDownloadLink" -Tag "module", "unit" {

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

    # Declare external functions
    function Test-Path {
        return $true
    }
    function Get-Item {
        return [PSCustomObject]@{
            Length = 1111
        }
    }
    function Get-MicrosoftGraphAuthenticationToken {
        param($endpoint)
        return "This is the token!"
    }
    function Invoke-RestMethod {
        param($Uri, $Method, $Headers)
        return [PSCustomObject]@{
            '@microsoft.graph.downloadUrl' = "https://www.google.com/"
        }
    }

    # Declare the function inputs
    $filePath = "/folder/file.extension"
    $endpoint = "endpoint"
    $siteUrl = "organization.sharepoint.com"
    $outputStream = "Warning"

    context "when there are no issues" {
        # Declare mocks
        mock Invoke-RestMethod {
            param($Uri, $Method, $Headers)
            return [PSCustomObject]@{
                '@microsoft.graph.downloadUrl' = "https://www.google.com/"
            }
        }

        it "retrieves the download link" {
            # Call the function
            $output = Get-SharePointOnlineFileDownloadLink -filePath $filePath -Endpoint $endpoint -siteUrl $siteUrl

            # Verify the mocks
            Assert-MockCalled Invoke-RestMethod -Times 1 -Exactly -ParameterFilter {
                $Uri -eq "https://graph.microsoft.com/v1.0/sites/organization.sharepoint.com/drive/root:/folder/file.extension" -and
                $Method -eq "GET" -and
                $Headers.Accept -eq "application/json" -and
                $Headers.Authorization -eq "bearer This is the token!"
            } -Scope it

            # Verify the output
            $output | Should Be "https://www.google.com/"
        }
    }

    context "when the token cannot be retrieved" {
        # Declare mocks
        mock Get-MicrosoftGraphAuthenticationToken {
            param($endpoint)
            return " "
        }

        it "outputs a warning and returns false" {
            # Call the function
            $output = Get-SharePointOnlineFileDownloadLink -filePath $filePath -Endpoint $endpoint -siteUrl $siteUrl -OutputStream $outputStream -WarningVariable warningVariable

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

    context "when the siteUrl is not valid" {
        # Declare the function inputs
        $siteUrl = "siteUrl"

        it "outputs a warning and returns false" {
            # Call the function
            $output = Get-SharePointOnlineFileDownloadLink -filePath $filePath -Endpoint $endpoint -siteUrl $siteUrl -OutputStream $outputStream -WarningVariable warningVariable

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

    context "when the file path does not contain a file name" {
        # Declare the function inputs
        $filePath = "\ "

        it "outputs a warning and returns false" {
            # Call the function
            $output = Get-SharePointOnlineFileDownloadLink -filePath $filePath -Endpoint $endpoint -siteUrl $siteUrl -OutputStream $outputStream -WarningVariable warningVariable

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

    context "when the file does not exist on SharePoint Online" {
        # Declare mocks
        mock Invoke-RestMethod {
            param($Uri, $Method, $Headers)
            throw "404"
        }

        # Declare function input variable
        $filePath = "\folder\file.extension"

        it "outputs a warning and returns null" {
            # Call the function
            $output = Get-SharePointOnlineFileDownloadLink -filePath $filePath -Endpoint $endpoint -siteUrl $siteUrl -OutputStream $outputStream -WarningVariable warningVariable

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

    context "when Invoke-RestMethod does not return anything" {
        # Declare mocks
        mock Invoke-RestMethod {}

        # Declare function input variable
        $filePath = "\folder\file.extension"

        it "outputs a warning and returns null" {
            # Call the function
            $output = Get-SharePointOnlineFileDownloadLink -filePath $filePath -Endpoint $endpoint -siteUrl $siteUrl -OutputStream $outputStream -WarningVariable warningVariable

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