Functions/Download-FileFromSharePointOnline.Tests.ps1
describe "BitTitan.Runbooks.SharePointOnline/Download-FileFromSharePointOnline" -Tag "module", "unit" { # Import the function to test . "$($PSScriptRoot)\Download-FileFromSharePointOnline.ps1" # Declare external functions function Get-MicrosoftGraphAuthenticationToken { param($endpoint) return "This is the token!" } function Get-SharePointOnlineFileDownloadLink{ param($filePath, $siteUrl, $token) return "https://www.google.com/" } function Invoke-RestMethod { param($Uri, $Method, $Headers) return [PSCustomObject]@{ '@microsoft.graph.downloadUrl' = "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" $siteUrl = "site.sharepoint.com" $outputStream = "Warning" context "when there are no issues" { # Declare mocks mock Get-SharePointOnlineFileDownloadLink{ param($filePath, $siteUrl, $token) return "https://www.google.com/" } mock Get-FileFromUrl { param($FileUrl, $DestinationPath) return $true } it "downloads a file from SharePoint Online" { # Call the function $output = Download-FileFromSharePointOnline -filePath $filePath -destinationFilePath $destination -Endpoint $endpoint -siteUrl $siteUrl -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 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-FileFromSharePointOnline -filePath $filePath -destinationFilePath $destination -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 = "invalid" it "outputs a warning and returns false" { # Call the function $output = Download-FileFromSharePointOnline -filePath $filePath -destinationFilePath $destination -Endpoint $endpoint -siteUrl $siteUrl -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-FileFromSharePointOnline -filePath $filePath -destinationFilePath $destination -Endpoint $endpoint -siteUrl $siteUrl -OutputStream $outputStream -WarningVariable warningVariable # Verify the output $output | Should Be $false $warningVariable | Should Not BeNullOrEmpty } } context "when Get-SharePointOnlineFileDownloadLink does not return the link" { # Declare mocks mock Get-SharePointOnlineFileDownloadLink{} # Declare function input variable $filePath = "/folder/file.extension" it "outputs a warning and returns false" { # Call the function $output = Download-FileFromSharePointOnline -filePath $filePath -destinationFilePath $destination -Endpoint $endpoint -siteUrl $siteUrl -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-FileFromSharePointOnline -filePath $filePath -destinationFilePath $destination -Endpoint $endpoint -siteUrl $siteUrl -OutputStream $outputStream -WarningVariable warningVariable # Verify the output $output | Should Be $false $warningVariable | Should Not BeNullOrEmpty } } } |