From f0121973d2ba28b78b542c106f4f92047c76c47c Mon Sep 17 00:00:00 2001 From: Abhijeet Kaur Date: Thu, 16 Feb 2017 01:36:07 +0530 Subject: [PATCH] bug fix: Fix error when admin renames a bot after reactivating it. Fix administration page javascript issue of TypeError that occurs due to undefined variable access in static/js/bot_data.js file. Reactivating a bot was not updating the state in `bot_data`. Sending an event on reactivating a bot fixes this issue. Fixes: #2840 --- zerver/lib/actions.py | 33 +++++++++++++++++++++------------ zerver/tests/test_events.py | 26 ++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 12 deletions(-) diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py index d27dd5d1cb..25a12b2b85 100644 --- a/zerver/lib/actions.py +++ b/zerver/lib/actions.py @@ -372,18 +372,24 @@ def notify_created_bot(user_profile): default_sending_stream_name = stream_name(user_profile.default_sending_stream) default_events_register_stream_name = stream_name(user_profile.default_events_register_stream) - event = dict(type="realm_bot", op="add", - bot=dict(email=user_profile.email, - user_id=user_profile.id, - full_name=user_profile.full_name, - is_active=user_profile.is_active, - api_key=user_profile.api_key, - default_sending_stream=default_sending_stream_name, - default_events_register_stream=default_events_register_stream_name, - default_all_public_streams=user_profile.default_all_public_streams, - avatar_url=avatar_url(user_profile), - owner=user_profile.bot_owner.email, - )) + bot = dict(email=user_profile.email, + user_id=user_profile.id, + full_name=user_profile.full_name, + is_active=user_profile.is_active, + api_key=user_profile.api_key, + default_sending_stream=default_sending_stream_name, + default_events_register_stream=default_events_register_stream_name, + default_all_public_streams=user_profile.default_all_public_streams, + avatar_url=avatar_url(user_profile), + ) + + # Set the owner key only when the bot has an owner. + # The default bots don't have an owner. So don't + # set the owner key while reactivating them. + if user_profile.bot_owner is not None: + bot['owner'] = user_profile.bot_owner.email + + event = dict(type="realm_bot", op="add", bot=bot) send_event(event, bot_owner_userids(user_profile)) def do_create_user(email, password, realm, full_name, short_name, @@ -1862,6 +1868,9 @@ def do_reactivate_user(user_profile): notify_created_user(user_profile) + if user_profile.is_bot: + notify_created_bot(user_profile) + def do_change_password(user_profile, password, log=True, commit=True, hashed_password=False): # type: (UserProfile, Text, bool, bool, bool) -> None diff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.py index b12df78ef9..1625b71bf5 100644 --- a/zerver/tests/test_events.py +++ b/zerver/tests/test_events.py @@ -31,6 +31,7 @@ from zerver.lib.actions import ( do_create_user, do_deactivate_stream, do_deactivate_user, + do_reactivate_user, do_regenerate_api_key, do_remove_alert_words, do_remove_realm_emoji, @@ -1038,6 +1039,31 @@ class EventsRegisterTest(ZulipTestCase): error = bot_deactivate_checker('events[1]', events[1]) self.assert_on_error(error) + def test_do_reactivate_user(self): + # type: () -> None + bot_reactivate_checker = check_dict([ + ('type', equals('realm_bot')), + ('op', equals('add')), + ('bot', check_dict([ + ('email', check_string), + ('user_id', check_int), + ('full_name', check_string), + ('is_active', check_bool), + ('api_key', check_string), + ('default_sending_stream', check_none_or(check_string)), + ('default_events_register_stream', check_none_or(check_string)), + ('default_all_public_streams', check_bool), + ('avatar_url', check_string), + ('owner', check_none_or(check_string)), + ])), + ]) + bot = self.create_bot('foo-bot@zulip.com') + do_deactivate_user(bot) + action = lambda: do_reactivate_user(bot) + events = self.do_test(action) + error = bot_reactivate_checker('events[1]', events[1]) + self.assert_on_error(error) + def test_rename_stream(self): # type: () -> None stream = self.make_stream('old_name')