Skip to content

Commit

Permalink
fix: don't delete users when cleaning up old models (#1148)
Browse files Browse the repository at this point in the history
* fix: don't delete users when cleaning up old models

* test: add user not deleted test; fix deletion test
  • Loading branch information
paulschreiber authored Feb 23, 2024
1 parent 4b305a5 commit 46fd18b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
2 changes: 2 additions & 0 deletions terraso_backend/apps/core/management/commands/harddelete.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
3 changes: 3 additions & 0 deletions terraso_backend/config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down
14 changes: 13 additions & 1 deletion terraso_backend/tests/core/commands/test_harddelete.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand Down

0 comments on commit 46fd18b

Please sign in to comment.