From 0258d7db0d4287864d076653e0a61fe42c240627 Mon Sep 17 00:00:00 2001 From: Preston Hansen Date: Mon, 23 Apr 2018 13:02:45 -0500 Subject: [PATCH] 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. --- zerver/forms.py | 4 +++- zerver/management/commands/import.py | 2 +- zerver/tests/test_signup.py | 12 +++++++++++- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/zerver/forms.py b/zerver/forms.py index 0378c70b03..68a72429a6 100644 --- a/zerver/forms.py +++ b/zerver/forms.py @@ -55,7 +55,7 @@ def email_is_not_mit_mailing_list(email: Text) -> None: else: 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 = { 'too short': _("Subdomain needs to have length 3 or greater."), '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']) if not re.match('^[a-z0-9-]*$', subdomain): raise ValidationError(error_strings['bad character']) + if from_management_command: + return if len(subdomain) < 3: raise ValidationError(error_strings['too short']) if is_reserved_subdomain(subdomain) or \ diff --git a/zerver/management/commands/import.py b/zerver/management/commands/import.py index 45c524c03a..7d616eb2a8 100644 --- a/zerver/management/commands/import.py +++ b/zerver/management/commands/import.py @@ -74,7 +74,7 @@ import a database dump from one or more JSON files.""" for model in models_to_import: self.new_instance_check(model) - check_subdomain_available(subdomain) + check_subdomain_available(subdomain, from_management_command=True) for path in options['export_files']: if not os.path.exists(path): diff --git a/zerver/tests/test_signup.py b/zerver/tests/test_signup.py index 7ea413a962..c68cb5fe62 100644 --- a/zerver/tests/test_signup.py +++ b/zerver/tests/test_signup.py @@ -6,6 +6,7 @@ from django.contrib.sites.models import Site from django.http import HttpResponse from django.test import TestCase, override_settings from django.utils.timezone import now as timezone_now +from django.core.exceptions import ValidationError from mock import patch, MagicMock 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 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.views.auth import login_or_register_remote_user, \ redirect_and_log_into_subdomain @@ -1521,6 +1522,15 @@ class RealmCreationTest(ZulipTestCase): self.assert_in_success_response(["available"], 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): def _assert_redirected_to(self, result: HttpResponse, url: Text) -> None: