Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
export: Fix export of child tables when exporting mirror dummy users.
Mateusz Manderaacb1731bf903
diff --git a/zerver/lib/export.py b/zerver/lib/export.pyindex 2e67a8d82a5..011f9c92066 100644--- a/zerver/lib/export.py+++ b/zerver/lib/export.py@@ -102,6 +102,7 @@ IdSource: TypeAlias = tuple[TableName, Field]SourceFilter: TypeAlias = Callable[[Record], bool]CustomFetch: TypeAlias = Callable[[TableData, Context], None]+CustomReturnIds: TypeAlias = Callable[[TableData], set[int]]class MessagePartial(TypedDict):@@ -507,6 +508,7 @@ class Config:filter_args: FilterArgs | None = None,custom_fetch: CustomFetch | None = None,custom_tables: list[TableName] | None = None,+ custom_return_ids: CustomReturnIds | None = None,concat_and_destroy: list[TableName] | None = None,id_source: IdSource | None = None,source_filter: SourceFilter | None = None,@@ -527,6 +529,7 @@ class Config:self.exclude = excludeself.custom_fetch = custom_fetchself.custom_tables = custom_tables+ self.custom_return_ids = custom_return_idsself.concat_and_destroy = concat_and_destroyself.id_source = id_sourceself.source_filter = source_filter@@ -590,6 +593,13 @@ class Config:may be deeper issues going on.""")+ def return_ids(self, response: TableData) -> set[int]:+ if self.custom_return_ids is not None:+ return self.custom_return_ids(response)+ else:+ assert self.table is not None+ return {row["id"] for row in response[self.table]}+def export_from_config(response: TableData,@@ -655,7 +665,7 @@ def export_from_config(assert parent is not Noneassert parent.table is not Noneassert config.include_rows is not None- parent_ids = [r["id"] for r in response[parent.table]]+ parent_ids = parent.return_ids(response)filter_params: dict[str, Any] = {config.include_rows: parent_ids}if config.filter_args is not None:filter_params.update(config.filter_args)@@ -825,6 +835,12 @@ def get_realm_config() -> Config:"zerver_userprofile","zerver_userprofile_mirrordummy",],+ # When child tables want to fetch the list of ids of objects exported from+ # the parent table, they should get ids from both zerver_userprofile and+ # zerver_userprofile_mirrordummy:+ custom_return_ids=lambda table_data: {+ row["id"] for row in table_data["zerver_userprofile"]+ }.union({row["id"] for row in table_data["zerver_userprofile_mirrordummy"]}),# set table for children who treat us as normal parenttable="zerver_userprofile",virtual_parent=realm_config,diff --git a/zerver/tests/test_import_export.py b/zerver/tests/test_import_export.pyindex e6874dd6f94..250ad99a481 100644--- a/zerver/tests/test_import_export.py+++ b/zerver/tests/test_import_export.py@@ -594,48 +594,60 @@ class RealmImportExportTest(ExportFile):def test_export_realm_with_exportable_user_ids(self) -> None:realm = Realm.objects.get(string_id="zulip")- cordelia = self.example_user("iago")+ cordelia = self.example_user("cordelia")hamlet = self.example_user("hamlet")- user_ids = {cordelia.id, hamlet.id}+ polonius = self.example_user("polonius")+ iago = self.example_user("iago")+ othello = self.example_user("othello")- pm_a_msg_id = self.send_personal_message(- self.example_user("AARON"), self.example_user("othello")- )- pm_b_msg_id = self.send_personal_message(- self.example_user("cordelia"), self.example_user("iago")- )- pm_c_msg_id = self.send_personal_message(- self.example_user("hamlet"), self.example_user("othello")- )- pm_d_msg_id = self.send_personal_message(- self.example_user("iago"), self.example_user("hamlet")- )+ do_change_user_setting(cordelia, "allow_private_data_export", True, acting_user=cordelia)+ do_change_user_setting(hamlet, "allow_private_data_export", True, acting_user=hamlet)+ exportable_user_ids = {cordelia.id, hamlet.id}- self.export_realm_and_create_auditlog(realm, exportable_user_ids=user_ids)+ pm_a_msg_id = self.send_personal_message(polonius, othello)+ pm_b_msg_id = self.send_personal_message(cordelia, iago)+ pm_c_msg_id = self.send_personal_message(hamlet, othello)+ pm_d_msg_id = self.send_personal_message(iago, hamlet)- data = read_json("realm.json")+ self.export_realm_and_create_auditlog(+ realm,+ export_type=RealmExport.EXPORT_FULL_WITH_CONSENT,+ exportable_user_ids=exportable_user_ids,+ )- exported_user_emails = self.get_set(data["zerver_userprofile"], "delivery_email")- self.assertIn(self.example_email("iago"), exported_user_emails)- self.assertIn(self.example_email("hamlet"), exported_user_emails)+ realm_data = read_json("realm.json")++ exported_user_emails = self.get_set(realm_data["zerver_userprofile"], "delivery_email")+ self.assertIn(cordelia.delivery_email, exported_user_emails)+ self.assertIn(hamlet.delivery_email, exported_user_emails)self.assertNotIn("[email protected]", exported_user_emails)- self.assertNotIn(self.example_email("cordelia"), exported_user_emails)+ self.assertNotIn(iago.delivery_email, exported_user_emails)+ self.assertNotIn(polonius.delivery_email, exported_user_emails)- dummy_user_emails = self.get_set(data["zerver_userprofile_mirrordummy"], "delivery_email")- self.assertIn(self.example_email("cordelia"), dummy_user_emails)- self.assertIn(self.example_email("othello"), dummy_user_emails)+ dummy_user_emails = self.get_set(+ realm_data["zerver_userprofile_mirrordummy"], "delivery_email"+ )+ self.assertIn(iago.delivery_email, dummy_user_emails)+ self.assertIn(othello.delivery_email, dummy_user_emails)+ self.assertIn(polonius.delivery_email, dummy_user_emails)self.assertIn("[email protected]", dummy_user_emails)- self.assertNotIn(self.example_email("iago"), dummy_user_emails)- self.assertNotIn(self.example_email("hamlet"), dummy_user_emails)+ self.assertNotIn(cordelia.delivery_email, dummy_user_emails)+ self.assertNotIn(hamlet.delivery_email, dummy_user_emails)- data = read_json("messages-000001.json")+ message_data = read_json("messages-000001.json")- exported_message_ids = self.get_set(data["zerver_message"], "id")+ exported_message_ids = self.get_set(message_data["zerver_message"], "id")self.assertNotIn(pm_a_msg_id, exported_message_ids)self.assertIn(pm_b_msg_id, exported_message_ids)self.assertIn(pm_c_msg_id, exported_message_ids)self.assertIn(pm_d_msg_id, exported_message_ids)+ personal_recipient_type_ids = (+ r["type_id"] for r in realm_data["zerver_recipient"] if r["type"] == Recipient.PERSONAL+ )+ for user_profile_id in [cordelia.id, hamlet.id, iago.id, othello.id, polonius.id]:+ self.assertIn(user_profile_id, personal_recipient_type_ids)+def test_export_realm_with_member_consent(self) -> None:realm = Realm.objects.get(string_id="zulip")@@ -895,6 +907,12 @@ class RealmImportExportTest(ExportFile):# Deactivate a user to ensure such a case is covered.do_deactivate_user(self.example_user("aaron"), acting_user=None)+ # Turn another user into a mirror dummy+ prospero = self.example_user("prospero")+ prospero_email = prospero.delivery_email+ do_deactivate_user(prospero, acting_user=None)+ prospero.is_mirror_dummy = True+ prospero.save()# Change some authentication_methods so that some are enabled and some disabled# for this to be properly tested, as opposed to some special case@@ -1298,6 +1316,9 @@ class RealmImportExportTest(ExportFile):imported_message_with_thumbnail.rendered_content, expected_rendered_preview)+ imported_prospero_user = get_user_by_delivery_email(prospero_email, imported_realm)+ self.assertIsNotNone(imported_prospero_user.recipient)+def test_import_message_edit_history(self) -> None:realm = get_realm("zulip")iago = self.example_user("iago")@@ -1620,7 +1641,9 @@ class RealmImportExportTest(ExportFile):def get_usermessages_user(r: Realm) -> set[str]:messages = get_stream_messages(r).order_by("content")usermessage = UserMessage.objects.filter(message=messages[0])- usermessage_user = {um.user_profile.email for um in usermessage}+ usermessage_user = {+ um.user_profile.email for um in usermessage if not um.user_profile.is_mirror_dummy+ }return usermessage_user# tests to make sure that various data-*-ids in rendered_content