Functions/Remove-FileFromSharePointOnline.Tests.ps1
describe "BitTitan.Runbooks.SharePointOnline/Remove-FileFromSharePointOnline" -Tag "module", "unit" { # Import the function to test . "$($PSScriptRoot)\Remove-FileFromSharePointOnline.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" $siteUrl = "site.sharepoint.com" $outputStream = "Warning" context "when there are no issues" { # Declare mocks mock Invoke-RestMethod { param($Uri, $Method, $Headers) return "" } it "deletes a file from SharePoint Online" { # Call the function $output = Remove-FileFromSharePointOnline -filePath $filePath -Endpoint $endpoint -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:/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-FileFromSharePointOnline -filePath $filePath -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 return false" { # Call the function $output = Remove-FileFromSharePointOnline -filePath $filePath -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 = "siteUrl" it "outputs a warning and returns false" { # Call the function $output = Remove-FileFromSharePointOnline -filePath $filePath -Endpoint $endpoint -siteUrl $siteUrl -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-FileFromSharePointOnline -filePath $filePath -Endpoint $endpoint -siteUrl $siteUrl -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-FileFromSharePointOnline -filePath $filePath -Endpoint $endpoint -siteUrl $siteUrl -OutputStream $outputStream -WarningVariable warningVariable # Verify the output $output | Should Be $false $warningVariable | Should Not BeNullOrEmpty } } } |