realm: Differentiate reserved realms from in-use realms.

Fixes: #23896.
This commit is contained in:
Alex Vandiver 2023-09-25 18:39:58 +00:00 committed by Tim Abbott
parent 8df42a729d
commit 71e297efb4
5 changed files with 39 additions and 20 deletions

View File

@ -599,21 +599,29 @@ class TestSupportEndpoint(ZulipTestCase):
"/activity/support", {"realm_id": f"{lear_realm.id}", "new_subdomain": "new-name"}
)
self.assert_in_success_response(
["Subdomain unavailable. Please choose a different one."], result
["Subdomain already in use. Please choose a different one."], result
)
result = self.client_post(
"/activity/support", {"realm_id": f"{lear_realm.id}", "new_subdomain": "zulip"}
)
self.assert_in_success_response(
["Subdomain unavailable. Please choose a different one."], result
["Subdomain already in use. Please choose a different one."], result
)
result = self.client_post(
"/activity/support", {"realm_id": f"{lear_realm.id}", "new_subdomain": "lear"}
)
self.assert_in_success_response(
["Subdomain unavailable. Please choose a different one."], result
["Subdomain already in use. Please choose a different one."], result
)
# Test renaming to a "reserved" subdomain
result = self.client_post(
"/activity/support", {"realm_id": f"{lear_realm.id}", "new_subdomain": "your-org"}
)
self.assert_in_success_response(
["Subdomain reserved. Please choose a different one."], result
)
def test_downgrade_realm(self) -> None:

View File

@ -81,12 +81,17 @@ def email_is_not_mit_mailing_list(email: str) -> None:
raise AssertionError("Unexpected DNS error")
class OverridableValidationError(ValidationError):
pass
def check_subdomain_available(subdomain: str, allow_reserved_subdomain: bool = False) -> None:
error_strings = {
"too short": _("Subdomain needs to have length 3 or greater."),
"extremal dash": _("Subdomain cannot start or end with a '-'."),
"bad character": _("Subdomain can only have lowercase letters, numbers, and '-'s."),
"unavailable": _("Subdomain unavailable. Please choose a different one."),
"unavailable": _("Subdomain already in use. Please choose a different one."),
"reserved": _("Subdomain reserved. Please choose a different one."),
}
if subdomain == Realm.SUBDOMAIN_FOR_ROOT_DOMAIN:
@ -102,7 +107,10 @@ def check_subdomain_available(subdomain: str, allow_reserved_subdomain: bool = F
if Realm.objects.filter(string_id=subdomain).exists():
raise ValidationError(error_strings["unavailable"])
if is_reserved_subdomain(subdomain) and not allow_reserved_subdomain:
raise ValidationError(error_strings["unavailable"])
raise OverridableValidationError(
error_strings["reserved"],
"Pass --allow-reserved-subdomain to override",
)
def email_not_system_bot(email: str) -> None:

View File

@ -10,7 +10,7 @@ from django.core.exceptions import ValidationError
from django.core.management import call_command
from django.core.management.base import BaseCommand, CommandError, CommandParser
from zerver.forms import check_subdomain_available
from zerver.forms import OverridableValidationError, check_subdomain_available
from zerver.lib.import_realm import do_import_realm
@ -79,11 +79,13 @@ import a database dump from one or more JSON files."""
try:
check_subdomain_available(subdomain, allow_reserved_subdomain)
except ValidationError as e:
except OverridableValidationError as e:
raise CommandError(
e.messages[0]
+ "\nPass --allow-reserved-subdomain to override subdomain restrictions."
)
except ValidationError as e:
raise CommandError(e.messages[0])
paths = []
for path in options["export_paths"]:

View File

@ -223,7 +223,7 @@ class RealmTest(ZulipTestCase):
)
realm.save()
result = self.client_patch("/json/realm", data)
self.assert_json_error(result, "Subdomain unavailable. Please choose a different one.")
self.assert_json_error(result, "Subdomain already in use. Please choose a different one.")
# Now try to change the string_id to something available.
data = dict(string_id="coolrealm")

View File

@ -1845,11 +1845,11 @@ class RealmCreationTest(ZulipTestCase):
"-id": "cannot start or end with a",
"string-ID": "lowercase letters",
"string_id": "lowercase letters",
"stream": "unavailable",
"streams": "unavailable",
"about": "unavailable",
"abouts": "unavailable",
"zephyr": "unavailable",
"stream": "reserved",
"streams": "reserved",
"about": "reserved",
"abouts": "reserved",
"zephyr": "already in use",
}
for string_id, error_msg in errors.items():
result = self.submit_realm_creation_form(
@ -1881,7 +1881,7 @@ class RealmCreationTest(ZulipTestCase):
email = "user1@test.com"
result = self.submit_realm_creation_form(email, realm_subdomain="test", realm_name="Test")
self.assert_in_response("Subdomain unavailable. Please choose a different one.", result)
self.assert_in_response("Subdomain reserved. Please choose a different one.", result)
@override_settings(OPEN_REALM_CREATION=True)
def test_subdomain_restrictions_root_domain(self) -> None:
@ -1894,7 +1894,7 @@ class RealmCreationTest(ZulipTestCase):
result = self.submit_realm_creation_form(
email, realm_subdomain="", realm_name=realm_name
)
self.assert_in_response("unavailable", result)
self.assert_in_response("already in use", result)
# test valid use of root domain
result = self.submit_realm_creation_form(email, realm_subdomain="", realm_name=realm_name)
@ -1921,7 +1921,7 @@ class RealmCreationTest(ZulipTestCase):
result = self.submit_realm_creation_form(
email, realm_subdomain="abcdef", realm_name=realm_name, realm_in_root_domain="true"
)
self.assert_in_response("unavailable", result)
self.assert_in_response("already in use", result)
# test valid use of root domain
result = self.submit_realm_creation_form(
@ -1956,7 +1956,7 @@ class RealmCreationTest(ZulipTestCase):
def test_subdomain_check_api(self) -> None:
result = self.client_get("/json/realm/subdomain/zulip")
self.assert_in_success_response(
["Subdomain unavailable. Please choose a different one."], result
["Subdomain already in use. Please choose a different one."], result
)
result = self.client_get("/json/realm/subdomain/zu_lip")
@ -1967,12 +1967,13 @@ class RealmCreationTest(ZulipTestCase):
with self.settings(SOCIAL_AUTH_SUBDOMAIN="zulipauth"):
result = self.client_get("/json/realm/subdomain/zulipauth")
self.assert_in_success_response(
["Subdomain unavailable. Please choose a different one."], result
["Subdomain reserved. Please choose a different one."], result
)
result = self.client_get("/json/realm/subdomain/hufflepuff")
self.assert_in_success_response(["available"], result)
self.assert_not_in_success_response(["unavailable"], result)
self.assert_not_in_success_response(["already in use"], result)
self.assert_not_in_success_response(["reserved"], result)
def test_subdomain_check_management_command(self) -> None:
# Short names should not work, even with the flag
@ -2719,7 +2720,7 @@ class UserSignUpTest(ZulipTestCase):
)
self.assert_in_success_response(
[
"Subdomain unavailable. Please choose a different one.",
"Subdomain already in use. Please choose a different one.",
'value="Test"',
'name="realm_name"',
],