Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

bots: Don't send realm_bot/delete event to a deactivated previous owner.

Sahil Batradc7d57c96d34
diff --git a/zerver/actions/bots.py b/zerver/actions/bots.py
index 9e460159f3b..08f4d04be34 100644
--- a/zerver/actions/bots.py
+++ b/zerver/actions/bots.py
@@ -27,7 +27,7 @@ def send_bot_owner_update_events(
send_event_on_commit(user_profile.realm, event, active_user_ids(user_profile.realm_id))
# Delete the bot from previous owner's bot data.
- if previous_owner and not previous_owner.is_realm_admin:
+ if previous_owner and previous_owner.is_active and not previous_owner.is_realm_admin:
delete_event = RealmBotDeleteEvent(bot=BotTypeForDelete(user_id=user_profile.id))
previous_owner_id = previous_owner.id
send_event_on_commit(
diff --git a/zerver/openapi/zulip.yaml b/zerver/openapi/zulip.yaml
index c90995e51ea..b057ed0d947 100644
--- a/zerver/openapi/zulip.yaml
+++ b/zerver/openapi/zulip.yaml
@@ -4934,7 +4934,7 @@ paths:
description: |
Event sent to the previous owner of a bot when the bot's
owner is changed, unless the previous owner is an
- organization administrator.
+ organization administrator or is deactivated.
properties:
id:
$ref: "#/components/schemas/EventIdSchema"
diff --git a/zerver/tests/test_bots.py b/zerver/tests/test_bots.py
index 781f97b6660..fbe65070342 100644
--- a/zerver/tests/test_bots.py
+++ b/zerver/tests/test_bots.py
@@ -1096,12 +1096,29 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
do_deactivate_user(self.example_user("hamlet"), acting_user=None)
self.login("iago")
- result = self.client_post(f"/json/users/{bot_id}/reactivate")
+ with self.capture_send_event_calls(expected_num_events=7) as events:
+ result = self.client_post(f"/json/users/{bot_id}/reactivate")
self.assert_json_success(result)
test_bot = UserProfile.objects.get(id=bot_id, is_bot=True)
assert test_bot.bot_owner is not None
self.assertEqual(test_bot.bot_owner.id, self.example_user("iago").id)
+ # There is no realm_bot delete event, since the previous
+ # owner is deactivated.
+ self.assertEqual(events[0]["event"]["type"], "realm_user")
+ self.assertEqual(events[0]["event"]["person"], {"user_id": bot_id, "is_active": True})
+ self.assertEqual(events[1]["event"]["type"], "realm_user")
+ self.assertEqual(
+ events[1]["event"]["person"],
+ {"user_id": bot_id, "bot_owner_id": self.example_user("iago").id},
+ )
+ for event in events[2:5]:
+ self.assertEqual(event["event"]["type"], "subscription")
+ self.assertEqual(event["event"]["op"], "peer_add")
+ for event in events[5:]:
+ self.assertEqual(event["event"]["type"], "user_group")
+ self.assertEqual(event["event"]["op"], "add_members")
+
self.assertTrue(
Subscription.objects.filter(
user_profile=test_bot, recipient__type_id=private_stream.id, active=True