Change bot domains to string_id.EXTERNAL_HOST.

Change applies to both subdomains and non-subdomains case, though we use
just the EXTERNAL_HOST in the non-subdomains case if there is only 1 realm.

Fixes #3903.
This commit is contained in:
Rishi Gupta
2017-03-04 19:17:12 -08:00
committed by Tim Abbott
parent 4a1f1db9ee
commit 3797fa657e
11 changed files with 93 additions and 56 deletions

View File

@@ -115,12 +115,19 @@ casper.then(function create_bot() {
bot_default_events_register_stream: 'Rome', bot_default_events_register_stream: 'Rome',
}); });
casper.test.info('Submiting the create bot form'); casper.test.info('Submitting the create bot form');
casper.click('#create_bot_button'); casper.click('#create_bot_button');
}); });
var bot_email;
if (REALMS_HAVE_SUBDOMAINS) {
bot_email = '1-bot@zulip.zulipdev.com';
} else {
bot_email = '1-bot@zulip.localhost';
}
casper.then(function () { casper.then(function () {
var button_sel = '.download_bot_zuliprc[data-email="1-bot@zulip.com"]'; var button_sel = '.download_bot_zuliprc[data-email="' + bot_email + '"]';
casper.waitUntilVisible(button_sel, function () { casper.waitUntilVisible(button_sel, function () {
casper.click(button_sel); casper.click(button_sel);
@@ -135,15 +142,15 @@ casper.then(function () {
}); });
casper.then(function () { casper.then(function () {
casper.waitUntilVisible('.open_edit_bot_form[data-email="1-bot@zulip.com"]', function open_edit_bot_form() { casper.waitUntilVisible('.open_edit_bot_form[data-email="' + bot_email + '"]', function open_edit_bot_form() {
casper.test.info('Opening edit bot form'); casper.test.info('Opening edit bot form');
casper.click('.open_edit_bot_form[data-email="1-bot@zulip.com"]'); casper.click('.open_edit_bot_form[data-email="' + bot_email + '"]');
}); });
}); });
casper.then(function () { casper.then(function () {
casper.waitUntilVisible('.edit_bot_form[data-email="1-bot@zulip.com"]', function test_edit_bot_form_values() { casper.waitUntilVisible('.edit_bot_form[data-email="' + bot_email + '"]', function test_edit_bot_form_values() {
var form_sel = '.edit_bot_form[data-email="1-bot@zulip.com"]'; var form_sel = '.edit_bot_form[data-email="' + bot_email + '"]';
casper.test.info('Testing edit bot form values'); casper.test.info('Testing edit bot form values');
// casper.test.assertEqual( // casper.test.assertEqual(

View File

@@ -31,7 +31,7 @@
<label for="bot_short_name">{{t "Username" }}</label> <label for="bot_short_name">{{t "Username" }}</label>
<input type="text" name="bot_short_name" id="create_bot_short_name" class="required bot_local_part" <input type="text" name="bot_short_name" id="create_bot_short_name" class="required bot_local_part"
placeholder="bot_user_name" value="" /> placeholder="bot_user_name" value="" />
-{{t "bot" }}@{{ page_params.domain }} -bot@{{ page_params.realm_bot_domain }}
<div> <div>
<label for="create_bot_short_name" generated="true" class="text-error"></label> <label for="create_bot_short_name" generated="true" class="text-error"></label>
</div> </div>

View File

@@ -52,7 +52,8 @@ def server_is_up(server, log_file):
return False return False
@contextmanager @contextmanager
def test_server_running(force=False, external_host='testserver', log_file=None, dots=False, use_db=True): def test_server_running(force=False, external_host='testserver',
log_file=None, dots=False, use_db=True):
# type: (bool, str, str, bool, bool) -> Iterator[None] # type: (bool, str, str, bool, bool) -> Iterator[None]
if log_file: if log_file:
if os.path.exists(log_file) and os.path.getsize(log_file) < 100000: if os.path.exists(log_file) and os.path.getsize(log_file) < 100000:

View File

@@ -107,6 +107,7 @@ def fetch_initial_state_data(user_profile, event_types, queue_id,
state['realm_icon_source'] = user_profile.realm.icon_source state['realm_icon_source'] = user_profile.realm.icon_source
state['realm_email_changes_disabled'] = user_profile.realm.email_changes_disabled state['realm_email_changes_disabled'] = user_profile.realm.email_changes_disabled
state['max_icon_file_size'] = settings.MAX_ICON_FILE_SIZE state['max_icon_file_size'] = settings.MAX_ICON_FILE_SIZE
state['realm_bot_domain'] = user_profile.realm.get_bot_domain()
if want('realm_domain'): if want('realm_domain'):
state['realm_domain'] = user_profile.realm.domain state['realm_domain'] = user_profile.realm.domain

View File

@@ -191,6 +191,16 @@ class Realm(ModelReprMixin, models.Model):
# TODO: Change return type to QuerySet[UserProfile] # TODO: Change return type to QuerySet[UserProfile]
return UserProfile.objects.filter(realm=self, is_active=True).select_related() return UserProfile.objects.filter(realm=self, is_active=True).select_related()
def get_bot_domain(self):
# type: () -> str
# Remove the port. Mainly needed for development environment.
external_host = settings.EXTERNAL_HOST.split(':')[0]
if settings.REALMS_HAVE_SUBDOMAINS or \
Realm.objects.filter(deactivated=False) \
.exclude(string_id__in=settings.SYSTEM_ONLY_REALMS).count() > 1:
return "%s.%s" % (self.string_id, external_host)
return external_host
@property @property
def subdomain(self): def subdomain(self):
# type: () -> Optional[Text] # type: () -> Optional[Text]

View File

@@ -1091,7 +1091,8 @@ class TestZulipRemoteUserBackend(ZulipTestCase):
with self.settings(REALMS_HAVE_SUBDOMAINS=True, with self.settings(REALMS_HAVE_SUBDOMAINS=True,
AUTHENTICATION_BACKENDS=('zproject.backends.ZulipRemoteUserBackend',)): AUTHENTICATION_BACKENDS=('zproject.backends.ZulipRemoteUserBackend',)):
with mock.patch('zerver.views.auth.get_subdomain', return_value='acme'): with mock.patch('zerver.views.auth.get_subdomain', return_value='acme'):
result = self.client_post('http://testserver:9080/accounts/login/sso/', REMOTE_USER=email) result = self.client_post('http://testserver:9080/accounts/login/sso/',
REMOTE_USER=email)
self.assertEqual(result.status_code, 200) self.assertEqual(result.status_code, 200)
self.assertIs(get_session_dict_user(self.client.session), None) self.assertIs(get_session_dict_user(self.client.session), None)
self.assertIn(b"Let's get started", result.content) self.assertIn(b"Let's get started", result.content)
@@ -1102,7 +1103,8 @@ class TestZulipRemoteUserBackend(ZulipTestCase):
with self.settings(REALMS_HAVE_SUBDOMAINS=True, with self.settings(REALMS_HAVE_SUBDOMAINS=True,
AUTHENTICATION_BACKENDS=('zproject.backends.ZulipRemoteUserBackend',)): AUTHENTICATION_BACKENDS=('zproject.backends.ZulipRemoteUserBackend',)):
with mock.patch('zerver.views.auth.get_subdomain', return_value=''): with mock.patch('zerver.views.auth.get_subdomain', return_value=''):
result = self.client_post('http://testserver:9080/accounts/login/sso/', REMOTE_USER=email) result = self.client_post('http://testserver:9080/accounts/login/sso/',
REMOTE_USER=email)
self.assertEqual(result.status_code, 200) self.assertEqual(result.status_code, 200)
self.assertIs(get_session_dict_user(self.client.session), None) self.assertIs(get_session_dict_user(self.client.session), None)
self.assertIn(b"Let's get started", result.content) self.assertIn(b"Let's get started", result.content)

View File

@@ -38,9 +38,23 @@ class BotTest(ZulipTestCase):
self.assert_json_success(result) self.assert_json_success(result)
return ujson.loads(result.content) return ujson.loads(result.content)
def test_bot_domain(self):
# type: () -> None
self.login("hamlet@zulip.com")
self.create_bot()
self.assertTrue(UserProfile.objects.filter(email='hambot-bot@zulip.testserver').exists())
# The other cases are hard to test directly, since we don't allow creating bots from
# the wrong subdomain, and because 'testserver.example.com' is not a valid domain for the bot's email.
# So we just test the Raelm.get_bot_domain function.
realm = get_realm('zulip')
with self.settings(REALMS_HAVE_SUBDOMAINS=True):
self.assertEqual(realm.get_bot_domain(), 'zulip.testserver')
Realm.objects.exclude(string_id='zulip').update(deactivated=True)
self.assertEqual(realm.get_bot_domain(), 'testserver')
def deactivate_bot(self): def deactivate_bot(self):
# type: () -> None # type: () -> None
result = self.client_delete("/json/bots/hambot-bot@zulip.com") result = self.client_delete("/json/bots/hambot-bot@zulip.testserver")
self.assert_json_success(result) self.assert_json_success(result)
def test_add_bot_with_bad_username(self): def test_add_bot_with_bad_username(self):
@@ -64,14 +78,14 @@ class BotTest(ZulipTestCase):
result = self.create_bot() result = self.create_bot()
self.assert_num_bots_equal(1) self.assert_num_bots_equal(1)
bot = get_user_profile_by_email('hambot-bot@zulip.com') bot = get_user_profile_by_email('hambot-bot@zulip.testserver')
event = [e for e in events if e['event']['type'] == 'realm_bot'][0] event = [e for e in events if e['event']['type'] == 'realm_bot'][0]
self.assertEqual( self.assertEqual(
dict( dict(
type='realm_bot', type='realm_bot',
op='add', op='add',
bot=dict(email='hambot-bot@zulip.com', bot=dict(email='hambot-bot@zulip.testserver',
user_id=bot.id, user_id=bot.id,
full_name='The Bot of Hamlet', full_name='The Bot of Hamlet',
is_active=True, is_active=True,
@@ -87,11 +101,11 @@ class BotTest(ZulipTestCase):
users_result = self.client_get('/json/users') users_result = self.client_get('/json/users')
members = ujson.loads(users_result.content)['members'] members = ujson.loads(users_result.content)['members']
bots = [m for m in members if m['email'] == 'hambot-bot@zulip.com'] bots = [m for m in members if m['email'] == 'hambot-bot@zulip.testserver']
self.assertEqual(len(bots), 1) self.assertEqual(len(bots), 1)
bot = bots[0] bot = bots[0]
self.assertEqual(bot['bot_owner'], 'hamlet@zulip.com') self.assertEqual(bot['bot_owner'], 'hamlet@zulip.com')
self.assertEqual(bot['user_id'], get_user_profile_by_email('hambot-bot@zulip.com').id) self.assertEqual(bot['user_id'], get_user_profile_by_email('hambot-bot@zulip.testserver').id)
def test_add_bot_with_username_in_use(self): def test_add_bot_with_username_in_use(self):
# type: () -> None # type: () -> None
@@ -113,7 +127,7 @@ class BotTest(ZulipTestCase):
self.assert_num_bots_equal(0) self.assert_num_bots_equal(0)
with get_test_image_file('img.png') as fp: with get_test_image_file('img.png') as fp:
self.create_bot(file=fp) self.create_bot(file=fp)
profile = get_user_profile_by_email('hambot-bot@zulip.com') profile = get_user_profile_by_email('hambot-bot@zulip.testserver')
# Make sure that avatar image that we've uploaded is same with avatar image in the server # Make sure that avatar image that we've uploaded is same with avatar image in the server
self.assertTrue(filecmp.cmp(fp.name, self.assertTrue(filecmp.cmp(fp.name,
os.path.splitext(avatar_disk_path(profile))[0] + os.path.splitext(avatar_disk_path(profile))[0] +
@@ -147,7 +161,7 @@ class BotTest(ZulipTestCase):
self.assert_num_bots_equal(1) self.assert_num_bots_equal(1)
self.assertEqual(result['default_sending_stream'], 'Denmark') self.assertEqual(result['default_sending_stream'], 'Denmark')
profile = get_user_profile_by_email('hambot-bot@zulip.com') profile = get_user_profile_by_email('hambot-bot@zulip.testserver')
self.assertEqual(profile.default_sending_stream.name, 'Denmark') self.assertEqual(profile.default_sending_stream.name, 'Denmark')
def test_add_bot_with_default_sending_stream_not_subscribed(self): def test_add_bot_with_default_sending_stream_not_subscribed(self):
@@ -158,7 +172,7 @@ class BotTest(ZulipTestCase):
self.assert_num_bots_equal(1) self.assert_num_bots_equal(1)
self.assertEqual(result['default_sending_stream'], 'Rome') self.assertEqual(result['default_sending_stream'], 'Rome')
profile = get_user_profile_by_email('hambot-bot@zulip.com') profile = get_user_profile_by_email('hambot-bot@zulip.testserver')
self.assertEqual(profile.default_sending_stream.name, 'Rome') self.assertEqual(profile.default_sending_stream.name, 'Rome')
def test_bot_add_subscription(self): def test_bot_add_subscription(self):
@@ -192,7 +206,7 @@ class BotTest(ZulipTestCase):
# A bot # A bot
bot_request_data = { bot_request_data = {
'principals': '["hambot-bot@zulip.com"]' 'principals': '["hambot-bot@zulip.testserver"]'
} }
events_bot = [] # type: List[Dict[str, Any]] events_bot = [] # type: List[Dict[str, Any]]
with tornado_redirected_to_list(events_bot): with tornado_redirected_to_list(events_bot):
@@ -222,7 +236,7 @@ class BotTest(ZulipTestCase):
self.assert_num_bots_equal(1) self.assert_num_bots_equal(1)
self.assertEqual(result['default_sending_stream'], 'Denmark') self.assertEqual(result['default_sending_stream'], 'Denmark')
profile = get_user_profile_by_email('hambot-bot@zulip.com') profile = get_user_profile_by_email('hambot-bot@zulip.testserver')
self.assertEqual(profile.default_sending_stream.name, 'Denmark') self.assertEqual(profile.default_sending_stream.name, 'Denmark')
event = [e for e in events if e['event']['type'] == 'realm_bot'][0] event = [e for e in events if e['event']['type'] == 'realm_bot'][0]
@@ -230,7 +244,7 @@ class BotTest(ZulipTestCase):
dict( dict(
type='realm_bot', type='realm_bot',
op='add', op='add',
bot=dict(email='hambot-bot@zulip.com', bot=dict(email='hambot-bot@zulip.testserver',
user_id=profile.id, user_id=profile.id,
full_name='The Bot of Hamlet', full_name='The Bot of Hamlet',
is_active=True, is_active=True,
@@ -269,7 +283,7 @@ class BotTest(ZulipTestCase):
self.assert_num_bots_equal(1) self.assert_num_bots_equal(1)
self.assertEqual(result['default_events_register_stream'], 'Denmark') self.assertEqual(result['default_events_register_stream'], 'Denmark')
profile = get_user_profile_by_email('hambot-bot@zulip.com') profile = get_user_profile_by_email('hambot-bot@zulip.testserver')
self.assertEqual(profile.default_events_register_stream.name, 'Denmark') self.assertEqual(profile.default_events_register_stream.name, 'Denmark')
def test_add_bot_with_default_events_register_stream_private_allowed(self): def test_add_bot_with_default_events_register_stream_private_allowed(self):
@@ -286,7 +300,7 @@ class BotTest(ZulipTestCase):
self.assert_num_bots_equal(1) self.assert_num_bots_equal(1)
self.assertEqual(result['default_events_register_stream'], 'Denmark') self.assertEqual(result['default_events_register_stream'], 'Denmark')
bot_profile = get_user_profile_by_email('hambot-bot@zulip.com') bot_profile = get_user_profile_by_email('hambot-bot@zulip.testserver')
self.assertEqual(bot_profile.default_events_register_stream.name, 'Denmark') self.assertEqual(bot_profile.default_events_register_stream.name, 'Denmark')
event = [e for e in events if e['event']['type'] == 'realm_bot'][0] event = [e for e in events if e['event']['type'] == 'realm_bot'][0]
@@ -294,7 +308,7 @@ class BotTest(ZulipTestCase):
dict( dict(
type='realm_bot', type='realm_bot',
op='add', op='add',
bot=dict(email='hambot-bot@zulip.com', bot=dict(email='hambot-bot@zulip.testserver',
full_name='The Bot of Hamlet', full_name='The Bot of Hamlet',
user_id=bot_profile.id, user_id=bot_profile.id,
is_active=True, is_active=True,
@@ -334,7 +348,7 @@ class BotTest(ZulipTestCase):
self.assert_num_bots_equal(1) self.assert_num_bots_equal(1)
self.assertTrue(result['default_all_public_streams']) self.assertTrue(result['default_all_public_streams'])
profile = get_user_profile_by_email('hambot-bot@zulip.com') profile = get_user_profile_by_email('hambot-bot@zulip.testserver')
self.assertEqual(profile.default_all_public_streams, True) self.assertEqual(profile.default_all_public_streams, True)
def test_deactivate_bot(self): def test_deactivate_bot(self):
@@ -375,7 +389,7 @@ class BotTest(ZulipTestCase):
result = self.client_delete("/json/bots/hamlet@zulip.com") result = self.client_delete("/json/bots/hamlet@zulip.com")
self.assert_json_error(result, 'No such bot') self.assert_json_error(result, 'No such bot')
result = self.client_delete("/json/bots/hambot-bot@zulip.com") result = self.client_delete("/json/bots/hambot-bot@zulip.testserver")
self.assert_json_error(result, 'Insufficient permission') self.assert_json_error(result, 'Insufficient permission')
# But we don't actually deactivate the other person's bot. # But we don't actually deactivate the other person's bot.
@@ -383,7 +397,7 @@ class BotTest(ZulipTestCase):
self.assert_num_bots_equal(1) self.assert_num_bots_equal(1)
# Can not deactivate a bot as a user # Can not deactivate a bot as a user
result = self.client_delete("/json/users/hambot-bot@zulip.com") result = self.client_delete("/json/users/hambot-bot@zulip.testserver")
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)
@@ -397,13 +411,13 @@ class BotTest(ZulipTestCase):
# Have Othello try to mess with Hamlet's bots. # Have Othello try to mess with Hamlet's bots.
self.login("othello@zulip.com") self.login("othello@zulip.com")
result = self.client_post("/json/bots/hambot-bot@zulip.com/api_key/regenerate") result = self.client_post("/json/bots/hambot-bot@zulip.testserver/api_key/regenerate")
self.assert_json_error(result, 'Insufficient permission') self.assert_json_error(result, 'Insufficient permission')
bot_info = { bot_info = {
'full_name': 'Fred', 'full_name': 'Fred',
} }
result = self.client_patch("/json/bots/hambot-bot@zulip.com", bot_info) result = self.client_patch("/json/bots/hambot-bot@zulip.testserver", bot_info)
self.assert_json_error(result, 'Insufficient permission') self.assert_json_error(result, 'Insufficient permission')
def get_bot(self): def get_bot(self):
@@ -418,7 +432,7 @@ class BotTest(ZulipTestCase):
self.create_bot() self.create_bot()
bot = self.get_bot() bot = self.get_bot()
old_api_key = bot['api_key'] old_api_key = bot['api_key']
result = self.client_post('/json/bots/hambot-bot@zulip.com/api_key/regenerate') result = self.client_post('/json/bots/hambot-bot@zulip.testserver/api_key/regenerate')
self.assert_json_success(result) self.assert_json_success(result)
new_api_key = ujson.loads(result.content)['api_key'] new_api_key = ujson.loads(result.content)['api_key']
self.assertNotEqual(old_api_key, new_api_key) self.assertNotEqual(old_api_key, new_api_key)
@@ -443,7 +457,7 @@ class BotTest(ZulipTestCase):
bot_info = { bot_info = {
'full_name': 'Fred', 'full_name': 'Fred',
} }
result = self.client_patch("/json/bots/hambot-bot@zulip.com", bot_info) result = self.client_patch("/json/bots/hambot-bot@zulip.testserver", bot_info)
self.assert_json_success(result) self.assert_json_success(result)
full_name = ujson.loads(result.content)['full_name'] full_name = ujson.loads(result.content)['full_name']
@@ -464,7 +478,7 @@ class BotTest(ZulipTestCase):
bot_info = { bot_info = {
'bot_owner': 'othello@zulip.com', 'bot_owner': 'othello@zulip.com',
} }
result = self.client_patch("/json/bots/hambot-bot@zulip.com", bot_info) result = self.client_patch("/json/bots/hambot-bot@zulip.testserver", bot_info)
self.assert_json_success(result) self.assert_json_success(result)
# Test bot's owner has been changed successfully. # Test bot's owner has been changed successfully.
@@ -486,26 +500,26 @@ class BotTest(ZulipTestCase):
result = self.client_post("/json/bots", bot_info) result = self.client_post("/json/bots", bot_info)
self.assert_json_success(result) self.assert_json_success(result)
profile = get_user_profile_by_email('hambot-bot@zulip.com') profile = get_user_profile_by_email('hambot-bot@zulip.testserver')
self.assertEqual(profile.avatar_source, UserProfile.AVATAR_FROM_GRAVATAR) self.assertEqual(profile.avatar_source, UserProfile.AVATAR_FROM_GRAVATAR)
# Try error case first (too many files): # Try error case first (too many files):
with get_test_image_file('img.png') as fp1, \ with get_test_image_file('img.png') as fp1, \
get_test_image_file('img.gif') as fp2: get_test_image_file('img.gif') as fp2:
result = self.client_patch_multipart( result = self.client_patch_multipart(
'/json/bots/hambot-bot@zulip.com', '/json/bots/hambot-bot@zulip.testserver',
dict(file1=fp1, file2=fp2)) dict(file1=fp1, file2=fp2))
self.assert_json_error(result, 'You may only upload one file at a time') self.assert_json_error(result, 'You may only upload one file at a time')
profile = get_user_profile_by_email("hambot-bot@zulip.com") profile = get_user_profile_by_email("hambot-bot@zulip.testserver")
self.assertEqual(profile.avatar_version, 1) self.assertEqual(profile.avatar_version, 1)
# HAPPY PATH # HAPPY PATH
with get_test_image_file('img.png') as fp: with get_test_image_file('img.png') as fp:
result = self.client_patch_multipart( result = self.client_patch_multipart(
'/json/bots/hambot-bot@zulip.com', '/json/bots/hambot-bot@zulip.testserver',
dict(file=fp)) dict(file=fp))
profile = get_user_profile_by_email('hambot-bot@zulip.com') profile = get_user_profile_by_email('hambot-bot@zulip.testserver')
self.assertEqual(profile.avatar_version, 2) self.assertEqual(profile.avatar_version, 2)
# Make sure that avatar image that we've uploaded is same with avatar image in the server # Make sure that avatar image that we've uploaded is same with avatar image in the server
self.assertTrue(filecmp.cmp(fp.name, self.assertTrue(filecmp.cmp(fp.name,
@@ -528,7 +542,7 @@ class BotTest(ZulipTestCase):
bot_info = { bot_info = {
'default_sending_stream': 'Denmark', 'default_sending_stream': 'Denmark',
} }
result = self.client_patch("/json/bots/hambot-bot@zulip.com", bot_info) result = self.client_patch("/json/bots/hambot-bot@zulip.testserver", bot_info)
self.assert_json_success(result) self.assert_json_success(result)
default_sending_stream = ujson.loads(result.content)['default_sending_stream'] default_sending_stream = ujson.loads(result.content)['default_sending_stream']
@@ -549,7 +563,7 @@ class BotTest(ZulipTestCase):
bot_info = { bot_info = {
'default_sending_stream': 'Rome', 'default_sending_stream': 'Rome',
} }
result = self.client_patch("/json/bots/hambot-bot@zulip.com", bot_info) result = self.client_patch("/json/bots/hambot-bot@zulip.testserver", bot_info)
self.assert_json_success(result) self.assert_json_success(result)
default_sending_stream = ujson.loads(result.content)['default_sending_stream'] default_sending_stream = ujson.loads(result.content)['default_sending_stream']
@@ -570,11 +584,11 @@ class BotTest(ZulipTestCase):
bot_info = { bot_info = {
'default_sending_stream': '', 'default_sending_stream': '',
} }
result = self.client_patch("/json/bots/hambot-bot@zulip.com", bot_info) result = self.client_patch("/json/bots/hambot-bot@zulip.testserver", bot_info)
self.assert_json_success(result) self.assert_json_success(result)
default_sending_stream = get_user_profile_by_email( default_sending_stream = get_user_profile_by_email(
"hambot-bot@zulip.com").default_sending_stream "hambot-bot@zulip.testserver").default_sending_stream
self.assertEqual(None, default_sending_stream) self.assertEqual(None, default_sending_stream)
bot = self.get_bot() bot = self.get_bot()
@@ -597,7 +611,7 @@ class BotTest(ZulipTestCase):
bot_info = { bot_info = {
'default_sending_stream': 'Denmark', 'default_sending_stream': 'Denmark',
} }
result = self.client_patch("/json/bots/hambot-bot@zulip.com", bot_info) result = self.client_patch("/json/bots/hambot-bot@zulip.testserver", bot_info)
self.assert_json_success(result) self.assert_json_success(result)
default_sending_stream = ujson.loads(result.content)['default_sending_stream'] default_sending_stream = ujson.loads(result.content)['default_sending_stream']
@@ -624,7 +638,7 @@ class BotTest(ZulipTestCase):
bot_info = { bot_info = {
'default_sending_stream': 'Denmark', 'default_sending_stream': 'Denmark',
} }
result = self.client_patch("/json/bots/hambot-bot@zulip.com", 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'")
def test_patch_bot_to_stream_not_found(self): def test_patch_bot_to_stream_not_found(self):
@@ -639,7 +653,7 @@ class BotTest(ZulipTestCase):
bot_info = { bot_info = {
'default_sending_stream': 'missing', 'default_sending_stream': 'missing',
} }
result = self.client_patch("/json/bots/hambot-bot@zulip.com", 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'")
def test_patch_bot_events_register_stream(self): def test_patch_bot_events_register_stream(self):
@@ -654,7 +668,7 @@ class BotTest(ZulipTestCase):
bot_info = { bot_info = {
'default_events_register_stream': 'Denmark', 'default_events_register_stream': 'Denmark',
} }
result = self.client_patch("/json/bots/hambot-bot@zulip.com", bot_info) result = self.client_patch("/json/bots/hambot-bot@zulip.testserver", bot_info)
self.assert_json_success(result) self.assert_json_success(result)
default_events_register_stream = ujson.loads(result.content)['default_events_register_stream'] default_events_register_stream = ujson.loads(result.content)['default_events_register_stream']
@@ -679,7 +693,7 @@ class BotTest(ZulipTestCase):
bot_info = { bot_info = {
'default_events_register_stream': 'Denmark', 'default_events_register_stream': 'Denmark',
} }
result = self.client_patch("/json/bots/hambot-bot@zulip.com", bot_info) result = self.client_patch("/json/bots/hambot-bot@zulip.testserver", bot_info)
self.assert_json_success(result) self.assert_json_success(result)
default_events_register_stream = ujson.loads(result.content)['default_events_register_stream'] default_events_register_stream = ujson.loads(result.content)['default_events_register_stream']
@@ -705,7 +719,7 @@ class BotTest(ZulipTestCase):
bot_info = { bot_info = {
'default_events_register_stream': 'Denmark', 'default_events_register_stream': 'Denmark',
} }
result = self.client_patch("/json/bots/hambot-bot@zulip.com", 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'")
def test_patch_bot_events_register_stream_none(self): def test_patch_bot_events_register_stream_none(self):
@@ -720,11 +734,11 @@ class BotTest(ZulipTestCase):
bot_info = { bot_info = {
'default_events_register_stream': '', 'default_events_register_stream': '',
} }
result = self.client_patch("/json/bots/hambot-bot@zulip.com", bot_info) result = self.client_patch("/json/bots/hambot-bot@zulip.testserver", bot_info)
self.assert_json_success(result) self.assert_json_success(result)
default_events_register_stream = get_user_profile_by_email( default_events_register_stream = get_user_profile_by_email(
"hambot-bot@zulip.com").default_events_register_stream "hambot-bot@zulip.testserver").default_events_register_stream
self.assertEqual(None, default_events_register_stream) self.assertEqual(None, default_events_register_stream)
bot = self.get_bot() bot = self.get_bot()
@@ -742,7 +756,7 @@ class BotTest(ZulipTestCase):
bot_info = { bot_info = {
'default_events_register_stream': 'missing', 'default_events_register_stream': 'missing',
} }
result = self.client_patch("/json/bots/hambot-bot@zulip.com", 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'")
def test_patch_bot_default_all_public_streams_true(self): def test_patch_bot_default_all_public_streams_true(self):
@@ -757,7 +771,7 @@ class BotTest(ZulipTestCase):
bot_info = { bot_info = {
'default_all_public_streams': ujson.dumps(True), 'default_all_public_streams': ujson.dumps(True),
} }
result = self.client_patch("/json/bots/hambot-bot@zulip.com", bot_info) result = self.client_patch("/json/bots/hambot-bot@zulip.testserver", bot_info)
self.assert_json_success(result) self.assert_json_success(result)
default_events_register_stream = ujson.loads(result.content)['default_all_public_streams'] default_events_register_stream = ujson.loads(result.content)['default_all_public_streams']
@@ -778,7 +792,7 @@ class BotTest(ZulipTestCase):
bot_info = { bot_info = {
'default_all_public_streams': ujson.dumps(False), 'default_all_public_streams': ujson.dumps(False),
} }
result = self.client_patch("/json/bots/hambot-bot@zulip.com", bot_info) result = self.client_patch("/json/bots/hambot-bot@zulip.testserver", bot_info)
self.assert_json_success(result) self.assert_json_success(result)
default_events_register_stream = ujson.loads(result.content)['default_all_public_streams'] default_events_register_stream = ujson.loads(result.content)['default_all_public_streams']
@@ -800,7 +814,7 @@ class BotTest(ZulipTestCase):
'full_name': 'Fred', 'full_name': 'Fred',
'method': 'PATCH' 'method': 'PATCH'
} }
result = self.client_post("/json/bots/hambot-bot@zulip.com", bot_info) result = self.client_post("/json/bots/hambot-bot@zulip.testserver", bot_info)
self.assert_json_success(result) self.assert_json_success(result)
full_name = ujson.loads(result.content)['full_name'] full_name = ujson.loads(result.content)['full_name']

View File

@@ -97,6 +97,7 @@ class HomeTest(ZulipTestCase):
"realm_add_emoji_by_admins_only", "realm_add_emoji_by_admins_only",
"realm_allow_message_editing", "realm_allow_message_editing",
"realm_authentication_methods", "realm_authentication_methods",
"realm_bot_domain",
"realm_create_stream_by_admins_only", "realm_create_stream_by_admins_only",
"realm_default_language", "realm_default_language",
"realm_default_streams", "realm_default_streams",

View File

@@ -1025,12 +1025,12 @@ class MessagePOSTTest(ZulipTestCase):
result = self.client_post("/json/bots", bot_info) result = self.client_post("/json/bots", bot_info)
self.assert_json_success(result) self.assert_json_success(result)
email = "irc-bot@zulip.com" email = "irc-bot@zulip.testserver"
user = get_user_profile_by_email(email) user = get_user_profile_by_email(email)
user.is_api_super_user = True user.is_api_super_user = True
user.save() user.save()
user = get_user_profile_by_email(email) user = get_user_profile_by_email(email)
self.subscribe_to_stream(email, "#IRCland") self.subscribe_to_stream(email, "#IRCland", realm=user.realm)
result = self.client_post("/api/v1/messages", result = self.client_post("/api/v1/messages",
{"type": "stream", {"type": "stream",
"forged": "true", "forged": "true",

View File

@@ -291,6 +291,7 @@ def home_real(request):
'realm_add_emoji_by_admins_only', 'realm_add_emoji_by_admins_only',
'realm_allow_message_editing', 'realm_allow_message_editing',
'realm_authentication_methods', 'realm_authentication_methods',
'realm_bot_domain',
'realm_create_stream_by_admins_only', 'realm_create_stream_by_admins_only',
'realm_default_language', 'realm_default_language',
'realm_default_streams', 'realm_default_streams',

View File

@@ -238,7 +238,7 @@ def add_bot_backend(request, user_profile, full_name_raw=REQ("full_name"), short
# type: (HttpRequest, UserProfile, Text, Text, Optional[Text], Optional[Text], Optional[bool]) -> HttpResponse # type: (HttpRequest, UserProfile, Text, Text, Optional[Text], Optional[Text], Optional[bool]) -> HttpResponse
short_name += "-bot" short_name += "-bot"
full_name = check_full_name(full_name_raw) full_name = check_full_name(full_name_raw)
email = short_name + "@" + user_profile.realm.domain email = '%s@%s' % (short_name, user_profile.realm.get_bot_domain())
form = CreateUserForm({'full_name': full_name, 'email': email}) form = CreateUserForm({'full_name': full_name, 'email': email})
if not form.is_valid(): if not form.is_valid():
# We validate client-side as well # We validate client-side as well