PrivateFunctions/Upload-SmallFileToSharePointOnline.Tests.ps1
describe "BitTitan.Runbooks.SharePointOnline/Upload-SmallFileToSharePointOnline" -Tag "module", "unit" { # Import the function to test . "$($PSScriptRoot)\Upload-SmallFileToSharePointOnline.ps1" # Declare external functions function Get-Content { return @("1", "2") } function Invoke-RestMethod { param($Uri, $Method, $Body, $Headers) return [PSCustomObject]@{ ID = 'id' } } # Declare the function inputs $filePath = "folder/file.extension" $destination = "hello/world.exe" $token = "This is the token!" $siteUrl = "site.sharepoint.com" $outputStream = "Warning" context "when there are no issues" { # Declare mocks mock Invoke-RestMethod { param($Uri, $Method, $Body, $Headers) return [PSCustomObject]@{ ID = 'id' } } it "uploads a small file to SharePoint Online" { # Call the function $output = Upload-SmallFileToSharePointOnline -filePath $filePath -destinationFilePath $destination -Token $token -siteUrl $siteUrl # Verify the mocks Assert-MockCalled Invoke-RestMethod -Times 1 -Exactly -ParameterFilter { $Uri -eq "https://graph.microsoft.com/v1.0/sites/site.sharepoint.com/drive/root:/hello/world.exe:/content" -and $Method -eq "PUT" -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 Invoke-RestMethod throws an exception" { # Declare mocks mock Invoke-RestMethod { throw "error" } it "outputs a warning and returns false" { # Call the function $output = Upload-SmallFileToSharePointOnline -filePath $filePath -destinationFilePath $destination -Token $token -siteUrl $siteUrl -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 {} it "outputs a warning and returns false" { # Call the function $output = Upload-SmallFileToSharePointOnline -filePath $filePath -destinationFilePath $destination -Token $token -siteUrl $siteUrl -OutputStream $outputStream -WarningVariable warningVariable # Verify the output $output | Should Be $false $warningVariable | Should Not BeNullOrEmpty } } } |