bots: Fix calculation of bot domain with REALMS_HAVE_SUBDOMAINS.

Previously, the bot domain was calculated correctly in most
circumstances, but if you were using the root domain, it would be
e.g. ".chat.zulip.org", not "chat.zulip.org".  We fix this, with
perhaps more use of setting REALMS_HAVE_SUBDOMAINS than would be ideal
if we weren't about to set that True unconditionally.
This commit is contained in:
Tim Abbott 2017-09-16 02:48:01 -07:00
parent 6d20557b61
commit a1ddd934b4
4 changed files with 31 additions and 3 deletions

View File

@ -246,9 +246,7 @@ class Realm(ModelReprMixin, models.Model):
# type: () -> str # type: () -> str
# Remove the port. Mainly needed for development environment. # Remove the port. Mainly needed for development environment.
external_host = settings.EXTERNAL_HOST.split(':')[0] external_host = settings.EXTERNAL_HOST.split(':')[0]
if settings.REALMS_HAVE_SUBDOMAINS or \ if self.subdomain not in [None, ""]:
Realm.objects.filter(deactivated=False) \
.exclude(string_id__in=settings.SYSTEM_ONLY_REALMS).count() > 1:
return "%s.%s" % (self.string_id, external_host) return "%s.%s" % (self.string_id, external_host)
return external_host return external_host

View File

@ -28,6 +28,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
self.assert_json_success(result) self.assert_json_success(result)
self.assertEqual(count, len(result.json()['bots'])) self.assertEqual(count, len(result.json()['bots']))
@override_settings(REALMS_HAVE_SUBDOMAINS=True)
def create_bot(self, **extras): def create_bot(self, **extras):
# type: (**Any) -> Dict[str, Any] # type: (**Any) -> Dict[str, Any]
bot_info = { bot_info = {
@ -135,6 +136,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
self.assertEqual(bot['bot_owner'], self.example_email('hamlet')) self.assertEqual(bot['bot_owner'], self.example_email('hamlet'))
self.assertEqual(bot['user_id'], get_user(email, realm).id) self.assertEqual(bot['user_id'], get_user(email, realm).id)
@override_settings(REALMS_HAVE_SUBDOMAINS=True)
def test_add_bot_with_username_in_use(self): def test_add_bot_with_username_in_use(self):
# type: () -> None # type: () -> None
self.login(self.example_email('hamlet')) self.login(self.example_email('hamlet'))
@ -167,6 +169,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
self.assertEqual(profile.avatar_source, UserProfile.AVATAR_FROM_USER) self.assertEqual(profile.avatar_source, UserProfile.AVATAR_FROM_USER)
self.assertTrue(os.path.exists(avatar_disk_path(profile))) self.assertTrue(os.path.exists(avatar_disk_path(profile)))
@override_settings(REALMS_HAVE_SUBDOMAINS=True)
def test_add_bot_with_too_many_files(self): def test_add_bot_with_too_many_files(self):
# type: () -> None # type: () -> None
self.login(self.example_email('hamlet')) self.login(self.example_email('hamlet'))
@ -296,6 +299,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
) )
self.assertEqual(event['users'], {user_profile.id, }) self.assertEqual(event['users'], {user_profile.id, })
@override_settings(REALMS_HAVE_SUBDOMAINS=True)
def test_add_bot_with_default_sending_stream_private_denied(self): def test_add_bot_with_default_sending_stream_private_denied(self):
# type: () -> None # type: () -> None
self.login(self.example_email('hamlet')) self.login(self.example_email('hamlet'))
@ -366,6 +370,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
) )
self.assertEqual(event['users'], {user_profile.id, }) self.assertEqual(event['users'], {user_profile.id, })
@override_settings(REALMS_HAVE_SUBDOMAINS=True)
def test_add_bot_with_default_events_register_stream_private_denied(self): def test_add_bot_with_default_events_register_stream_private_denied(self):
# type: () -> None # type: () -> None
self.login(self.example_email('hamlet')) self.login(self.example_email('hamlet'))
@ -516,6 +521,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
profile = get_user(bot_email, bot_realm) profile = get_user(bot_email, bot_realm)
self.assertEqual(profile.bot_type, UserProfile.INCOMING_WEBHOOK_BOT) self.assertEqual(profile.bot_type, UserProfile.INCOMING_WEBHOOK_BOT)
@override_settings(REALMS_HAVE_SUBDOMAINS=True)
def test_add_bot_with_bot_type_invalid(self): def test_add_bot_with_bot_type_invalid(self):
# type: () -> None # type: () -> None
bot_info = { bot_info = {
@ -530,6 +536,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
self.assert_num_bots_equal(0) self.assert_num_bots_equal(0)
self.assert_json_error(result, 'Invalid bot type') self.assert_json_error(result, 'Invalid bot type')
@override_settings(REALMS_HAVE_SUBDOMAINS=True)
def test_patch_bot_full_name(self): def test_patch_bot_full_name(self):
# type: () -> None # type: () -> None
self.login(self.example_email('hamlet')) self.login(self.example_email('hamlet'))
@ -550,6 +557,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
bot = self.get_bot() bot = self.get_bot()
self.assertEqual('Fred', bot['full_name']) self.assertEqual('Fred', bot['full_name'])
@override_settings(REALMS_HAVE_SUBDOMAINS=True)
def test_patch_bot_owner(self): def test_patch_bot_owner(self):
# type: () -> None # type: () -> None
self.login(self.example_email('hamlet')) self.login(self.example_email('hamlet'))
@ -572,6 +580,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
bot = self.get_bot() bot = self.get_bot()
self.assertEqual('The Bot of Hamlet', bot['full_name']) self.assertEqual('The Bot of Hamlet', bot['full_name'])
@override_settings(REALMS_HAVE_SUBDOMAINS=True)
@override_settings(LOCAL_UPLOADS_DIR='var/bot_avatar') @override_settings(LOCAL_UPLOADS_DIR='var/bot_avatar')
def test_patch_bot_avatar(self): def test_patch_bot_avatar(self):
# type: () -> None # type: () -> None
@ -615,6 +624,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
self.assertEqual(profile.avatar_source, UserProfile.AVATAR_FROM_USER) self.assertEqual(profile.avatar_source, UserProfile.AVATAR_FROM_USER)
self.assertTrue(os.path.exists(avatar_disk_path(profile))) self.assertTrue(os.path.exists(avatar_disk_path(profile)))
@override_settings(REALMS_HAVE_SUBDOMAINS=True)
def test_patch_bot_to_stream(self): def test_patch_bot_to_stream(self):
# type: () -> None # type: () -> None
self.login(self.example_email('hamlet')) self.login(self.example_email('hamlet'))
@ -635,6 +645,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
bot = self.get_bot() bot = self.get_bot()
self.assertEqual('Denmark', bot['default_sending_stream']) self.assertEqual('Denmark', bot['default_sending_stream'])
@override_settings(REALMS_HAVE_SUBDOMAINS=True)
def test_patch_bot_to_stream_not_subscribed(self): def test_patch_bot_to_stream_not_subscribed(self):
# type: () -> None # type: () -> None
self.login(self.example_email('hamlet')) self.login(self.example_email('hamlet'))
@ -655,6 +666,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
bot = self.get_bot() bot = self.get_bot()
self.assertEqual('Rome', bot['default_sending_stream']) self.assertEqual('Rome', bot['default_sending_stream'])
@override_settings(REALMS_HAVE_SUBDOMAINS=True)
def test_patch_bot_to_stream_none(self): def test_patch_bot_to_stream_none(self):
# type: () -> None # type: () -> None
self.login(self.example_email('hamlet')) self.login(self.example_email('hamlet'))
@ -678,6 +690,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
bot = self.get_bot() bot = self.get_bot()
self.assertEqual(None, bot['default_sending_stream']) self.assertEqual(None, bot['default_sending_stream'])
@override_settings(REALMS_HAVE_SUBDOMAINS=True)
def test_patch_bot_to_stream_private_allowed(self): def test_patch_bot_to_stream_private_allowed(self):
# type: () -> None # type: () -> None
self.login(self.example_email('hamlet')) self.login(self.example_email('hamlet'))
@ -703,6 +716,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
bot = self.get_bot() bot = self.get_bot()
self.assertEqual('Denmark', bot['default_sending_stream']) self.assertEqual('Denmark', bot['default_sending_stream'])
@override_settings(REALMS_HAVE_SUBDOMAINS=True)
def test_patch_bot_to_stream_private_denied(self): def test_patch_bot_to_stream_private_denied(self):
# type: () -> None # type: () -> None
self.login(self.example_email('hamlet')) self.login(self.example_email('hamlet'))
@ -724,6 +738,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
result = self.client_patch("/json/bots/hambot-bot@zulip.testserver", bot_info) result = self.client_patch("/json/bots/hambot-bot@zulip.testserver", bot_info)
self.assert_json_error(result, "Invalid stream name 'Denmark'") self.assert_json_error(result, "Invalid stream name 'Denmark'")
@override_settings(REALMS_HAVE_SUBDOMAINS=True)
def test_patch_bot_to_stream_not_found(self): def test_patch_bot_to_stream_not_found(self):
# type: () -> None # type: () -> None
self.login(self.example_email('hamlet')) self.login(self.example_email('hamlet'))
@ -739,6 +754,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
result = self.client_patch("/json/bots/hambot-bot@zulip.testserver", bot_info) result = self.client_patch("/json/bots/hambot-bot@zulip.testserver", bot_info)
self.assert_json_error(result, "Invalid stream name 'missing'") self.assert_json_error(result, "Invalid stream name 'missing'")
@override_settings(REALMS_HAVE_SUBDOMAINS=True)
def test_patch_bot_events_register_stream(self): def test_patch_bot_events_register_stream(self):
# type: () -> None # type: () -> None
self.login(self.example_email('hamlet')) self.login(self.example_email('hamlet'))
@ -759,6 +775,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
bot = self.get_bot() bot = self.get_bot()
self.assertEqual('Denmark', bot['default_events_register_stream']) self.assertEqual('Denmark', bot['default_events_register_stream'])
@override_settings(REALMS_HAVE_SUBDOMAINS=True)
def test_patch_bot_events_register_stream_allowed(self): def test_patch_bot_events_register_stream_allowed(self):
# type: () -> None # type: () -> None
self.login(self.example_email('hamlet')) self.login(self.example_email('hamlet'))
@ -783,6 +800,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
bot = self.get_bot() bot = self.get_bot()
self.assertEqual('Denmark', bot['default_events_register_stream']) self.assertEqual('Denmark', bot['default_events_register_stream'])
@override_settings(REALMS_HAVE_SUBDOMAINS=True)
def test_patch_bot_events_register_stream_denied(self): def test_patch_bot_events_register_stream_denied(self):
# type: () -> None # type: () -> None
self.login(self.example_email('hamlet')) self.login(self.example_email('hamlet'))
@ -803,6 +821,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
result = self.client_patch("/json/bots/hambot-bot@zulip.testserver", bot_info) result = self.client_patch("/json/bots/hambot-bot@zulip.testserver", bot_info)
self.assert_json_error(result, "Invalid stream name 'Denmark'") self.assert_json_error(result, "Invalid stream name 'Denmark'")
@override_settings(REALMS_HAVE_SUBDOMAINS=True)
def test_patch_bot_events_register_stream_none(self): def test_patch_bot_events_register_stream_none(self):
# type: () -> None # type: () -> None
self.login(self.example_email('hamlet')) self.login(self.example_email('hamlet'))
@ -826,6 +845,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
bot = self.get_bot() bot = self.get_bot()
self.assertEqual(None, bot['default_events_register_stream']) self.assertEqual(None, bot['default_events_register_stream'])
@override_settings(REALMS_HAVE_SUBDOMAINS=True)
def test_patch_bot_events_register_stream_not_found(self): def test_patch_bot_events_register_stream_not_found(self):
# type: () -> None # type: () -> None
self.login(self.example_email('hamlet')) self.login(self.example_email('hamlet'))
@ -841,6 +861,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
result = self.client_patch("/json/bots/hambot-bot@zulip.testserver", bot_info) result = self.client_patch("/json/bots/hambot-bot@zulip.testserver", bot_info)
self.assert_json_error(result, "Invalid stream name 'missing'") self.assert_json_error(result, "Invalid stream name 'missing'")
@override_settings(REALMS_HAVE_SUBDOMAINS=True)
def test_patch_bot_default_all_public_streams_true(self): def test_patch_bot_default_all_public_streams_true(self):
# type: () -> None # type: () -> None
self.login(self.example_email('hamlet')) self.login(self.example_email('hamlet'))
@ -861,6 +882,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
bot = self.get_bot() bot = self.get_bot()
self.assertEqual(bot['default_all_public_streams'], True) self.assertEqual(bot['default_all_public_streams'], True)
@override_settings(REALMS_HAVE_SUBDOMAINS=True)
def test_patch_bot_default_all_public_streams_false(self): def test_patch_bot_default_all_public_streams_false(self):
# type: () -> None # type: () -> None
self.login(self.example_email('hamlet')) self.login(self.example_email('hamlet'))
@ -881,6 +903,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
bot = self.get_bot() bot = self.get_bot()
self.assertEqual(bot['default_all_public_streams'], False) self.assertEqual(bot['default_all_public_streams'], False)
@override_settings(REALMS_HAVE_SUBDOMAINS=True)
def test_patch_bot_via_post(self): def test_patch_bot_via_post(self):
# type: () -> None # type: () -> None
self.login(self.example_email('hamlet')) self.login(self.example_email('hamlet'))
@ -902,6 +925,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
bot = self.get_bot() bot = self.get_bot()
self.assertEqual('Fred', bot['full_name']) self.assertEqual('Fred', bot['full_name'])
@override_settings(REALMS_HAVE_SUBDOMAINS=True)
def test_patch_bogus_bot(self): def test_patch_bogus_bot(self):
# type: () -> None # type: () -> None
"""Deleting a bogus bot will succeed silently.""" """Deleting a bogus bot will succeed silently."""
@ -914,6 +938,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
self.assert_json_error(result, 'No such user') self.assert_json_error(result, 'No such user')
self.assert_num_bots_equal(1) self.assert_num_bots_equal(1)
@override_settings(REALMS_HAVE_SUBDOMAINS=True)
def test_create_outgoing_webhook_bot(self, **extras): def test_create_outgoing_webhook_bot(self, **extras):
# type: (**Any) -> None # type: (**Any) -> None
self.login(self.example_email('hamlet')) self.login(self.example_email('hamlet'))
@ -943,6 +968,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
result = self.client_post("/json/bots", bot_info) result = self.client_post("/json/bots", bot_info)
self.assert_json_error(result, "Enter a valid URL.") self.assert_json_error(result, "Enter a valid URL.")
@override_settings(REALMS_HAVE_SUBDOMAINS=True)
def test_get_bot_handler(self): def test_get_bot_handler(self):
# type: () -> None # type: () -> None
# Test for valid service. # Test for valid service.
@ -966,6 +992,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
bot_handler_class_name = class_bot_handler.format(name=bot_name, Name=bot_name.title()) bot_handler_class_name = class_bot_handler.format(name=bot_name, Name=bot_name.title())
self.assertEqual(str(type(embedded_bot_handler)), bot_handler_class_name) self.assertEqual(str(type(embedded_bot_handler)), bot_handler_class_name)
@override_settings(REALMS_HAVE_SUBDOMAINS=True)
def test_outgoing_webhook_interface_type(self): def test_outgoing_webhook_interface_type(self):
# type: () -> None # type: () -> None
self.login(self.example_email('hamlet')) self.login(self.example_email('hamlet'))

View File

@ -6,6 +6,7 @@ import os
import ujson import ujson
from django.http import HttpResponse from django.http import HttpResponse
from django.test import override_settings
from mock import MagicMock, patch from mock import MagicMock, patch
from six.moves import urllib from six.moves import urllib
from typing import Any, Dict, List from typing import Any, Dict, List
@ -22,6 +23,7 @@ from zerver.models import (
from zerver.views.home import home, sent_time_in_epoch_seconds from zerver.views.home import home, sent_time_in_epoch_seconds
class HomeTest(ZulipTestCase): class HomeTest(ZulipTestCase):
@override_settings(REALMS_HAVE_SUBDOMAINS=True)
@slow('big method') @slow('big method')
def test_home(self): def test_home(self):
# type: () -> None # type: () -> None

View File

@ -1223,6 +1223,7 @@ class MessagePOSTTest(ZulipTestCase):
"to": email}, name='gownooo') "to": email}, name='gownooo')
self.assert_json_error(result, "Invalid mirrored realm") self.assert_json_error(result, "Invalid mirrored realm")
@override_settings(REALMS_HAVE_SUBDOMAINS=True)
def test_send_message_irc_mirror(self): def test_send_message_irc_mirror(self):
# type: () -> None # type: () -> None
self.login(self.example_email('hamlet')) self.login(self.example_email('hamlet'))