From 631577a892520364c81810b7e3fbb82d329ba3d6 Mon Sep 17 00:00:00 2001 From: Hugo Rodger-Brown Date: Mon, 18 Sep 2023 16:22:38 +0100 Subject: [PATCH] Add tests for auto-redact --- tests/test_models.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/test_models.py b/tests/test_models.py index 186f929..f5345c3 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -1,6 +1,7 @@ from unittest import mock import pytest +from django.db import models from anonymiser.models import FieldSummaryData @@ -90,6 +91,30 @@ def test_anonymise_queryset( assert user_anonymiser.anonymise_queryset(User.objects.none()) == 0 assert user_anonymiser.anonymise_queryset(User.objects.all()) == 1 + @mock.patch.object(UserAnonymiser, "get_model_fields") + def test_auto_redact( + self, mock_get_fields: mock.Mock, user_anonymiser: UserAnonymiser + ) -> None: + mock_get_fields.return_value = [ + # redact to 255 chars + models.CharField(name="char_field", max_length=255), + # redact to 400 chars + models.TextField(name="text_field"), + # don't redact (choices) + models.CharField(name="choices", max_length=255, choices=[("a", "A")]), + # don't redact (unique) + models.CharField(name="unique", max_length=255, unique=True), + # don't redact (primary key) + models.CharField(name="primary_key", max_length=255, primary_key=True), + # don't redact (IntegerField, DateField, etc) + models.IntegerField(name="integer_field"), + models.DateField(name="date_field"), + ] + assert user_anonymiser.auto_field_redactions() == { + "char_field": 255 * "X", + "text_field": 400 * "X", + } + def test_bad_anonymiser() -> None: with pytest.raises(AttributeError):