From f754ca2cad16a848ea902448a9103acd7643729b Mon Sep 17 00:00:00 2001 From: Hugo Rodger-Brown Date: Tue, 26 Sep 2023 17:44:14 +0300 Subject: [PATCH] Fix bug in auto redaction --- anonymiser/models.py | 7 +++++-- tests/test_models.py | 6 +++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/anonymiser/models.py b/anonymiser/models.py index cd295f6..e043800 100644 --- a/anonymiser/models.py +++ b/anonymiser/models.py @@ -189,7 +189,7 @@ def field_redaction_strategy(self, field: models.Field) -> FieldRedactionStrateg def redact_queryset( self, queryset: models.QuerySet[models.Model], - auto_redact: bool = auto_redact, + auto_redact_override: bool | None = None, **field_overrides: Any, ) -> int: """ @@ -210,7 +210,10 @@ def redact_queryset( """ redactions: dict[str, Any] = {} - if auto_redact: + auto = ( + self.auto_redact if auto_redact_override is None else auto_redact_override + ) + if auto: redactions.update(self.auto_field_redactions()) redactions.update(self.custom_field_redactions) redactions.update(field_overrides) diff --git a/tests/test_models.py b/tests/test_models.py index 0df971f..146bbe2 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -115,7 +115,7 @@ def test_redact_queryset_two( assert user.uuid != user2.uuid @pytest.mark.parametrize( - "auto_redact,location,biography", + "override,location,biography", [ (True, 255 * "X", 400 * "X"), (False, "London", "I am a test user"), @@ -125,11 +125,11 @@ def test_redact_queryset__auto_redact( self, user: User, user_redacter: UserRedacter, - auto_redact: bool, + override: bool, location: str, biography: str, ) -> None: - user_redacter.redact_queryset(User.objects.all(), auto_redact=auto_redact) + user_redacter.redact_queryset(User.objects.all(), auto_redact_override=override) user.refresh_from_db() # auto-redacted fields assert user.location == location