Skip to content

Commit

Permalink
(chocolatey#3565) Add Pester tests for Credential Provider
Browse files Browse the repository at this point in the history
Add Pester tests to ensure we don't inadvertently bleed configured
credentials into scenarios where they should not be used.
tests
  • Loading branch information
corbob committed Nov 25, 2024
1 parent d2ac771 commit dd50a37
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 7 deletions.
6 changes: 2 additions & 4 deletions tests/helpers/common/Chocolatey/Disable-ChocolateySource.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 1 addition & 3 deletions tests/helpers/common/Chocolatey/Enable-ChocolateySource.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
11 changes: 11 additions & 0 deletions tests/helpers/common/Chocolatey/Get-ChocolateySource.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function Get-ChocolateySource {
[CmdletBinding()]
param(
[Parameter()]
[string]$Name = "*"
)
# 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
}
}
63 changes: 63 additions & 0 deletions tests/pester-tests/features/CredentialProvider.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
Describe 'Ensuring credentials do not bleed from configured sources' -Tag CredentialProvider -ForEach @(
@{
Command = 'info'
ExitCode = 0
}
@{
Command = 'install'
ExitCode = 1
}
@{
Command = 'outdated'
ExitCode = 0
}
@{
Command = 'search'
ExitCode = 1
}
@{
Command = 'upgrade'
ExitCode = 1
}
@{
Command = 'download'
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 (<Command>)' -Skip:($Command -eq 'download' -and -not $HasLicensedExtension) {
BeforeAll {
# The package used ultimately doesn't matter as we don't expect to find it.
# Picking a package that should be found if the behaviour changes.
$PackageUnderTest = 'chocolatey'
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 (<ExitCode>)' {
$Output.ExitCode | Should -Be $ExitCode -Because $Output.String
}

It 'Outputs error message' {
$FilteredOutput = $Output.Lines -match "Failed to fetch results from V2 feed at '$($SetupSource.Url.Trim('/'))"
$FilteredOutput.Count | Should -BeGreaterOrEqual 1 -Because $Output.String
}
}
}

0 comments on commit dd50a37

Please sign in to comment.