2020-06-13 05:24:42 +02:00
|
|
|
from typing import Any, Mapping, Sequence
|
2020-06-11 00:54:34 +02:00
|
|
|
from unittest import mock
|
2017-10-26 03:14:53 +02:00
|
|
|
|
|
|
|
from zerver.lib.subdomains import get_subdomain
|
2020-07-01 04:19:54 +02:00
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
2017-10-26 03:14:53 +02:00
|
|
|
from zerver.models import Realm
|
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2020-07-01 04:19:54 +02:00
|
|
|
class SubdomainsTest(ZulipTestCase):
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_get_subdomain(self) -> None:
|
2017-10-26 03:14:53 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def request_mock(host: str) -> Any:
|
2017-10-26 03:14:53 +02:00
|
|
|
request = mock.Mock(spec=['get_host'])
|
|
|
|
request.attach_mock(mock.Mock(return_value=host), 'get_host')
|
|
|
|
return request
|
|
|
|
|
2017-11-20 03:22:57 +01:00
|
|
|
def test(expected: str, host: str, *, plusport: bool=True,
|
|
|
|
external_host: str='example.org',
|
2020-06-13 05:24:42 +02:00
|
|
|
realm_hosts: Mapping[str, str]={},
|
|
|
|
root_aliases: Sequence[str]=[]) -> None:
|
2017-10-20 06:30:34 +02:00
|
|
|
with self.settings(EXTERNAL_HOST=external_host,
|
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-10-20 06:36:50 +02:00
|
|
|
REALM_HOSTS=realm_hosts,
|
2017-10-26 03:14:53 +02:00
|
|
|
ROOT_SUBDOMAIN_ALIASES=root_aliases):
|
|
|
|
self.assertEqual(get_subdomain(request_mock(host)), expected)
|
2017-10-20 06:30:34 +02:00
|
|
|
if plusport and ':' not in host:
|
|
|
|
self.assertEqual(get_subdomain(request_mock(host + ':443')),
|
|
|
|
expected)
|
2017-10-26 03:14:53 +02:00
|
|
|
|
|
|
|
ROOT = Realm.SUBDOMAIN_FOR_ROOT_DOMAIN
|
2017-10-20 06:30:34 +02:00
|
|
|
|
|
|
|
# Basics
|
2017-10-26 03:14:53 +02:00
|
|
|
test(ROOT, 'example.org')
|
|
|
|
test('foo', 'foo.example.org')
|
|
|
|
test(ROOT, 'www.example.org', root_aliases=['www'])
|
2017-10-20 06:30:34 +02:00
|
|
|
|
|
|
|
# Unrecognized patterns fall back to root
|
2017-10-26 03:14:53 +02:00
|
|
|
test(ROOT, 'arbitrary.com')
|
2017-10-20 06:30:34 +02:00
|
|
|
test(ROOT, 'foo.example.org.evil.com')
|
|
|
|
|
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-10-20 06:36:50 +02:00
|
|
|
# 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, ...
|
2017-10-20 06:30:34 +02:00
|
|
|
test('foo', 'foo.example.org:443', external_host='example.org')
|
|
|
|
test('foo', 'foo.example.org:12345', external_host='example.org')
|
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-10-20 06:36:50 +02:00
|
|
|
# ... but an explicit port in EXTERNAL_HOST must be explicitly matched in Host.
|
2017-10-20 06:30:34 +02:00
|
|
|
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')
|