Skip to content

Commit 175cde4

Browse files
Harden context lookup and WhatIf behavior
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent def7ed5 commit 175cde4

4 files changed

Lines changed: 36 additions & 2 deletions

File tree

src/functions/private/Auth/Set-DomeneshopDefaultContext.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ function Set-DomeneshopDefaultContext {
4040
)
4141

4242
$config = Get-DomeneshopConfig
43-
$config.DefaultContext = $Context
4443
if ($PSCmdlet.ShouldProcess('Domeneshop module configuration', "Set [$Context] as the default context")) {
44+
$config.DefaultContext = $Context
4545
$null = Set-Context -ID '__Domeneshop.Config' -Vault 'Domeneshop' -Context $config
4646
}
4747
}

src/functions/public/Auth/Get-DomeneshopContext.ps1

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,13 @@ function Get-DomeneshopContext {
6363
$id = $config.DefaultContext
6464
}
6565

66-
Get-Context -ID $id -Vault 'Domeneshop' | Where-Object { $_.ID -ne '__Domeneshop.Config' } | Sort-Object -Property ID
66+
$contexts = @(
67+
Get-Context -ID $id -Vault 'Domeneshop' |
68+
Where-Object { $_.ID -ne '__Domeneshop.Config' }
69+
)
70+
if (-not $ListAvailable -and $contexts.Count -eq 0) {
71+
throw "Domeneshop context [$id] was not found in the Domeneshop vault."
72+
}
73+
74+
$contexts | Sort-Object -Property ID
6775
}

tests/Domeneshop.Private.Tests.ps1

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ Describe 'Domeneshop private helpers' {
4848
Should -Throw '*reserved for Domeneshop module configuration*'
4949
}
5050

51+
It 'does not mutate the configuration when setting the default with WhatIf' {
52+
$config = [pscustomobject]@{ DefaultContext = 'original' }
53+
Mock Get-DomeneshopConfig { $config }
54+
Mock Set-Context {}
55+
56+
Set-DomeneshopDefaultContext -Context 'replacement' -WhatIf
57+
58+
$config.DefaultContext | Should -Be 'original'
59+
Should -Invoke Set-Context -Times 0 -Exactly
60+
}
61+
5162
It 'uses basic authentication and terminating transport errors' {
5263
Mock Invoke-RestMethod { [pscustomobject]@{ ok = $true } }
5364

tests/Get-DomeneshopContext.Tests.ps1

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,21 @@ Describe 'Get-DomeneshopContext' {
3939
Should -Throw '*reserved for Domeneshop module configuration*'
4040
}
4141

42+
It 'throws when the configured default context is missing from the vault' {
43+
Mock Get-DomeneshopConfig { [pscustomobject]@{ DefaultContext = 'missing' } }
44+
Mock Get-Context {}
45+
46+
{ Get-DomeneshopContext } |
47+
Should -Throw '*Domeneshop context*missing*was not found in the Domeneshop vault*'
48+
}
49+
50+
It 'throws when an explicitly requested context is missing from the vault' {
51+
Mock Get-Context {}
52+
53+
{ Get-DomeneshopContext -Context 'missing' } |
54+
Should -Throw '*Domeneshop context*missing*was not found in the Domeneshop vault*'
55+
}
56+
4257
It 'excludes the module configuration when listing contexts' {
4358
Mock Get-Context {
4459
@(

0 commit comments

Comments
 (0)