Skip to content

Commit d22a10d

Browse files
committed
(chocolatey#3566) 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.
1 parent bafa7eb commit d22a10d

File tree

4 files changed

+83
-7
lines changed

4 files changed

+83
-7
lines changed

tests/helpers/common/Chocolatey/Disable-ChocolateySource.ps1

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ function Disable-ChocolateySource {
88
[Parameter()]
99
[switch]$All
1010
)
11-
# Significantly weird behaviour with piping this source list by property name.
12-
$CurrentSources = (Invoke-Choco source list -r).Lines | ConvertFrom-ChocolateyOutput -Command SourceList | Where-Object {
13-
$_.Name -like $Name
14-
}
11+
12+
$CurrentSources = Get-ChocolateySource -Name $Name
1513
foreach ($Source in $CurrentSources) {
1614
$null = Invoke-Choco source disable --name $Source.Name
1715
}

tests/helpers/common/Chocolatey/Enable-ChocolateySource.ps1

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ function Enable-ChocolateySource {
99
[switch]$All
1010
)
1111
# Significantly weird behaviour with piping this source list by property name.
12-
$CurrentSources = (Invoke-Choco source list -r).Lines | ConvertFrom-ChocolateyOutput -Command SourceList | Where-Object {
13-
$_.Name -like $Name
14-
}
12+
$CurrentSources = Get-ChocolateySource -Name $Name
1513
foreach ($Source in $CurrentSources) {
1614
$null = Invoke-Choco source enable --name $Source.Name
1715
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function Get-ChocolateySource {
2+
[CmdletBinding()]
3+
param(
4+
[Parameter()]
5+
[string]$Name = "*",
6+
7+
[Parameter()]
8+
[switch]$All
9+
)
10+
# Significantly weird behaviour with piping this source list by property name.
11+
(Invoke-Choco source list -r).Lines | ConvertFrom-ChocolateyOutput -Command SourceList | Where-Object {
12+
$_.Name -like $Name
13+
}
14+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
Describe 'Ensuring credentials do not bleed from configured sources' -Tag CredentialProvider -ForEach @(
2+
@{
3+
Command = 'info'
4+
ExitCode = 0
5+
}
6+
@{
7+
Command = 'install'
8+
ExitCode = 1
9+
}
10+
@{
11+
Command = 'outdated'
12+
ExitCode = 0
13+
}
14+
@{
15+
Command = 'search'
16+
ExitCode = 0
17+
}
18+
@{
19+
Command = 'upgrade'
20+
ExitCode = 1
21+
}
22+
@{
23+
Command = 'download'
24+
ExitCode = 1
25+
}
26+
) {
27+
BeforeDiscovery {
28+
$HasLicensedExtension = Test-PackageIsEqualOrHigher -PackageName 'chocolatey.extension' -Version '5.0.0'
29+
}
30+
31+
BeforeAll {
32+
Initialize-ChocolateyTestInstall
33+
Disable-ChocolateySource -All
34+
Enable-ChocolateySource -Name 'hermes'
35+
$SetupSource = Get-ChocolateySource -Name 'hermes-setup'
36+
Remove-Item download -force -recurse
37+
}
38+
39+
# Skip the download command if chocolatey.extension is not installed.
40+
Context 'Command (<Command>)' -Skip:($Command -eq 'download' -and -not $HasLicensedExtension) {
41+
BeforeAll {
42+
# Picked a package that is on `hermes-setup` but not on `hermes`.
43+
$PackageUnderTest = 'chocolatey-compatibility.extension'
44+
Restore-ChocolateyInstallSnapshot
45+
# Chocolatey will prompt for credentials, we need to force something in there, and this will do that.
46+
$Output = 'n' | Invoke-Choco $Command $PackageUnderTest --confirm --source="'$($SetupSource.Url)'"
47+
}
48+
49+
AfterAll {
50+
Remove-ChocolateyInstallSnapshot
51+
}
52+
53+
It 'Exits Correctly (<ExitCode>)' {
54+
$Output.ExitCode | Should -Be $ExitCode -Because $Output.String
55+
}
56+
57+
It 'Outputs error message' {
58+
if ($Command -eq 'search') {
59+
$Output.Lines | Should -Contain "[NuGet] Not able to contact source '$($SetupSource.Url)'. Error was The remote server returned an error: (401) Unauthorized." -Because $Output.String
60+
} else {
61+
$Output.Lines | Should -Contain "Error retrieving packages from source '$($SetupSource.Url)':" -Because $Output.String
62+
$Output.Lines | Should -Contain "The remote server returned an error: (401) Unauthorized." -Because $Output.String
63+
}
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)