Functions/Upload-FileToSharePointOnline.Tests.ps1

describe "BitTitan.Runbooks.SharePointOnline/Upload-FileToSharePointOnline" -Tag "module", "unit" {

    # Import the function to test
    . "$($PSScriptRoot)\Upload-FileToSharePointOnline.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 Get-SharePointOnlineFileDownloadLink {
        param($filePath, $siteUrl, $token)
        return $null
    }
    function Upload-SmallFileToSharePointOnline {
        param($FilePath, $DestinationFilePath, $siteUrl, $Token)
        return $true
    }
    function Upload-LargeFileToSharePointOnline {
        param($FilePath, $DestinationFilePath, $siteUrl, $Token)
        return $true
    }

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

    context "when there are no issues" {
        # Declare mocks
        mock Invoke-RestMethod {
            param($Uri, $Method, $Headers)
            throw "does not exist"
        }
        mock Get-SharePointOnlineFileDownloadLink {
            param($filePath, $siteUrl, $token)
            return $null
        }

        mock Upload-SmallFileToSharePointOnline {
            param($FilePath, $DestinationFilePath, $siteUrl, $Token)
            return $true
        }
        mock Upload-LargeFileToSharePointOnline {
            param($FilePath, $DestinationFilePath, $siteUrl, $Token)
            return $true
        }

        it "uploads a small file" {
            # Declare mocks
            mock Get-Item {
                return [PSCustomObject]@{
                    Length = 1000
                }
            }

            # Call the function
            $output = Upload-FileToSharePointOnline -filePath $filePath -destinationFilePath $destinationFilePath -Endpoint $endpoint -siteUrl $siteUrl

            # Verify the mocks
            Assert-MockCalled Get-SharePointOnlineFileDownloadLink -Times 1 -Exactly -ParameterFilter {
                $filePath -eq "destination\file.extension" -and
                $siteUrl -eq "site.sharepoint.com" -and
                $token -eq "This is the token!"
            } -Scope it
            Assert-MockCalled Upload-SmallFileToSharePointOnline -Times 1 -Exactly -ParameterFilter {
                $FilePath -eq "folder/file.extension" -and
                $DestinationFilePath -eq "destination\file.extension" -and
                $siteUrl -eq "site.sharepoint.com" -and
                $Token -eq "This is the token!"
            } -Scope it

            # Verify the output
            $output | Should Be $true
        }

        it "uploads a large file" {
            # Declare mocks
            mock Get-Item {
                return [PSCustomObject]@{
                    Length = 60000000
                }
            }

            # Call the function
            $output = Upload-FileToSharePointOnline -filePath $filePath -destinationFilePath $destinationFilePath -Endpoint $endpoint -siteUrl $siteUrl

            # Verify the mocks
            Assert-MockCalled Get-SharePointOnlineFileDownloadLink -Times 1 -Exactly -ParameterFilter {
                $filePath -eq "destination\file.extension" -and
                $siteUrl -eq "site.sharepoint.com" -and
                $token -eq "This is the token!"
            } -Scope it
            Assert-MockCalled Upload-LargeFileToSharePointOnline -Times 1 -Exactly -ParameterFilter {
                $FilePath -eq "folder/file.extension" -and
                $DestinationFilePath -eq "destination\file.extension" -and
                $siteUrl -eq "site.sharepoint.com" -and
                $Token -eq "This is the token!"
            } -Scope it

            # Verify the output
            $output | Should Be $true
        }
    }

    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 = Upload-FileToSharePointOnline -filePath $filePath -destinationFilePath $destinationFilePath -Endpoint $endpoint -siteUrl $siteUrl -OutputStream $outputStream -WarningVariable warningVariable

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

    context "when the file does not exist on the local machine" {
        # Declare mocks
        mock Test-Path {
            return $false
        }

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

            # Verify the output
            $output | Should Be $false
            $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 = Upload-FileToSharePointOnline -filePath $filePath -destinationFilePath $destinationFilePath -Endpoint $endpoint -siteUrl $siteUrl -OutputStream $outputStream -WarningVariable warningVariable

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

    context "when the file already exists on SharePoint Online" {
        # Declare mocks
        mock Get-SharePointOnlineFileDownloadLink {
            param($Uri, $Method, $Headers)
            return $true
        }
        mock Upload-SmallFileToSharePointOnline {
            param($FilePath, $DestinationFilePath, $UserPrincipalName, $Token)
            return $true
        }

        # Declare function input variable
        $destinationFilePath = "/folder/file.extension"

        it "outputs a warning and returns false, when the -Overwrite switch is set to false" {
            # Call the function
            $output = Upload-FileToSharePointOnline -filePath $filePath -destinationFilePath $destinationFilePath -Endpoint $endpoint -siteUrl $siteUrl -Overwrite:$false -OutputStream $outputStream -WarningVariable warningVariable

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

        it "proceeds with the upload if the -Overwrite switch is set, but outputs a warning" {
            # Call the function
            $output = Upload-FileToSharePointOnline -filePath $filePath -destinationFilePath $destinationFilePath -Endpoint $endpoint -SiteUrl $siteUrl -Overwrite -OutputStream $outputStream -WarningVariable warningVariable

            # Verify the mocks
            Assert-MockCalled Get-SharePointOnlineFileDownloadLink -Times 1 -Exactly -ParameterFilter {
                $filePath -eq "folder/file.extension" -and
                $siteUrl -eq "site.sharepoint.com" -and
                $token -eq "This is the token!"
            } -Scope it
            Assert-MockCalled Upload-SmallFileToSharePointOnline -Times 1 -Exactly -ParameterFilter {
                $FilePath -eq "folder/file.extension" -and
                $DestinationFilePath -eq "folder/file.extension" -and
                $SiteUrl -eq "site.sharepoint.com" -and
                $Token -eq "This is the token!"
            } -Scope it

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

    context "when Upload-SmallFileToSharePointOnline returns false" {
        # Declare mocks
        mock Upload-SmallFileToSharePointOnline {
            return $false
        }

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

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

    context "when Upload-LargeFileToSharePointOnline returns false" {
        # Declare mocks
        mock Upload-LargeFileToSharePointOnline {
            return $false
        }
        mock Get-Item {
            return [PSCustomObject]@{
                Length = 60000000
            }
        }

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

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

    context "when the file size is larger than 60 MB" {
        # Declare mocks
        mock Get-Item {
            return [PSCustomObject]@{
                Length = 100000000
            }
        }

        it "returns false" {
            # Call the function
            $output = Upload-FileToSharePointOnline -filePath $filePath -destinationFilePath $destinationFilePath -Endpoint $endpoint -siteUrl $siteUrl -OutputStream $outputStream

            # Verify the output
            $output | Should Be $false
        }
    }
}