Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
scrub_realm: Delete dangling ImageAttachment rows as well.
Alex Vandiver3a57feccaa5d
diff --git a/zerver/actions/realm_settings.py b/zerver/actions/realm_settings.pyindex 1964b6adcb9..d29f9ccf178 100644--- a/zerver/actions/realm_settings.py+++ b/zerver/actions/realm_settings.py@@ -37,6 +37,7 @@ from zerver.lib.utils import optional_bytes_to_mibfrom zerver.models import (ArchivedAttachment,Attachment,+ ImageAttachment,Message,NamedUserGroup,Realm,@@ -724,6 +725,7 @@ def do_delete_all_realm_attachments(realm: Realm, *, batch_size: int = 1000) ->if len(to_delete) > 0:delete_message_attachments([row[1] for row in to_delete])last_id = to_delete[len(to_delete) - 1][0]+ ImageAttachment.objects.filter(path_id__in=[row[1] for row in to_delete]).delete()if len(to_delete) < batch_size:breakobj_class._default_manager.filter(realm=realm).delete()diff --git a/zerver/tests/test_realm.py b/zerver/tests/test_realm.pyindex 4d76dfffc6b..f18f98d3e05 100644--- a/zerver/tests/test_realm.py+++ b/zerver/tests/test_realm.py@@ -6,6 +6,7 @@ import reimport stringfrom datetime import datetime, timedeltafrom enum import Enum+from pathlib import Pathfrom typing import Anyfrom unittest import mock, skipUnless@@ -51,7 +52,12 @@ from zerver.lib.realm_description import get_realm_rendered_description, get_reafrom zerver.lib.send_email import send_future_emailfrom zerver.lib.streams import create_stream_if_neededfrom zerver.lib.test_classes import ZulipTestCase-from zerver.lib.test_helpers import activate_push_notification_service, get_test_image_file+from zerver.lib.test_helpers import (+ activate_push_notification_service,+ get_test_image_file,+ read_test_image_file,+)+from zerver.lib.thumbnail import ThumbnailFormatfrom zerver.lib.upload import (delete_message_attachments,upload_avatar_image,@@ -60,6 +66,7 @@ from zerver.lib.upload import (from zerver.models import (Attachment,CustomProfileField,+ ImageAttachment,Message,NamedUserGroup,Realm,@@ -3055,19 +3062,28 @@ class ScrubRealmTest(ZulipTestCase):realm = get_realm("zulip")hamlet = self.example_user("hamlet")Attachment.objects.filter(realm=realm).delete()+ self.assertEqual(ImageAttachment.objects.all().count(), 0)assert settings.LOCAL_UPLOADS_DIR is not Noneassert settings.LOCAL_FILES_DIR is not None- path_ids = []- for n in range(1, 4):- content = f"content{n}".encode()- url = upload_message_attachment(f"dummy{n}.txt", "text/plain", content, hamlet)[0]- base = "/user_uploads/"- self.assertEqual(base, url[: len(base)])- path_id = re.sub(r"/user_uploads/", "", url)- self.assertTrue(os.path.isfile(os.path.join(settings.LOCAL_FILES_DIR, path_id)))- path_ids.append(path_id)+ self.assertEqual([p for p in Path(settings.LOCAL_FILES_DIR).rglob("*") if p.is_file()], [])+ path_ids = []+ small_thumb = ThumbnailFormat("webp", 50, 50, animated=False)+ big_thumb = ThumbnailFormat("webp", 100, 100, animated=False)+ with (+ self.thumbnail_formats(small_thumb, big_thumb),+ self.captureOnCommitCallbacks(execute=True),+ ):+ for n in range(1, 4):+ url = upload_message_attachment(+ f"img-{n}.png", "image/png", read_test_image_file("img.png"), hamlet+ )[0]+ path_id = re.sub(r"/user_uploads/", "", url)+ self.assertTrue(os.path.isfile(os.path.join(settings.LOCAL_FILES_DIR, path_id)))+ path_ids.append(path_id)++ self.assert_length([p for p in Path(settings.LOCAL_FILES_DIR).rglob("*") if p.is_file()], 9)with mock.patch("zerver.actions.realm_settings.delete_message_attachments",side_effect=delete_message_attachments,@@ -3082,8 +3098,9 @@ class ScrubRealmTest(ZulipTestCase):])self.assertEqual(Attachment.objects.filter(realm=realm).count(), 0)- for file_path in path_ids:- self.assertFalse(os.path.isfile(os.path.join(settings.LOCAL_FILES_DIR, path_id)))+ self.assertEqual(ImageAttachment.objects.all().count(), 0)++ self.assertEqual([p for p in Path(settings.LOCAL_FILES_DIR).rglob("*") if p.is_file()], [])def test_scrub_realm(self) -> None:zulip = get_realm("zulip")