@@ -242,7 +242,7 @@ func TestReplaceSensitiveValues(t *testing.T) {
242
242
"ShouldHandleEmptyHeadersGracefully" : {
243
243
args : args {
244
244
data : & httpClient.HttpResponse {
245
- Body : " Sensitive value in the body." ,
245
+ Body : `{"message": " Sensitive value in the body", "token": "value"}` ,
246
246
Headers : map [string ][]string {
247
247
"Authorization" : {},
248
248
},
@@ -257,7 +257,7 @@ func TestReplaceSensitiveValues(t *testing.T) {
257
257
valueToPatch : ptr .To ("value" ),
258
258
},
259
259
want : want {
260
- body : " Sensitive {{my-secret:default:sensitiveKey}} in the body." ,
260
+ body : `{"message": " Sensitive value in the body", "token": " {{my-secret:default:sensitiveKey}}"}` ,
261
261
headers : map [string ][]string {
262
262
"Authorization" : {},
263
263
},
@@ -282,10 +282,10 @@ func TestReplaceSensitiveValues(t *testing.T) {
282
282
283
283
func TestUpdateSecretData (t * testing.T ) {
284
284
type args struct {
285
- secret * corev1.Secret
286
- secretKey string
287
- valueToPatch * string
288
- missingStrategy common.MissingFieldStrategy
285
+ secret * corev1.Secret
286
+ secretKey string
287
+ valueToPatch * string
288
+ missingFieldStrategy common.MissingFieldStrategy
289
289
}
290
290
291
291
type want struct {
@@ -341,12 +341,50 @@ func TestUpdateSecretData(t *testing.T) {
341
341
},
342
342
},
343
343
},
344
+ "ShouldSetEmptyMissingFieldWhenFieldMissing" : {
345
+ // Secret already contains key "key1" but the response did not return a value;
346
+ // missing field strategy "setEmpty" should override it to empty string.
347
+ args : args {
348
+ secret : & corev1.Secret {
349
+ Data : map [string ][]byte {
350
+ "key1" : []byte ("existingValue" ),
351
+ },
352
+ },
353
+ secretKey : "key1" ,
354
+ valueToPatch : nil ,
355
+ missingFieldStrategy : common .SetEmptyMissingField ,
356
+ },
357
+ want : want {
358
+ data : map [string ][]byte {
359
+ "key1" : []byte ("" ),
360
+ },
361
+ },
362
+ },
363
+ "ShouldPreserveExistingValueWhenFieldMissing" : {
364
+ // Secret already contains key "key1" but the response did not return a value;
365
+ // missing field strategy "preserve" should leave the value unchanged.
366
+ args : args {
367
+ secret : & corev1.Secret {
368
+ Data : map [string ][]byte {
369
+ "key1" : []byte ("existingValue" ),
370
+ },
371
+ },
372
+ secretKey : "key1" ,
373
+ valueToPatch : nil ,
374
+ missingFieldStrategy : common .PreserveMissingField ,
375
+ },
376
+ want : want {
377
+ data : map [string ][]byte {
378
+ "key1" : []byte ("existingValue" ),
379
+ },
380
+ },
381
+ },
344
382
}
345
383
346
384
for name , tc := range cases {
385
+ tc := tc // Create local copies of loop variables
347
386
t .Run (name , func (t * testing.T ) {
348
- updateSecretData (tc .args .secret , tc .args .secretKey , tc .args .valueToPatch , tc .args .missingStrategy )
349
-
387
+ updateSecretData (tc .args .secret , tc .args .secretKey , tc .args .valueToPatch , tc .args .missingFieldStrategy )
350
388
if diff := cmp .Diff (tc .want .data , tc .args .secret .Data ); diff != "" {
351
389
t .Errorf ("updateSecretData(...): -want data, +got data: %s" , diff )
352
390
}
0 commit comments