2016-12-26 19:19:02 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2017-01-21 08:19:03 +01:00
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
from django.db.utils import IntegrityError
|
2017-03-20 05:32:04 +01:00
|
|
|
from typing import Optional, Text
|
2017-01-21 08:19:03 +01:00
|
|
|
|
|
|
|
from zerver.lib.actions import do_change_is_admin, \
|
2017-03-31 19:57:46 +02:00
|
|
|
do_change_realm_domain, do_create_realm, \
|
2017-03-31 20:00:05 +02:00
|
|
|
do_remove_realm_domain
|
2017-01-21 08:19:03 +01:00
|
|
|
from zerver.lib.domains import validate_domain
|
2016-12-26 19:19:02 +01:00
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
2017-01-21 08:19:03 +01:00
|
|
|
from zerver.models import email_allowed_for_realm, get_realm, \
|
|
|
|
get_realm_by_email_domain, get_user_profile_by_email, \
|
2017-03-31 16:20:07 +02:00
|
|
|
GetRealmByDomainException, RealmDomain
|
2017-01-21 08:19:03 +01:00
|
|
|
|
2016-12-26 19:19:02 +01:00
|
|
|
import ujson
|
|
|
|
|
|
|
|
|
2017-03-31 16:20:07 +02:00
|
|
|
class RealmDomainTest(ZulipTestCase):
|
2017-03-31 22:21:13 +02:00
|
|
|
def test_list_realm_domains(self):
|
2016-12-26 19:19:02 +01:00
|
|
|
# type: () -> None
|
|
|
|
self.login("iago@zulip.com")
|
|
|
|
realm = get_realm('zulip')
|
2017-03-31 16:20:07 +02:00
|
|
|
RealmDomain.objects.create(realm=realm, domain='acme.com', allow_subdomains=True)
|
2016-12-26 19:19:02 +01:00
|
|
|
result = self.client_get("/json/realm/domains")
|
|
|
|
self.assert_json_success(result)
|
2017-01-21 08:19:03 +01:00
|
|
|
received = ujson.dumps(ujson.loads(result.content)['domains'], sort_keys=True)
|
|
|
|
expected = ujson.dumps([{'domain': 'zulip.com', 'allow_subdomains': False},
|
|
|
|
{'domain': 'acme.com', 'allow_subdomains': True}],
|
|
|
|
sort_keys=True)
|
|
|
|
self.assertEqual(received, expected)
|
2016-12-26 19:19:02 +01:00
|
|
|
|
|
|
|
def test_not_realm_admin(self):
|
|
|
|
# type: () -> None
|
|
|
|
self.login("hamlet@zulip.com")
|
|
|
|
result = self.client_post("/json/realm/domains")
|
|
|
|
self.assert_json_error(result, 'Must be a realm administrator')
|
2017-01-21 08:19:03 +01:00
|
|
|
result = self.client_patch("/json/realm/domains/15")
|
|
|
|
self.assert_json_error(result, 'Must be a realm administrator')
|
2016-12-26 19:19:02 +01:00
|
|
|
result = self.client_delete("/json/realm/domains/15")
|
|
|
|
self.assert_json_error(result, 'Must be a realm administrator')
|
|
|
|
|
2017-03-31 22:21:13 +02:00
|
|
|
def test_create_realm_domain(self):
|
2016-12-26 19:19:02 +01:00
|
|
|
# type: () -> None
|
|
|
|
self.login("iago@zulip.com")
|
2017-01-21 08:19:03 +01:00
|
|
|
data = {'domain': ujson.dumps(''),
|
|
|
|
'allow_subdomains': ujson.dumps(True)}
|
2016-12-26 19:19:02 +01:00
|
|
|
result = self.client_post("/json/realm/domains", info=data)
|
2017-01-21 08:27:36 +01:00
|
|
|
self.assert_json_error(result, 'Invalid domain: Domain can\'t be empty.')
|
2016-12-26 19:19:02 +01:00
|
|
|
|
2017-01-21 08:19:03 +01:00
|
|
|
data['domain'] = ujson.dumps('acme.com')
|
2016-12-26 19:19:02 +01:00
|
|
|
result = self.client_post("/json/realm/domains", info=data)
|
|
|
|
self.assert_json_success(result)
|
2017-01-21 08:19:03 +01:00
|
|
|
realm = get_realm('zulip')
|
2017-03-31 16:20:07 +02:00
|
|
|
self.assertTrue(RealmDomain.objects.filter(realm=realm, domain='acme.com',
|
|
|
|
allow_subdomains=True).exists())
|
2016-12-26 19:19:02 +01:00
|
|
|
|
|
|
|
result = self.client_post("/json/realm/domains", info=data)
|
2017-01-21 08:19:03 +01:00
|
|
|
self.assert_json_error(result, 'The domain acme.com is already a part of your organization.')
|
2016-12-26 19:19:02 +01:00
|
|
|
|
2017-05-23 01:27:31 +02:00
|
|
|
mit_user_profile = self.mit_user("sipbtest")
|
|
|
|
self.login(mit_user_profile.email)
|
|
|
|
|
2017-01-30 20:54:28 +01:00
|
|
|
do_change_is_admin(mit_user_profile, True)
|
2017-01-30 19:30:57 +01:00
|
|
|
result = self.client_post("/json/realm/domains", info=data)
|
2017-01-21 08:19:03 +01:00
|
|
|
self.assert_json_error(result, 'The domain acme.com belongs to another organization.')
|
2017-01-30 20:54:28 +01:00
|
|
|
with self.settings(REALMS_HAVE_SUBDOMAINS=True):
|
|
|
|
result = self.client_post("/json/realm/domains", info=data,
|
|
|
|
HTTP_HOST=mit_user_profile.realm.host)
|
|
|
|
self.assert_json_success(result)
|
2017-01-30 19:30:57 +01:00
|
|
|
|
2017-03-31 22:21:13 +02:00
|
|
|
def test_patch_realm_domain(self):
|
2016-12-26 19:19:02 +01:00
|
|
|
# type: () -> None
|
|
|
|
self.login("iago@zulip.com")
|
|
|
|
realm = get_realm('zulip')
|
2017-03-31 16:20:07 +02:00
|
|
|
RealmDomain.objects.create(realm=realm, domain='acme.com',
|
|
|
|
allow_subdomains=False)
|
2017-01-21 08:19:03 +01:00
|
|
|
data = {
|
|
|
|
'allow_subdomains': ujson.dumps(True),
|
|
|
|
}
|
|
|
|
url = "/json/realm/domains/acme.com"
|
|
|
|
result = self.client_patch(url, data)
|
|
|
|
self.assert_json_success(result)
|
2017-03-31 16:20:07 +02:00
|
|
|
self.assertTrue(RealmDomain.objects.filter(realm=realm, domain='acme.com',
|
|
|
|
allow_subdomains=True).exists())
|
2017-01-21 08:19:03 +01:00
|
|
|
|
|
|
|
url = "/json/realm/domains/non-existent.com"
|
|
|
|
result = self.client_patch(url, data)
|
|
|
|
self.assertEqual(result.status_code, 400)
|
|
|
|
self.assert_json_error(result, 'No entry found for domain non-existent.com.')
|
|
|
|
|
2017-03-31 22:21:13 +02:00
|
|
|
def test_delete_realm_domain(self):
|
2017-01-21 08:19:03 +01:00
|
|
|
# type: () -> None
|
|
|
|
self.login("iago@zulip.com")
|
|
|
|
realm = get_realm('zulip')
|
2017-03-31 16:20:07 +02:00
|
|
|
RealmDomain.objects.create(realm=realm, domain='acme.com')
|
2017-01-21 09:09:27 +01:00
|
|
|
result = self.client_delete("/json/realm/domains/non-existent.com")
|
|
|
|
self.assertEqual(result.status_code, 400)
|
|
|
|
self.assert_json_error(result, 'No entry found for domain non-existent.com.')
|
2016-12-26 19:19:02 +01:00
|
|
|
|
2017-01-21 08:19:03 +01:00
|
|
|
result = self.client_delete("/json/realm/domains/acme.com")
|
2016-12-26 19:19:02 +01:00
|
|
|
self.assert_json_success(result)
|
2017-03-31 16:20:07 +02:00
|
|
|
self.assertFalse(RealmDomain.objects.filter(domain='acme.com').exists())
|
2017-01-26 10:52:56 +01:00
|
|
|
self.assertTrue(realm.restricted_to_domain)
|
|
|
|
|
2017-03-31 22:21:13 +02:00
|
|
|
def test_delete_all_realm_domains(self):
|
2017-01-26 10:52:56 +01:00
|
|
|
# type: () -> None
|
|
|
|
self.login("iago@zulip.com")
|
|
|
|
realm = get_realm('zulip')
|
2017-03-31 16:20:07 +02:00
|
|
|
query = RealmDomain.objects.filter(realm=realm)
|
2017-01-26 10:52:56 +01:00
|
|
|
|
|
|
|
self.assertTrue(realm.restricted_to_domain)
|
2017-03-31 22:21:13 +02:00
|
|
|
for realm_domain in query.all():
|
|
|
|
do_remove_realm_domain(realm_domain)
|
2017-01-26 10:52:56 +01:00
|
|
|
self.assertEqual(query.count(), 0)
|
2017-03-31 22:21:13 +02:00
|
|
|
# Deleting last realm_domain should set `restricted_to_domain` to False.
|
|
|
|
# This should be tested on a fresh instance, since the cached objects
|
|
|
|
# would not be updated.
|
2017-01-26 10:52:56 +01:00
|
|
|
self.assertFalse(get_realm('zulip').restricted_to_domain)
|
2016-12-26 19:19:02 +01:00
|
|
|
|
|
|
|
def test_get_realm_by_email_domain(self):
|
|
|
|
# type: () -> None
|
2017-01-21 08:19:03 +01:00
|
|
|
realm1, created = do_create_realm('testrealm1', 'Test Realm 1')
|
|
|
|
realm2, created = do_create_realm('testrealm2', 'Test Realm 2')
|
|
|
|
realm3, created = do_create_realm('testrealm3', 'Test Realm 3')
|
|
|
|
|
2017-03-31 22:21:13 +02:00
|
|
|
realm_domain_1 = RealmDomain.objects.create(realm=realm1, domain='test1.com', allow_subdomains=True)
|
|
|
|
realm_domain_2 = RealmDomain.objects.create(realm=realm2, domain='test2.test1.com', allow_subdomains=False)
|
2017-03-31 16:20:07 +02:00
|
|
|
RealmDomain.objects.create(realm=realm3, domain='test3.test2.test1.com', allow_subdomains=True)
|
2017-01-21 08:19:03 +01:00
|
|
|
|
2017-03-20 05:32:04 +01:00
|
|
|
def assert_and_check(email, realm_string_id):
|
|
|
|
# type: (Text, Optional[Text]) -> None
|
|
|
|
realm = get_realm_by_email_domain(email)
|
|
|
|
if realm_string_id is None:
|
|
|
|
self.assertIsNone(realm)
|
|
|
|
else:
|
2017-05-24 04:21:29 +02:00
|
|
|
assert(realm is not None)
|
2017-03-20 05:32:04 +01:00
|
|
|
self.assertEqual(realm.string_id, realm_string_id)
|
|
|
|
|
|
|
|
assert_and_check('user@zulip.com', 'zulip')
|
|
|
|
assert_and_check('user@fakedomain.com', None)
|
|
|
|
assert_and_check('user@test1.com', 'testrealm1')
|
|
|
|
assert_and_check('user@test2.test1.com', 'testrealm2')
|
|
|
|
assert_and_check('user@test3.test2.test1.com', 'testrealm3')
|
|
|
|
assert_and_check('user@test2.test1.com', 'testrealm2')
|
|
|
|
assert_and_check('user@test2.test2.test1.com', 'testrealm1')
|
|
|
|
assert_and_check('user@test1.test3.test2.test1.com', 'testrealm3')
|
2017-01-21 08:19:03 +01:00
|
|
|
|
2017-03-31 22:21:13 +02:00
|
|
|
do_change_realm_domain(realm_domain_1, False)
|
2017-03-20 05:32:04 +01:00
|
|
|
assert_and_check('user@test1.test1.com', None)
|
|
|
|
assert_and_check('user@test1.com', 'testrealm1')
|
2017-01-21 08:19:03 +01:00
|
|
|
|
2017-03-31 22:21:13 +02:00
|
|
|
do_change_realm_domain(realm_domain_2, True)
|
2017-03-20 05:32:04 +01:00
|
|
|
assert_and_check('user@test2.test1.com', 'testrealm2')
|
|
|
|
assert_and_check('user@test2.test2.test1.com', 'testrealm2')
|
2017-01-21 08:19:03 +01:00
|
|
|
|
2016-12-26 19:19:02 +01:00
|
|
|
with self.settings(REALMS_HAVE_SUBDOMAINS = True), (
|
2017-01-24 07:06:13 +01:00
|
|
|
self.assertRaises(GetRealmByDomainException)):
|
2016-12-26 19:19:02 +01:00
|
|
|
get_realm_by_email_domain('user@zulip.com')
|
2017-01-21 08:19:03 +01:00
|
|
|
|
|
|
|
def test_email_allowed_for_realm(self):
|
|
|
|
# type: () -> None
|
|
|
|
realm1, created = do_create_realm('testrealm1', 'Test Realm 1', restricted_to_domain=True)
|
|
|
|
realm2, created = do_create_realm('testrealm2', 'Test Realm 2', restricted_to_domain=True)
|
|
|
|
|
2017-03-31 22:21:13 +02:00
|
|
|
realm_domain = RealmDomain.objects.create(realm=realm1, domain='test1.com', allow_subdomains=False)
|
2017-03-31 16:20:07 +02:00
|
|
|
RealmDomain.objects.create(realm=realm2, domain='test2.test1.com', allow_subdomains=True)
|
2017-01-21 08:19:03 +01:00
|
|
|
|
|
|
|
self.assertEqual(email_allowed_for_realm('user@test1.com', realm1), True)
|
|
|
|
self.assertEqual(email_allowed_for_realm('user@test2.test1.com', realm1), False)
|
|
|
|
self.assertEqual(email_allowed_for_realm('user@test2.test1.com', realm2), True)
|
|
|
|
self.assertEqual(email_allowed_for_realm('user@test3.test2.test1.com', realm2), True)
|
|
|
|
self.assertEqual(email_allowed_for_realm('user@test3.test1.com', realm2), False)
|
|
|
|
|
2017-03-31 22:21:13 +02:00
|
|
|
do_change_realm_domain(realm_domain, True)
|
2017-01-21 08:19:03 +01:00
|
|
|
self.assertEqual(email_allowed_for_realm('user@test1.com', realm1), True)
|
|
|
|
self.assertEqual(email_allowed_for_realm('user@test2.test1.com', realm1), True)
|
|
|
|
self.assertEqual(email_allowed_for_realm('user@test2.com', realm1), False)
|
|
|
|
|
2017-03-31 22:21:13 +02:00
|
|
|
def test_realm_realm_domains_uniqueness(self):
|
2017-01-21 08:19:03 +01:00
|
|
|
# type: () -> None
|
|
|
|
realm = get_realm('zulip')
|
|
|
|
with self.settings(REALMS_HAVE_SUBDOMAINS=True), self.assertRaises(IntegrityError):
|
2017-03-31 16:20:07 +02:00
|
|
|
RealmDomain.objects.create(realm=realm, domain='zulip.com', allow_subdomains=True)
|
2017-01-21 08:19:03 +01:00
|
|
|
|
|
|
|
def test_validate_domain(self):
|
|
|
|
# type: () -> None
|
|
|
|
invalid_domains = ['', 'test', 't.', 'test.', '.com', '-test', 'test...com',
|
|
|
|
'test-', 'test_domain.com', 'test.-domain.com']
|
|
|
|
for domain in invalid_domains:
|
|
|
|
with self.assertRaises(ValidationError):
|
|
|
|
validate_domain(domain)
|
|
|
|
|
|
|
|
valid_domains = ['acme.com', 'x-x.y.3.z']
|
|
|
|
for domain in valid_domains:
|
|
|
|
validate_domain(domain)
|