Functions/Remove-FileFromOneDrive.Tests.ps1

describe "BitTitan.Runbooks.OneDrive/Remove-FileFromOneDrive" -Tag "module", "unit" {

    # Import the function to test
    . "$($PSScriptRoot)\Remove-FileFromOneDrive.ps1"

    # Declare external functions
    function Get-MicrosoftGraphAuthenticationToken {
        param($endpoint)
        return "This is the token!"
    }
    function Invoke-RestMethod {
        param($Uri, $Method, $Headers)
        return "`r`n"
    }
    function Get-FileFromUrl {
        param($FileUrl, $DestinationPath)
        return $true
    }

    # Declare the function inputs
    $filePath = "folder/file.extension"
    $endpoint = "endpoint"
    $userPrincipalName = "userPrincipalName@domain.com"
    $outputStream = "Warning"

    context "when there are no issues" {
        # Declare mocks
        mock Invoke-RestMethod {
            param($Uri, $Method, $Headers)
            return ""
        }

        it "deletes a file from OneDrive" {
            # Call the function
            $output = Remove-FileFromOneDrive -filePath $filePath -Endpoint $endpoint -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:/folder/file.extension" -and
                $Method -eq "DELETE" -and
                $Headers.Accept -eq "application/json" -and
                $Headers.Authorization -eq "bearer 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 return false" {
            # Call the function
            $output = Remove-FileFromOneDrive -filePath $filePath -Endpoint $endpoint -userPrincipalName $userPrincipalName -OutputStream $outputStream -WarningVariable warningVariable

            # Verify the output
            $output | Should Be $false
            $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 return false" {
            # Call the function
            $output = Remove-FileFromOneDrive -filePath $filePath -Endpoint $endpoint -userPrincipalName $userPrincipalName -OutputStream $outputStream -WarningVariable warningVariable

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

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

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

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

    context "when Invoke-RestMethod throws an exception" {
        # Declare mocks
        mock Invoke-RestMethod {
            throw "error"
        }

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

        it "outputs a warning and return false" {
            # Call the function
            $output = Remove-FileFromOneDrive -filePath $filePath -Endpoint $endpoint -userPrincipalName $userPrincipalName -OutputStream $outputStream -WarningVariable warningVariable

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

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

        it "outputs a warning and return false" {
            # Call the function
            $output = Remove-FileFromOneDrive -filePath $filePath -Endpoint $endpoint -userPrincipalName $userPrincipalName -OutputStream $outputStream -WarningVariable warningVariable

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