Files
zulip/zerver/tests/test_subdomains.py
Greg Price 119bc14182 alias domains: Add a v1 of this feature.
The main limitation of this version is that it's controlled entirely
from settings, with nothing in the database and no web UI or even
management command to control it.  That makes it a bit more of a
burden for the server admins than it'd ideally be, but that's fine
for now.

Relatedly, the web flow for realm creation still requires choosing a
subdomain even if the realm is destined to live at an alias domain.

Specific to the dev environment, there is an annoying quirk: the
special dev login flow doesn't work on a REALM_HOSTS realm.  Also,
in this version the `add_new_realm` and `add_new_user` management
commands, which are intended for use in development environments only,
don't support this feature.

In manual testing, I've confirmed that a REALM_HOSTS realm works for
signup and login, with email/password, Google SSO, or GitHub SSO.
Most of that was in dev; I used zulipstaging.com to also test
 * logging in with email and password;
 * logging in with Google SSO... far enough to correctly determine
   that my email address is associated with some other realm.
2017-11-03 13:38:59 -07:00

57 lines
2.4 KiB
Python

import mock
from typing import Any, Dict, List
from django.test import TestCase, override_settings
from zerver.lib.subdomains import get_subdomain
from zerver.models import Realm
class SubdomainsTest(TestCase):
def test_get_subdomain(self):
# type: () -> None
def request_mock(host):
# type: (str) -> Any
request = mock.Mock(spec=['get_host'])
request.attach_mock(mock.Mock(return_value=host), 'get_host')
return request
def test(expected, host, *, plusport=True,
external_host='example.org', realm_hosts={}, root_aliases=[]):
# type: (str, str, bool, str, Dict[str, str], List[str]) -> None
with self.settings(EXTERNAL_HOST=external_host,
REALM_HOSTS=realm_hosts,
ROOT_SUBDOMAIN_ALIASES=root_aliases):
self.assertEqual(get_subdomain(request_mock(host)), expected)
if plusport and ':' not in host:
self.assertEqual(get_subdomain(request_mock(host + ':443')),
expected)
ROOT = Realm.SUBDOMAIN_FOR_ROOT_DOMAIN
# Basics
test(ROOT, 'example.org')
test('foo', 'foo.example.org')
test(ROOT, 'www.example.org', root_aliases=['www'])
# Unrecognized patterns fall back to root
test(ROOT, 'arbitrary.com')
test(ROOT, 'foo.example.org.evil.com')
# REALM_HOSTS adds a name,
test('bar', 'chat.barbar.com', realm_hosts={'bar': 'chat.barbar.com'})
# ... exactly, ...
test(ROOT, 'surchat.barbar.com', realm_hosts={'bar': 'chat.barbar.com'})
test(ROOT, 'foo.chat.barbar.com', realm_hosts={'bar': 'chat.barbar.com'})
# ... and leaves the subdomain in place too.
test('bar', 'bar.example.org', realm_hosts={'bar': 'chat.barbar.com'})
# Any port is fine in Host if there's none in EXTERNAL_HOST, ...
test('foo', 'foo.example.org:443', external_host='example.org')
test('foo', 'foo.example.org:12345', external_host='example.org')
# ... but an explicit port in EXTERNAL_HOST must be explicitly matched in Host.
test(ROOT, 'foo.example.org', external_host='example.org:12345')
test(ROOT, 'foo.example.org', external_host='example.org:443', plusport=False)
test('foo', 'foo.example.org:443', external_host='example.org:443')