PrivateFunctions/Upload-SmallFileToOneDrive.Tests.ps1
describe "BitTitan.Runbooks.OneDrive/Upload-SmallFileToOneDrive" -Tag "module", "unit" { # Import the function to test . "$($PSScriptRoot)\Upload-SmallFileToOneDrive.ps1" # Declare external functions function Invoke-RestMethod { param($Uri, $Method, $Body, $Headers) return [PSCustomObject]@{ ID = 'id' } } # 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' } } it "uploads a small file to OneDrive" { # Call the function $output = Upload-SmallFileToOneDrive -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:/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-SmallFileToOneDrive -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 {} it "outputs a warning and returns false" { # Call the function $output = Upload-SmallFileToOneDrive -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 } |