Functions/Download-FileFromOneDrive.Tests.ps1
describe "BitTitan.Runbooks.OneDrive/Download-FileFromOneDrive" -Tag "module", "unit" { # Import the function to test . "$($PSScriptRoot)\Download-FileFromOneDrive.ps1" # Declare external functions function Get-MicrosoftGraphAuthenticationToken { param($endpoint) return "This is the token!" } function Get-OneDriveFileDownloadLink{ param($filePath, $userPrincipalName, $token) return "https://www.google.com/" } function Get-FileFromUrl { param($FileUrl, $DestinationPath) return $true } # Declare the function inputs $filePath = "folder/file.extension" $destination = "hello\world.exe" $endpoint = "endpoint" $userPrincipalName = "userPrincipalName@domain.com" $outputStream = "Warning" context "when there are no issues" { # Declare mocks mock Get-OneDriveFileDownloadLink{ param($filePath, $userPrincipalName, $token) return "https://www.google.com/" } mock Get-FileFromUrl { param($FileUrl, $DestinationPath) return $true } it "downloads a file from OneDrive" { # Call the function $output = Download-FileFromOneDrive -filePath $filePath -destinationFilePath $destination -Endpoint $endpoint -userPrincipalName $userPrincipalName -WarningVariable warningVariable # Verify the mocks Assert-MockCalled Get-OneDriveFileDownloadLink -Times 1 -Exactly -ParameterFilter { $FilePath -eq "folder/file.extension" -and $UserPrincipalName -eq "userPrincipalName@domain.com" -and $Token -eq "This is the token!" } -Scope it Assert-MockCalled Get-FileFromUrl -Times 1 -Exactly -ParameterFilter { $FileUrl -eq "https://www.google.com/" -and $DestinationPath -eq "hello\world.exe" } -Scope it # Verify the output $output | Should Be $true $warningVariable | Should BeNullOrEmpty } } 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 = Download-FileFromOneDrive -filePath $filePath -destinationFilePath $destination -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 = "invalid" it "outputs a warning and returns false" { # Call the function $output = Download-FileFromOneDrive -filePath $filePath -destinationFilePath $destination -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 returns false" { # Call the function $output = Download-FileFromOneDrive -filePath $filePath -destinationFilePath $destination -Endpoint $endpoint -userPrincipalName $userPrincipalName -OutputStream $outputStream -WarningVariable warningVariable # Verify the output $output | Should Be $false $warningVariable | Should Not BeNullOrEmpty } } context "when Get-OneDriveFileDownloadLink does not return the link" { # Declare mocks mock Get-OneDriveFileDownloadLink{} # Declare function input variable $filePath = "/folder/file.extension" it "outputs a warning and returns false" { # Call the function $output = Download-FileFromOneDrive -filePath $filePath -destinationFilePath $destination -Endpoint $endpoint -userPrincipalName $userPrincipalName -OutputStream $outputStream -WarningVariable warningVariable # Verify the output $output | Should Be $false $warningVariable | Should Not BeNullOrEmpty } } context "when Get-FileFromUrl does not return anything" { # Declare mocks mock Get-FileFromUrl{} it "outputs a warning and returns false" { # Call the function $output = Download-FileFromOneDrive -filePath $filePath -destinationFilePath $destination -Endpoint $endpoint -userPrincipalName $userPrincipalName -OutputStream $outputStream -WarningVariable warningVariable # Verify the output $output | Should Be $false $warningVariable | Should Not BeNullOrEmpty } } } |