From 46fd18b6d1fee3d171b85c49b54aad02e608609f Mon Sep 17 00:00:00 2001 From: Paul Schreiber Date: Fri, 23 Feb 2024 12:43:26 -0500 Subject: [PATCH] fix: don't delete users when cleaning up old models (#1148) * fix: don't delete users when cleaning up old models * test: add user not deleted test; fix deletion test --- .../apps/core/management/commands/harddelete.py | 2 ++ terraso_backend/config/settings.py | 3 +++ .../tests/core/commands/test_harddelete.py | 14 +++++++++++++- 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/terraso_backend/apps/core/management/commands/harddelete.py b/terraso_backend/apps/core/management/commands/harddelete.py index b1c015f24..38e3ed3ec 100644 --- a/terraso_backend/apps/core/management/commands/harddelete.py +++ b/terraso_backend/apps/core/management/commands/harddelete.py @@ -43,6 +43,8 @@ def all_objects(cutoff_date): app_models = apps.get_models() objects = [] for model in app_models: + if model._meta.label in settings.DELETION_EXCEPTION_LIST: + continue for field in model._meta.fields: if field.name == "deleted_at" and isinstance(field, models.fields.DateTimeField): objects.extend( diff --git a/terraso_backend/config/settings.py b/terraso_backend/config/settings.py index cd29b9f62..721b0243c 100644 --- a/terraso_backend/config/settings.py +++ b/terraso_backend/config/settings.py @@ -385,6 +385,9 @@ HARD_DELETE_DELETION_GAP = config("HARD_DELETE_DELETION_GAP_DAYS", default="30", cast=config.eval) +# Preserve users to ensure audit logs are readable. +DELETION_EXCEPTION_LIST = ["core.User"] + class JWTProvider(TypedDict): """Type hint to indicate correct config for JWT_EXCHANGE_PROVIDERS""" diff --git a/terraso_backend/tests/core/commands/test_harddelete.py b/terraso_backend/tests/core/commands/test_harddelete.py index d647cc319..38f740f4a 100644 --- a/terraso_backend/tests/core/commands/test_harddelete.py +++ b/terraso_backend/tests/core/commands/test_harddelete.py @@ -23,7 +23,7 @@ pytestmark = pytest.mark.django_db -@pytest.mark.parametrize("model", [User, Group, DataEntry]) +@pytest.mark.parametrize("model", [Group, DataEntry]) def test_delete_model_deleted(model, delete_date): obj = mixer.blend(model) obj.delete() @@ -35,6 +35,18 @@ def test_delete_model_deleted(model, delete_date): ), "Model should be deleted" +@pytest.mark.parametrize("model", [User]) +def test_delete_user_not_deleted(model, delete_date): + obj = mixer.blend(model) + obj.delete() + obj.deleted_at = delete_date + obj.save(keep_deleted=True) + call_command("harddelete") + assert ( + model.objects.all(force_visibility=True).filter(id=obj.id).exists() + ), "Model should not be deleted" + + @pytest.mark.parametrize("model", [User, Group, DataEntry]) def test_delete_model_not_deleted(model, no_delete_date): obj = mixer.blend(model)