PrivateFunctions/Upload-LargeFileToOneDrive.Tests.ps1

describe "BitTitan.Runbooks.OneDrive/Upload-LargeFileToOneDrive" -Tag "module", "unit" {

    # Import the function to test
    . "$($PSScriptRoot)\Upload-LargeFileToOneDrive.ps1"

    # Declare external functions
    function Invoke-RestMethod {
        param($Uri, $Method, $Body, $Headers)
        return [PSCustomObject]@{
            ID = 'id'
            uploadUrl = "url"
        }
    }

    # Declare the function inputs
    $filePath = "$($PSScriptRoot)\file.extension"
    $destination = "hello/world.exe"
    $token = "This is the token!"
    $userPrincipalName = "userPrincipalName@domain.com"
    $outputStream = "Warning"

    # Create the test file
    New-Item -Path $filePath -ItemType "file" -Value "This is a text string."

    context "when there are no issues" {
        # Declare mocks
        mock Invoke-RestMethod {
            param($Uri, $Method, $Body, $Headers)
            return [PSCustomObject]@{
                ID = 'id'
                uploadUrl = "url"
            }
        }

        it "uploads a large file to OneDrive" {
            # Call the function
            $output = Upload-LargeFileToOneDrive -filePath $filePath -destinationFilePath $destination -Token $token -userPrincipalName $userPrincipalName

            # Verify the mocks
            Assert-MockCalled Invoke-RestMethod -Times 1 -Exactly -ParameterFilter {
                $Uri -eq "https://graph.microsoft.com/v1.0/users/userPrincipalName@domain.com/drive/root:/hello/world.exe:/createUploadSession" -and
                $Method -eq "POST" -and
                $Headers.Accept -eq "application/json" -and
                $Headers.Authorization -eq "bearer This is the token!"
            } -Scope it
            Assert-MockCalled Invoke-RestMethod -Times 1 -Exactly -ParameterFilter {
                $Uri -eq "url" -and
                $Method -eq "PUT"
            } -Scope it

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

    context "when Invoke-RestMethod throws an exception while retrieving the upload link" {
        # Declare mocks
        mock Invoke-RestMethod{
            throw "error"
        }

        it "outputs a warning and returns false" {
            # Call the function
            $output = Upload-LargeFileToOneDrive -filePath $filePath -destinationFilePath $destination -Token $token -userPrincipalName $userPrincipalName -OutputStream $outputStream -WarningVariable warningVariable

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

    context "when Invoke-RestMethod throws an exception while uploading the file" {
        # Declare mocks
        mock Invoke-RestMethod{
            param($Uri, $Method, $Body)
            if($Method -eq "PUT"){
                throw "error"
            }
            return [PSCustomObject]@{
                ID = 'id'
                uploadUrl = "url"
            }
        }

        it "outputs a warning and returns false" {
            # Call the function
            $output = Upload-LargeFileToOneDrive -filePath $filePath -destinationFilePath $destination -Token $token -userPrincipalName $userPrincipalName -OutputStream $outputStream -WarningVariable warningVariable

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

    context "when Invoke-RestMethod does not return the upload url" {
        # Declare mocks
        mock Invoke-RestMethod{
            return [PSCustomObject]@{
                ID = 'id'
            }
        }

        it "outputs a warning and returns false" {
            # Call the function
            $output = Upload-LargeFileToOneDrive -filePath $filePath -destinationFilePath $destination -Token $token -userPrincipalName $userPrincipalName -OutputStream $outputStream -WarningVariable warningVariable

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

    context "when Invoke-RestMethod does not return the id" {
        # Declare mocks
        mock Invoke-RestMethod{
            return [PSCustomObject]@{
                uploadUrl = "url"
            }
        }

        it "outputs a warning and returns false" {
            # Call the function
            $output = Upload-LargeFileToOneDrive -filePath $filePath -destinationFilePath $destination -Token $token -userPrincipalName $userPrincipalName -OutputStream $outputStream -WarningVariable warningVariable

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

    # Remove the test file
    Remove-Item -Path $filePath
}