From a3b892fc24fefd91edbbd32de446abb444530b3a Mon Sep 17 00:00:00 2001 From: Hugo Rodger-Brown Date: Mon, 18 Sep 2023 13:25:12 +0100 Subject: [PATCH] Update tests --- .github/workflows/tox.yml | 15 +++------ tests/conftest.py | 12 +++++-- ...graphy_user_date_of_birth_user_location.py | 27 ++++++++++++++++ tests/models.py | 3 ++ tests/test_models.py | 3 ++ tests/test_models_pg.py | 31 +++++++++++++++++++ tox.ini | 2 +- 7 files changed, 80 insertions(+), 13 deletions(-) create mode 100644 tests/migrations/0003_user_biography_user_date_of_birth_user_location.py diff --git a/.github/workflows/tox.yml b/.github/workflows/tox.yml index dad5684..34a8608 100644 --- a/.github/workflows/tox.yml +++ b/.github/workflows/tox.yml @@ -74,24 +74,19 @@ jobs: --health-timeout 5s --health-retries 5 - strategy: - matrix: - python: ["3.11"] - django: ["42"] - - env: - TOXENV: postgres + # env: + # TOXENV: pg-only steps: - name: Check out the repository uses: actions/checkout@v3 - - name: Set up Python ${{ matrix.python }} + - name: Set up Python 3.11 uses: actions/setup-python@v4 with: - python-version: ${{ matrix.python }} + python-version: "3.11" - name: Install and run tox run: | pip install tox - tox + tox -e pg-only diff --git a/tests/conftest.py b/tests/conftest.py index 55feec2..17c5082 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -13,14 +13,22 @@ @pytest.fixture def user() -> User: return User.objects.create_user( - username="testuser1", first_name="fred", last_name="flintstone" + username="testuser1", + first_name="fred", + last_name="flintstone", + location="London", + biography="I am a test user", ) @pytest.fixture def user2() -> User: return User.objects.create_user( - username="testuser2", first_name="ginger", last_name="rogers" + username="testuser2", + first_name="ginger", + last_name="rogers", + location="New York", + biography="I am another test user", ) diff --git a/tests/migrations/0003_user_biography_user_date_of_birth_user_location.py b/tests/migrations/0003_user_biography_user_date_of_birth_user_location.py new file mode 100644 index 0000000..a95864f --- /dev/null +++ b/tests/migrations/0003_user_biography_user_date_of_birth_user_location.py @@ -0,0 +1,27 @@ +# Generated by Django 4.2.5 on 2023-09-18 12:12 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("tests", "0002_user_uuid"), + ] + + operations = [ + migrations.AddField( + model_name="user", + name="biography", + field=models.TextField(blank=True), + ), + migrations.AddField( + model_name="user", + name="date_of_birth", + field=models.DateField(blank=True, null=True), + ), + migrations.AddField( + model_name="user", + name="location", + field=models.CharField(blank=True, max_length=255), + ), + ] diff --git a/tests/models.py b/tests/models.py index 56cf406..6beae49 100644 --- a/tests/models.py +++ b/tests/models.py @@ -6,3 +6,6 @@ class User(AbstractUser): uuid = models.UUIDField(unique=True, default=uuid4, editable=False) + location = models.CharField(max_length=255, blank=True) + biography = models.TextField(blank=True) + date_of_birth = models.DateField(blank=True, null=True) diff --git a/tests/test_models.py b/tests/test_models.py index 4993e9b..186f929 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -22,6 +22,9 @@ def test_model_fields_summary(user_anonymiser: UserAnonymiser) -> None: FieldSummaryData(User._meta.get_field("is_active"), False), FieldSummaryData(User._meta.get_field("date_joined"), False), FieldSummaryData(User._meta.get_field("uuid"), False), + FieldSummaryData(User._meta.get_field("location"), False), + FieldSummaryData(User._meta.get_field("biography"), False), + FieldSummaryData(User._meta.get_field("date_of_birth"), False), FieldSummaryData(User._meta.get_field("groups"), False), FieldSummaryData(User._meta.get_field("user_permissions"), False), ] diff --git a/tests/test_models_pg.py b/tests/test_models_pg.py index 47a5ffb..d770ed2 100644 --- a/tests/test_models_pg.py +++ b/tests/test_models_pg.py @@ -60,3 +60,34 @@ def test_redact_queryset_two( user2.refresh_from_db() # confirm that we haven't reused the same uuid for all objects assert user.uuid != user2.uuid + + @pytest.mark.parametrize( + "auto_redact,location,biography", + [ + (True, 255 * "X", 400 * "X"), + (False, "London", "I am a test user"), + ], + ) + def test_redact_queryset__auto_redact( + self, + user: User, + user_anonymiser: UserAnonymiser, + auto_redact: bool, + location: str, + biography: str, + ) -> None: + user_anonymiser.redact_queryset(User.objects.all(), auto_redact=auto_redact) + user.refresh_from_db() + # auto-redacted fields + assert user.location == location + assert user.biography == biography + + def test_redact_queryset__field_overrides( + self, + user: User, + user_anonymiser: UserAnonymiser, + ) -> None: + user_anonymiser.redact_queryset(User.objects.all(), location="Area 51") + user.refresh_from_db() + # auto-redacted fields + assert user.location == "Area 51" diff --git a/tox.ini b/tox.ini index e1a8154..dfb035b 100644 --- a/tox.ini +++ b/tox.ini @@ -22,7 +22,7 @@ deps = commands = pytest --cov=anonymiser --verbose --ds={env:DJANGO_SETTINGS_MODULE} tests/ -[testenv:postgres] +[testenv:pg-only] deps = pytest pytest-django