Skip to content

Commit

Permalink
Add tests for auto-redact
Browse files Browse the repository at this point in the history
  • Loading branch information
hugorodgerbrown committed Sep 18, 2023
1 parent 673f2a1 commit 631577a
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from unittest import mock

import pytest
from django.db import models

from anonymiser.models import FieldSummaryData

Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 631577a

Please sign in to comment.