Skip to content

Commit ce4fbac

Browse files
Refactoring tests
1 parent ee86a06 commit ce4fbac

File tree

3 files changed

+56
-18
lines changed

3 files changed

+56
-18
lines changed

noxfile.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ def test_with_aws_encryption_sdk_as_required_package(session: nox.Session):
140140
folders=[
141141
f"{PREFIX_TESTS_FUNCTIONAL}/data_masking/_aws_encryption_sdk/",
142142
f"{PREFIX_TESTS_UNIT}/data_masking/_aws_encryption_sdk/",
143+
f"{PREFIX_TESTS_FUNCTIONAL}/data_masking/required_dependencies/",
144+
f"{PREFIX_TESTS_UNIT}/data_masking/required_dependencies/",
143145
],
144146
extras="datamasking",
145147
)

tests/unit/data_masking/_aws_encryption_sdk/test_unit_data_masking.py renamed to tests/functional/data_masking/required_dependencies/test_erase_data_masking.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ def test_erase_json_dict_with_complex_masking_rules(data_masker):
269269
"address.zip": {"custom_mask": "xxx"},
270270
}
271271

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)
273273

274274
# THEN the result should have all specified fields masked according to their rules
275275
assert masked_json_string == {
@@ -285,8 +285,8 @@ def test_no_matches_for_masking_rule(data_masker):
285285
masking_rules = {"$.missing_field": {"dynamic_mask": True}}
286286

287287
# 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)
290290

291291
# THEN the original data remains unchanged
292292
assert result == data
@@ -311,29 +311,16 @@ def erase(self, value, **kwargs):
311311
assert masked_data["value"] == "test"
312312

313313

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-
325314
def test_mask_nested_field_success(data_masker):
326315
# GIVEN nested data with a field to mask
327316
data = {"user": {"contact": {"details": {"address": {"street": "123 Main St", "zip": "12345"}}}}}
328317

329318
# 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")
331320

332321
# 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"}}}}}
335323

336-
## teste aqui
337324
def test_erase_dictionary_with_masking_rules(data_masker):
338325
# GIVEN a dictionary with nested sensitive data
339326
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):
423410

424411
# THEN empty string should be returned
425412
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)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
import json
3+
4+
import pytest
5+
6+
from aws_lambda_powertools.utilities.data_masking.base import DataMasking
7+
from aws_lambda_powertools.utilities.data_masking.constants import DATA_MASKING_STRING
8+
from aws_lambda_powertools.utilities.data_masking.exceptions import (
9+
DataMaskingFieldNotFoundError,
10+
DataMaskingUnsupportedTypeError,
11+
)
12+
13+
14+
@pytest.fixture
15+
def data_masker() -> DataMasking:
16+
return DataMasking()
17+
18+
def test_mask_nested_field_with_non_dict_value(data_masker):
19+
# GIVEN nested data where a middle path component is not a dictionary
20+
data = {"user": {"contact": "not_a_dict", "details": {"ssn": "123-45-6789"}}} # This will stop the traversal
21+
22+
# WHEN attempting to mask a field through a path containing a non-dict value
23+
data_masker._mask_nested_field(data, "user.contact.details.ssn", lambda x: "MASKED")
24+
25+
# THEN the data should remain unchanged since traversal stopped at non-dict value
26+
assert data == {"user": {"contact": "not_a_dict", "details": {"ssn": "123-45-6789"}}}
27+
28+
29+
def test_mask_nested_field_success(data_masker):
30+
# GIVEN nested data with a field to mask
31+
data = {"user": {"contact": {"details": {"address": {"street": "123 Main St", "zip": "12345"}}}}}
32+
33+
# WHEN masking a nested field with a masking rule
34+
data_masker._mask_nested_field(data, "user.contact.details.address.zip", {"custom_mask": "xxx"})
35+
36+
# THEN the nested field should be masked while other data remains unchanged
37+
assert data == {"user": {"contact": {"details": {"address": {"street": "123 Main St", "zip": "xxx"}}}}}

0 commit comments

Comments
 (0)