2016-12-26 19:19:02 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
|
|
|
from zerver.models import get_realm, get_realm_by_email_domain, \
|
|
|
|
GetRealmByDomainException, RealmAlias
|
|
|
|
import ujson
|
|
|
|
|
|
|
|
|
|
|
|
class RealmAliasTest(ZulipTestCase):
|
|
|
|
|
|
|
|
def test_list(self):
|
|
|
|
# type: () -> None
|
|
|
|
self.login("iago@zulip.com")
|
|
|
|
realm = get_realm('zulip')
|
|
|
|
alias = RealmAlias(realm=realm, domain='zulip.org')
|
|
|
|
alias.save()
|
|
|
|
result = self.client_get("/json/realm/domains")
|
|
|
|
self.assert_json_success(result)
|
|
|
|
self.assertEqual(200, result.status_code)
|
|
|
|
content = ujson.loads(result.content)
|
|
|
|
self.assertEqual(len(content['domains']), 2)
|
|
|
|
|
|
|
|
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')
|
|
|
|
result = self.client_delete("/json/realm/domains/15")
|
|
|
|
self.assert_json_error(result, 'Must be a realm administrator')
|
|
|
|
|
|
|
|
def test_create(self):
|
|
|
|
# type: () -> None
|
|
|
|
self.login("iago@zulip.com")
|
2017-01-21 08:27:36 +01:00
|
|
|
data = {'domain': ujson.dumps('')}
|
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:27:36 +01:00
|
|
|
data = {'domain': ujson.dumps('zulip.org')}
|
2016-12-26 19:19:02 +01:00
|
|
|
result = self.client_post("/json/realm/domains", info=data)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
result = self.client_post("/json/realm/domains", info=data)
|
2017-01-21 08:27:36 +01:00
|
|
|
self.assert_json_error(result, 'The domain zulip.org is already a part of your organization.')
|
2016-12-26 19:19:02 +01:00
|
|
|
|
|
|
|
def test_delete(self):
|
|
|
|
# type: () -> None
|
|
|
|
self.login("iago@zulip.com")
|
|
|
|
realm = get_realm('zulip')
|
2017-01-21 09:09:27 +01:00
|
|
|
RealmAlias.objects.create(realm=realm, domain='zulip.org')
|
2016-12-26 19:19:02 +01:00
|
|
|
aliases_count = RealmAlias.objects.count()
|
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 09:09:27 +01:00
|
|
|
result = self.client_delete("/json/realm/domains/zulip.org")
|
2016-12-26 19:19:02 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
self.assertEqual(RealmAlias.objects.count(), aliases_count - 1)
|
|
|
|
|
|
|
|
def test_get_realm_by_email_domain(self):
|
|
|
|
# type: () -> None
|
|
|
|
self.assertEqual(get_realm_by_email_domain('user@zulip.com').string_id, 'zulip')
|
|
|
|
self.assertEqual(get_realm_by_email_domain('user@fakedomain.com'), None)
|
|
|
|
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')
|