Skip to content

Commit a7fbeea

Browse files
🪲 [Fix]: Fix so that the module can manage context with properties with null values (#99)
## Description This pull request enhances the handling of `null` values in context objects and improves test coverage to ensure proper behavior when working with `null` values. Key changes include updates to recursive conversion functions and the addition of comprehensive test cases. ### Handling of `null` values in conversion functions: * [`src/functions/private/JsonToObject/Convert-ContextHashtableToObjectRecursive.ps1`](diffhunk://#diff-56b4d6d08d73e9f82d34b120f3ba7b16b8d60ca37531caebd2c7bec68f9ead42R69-R73): Added logic to detect and preserve `null` values when converting hashtables to objects. * [`src/functions/private/ObjectToJson/Convert-ContextObjectToHashtableRecursive.ps1`](diffhunk://#diff-06bd0c6e7bea4b0926769c56c0d8d223b38ef3bd16f9c41c2b43c4a188ad3807R71-R75): Added logic to detect and preserve `null` values when converting objects to hashtables. ### Enhanced test coverage: * [`tests/Context.Tests.ps1`](diffhunk://#diff-b7c75aeea4a2f7050d610f7ab27834a431ecf4ace5b964b30cffddb3ef37e921L43-R63): Updated multiple test cases to include validation of `null` properties in context objects, ensuring `null` values are handled correctly during context creation and retrieval. [[1]](diffhunk://#diff-b7c75aeea4a2f7050d610f7ab27834a431ecf4ace5b964b30cffddb3ef37e921L43-R63) [[2]](diffhunk://#diff-b7c75aeea4a2f7050d610f7ab27834a431ecf4ace5b964b30cffddb3ef37e921R132) [[3]](diffhunk://#diff-b7c75aeea4a2f7050d610f7ab27834a431ecf4ace5b964b30cffddb3ef37e921R318-R326) * [`tests/Context.Tests.ps1`](diffhunk://#diff-b7c75aeea4a2f7050d610f7ab27834a431ecf4ace5b964b30cffddb3ef37e921R163-R208): Added a new test case for complex objects containing `null` values, nested objects, and arrays with mixed values, verifying correct behavior across various scenarios. * [`tests/Context.Tests.ps1`](diffhunk://#diff-b7c75aeea4a2f7050d610f7ab27834a431ecf4ace5b964b30cffddb3ef37e921R163-R208): Adjusted expected counts for context retrieval tests to account for the newly added test context. ## 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
1 parent b640cb8 commit a7fbeea

File tree

3 files changed

+60
-6
lines changed

3 files changed

+60
-6
lines changed

src/functions/private/JsonToObject/Convert-ContextHashtableToObjectRecursive.ps1

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@
6666
$value = $Hashtable[$key]
6767
Write-Debug "Processing [$key]"
6868
Write-Debug "Value: $value"
69+
if ($null -eq $value) {
70+
Write-Debug "- as null value"
71+
$result | Add-Member -NotePropertyName $key -NotePropertyValue $null
72+
continue
73+
}
6974
Write-Debug "Type: $($value.GetType().Name)"
7075
if ($value -is [string] -and $value -like '`[SECURESTRING`]*') {
7176
Write-Debug "Converting [$key] as [SecureString]"

src/functions/private/ObjectToJson/Convert-ContextObjectToHashtableRecursive.ps1

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@
6868
$value = $property.Value
6969
Write-Debug "Processing [$name]"
7070
Write-Debug "Value: $value"
71+
if ($null -eq $value) {
72+
Write-Debug '- as null value'
73+
$result[$property.Name] = $null
74+
continue
75+
}
7176
Write-Debug "Type: $($value.GetType().Name)"
7277
if ($value -is [datetime]) {
7378
Write-Debug '- as DateTime'

tests/Context.Tests.ps1

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,27 @@ Describe 'Context' {
4040
$result.ID | Should -Be 'TestID1'
4141
}
4242
It "Set-Context -ID 'TestID2' -Context @{} -Vault 'VaultA'" {
43-
{ Set-Context -ID 'TestID2' -Context @{} -Vault 'VaultA' } | Should -Not -Throw
43+
{ Set-Context -ID 'TestID2' -Context @{ NullProperty = $null; StringProperty = 'test' } -Vault 'VaultA' } | Should -Not -Throw
4444
$result = Get-Context -ID 'TestID2' -Vault 'VaultA'
4545
$result | Should -Not -BeNullOrEmpty
4646
$result.ID | Should -Be 'TestID2'
47+
$result.NullProperty | Should -BeNull
48+
$result.StringProperty | Should -Be 'test'
4749
}
4850
It "Set-Context -ID 'TestID2' -Context @{} - Again -Vault 'VaultA'" {
49-
{ Set-Context -ID 'TestID2' -Context @{} -Vault 'VaultA' } | Should -Not -Throw
51+
{ Set-Context -ID 'TestID2' -Context @{ NullProperty = $null; StringProperty = 'updated' } -Vault 'VaultA' } | Should -Not -Throw
5052
$result = Get-Context -ID 'TestID2' -Vault 'VaultA'
5153
$result | Should -Not -BeNullOrEmpty
5254
$result.ID | Should -Be 'TestID2'
55+
$result.NullProperty | Should -BeNull
56+
$result.StringProperty | Should -Be 'updated'
5357
}
5458
It "Set-Context -ID 'john_doe' -Context [advanced object] -Vault 'VaultA'" {
5559
$contextData = [PSCustomObject]@{
5660
Username = 'john_doe'
5761
AuthToken = 'ghp_12345ABCDE67890FGHIJ' | ConvertTo-SecureString -AsPlainText -Force #gitleaks:allow
5862
LoginTime = Get-Date
63+
MyNullValue = $null
5964
IsTwoFactorAuth = $true
6065
TwoFactorMethods = @('TOTP', 'SMS')
6166
LastLoginAttempts = @(
@@ -124,6 +129,7 @@ Describe 'Context' {
124129
$context.AuthToken | Should -BeOfType [System.Security.SecureString]
125130
$context.AuthToken | ConvertFrom-SecureString -AsPlainText | Should -Be 'ghp_12345ABCDE67890FGHIJ'
126131
$context.LoginTime | Should -BeOfType [System.DateTime]
132+
$context.MyNullValue | Should -BeNull
127133
$context.IsTwoFactorAuth | Should -Be $true
128134
$context.TwoFactorMethods | Should -Be @('TOTP', 'SMS')
129135
$context.LastLoginAttempts | Should -BeOfType [PSCustomObject]
@@ -154,16 +160,52 @@ Describe 'Context' {
154160
$context.SessionMetaData.BrowserInfo.Name | Should -Be 'Chrome'
155161
$context.SessionMetaData.BrowserInfo.Version | Should -Be '118.0.1'
156162
}
163+
It "Set-Context -ID 'null_test' -Context [object with null values] -Vault 'VaultA'" {
164+
$contextData = [PSCustomObject]@{
165+
StringValue = 'NotNull'
166+
NullValue1 = $null
167+
EmptyString = ''
168+
ZeroValue = 0
169+
FalseValue = $false
170+
NullValue2 = $null
171+
ArrayWithNull = @('value1', $null, 'value3')
172+
NestedObject = [PSCustomObject]@{
173+
Property1 = 'value'
174+
NullProp = $null
175+
Property2 = 42
176+
}
177+
}
178+
179+
{ Set-Context -ID 'null_test' -Context $contextData -Vault 'VaultA' } | Should -Not -Throw
180+
$context = Get-Context -ID 'null_test' -Vault 'VaultA'
181+
$context | Should -Not -BeNullOrEmpty
182+
$context.ID | Should -Be 'null_test'
183+
$context.StringValue | Should -Be 'NotNull'
184+
$context.NullValue1 | Should -BeNull
185+
$context.EmptyString | Should -Be ''
186+
$context.ZeroValue | Should -Be 0
187+
$context.FalseValue | Should -Be $false
188+
$context.NullValue2 | Should -BeNull
189+
$context.ArrayWithNull | Should -Not -BeNull
190+
$context.ArrayWithNull.Count | Should -Be 3
191+
$context.ArrayWithNull[0] | Should -Be 'value1'
192+
$context.ArrayWithNull[1] | Should -BeNull
193+
$context.ArrayWithNull[2] | Should -Be 'value3'
194+
$context.NestedObject | Should -Not -BeNull
195+
$context.NestedObject.Property1 | Should -Be 'value'
196+
$context.NestedObject.NullProp | Should -BeNull
197+
$context.NestedObject.Property2 | Should -Be 42
198+
}
157199
}
158200

159201
Context 'Get-Context' {
160202
It 'Get-Context - Should return all contexts in VaultA' {
161203
Write-Host (Get-Context -Vault 'VaultA' | Out-String)
162-
(Get-Context -Vault 'VaultA').Count | Should -Be 3
204+
(Get-Context -Vault 'VaultA').Count | Should -Be 4
163205
}
164206
It "Get-Context -ID '*' - Should return all contexts in VaultA" {
165207
Write-Host (Get-Context -ID '*' -Vault 'VaultA' | Out-String)
166-
(Get-Context -ID '*' -Vault 'VaultA').Count | Should -Be 3
208+
(Get-Context -ID '*' -Vault 'VaultA').Count | Should -Be 4
167209
}
168210
It "Get-Context -ID 'TestID*' - Should return all contexts in VaultA" {
169211
Write-Host (Get-Context -ID 'TestID*' -Vault 'VaultA' | Out-String)
@@ -271,15 +313,17 @@ Describe 'Context' {
271313

272314
It 'Sets a context where the ID is in the context data in VaultA' {
273315
$contextData = [PSCustomObject]@{
274-
ID = 'TestContext'
275-
Data = 'Some data'
316+
ID = 'TestContext'
317+
Data = 'Some data'
318+
NullValue = $null
276319
}
277320

278321
{ Set-Context -Context $contextData -Vault 'VaultA' } | Should -Not -Throw
279322
$result = Get-Context -ID 'TestContext' -Vault 'VaultA'
280323
$result | Should -Not -BeNullOrEmpty
281324
$result.ID | Should -Be 'TestContext'
282325
$result.Data | Should -Be 'Some data'
326+
$result.NullValue | Should -BeNull
283327
}
284328

285329
It 'Renaming a context to an existing context does not throw with force in VaultA' {

0 commit comments

Comments
 (0)