Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
slack_import: Abort early when the subdomain is already taken.
Aman Agrawalc574925541c1
diff --git a/zerver/actions/data_import.py b/zerver/actions/data_import.pyindex fe558346793..d07aee78c77 100644--- a/zerver/actions/data_import.py+++ b/zerver/actions/data_import.py@@ -47,6 +47,20 @@ def import_slack_data(event: dict[str, Any]) -> None:preregistration_realm = PreregistrationRealm.objects.get(id=event["preregistration_realm_id"])string_id = preregistration_realm.string_id++ if Realm.objects.filter(string_id=string_id).exists():+ # The subdomain is already taken -- e.g. by an earlier or+ # concurrent import of the same subdomain that won the race to+ # create the realm (string_id is unique). Importing would fail+ # on that unique constraint, and we must not touch the existing+ # realm, so abort before the expensive conversion work and+ # surface the conflict to the user.+ logger.error("(%s) Aborting Slack import: subdomain is already in use", string_id)+ preregistration_realm.data_import_metadata["is_import_work_queued"] = False+ preregistration_realm.data_import_metadata["subdomain_unavailable"] = True+ preregistration_realm.save(update_fields=["data_import_metadata"])+ return+output_dir = tempfile.mkdtemp(prefix=f"import-{preregistration_realm.id}-converted-",dir=settings.IMPORT_TMPFILE_DIRECTORY,diff --git a/zerver/tests/test_slack_importer.py b/zerver/tests/test_slack_importer.pyindex bff0cfa055a..4d82b119692 100644--- a/zerver/tests/test_slack_importer.py+++ b/zerver/tests/test_slack_importer.py@@ -3058,6 +3058,56 @@ To Do# A non-string_id error must not be reported as a taken subdomain.self.assertNotIn("subdomain_unavailable", prereg_realm.data_import_metadata)+ @mock.patch("zerver.actions.data_import.do_import_realm")+ @mock.patch("zerver.actions.data_import.do_convert_zipfile")+ @mock.patch("zerver.actions.data_import.save_attachment_contents")+ def test_import_slack_data_aborts_when_subdomain_taken(+ self,+ mock_save_attachment_contents: mock.Mock,+ mock_do_convert_zipfile: mock.Mock,+ mock_do_import_realm: mock.Mock,+ ) -> None:+ # If a realm with this subdomain already exists when the import job+ # starts -- e.g. an earlier import of the same subdomain finished+ # first -- abort without attempting the (doomed) import and without+ # touching the existing realm.+ prereg_realm = PreregistrationRealm.objects.create(+ string_id="test-realm",+ name="Test Realm",+ email="[email protected]",+ data_import_metadata={"import_from": "slack", "is_import_work_queued": True},+ )+ other_realm = do_create_realm(+ string_id=prereg_realm.string_id,+ name=prereg_realm.name,+ )+ other_user = UserProfile.objects.create(+ realm=other_realm,+ delivery_email="[email protected]",+ email="[email protected]",+ )+ event = {+ "preregistration_realm_id": prereg_realm.id,+ "filename": "import/test/slack.zip",+ "slack_access_token": "xoxb-valid-token",+ }++ with self.assertLogs("zulip.registration", "ERROR") as logs:+ import_slack_data(event)+ self.assertIn("subdomain is already in use", logs.output[0])++ # The import was not attempted, and the existing realm is untouched.+ mock_save_attachment_contents.assert_not_called()+ mock_do_convert_zipfile.assert_not_called()+ mock_do_import_realm.assert_not_called()+ self.assertTrue(Realm.objects.filter(id=other_realm.id).exists())+ self.assertTrue(UserProfile.objects.filter(id=other_user.id).exists())+ prereg_realm.refresh_from_db()+ self.assertIsNone(prereg_realm.created_realm)+ self.assertFalse(prereg_realm.data_import_metadata["is_import_work_queued"])+ # The conflict is recorded so the status poll can surface it.+ self.assertTrue(prereg_realm.data_import_metadata["subdomain_unavailable"])+@responses.activatedef test_cancel_realm_import(self) -> None:# Choose import from slack