Skip to content

Commit

Permalink
🩹 [Patch]: Update Remove-Context to retrieve context names from `Ge…
Browse files Browse the repository at this point in the history
…t-SecretInfo` (#44)

## Description

- Update `Remove-Context` to retrieve context names from
`Get-SecretInfo`.

## Type of change

<!-- Use the check-boxes [x] on the options that are relevant. -->

- [ ] 📖 [Docs]
- [x] 🪲 [Fix]
- [ ] 🩹 [Patch]
- [ ] ⚠️ [Security fix]
- [ ] 🚀 [Feature]
- [ ] 🌟 [Breaking change]

## Checklist

<!-- Use the check-boxes [x] on the options that are relevant. -->

- [x] I have performed a self-review of my own code
- [x] I have commented my code, particularly in hard-to-understand areas
  • Loading branch information
MariusStorhaug authored Nov 17, 2024
1 parent 2c2a019 commit 2bb6321
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 38 deletions.
14 changes: 6 additions & 8 deletions src/functions/public/Context/Remove-Context.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,20 @@ filter Remove-Context {
[CmdletBinding(SupportsShouldProcess)]
param(
# The name of the context to remove from the vault. Supports wildcard patterns.
[Parameter(
ValueFromPipeline,
ValueFromPipelineByPropertyName
)]
[Parameter()]
[SupportsWildcards()]
[Alias('Context', 'ContextName')]
[string] $Name = '*'
[object] $Name = '*'
)

$contextVault = Get-ContextVault
$contextNames = Get-Context -Name $Name -AsPlainText | Select-Object -ExpandProperty Name
$Name = "$($script:Config.Name)$Name"
$contextNames = Get-SecretInfo -Vault $script:Config.Context.VaultName | Where-Object { $_.Name -like "$Name" } |
Select-Object -ExpandProperty Name

Write-Verbose "Removing [$($contextNames.count)] contexts from vault [$($contextVault.Name)]"
Write-Verbose "Removing [$($contextNames.count)] contexts from vault [$($contextVault.Name)] using filter [$Name]"
foreach ($contextName in $contextNames) {
Write-Verbose "Removing context [$contextName]"
$contextName = $($script:Config.Name) + $contextName
if ($PSCmdlet.ShouldProcess('Remove-Secret', $contextName)) {
Write-Verbose "Removing secret [$contextName]"
Remove-Secret -Name $contextName -Vault $contextVault.Name
Expand Down
32 changes: 2 additions & 30 deletions tests/Context.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ Describe 'Context' {
}

It 'Get-Context - Should return all contexts' {
{ Get-Context } | Should -Not -Throw
(Get-Context).Count | Should -Be 2
}

It "Get-Context -Name '*' - Should return all contexts" {
{ Get-Context -Name '*' } | Should -Not -Throw
(Get-Context -Name '*').Count | Should -Be 2
}

It "Get-Context -Name '' - Should return no contexts" {
Expand Down Expand Up @@ -125,34 +125,6 @@ Describe 'Context' {
It 'Can delete using a wildcard' {
{ Remove-Context -Name 'Test*' } | Should -Not -Throw
}
It 'Can delete contexts using pipeline' {
$Context = @{
Name = 'Test6'
AccessToken = 'MySecret' | ConvertTo-SecureString -AsPlainText -Force
RefreshToken = 'MyRefreshedSecret' | ConvertTo-SecureString -AsPlainText -Force
}

{ Set-Context -Context $Context } | Should -Not -Throw

$Context = @{
Name = 'Test7'
AccessToken = 'MySecret' | ConvertTo-SecureString -AsPlainText -Force
RefreshToken = 'MyRefreshedSecret' | ConvertTo-SecureString -AsPlainText -Force
}

{ Set-Context -Context $Context } | Should -Not -Throw

$Context = @{
Name = 'Test8'
AccessToken = 'MySecret' | ConvertTo-SecureString -AsPlainText -Force
RefreshToken = 'MyRefreshedSecret' | ConvertTo-SecureString -AsPlainText -Force
}

{ Set-Context -Context $Context } | Should -Not -Throw

Get-Context -Name 'Test*' | Should -Not -BeNullOrEmpty
{ Get-Context -Name 'Test*' | Remove-Context } | Should -Not -Throw
}
}

Context 'Set-ContextSetting' {
Expand Down

0 comments on commit 2bb6321

Please sign in to comment.