slack import: Be less strict in `check_subdomain_available`.

If the sysadmin is doing something explicit in a management command,
it's OK to take a reserved or short subdomain.

Fixes #9166.
This commit is contained in:
Preston Hansen 2018-04-23 13:02:45 -05:00 committed by Tim Abbott
parent 048f15e975
commit 0258d7db0d
3 changed files with 15 additions and 3 deletions

View File

@ -55,7 +55,7 @@ def email_is_not_mit_mailing_list(email: Text) -> None:
else: else:
raise AssertionError("Unexpected DNS error") raise AssertionError("Unexpected DNS error")
def check_subdomain_available(subdomain: str) -> None: def check_subdomain_available(subdomain: str, from_management_command: bool=False) -> None:
error_strings = { error_strings = {
'too short': _("Subdomain needs to have length 3 or greater."), 'too short': _("Subdomain needs to have length 3 or greater."),
'extremal dash': _("Subdomain cannot start or end with a '-'."), 'extremal dash': _("Subdomain cannot start or end with a '-'."),
@ -70,6 +70,8 @@ def check_subdomain_available(subdomain: str) -> None:
raise ValidationError(error_strings['extremal dash']) raise ValidationError(error_strings['extremal dash'])
if not re.match('^[a-z0-9-]*$', subdomain): if not re.match('^[a-z0-9-]*$', subdomain):
raise ValidationError(error_strings['bad character']) raise ValidationError(error_strings['bad character'])
if from_management_command:
return
if len(subdomain) < 3: if len(subdomain) < 3:
raise ValidationError(error_strings['too short']) raise ValidationError(error_strings['too short'])
if is_reserved_subdomain(subdomain) or \ if is_reserved_subdomain(subdomain) or \

View File

@ -74,7 +74,7 @@ import a database dump from one or more JSON files."""
for model in models_to_import: for model in models_to_import:
self.new_instance_check(model) self.new_instance_check(model)
check_subdomain_available(subdomain) check_subdomain_available(subdomain, from_management_command=True)
for path in options['export_files']: for path in options['export_files']:
if not os.path.exists(path): if not os.path.exists(path):

View File

@ -6,6 +6,7 @@ from django.contrib.sites.models import Site
from django.http import HttpResponse from django.http import HttpResponse
from django.test import TestCase, override_settings from django.test import TestCase, override_settings
from django.utils.timezone import now as timezone_now from django.utils.timezone import now as timezone_now
from django.core.exceptions import ValidationError
from mock import patch, MagicMock from mock import patch, MagicMock
from zerver.lib.test_helpers import MockLDAP from zerver.lib.test_helpers import MockLDAP
@ -14,7 +15,7 @@ from confirmation.models import Confirmation, create_confirmation_link, Multiuse
generate_key, confirmation_url, get_object_from_key, ConfirmationKeyException generate_key, confirmation_url, get_object_from_key, ConfirmationKeyException
from confirmation import settings as confirmation_settings from confirmation import settings as confirmation_settings
from zerver.forms import HomepageForm, WRONG_SUBDOMAIN_ERROR from zerver.forms import HomepageForm, WRONG_SUBDOMAIN_ERROR, check_subdomain_available
from zerver.lib.actions import do_change_password from zerver.lib.actions import do_change_password
from zerver.views.auth import login_or_register_remote_user, \ from zerver.views.auth import login_or_register_remote_user, \
redirect_and_log_into_subdomain redirect_and_log_into_subdomain
@ -1521,6 +1522,15 @@ class RealmCreationTest(ZulipTestCase):
self.assert_in_success_response(["available"], result) self.assert_in_success_response(["available"], result)
self.assert_not_in_success_response(["unavailable"], result) self.assert_not_in_success_response(["unavailable"], result)
def test_subdomain_check_management_command(self) -> None:
# Short names should work
check_subdomain_available('aa', from_management_command=True)
# So should reserved ones
check_subdomain_available('zulip', from_management_command=True)
# malformed names should still not
with self.assertRaises(ValidationError):
check_subdomain_available('-ba_d-', from_management_command=True)
class UserSignUpTest(ZulipTestCase): class UserSignUpTest(ZulipTestCase):
def _assert_redirected_to(self, result: HttpResponse, url: Text) -> None: def _assert_redirected_to(self, result: HttpResponse, url: Text) -> None: