@@ -269,7 +269,7 @@ def test_erase_json_dict_with_complex_masking_rules(data_masker):
269
269
"address.zip" : {"custom_mask" : "xxx" },
270
270
}
271
271
272
- masked_json_string = data_masker .erase (data , masking_rules = masking_rules )
272
+ masked_json_string = data_masker .erase (data = data , masking_rules = masking_rules )
273
273
274
274
# THEN the result should have all specified fields masked according to their rules
275
275
assert masked_json_string == {
@@ -285,8 +285,8 @@ def test_no_matches_for_masking_rule(data_masker):
285
285
masking_rules = {"$.missing_field" : {"dynamic_mask" : True }}
286
286
287
287
# WHEN applying the masking rule
288
- with pytest .warns (UserWarning , match = r"No matches found for path: \$\.missing_field " ):
289
- result = data_masker ._apply_masking_rules (data , masking_rules )
288
+ with pytest .warns (UserWarning , match = r"No matches found * " ):
289
+ result = data_masker .erase (data = data , masking_rules = masking_rules )
290
290
291
291
# THEN the original data remains unchanged
292
292
assert result == data
@@ -311,29 +311,16 @@ def erase(self, value, **kwargs):
311
311
assert masked_data ["value" ] == "test"
312
312
313
313
314
- def test_mask_nested_field_with_non_dict_value (data_masker ):
315
- # GIVEN nested data where a middle path component is not a dictionary
316
- data = {"user" : {"contact" : "not_a_dict" , "details" : {"ssn" : "123-45-6789" }}} # This will stop the traversal
317
-
318
- # WHEN attempting to mask a field through a path containing a non-dict value
319
- data_masker ._mask_nested_field (data , "user.contact.details.ssn" , lambda x : "MASKED" )
320
-
321
- # THEN the data should remain unchanged since traversal stopped at non-dict value
322
- assert data == {"user" : {"contact" : "not_a_dict" , "details" : {"ssn" : "123-45-6789" }}}
323
-
324
-
325
314
def test_mask_nested_field_success (data_masker ):
326
315
# GIVEN nested data with a field to mask
327
316
data = {"user" : {"contact" : {"details" : {"address" : {"street" : "123 Main St" , "zip" : "12345" }}}}}
328
317
329
318
# WHEN masking a nested field with a masking rule
330
- data_masker ._mask_nested_field (data , "user.contact.details.address.zip" , { " custom_mask" : " xxx"} )
319
+ data_masked = data_masker .erase (data = data , fields = [ "user.contact.details.address.zip" ], custom_mask = " xxx" )
331
320
332
321
# THEN the nested field should be masked while other data remains unchanged
333
- assert data == {"user" : {"contact" : {"details" : {"address" : {"street" : "123 Main St" , "zip" : "xxx" }}}}}
334
-
322
+ assert data_masked == {"user" : {"contact" : {"details" : {"address" : {"street" : "123 Main St" , "zip" : "xxx" }}}}}
335
323
336
- ## teste aqui
337
324
def test_erase_dictionary_with_masking_rules (data_masker ):
338
325
# GIVEN a dictionary with nested sensitive data
339
326
data = {"user" : {"name" : "John Doe" , "ssn" : "123-45-6789" , "address" : {"street" : "123 Main St" , "zip" : "12345" }}}
@@ -423,3 +410,15 @@ def test_erase_handles_empty_string_with_dynamic_mask(data_masker):
423
410
424
411
# THEN empty string should be returned
425
412
assert result == ""
413
+
414
+ def test_erase_dictionary_with_masking_rules_wrong_field (data_masker ):
415
+ # GIVEN a dictionary with nested sensitive data
416
+ data = {"user" : {"name" : "John Doe" , "ssn" : "123-45-6789" , "address" : {"street" : "123 Main St" , "zip" : "12345" }}}
417
+
418
+ # AND masking rules for specific fields
419
+ masking_rules = {"user.ssn..." : {"custom_mask" : "XXX-XX-XXXX" }, "user.address.zip" : {"custom_mask" : "00000" }}
420
+
421
+ # WHEN erase is called with wrong masking rules
422
+ # We must have a warning
423
+ with pytest .warns (UserWarning , match = "Error processing path*" ):
424
+ data_masker .erase (data , masking_rules = masking_rules )
0 commit comments