Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

slack_import: Re-validate subdomain availability when polling status.

Aman Agrawale640ccffa694
diff --git a/zerver/tests/test_slack_importer.py b/zerver/tests/test_slack_importer.py
index bd12f345981..7c90419eafb 100644
--- a/zerver/tests/test_slack_importer.py
+++ b/zerver/tests/test_slack_importer.py
@@ -3241,6 +3241,38 @@ To Do
# The other realm must not be reported as this import's result.
self.assertNotIn("Done", response_dict["status"])
+ def test_realm_import_status_clears_stale_subdomain_unavailable_flag(self) -> None:
+ # If the import that won the race for the subdomain later failed and
+ # freed it, this import's stored subdomain_unavailable flag is stale.
+ # The status poll must clear the flag and report this import's own
+ # failure, rather than telling the user the URL is still taken.
+ prereg_realm = PreregistrationRealm.objects.create(
+ string_id="freed-subdomain",
+ name="Test Realm",
+ data_import_metadata={
+ "import_from": "slack",
+ "is_import_work_queued": False,
+ "subdomain_unavailable": True,
+ },
+ )
+ self.assertFalse(Realm.objects.filter(string_id="freed-subdomain").exists())
+ confirmation_key = create_confirmation_link(
+ prereg_realm,
+ Confirmation.NEW_REALM_USER_REGISTRATION,
+ no_associated_realm_object=True,
+ ).split("/")[-1]
+
+ result = self.client_get(f"/json/realm/import/status/{confirmation_key}")
+ response_dict = self.assert_json_success(result)
+ # The freed subdomain is not reported as taken; this import's own
+ # failure surfaces instead.
+ self.assertNotIn("no longer available", response_dict["status"])
+ self.assertEqual(response_dict["status"], "unexpected_import_failure")
+ # The stale flag has been cleared from the stored metadata.
+ prereg_realm.refresh_from_db()
+ self.assertNotIn("subdomain_unavailable", prereg_realm.data_import_metadata)
+
def test_start_slack_import_without_uploaded_file_renders_page(self) -> None:
# POSTing "start import" before the export file finished uploading must
# re-render the import page, not 500 on an assertion.
diff --git a/zerver/views/registration.py b/zerver/views/registration.py
index 13b2bea9caf..bde15de53fd 100644
--- a/zerver/views/registration.py
+++ b/zerver/views/registration.py
@@ -1221,21 +1221,27 @@ def realm_import_status(
# happens to share the subdomain.
if preregistration_realm.created_realm is None:
if metadata.get("subdomain_unavailable"):
- # Another import already registered this subdomain. Send the
- # user back to registration to choose a different one.
- return json_success(
- request,
- {
- "status": _(
- "This organization URL is no longer available. "
- "Please choose a different URL."
- ),
- "redirect": reverse(
- "get_prereg_key_and_redirect", kwargs={"confirmation_key": confirmation_key}
- ),
- },
- )
- elif metadata.get("invalid_file_error_message"):
+ if Realm.objects.filter(string_id=preregistration_realm.string_id).exists():
+ # A realm still holds this subdomain. Send the user back to
+ # registration to choose a different one.
+ return json_success(
+ request,
+ {
+ "status": _(
+ "This organization URL is no longer available. "
+ "Please choose a different URL."
+ ),
+ "redirect": reverse(
+ "get_prereg_key_and_redirect",
+ kwargs={"confirmation_key": confirmation_key},
+ ),
+ },
+ )
+ # The subdomain has since been freed, so the stored flag is stale;
+ # clear it and report this import's own failure below.
+ del metadata["subdomain_unavailable"]
+ preregistration_realm.save(update_fields=["data_import_metadata"])
+ if metadata.get("invalid_file_error_message"):
# Redirect user the file upload page if we have an error message to display.
return json_success(
request,