From 13531062213edeb20f5e433b1f702923b17cb71c Mon Sep 17 00:00:00 2001 From: Cory Knox Date: Wed, 20 Nov 2024 11:06:09 -0800 Subject: [PATCH] (#3565) Add Pester tests for Credential Provider Add Pester tests to ensure we don't inadvertently bleed configured credentials into scenarios where they should not be used. --- .../Chocolatey/Disable-ChocolateySource.ps1 | 6 +- .../Chocolatey/Enable-ChocolateySource.ps1 | 4 +- .../Chocolatey/Get-ChocolateySource.ps1 | 14 ++++ .../features/CredentialProvider.Tests.ps1 | 69 +++++++++++++++++++ 4 files changed, 86 insertions(+), 7 deletions(-) create mode 100644 tests/helpers/common/Chocolatey/Get-ChocolateySource.ps1 create mode 100644 tests/pester-tests/features/CredentialProvider.Tests.ps1 diff --git a/tests/helpers/common/Chocolatey/Disable-ChocolateySource.ps1 b/tests/helpers/common/Chocolatey/Disable-ChocolateySource.ps1 index 523839f5c9..58cb264f53 100644 --- a/tests/helpers/common/Chocolatey/Disable-ChocolateySource.ps1 +++ b/tests/helpers/common/Chocolatey/Disable-ChocolateySource.ps1 @@ -8,10 +8,8 @@ function Disable-ChocolateySource { [Parameter()] [switch]$All ) - # Significantly weird behaviour with piping this source list by property name. - $CurrentSources = (Invoke-Choco source list -r).Lines | ConvertFrom-ChocolateyOutput -Command SourceList | Where-Object { - $_.Name -like $Name - } + + $CurrentSources = Get-ChocolateySource -Name $Name foreach ($Source in $CurrentSources) { $null = Invoke-Choco source disable --name $Source.Name } diff --git a/tests/helpers/common/Chocolatey/Enable-ChocolateySource.ps1 b/tests/helpers/common/Chocolatey/Enable-ChocolateySource.ps1 index 6a44cd184e..6f6a2f1abd 100644 --- a/tests/helpers/common/Chocolatey/Enable-ChocolateySource.ps1 +++ b/tests/helpers/common/Chocolatey/Enable-ChocolateySource.ps1 @@ -9,9 +9,7 @@ function Enable-ChocolateySource { [switch]$All ) # Significantly weird behaviour with piping this source list by property name. - $CurrentSources = (Invoke-Choco source list -r).Lines | ConvertFrom-ChocolateyOutput -Command SourceList | Where-Object { - $_.Name -like $Name - } + $CurrentSources = Get-ChocolateySource -Name $Name foreach ($Source in $CurrentSources) { $null = Invoke-Choco source enable --name $Source.Name } diff --git a/tests/helpers/common/Chocolatey/Get-ChocolateySource.ps1 b/tests/helpers/common/Chocolatey/Get-ChocolateySource.ps1 new file mode 100644 index 0000000000..f4064b3f9e --- /dev/null +++ b/tests/helpers/common/Chocolatey/Get-ChocolateySource.ps1 @@ -0,0 +1,14 @@ +function Get-ChocolateySource { + [CmdletBinding()] + param( + [Parameter()] + [string]$Name = "*", + + [Parameter()] + [switch]$All + ) + # Significantly weird behaviour with piping this source list by property name. + (Invoke-Choco source list -r).Lines | ConvertFrom-ChocolateyOutput -Command SourceList | Where-Object { + $_.Name -like $Name + } +} diff --git a/tests/pester-tests/features/CredentialProvider.Tests.ps1 b/tests/pester-tests/features/CredentialProvider.Tests.ps1 new file mode 100644 index 0000000000..2792f25640 --- /dev/null +++ b/tests/pester-tests/features/CredentialProvider.Tests.ps1 @@ -0,0 +1,69 @@ +Describe 'Ensuring credentials do not bleed from configured sources' -Tag CredentialProvider -ForEach @( + @{ + Command = 'info' + QueryString = "/Packages()?`$filter=(tolower(Id)%20eq%20'chocolatey-compatibility.extension')%20and%20IsLatestVersion&semVerLevel=2.0.0" + ExitCode = 0 +} + @{ + Command = 'install' + QueryString = "/Packages()?`$filter=(tolower(Id)%20eq%20'chocolatey-compatibility.extension')%20and%20IsLatestVersion&semVerLevel=2.0.0" + ExitCode = 1 +} + @{ + Command = 'outdated' + # The QueryString here is emitted for each package installed. The chocolatey package is the only one we can be sure is present. + QueryString = "/Packages()?`$filter=tolower(Id)%20eq%20'chocolatey'&semVerLevel=2.0.0" + ExitCode = 0 +} + @{ + Command = 'search' + # + QueryString = "/Packages()?`$filter=((((Id%20ne%20null)%20and%20substringof('chocolatey-compatibility.extension',tolower(Id)))%20or%20((Description%20ne%20null)%20and%20substringof('chocolatey-compatibility.extension',tolower(Description))))%20or%20((Tags%20ne%20null)%20and%20substringof('%20chocolatey-compatibility.extension%20',tolower(Tags))))%20and%20IsLatestVersion&`$orderby=Id&`$skip=0&`$top=30&semVerLevel=2.0.0" + ExitCode = 1 +} + @{ + Command = 'upgrade' + QueryString = "/Packages()?`$filter=(tolower(Id)%20eq%20'chocolatey-compatibility.extension')%20and%20IsLatestVersion&semVerLevel=2.0.0" + ExitCode = 1 +} + @{ + Command = 'download' + QueryString = "/Packages()?`$filter=(tolower(Id)%20eq%20'chocolatey-compatibility.extension')%20and%20IsLatestVersion&semVerLevel=2.0.0" + ExitCode = 1 +} +) { + BeforeDiscovery { + $HasLicensedExtension = Test-PackageIsEqualOrHigher -PackageName 'chocolatey.extension' -Version '6.0.0' + } + + BeforeAll { + Initialize-ChocolateyTestInstall + Disable-ChocolateySource -All + Enable-ChocolateySource -Name 'hermes' + $SetupSource = Get-ChocolateySource -Name 'hermes-setup' + Remove-Item download -force -recurse + } + + # Skip the download command if chocolatey.extension is not installed. + Context 'Command ()' -Skip:($Command -eq 'download' -and -not $HasLicensedExtension) { + BeforeAll { + # Picked a package that is on `hermes-setup` but not on `hermes`. + $PackageUnderTest = 'chocolatey-compatibility.extension' + Restore-ChocolateyInstallSnapshot + # Chocolatey will prompt for credentials, we need to force something in there, and this will do that. + $Output = 'n' | Invoke-Choco $Command $PackageUnderTest --confirm --source="'$($SetupSource.Url)'" + } + + AfterAll { + Remove-ChocolateyInstallSnapshot + } + + It 'Exits Correctly ()' { + $Output.ExitCode | Should -Be $ExitCode -Because $Output.String + } + + It 'Outputs error message' { + $Output.Lines | Should -Contain "Failed to fetch results from V2 feed at '$($SetupSource.Url.Trim('/'))$QueryString' with following message : Response status code does not indicate success: 401 (Unauthorized)." -Because $Output.String + } + } +}