subdomains: Improve support for using the root domain.

This modifies the realm creation form to (1) support a
realm_in_root_domain flag and (2) clearly check whether the root
domain is available inside check_subdomain_available before trying to
create a realm with it; this should avoid IntegrityErrors.
This commit is contained in:
Tim Abbott
2017-10-18 23:30:40 -07:00
parent 2604eb0a07
commit 85917a7269
3 changed files with 70 additions and 6 deletions

View File

@@ -15,7 +15,7 @@ from zerver.lib.actions import do_change_password, user_email_is_unique, \
from zerver.lib.name_restrictions import is_reserved_subdomain, is_disposable_domain
from zerver.lib.request import JsonableError
from zerver.lib.send_email import send_email, FromAddress
from zerver.lib.subdomains import get_subdomain, check_subdomain
from zerver.lib.subdomains import get_subdomain, check_subdomain, is_root_domain_available
from zerver.lib.users import check_full_name
from zerver.models import Realm, get_user_profile_by_email, UserProfile, \
get_realm, email_to_domain, email_allowed_for_realm
@@ -57,6 +57,10 @@ def check_subdomain_available(subdomain):
'bad character': _("Subdomain can only have lowercase letters, numbers, and '-'s."),
'unavailable': _("Subdomain unavailable. Please choose a different one.")}
if subdomain == Realm.SUBDOMAIN_FOR_ROOT_DOMAIN:
if is_root_domain_available():
return
raise ValidationError(error_strings['unavailable'])
if len(subdomain) < 3:
raise ValidationError(error_strings['too short'])
if subdomain[0] == '-' or subdomain[-1] == '-':
@@ -100,10 +104,13 @@ class RegistrationForm(forms.Form):
def clean_realm_subdomain(self):
# type: () -> str
if not self.realm_creation:
return Realm.SUBDOMAIN_FOR_ROOT_DOMAIN
# This field is only used if realm_creation
return ""
subdomain = self.cleaned_data['realm_subdomain']
if not subdomain:
return Realm.SUBDOMAIN_FOR_ROOT_DOMAIN
if 'realm_in_root_domain' in self.data:
subdomain = Realm.SUBDOMAIN_FOR_ROOT_DOMAIN
check_subdomain_available(subdomain)
return subdomain

View File

@@ -307,8 +307,8 @@ class ZulipTestCase(TestCase):
def submit_reg_form_for_user(self, email, password, realm_name="Zulip Test",
realm_subdomain="zuliptest",
from_confirmation='', full_name=None, timezone=u'',
**kwargs):
# type: (Text, Text, Optional[Text], Optional[Text], Optional[Text], Optional[Text], Optional[Text], **Any) -> HttpResponse
realm_in_root_domain=None, **kwargs):
# type: (Text, Text, Optional[Text], Optional[Text], Optional[Text], Optional[Text], Optional[Text], Optional[Text], **Any) -> HttpResponse
"""
Stage two of the two-step registration process.
@@ -329,6 +329,8 @@ class ZulipTestCase(TestCase):
'terms': True,
'from_confirmation': from_confirmation,
}
if realm_in_root_domain is not None:
payload['realm_in_root_domain'] = realm_in_root_domain
return self.client_post('/accounts/register/', payload, **kwargs)
def get_confirmation_url_from_outbox(self, email_address, path_pattern="(\S+)>"):

View File

@@ -1194,6 +1194,61 @@ class RealmCreationTest(ZulipTestCase):
realm_subdomain = 'a-0',
realm_name = realm_name)
self.assertEqual(result.status_code, 302)
self.assertEqual(result.url, 'http://a-0.testserver/accounts/login/subdomain/')
@override_settings(OPEN_REALM_CREATION=True)
def test_subdomain_restrictions_root_domain(self):
# type: () -> None
password = "test"
email = "user1@test.com"
realm_name = "Test"
result = self.client_post('/create_realm/', {'email': email})
self.client_get(result["Location"])
confirmation_url = self.get_confirmation_url_from_outbox(email)
self.client_get(confirmation_url)
# test root domain will fail with ROOT_DOMAIN_LANDING_PAGE
with self.settings(ROOT_DOMAIN_LANDING_PAGE=True):
result = self.submit_reg_form_for_user(email, password,
realm_subdomain = '',
realm_name = realm_name)
self.assert_in_response('unavailable', result)
# test valid use of root domain
result = self.submit_reg_form_for_user(email, password,
realm_subdomain = '',
realm_name = realm_name)
self.assertEqual(result.status_code, 302)
self.assertEqual(result.url, 'http://testserver/accounts/login/subdomain/')
@override_settings(OPEN_REALM_CREATION=True)
def test_subdomain_restrictions_root_domain_option(self):
# type: () -> None
password = "test"
email = "user1@test.com"
realm_name = "Test"
result = self.client_post('/create_realm/', {'email': email})
self.client_get(result["Location"])
confirmation_url = self.get_confirmation_url_from_outbox(email)
self.client_get(confirmation_url)
# test root domain will fail with ROOT_DOMAIN_LANDING_PAGE
with self.settings(ROOT_DOMAIN_LANDING_PAGE=True):
result = self.submit_reg_form_for_user(email, password,
realm_subdomain = 'abcdef',
realm_in_root_domain = 'true',
realm_name = realm_name)
self.assert_in_response('unavailable', result)
# test valid use of root domain
result = self.submit_reg_form_for_user(email, password,
realm_subdomain = 'abcdef',
realm_in_root_domain = 'true',
realm_name = realm_name)
self.assertEqual(result.status_code, 302)
self.assertEqual(result.url, 'http://testserver/accounts/login/subdomain/')
def test_is_root_domain_available(self):
# type: () -> None