bots: Handle exception on changing bot owner to invalid user.

It catches the `UserProfile.DoesNotExist` exception and
hence prevent internal server error.
Also remove option to select empty bot owner.
Fixes: #8334.
This commit is contained in:
Shubham Dhama 2018-02-10 18:47:04 +05:30 committed by Tim Abbott
parent e1f943913a
commit adfc905c3f
3 changed files with 26 additions and 2 deletions

View File

@ -1,5 +1,4 @@
<select name="bot_owner_select">
<option value=''></option>
{{#each users_list}}
<option value='{{this.email}}'>{{this.full_name}}</option>
{{/each}}

View File

@ -642,6 +642,27 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
bot = self.get_bot()
self.assertEqual('The Bot of Hamlet', bot['full_name'])
def test_patch_bot_owner_bad_username(self) -> None:
self.login(self.example_email('hamlet'))
self.create_bot()
self.assert_num_bots_equal(1)
bot_info = {
'bot_owner': '',
}
result = self.client_patch("/json/bots/hambot-bot@zulip.testserver", bot_info)
self.assert_json_error(result, "Failed to change owner, no such user")
profile = get_user('hambot-bot@zulip.testserver', get_realm('zulip'))
self.assertEqual(profile.bot_owner, self.example_user("hamlet"))
bot_info = {
'bot_owner': 'Invalid name',
}
result = self.client_patch("/json/bots/hambot-bot@zulip.testserver", bot_info)
self.assert_json_error(result, "Failed to change owner, no such user")
profile = get_user('hambot-bot@zulip.testserver', get_realm('zulip'))
self.assertEqual(profile.bot_owner, self.example_user("hamlet"))
@override_settings(LOCAL_UPLOADS_DIR='var/bot_avatar')
def test_patch_bot_avatar(self) -> None:
self.login(self.example_email('hamlet'))

View File

@ -173,8 +173,12 @@ def patch_bot_backend(
if full_name is not None:
check_change_full_name(bot, full_name, user_profile)
if bot_owner is not None:
owner = get_user(bot_owner, user_profile.realm)
try:
owner = get_user(bot_owner, user_profile.realm)
except UserProfile.DoesNotExist:
return json_error(_('Failed to change owner, no such user'))
do_change_bot_owner(bot, owner, user_profile)
if default_sending_stream is not None:
if default_sending_stream == "":
stream = None # type: Optional[Stream]