Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
users: Soften assumptions that all bots have owners.
Alex Vandiver989d4250235f
diff --git a/zerver/models/users.py b/zerver/models/users.pyindex cdeab1d6e69..f7127e4b262 100644--- a/zerver/models/users.py+++ b/zerver/models/users.py@@ -1128,12 +1128,14 @@ def bot_owner_user_ids(user_profile: UserProfile) -> set[int]:user_profile.default_events_register_streamand user_profile.default_events_register_stream.invite_only)- assert user_profile.bot_owner_id is not Noneif is_private_bot:- return {user_profile.bot_owner_id}+ if user_profile.bot_owner_id is not None:+ return {user_profile.bot_owner_id}+ return set()else:users = {user.id for user in user_profile.realm.get_human_admin_users()}- users.add(user_profile.bot_owner_id)+ if user_profile.bot_owner_id is not None:+ users.add(user_profile.bot_owner_id)return usersdiff --git a/zerver/tests/test_bots.py b/zerver/tests/test_bots.pyindex 58ba3cd02ad..876cd21d6a3 100644--- a/zerver/tests/test_bots.py+++ b/zerver/tests/test_bots.py@@ -8,7 +8,7 @@ from django.core import mailfrom django.test import override_settingsfrom zulip_bots.custom_exceptions import ConfigValidationError-from zerver.actions.bots import do_change_bot_owner+from zerver.actions.bots import do_change_bot_owner, do_change_default_sending_streamfrom zerver.actions.realm_settings import do_set_realm_user_default_settingfrom zerver.actions.streams import do_change_stream_permissionfrom zerver.actions.users import do_change_can_create_users, do_change_user_role, do_deactivate_user@@ -17,11 +17,12 @@ from zerver.lib.bot_lib import get_bot_handlerfrom zerver.lib.integrations import EMBEDDED_BOTS, WebhookIntegrationfrom zerver.lib.test_classes import UploadSerializeMixin, ZulipTestCasefrom zerver.lib.test_helpers import avatar_disk_path, get_test_image_file+from zerver.lib.utils import assert_is_not_nonefrom zerver.models import RealmUserDefault, Service, Subscription, UserProfilefrom zerver.models.bots import get_bot_servicesfrom zerver.models.realms import BotCreationPolicyEnum, get_realmfrom zerver.models.streams import get_stream-from zerver.models.users import get_user, is_cross_realm_bot_email+from zerver.models.users import bot_owner_user_ids, get_user, is_cross_realm_bot_email# A test validator@@ -590,6 +591,32 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):self.assert_json_error(result, "No such bot")self.assert_num_bots_equal(1)+ def test_deactivate_bot_with_no_owners(self) -> None:+ iago = self.example_user("iago")+ self.login("iago")+ self.create_bot()+ self.assert_num_bots_equal(1)++ # Set up the bot to be a private bot, as otherwise realm admins+ # are used as default owners in the absence of .bot_owner_id.+ stream = get_stream("Denmark", get_realm("zulip"))+ do_change_stream_permission(+ stream,+ invite_only=True,+ history_public_to_subscribers=False,+ is_web_public=False,+ acting_user=iago,+ )++ new_bot = assert_is_not_none(UserProfile.objects.last())+ do_change_default_sending_stream(new_bot, stream, acting_user=iago)+ new_bot.bot_owner_id = None+ new_bot.save()++ result = self.client_delete(f"/json/bots/{new_bot.id}")+ self.assert_json_success(result)+ self.assert_num_bots_equal(0)+def test_deactivate_bot_with_owner_deactivation(self) -> None:user = self.example_user("hamlet")self.login_user(user)@@ -1833,6 +1860,26 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):self.assertTrue(is_cross_realm_bot_email("[email protected]"))self.assertFalse(is_cross_realm_bot_email("[email protected]"))+ def test_private_bot_empty_bot_owner_user_ids(self) -> None:+ hamlet = self.example_user("hamlet")+ self.login("hamlet")+ self.create_bot()+ stream = get_stream("Denmark", get_realm("zulip"))+ do_change_stream_permission(+ stream,+ invite_only=True,+ history_public_to_subscribers=False,+ is_web_public=False,+ acting_user=hamlet,+ )++ new_bot = assert_is_not_none(UserProfile.objects.last())+ do_change_default_sending_stream(new_bot, stream, acting_user=hamlet)+ new_bot.bot_owner_id = None+ new_bot.save()++ self.assertEqual(bot_owner_user_ids(new_bot), set())+@patch("zerver.lib.integrations.WEBHOOK_INTEGRATIONS", stripe_sample_config_options)def test_create_incoming_webhook_bot_with_service_name_and_with_keys(self) -> None:self.login("hamlet")