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: Remove subscription from inaccessible streams on reactivating bot.

Sahil Batra98b0fa8ae2eb
diff --git a/zerver/actions/create_user.py b/zerver/actions/create_user.py
index 90491b52590..3a1bc7ac6ed 100644
--- a/zerver/actions/create_user.py
+++ b/zerver/actions/create_user.py
@@ -646,6 +646,11 @@ def do_reactivate_user(user_profile: UserProfile, *, acting_user: Optional[UserP
assert acting_user is not None
send_bot_owner_update_events(user_profile, acting_user, previous_owner)
+ if bot_owner_changed:
+ from zerver.actions.bots import remove_bot_from_inaccessible_private_streams
+
+ remove_bot_from_inaccessible_private_streams(user_profile, acting_user=acting_user)
+
subscribed_recipient_ids = Subscription.objects.filter(
user_profile_id=user_profile.id, active=True, recipient__type=Recipient.STREAM
).values_list("recipient__type_id", flat=True)
diff --git a/zerver/tests/test_bots.py b/zerver/tests/test_bots.py
index bd41b235ee2..06a65616cc2 100644
--- a/zerver/tests/test_bots.py
+++ b/zerver/tests/test_bots.py
@@ -861,6 +861,17 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
bot_id = result.json()["user_id"]
test_bot = UserProfile.objects.get(id=bot_id, is_bot=True)
+ private_stream = self.make_stream("private_stream", invite_only=True)
+ public_stream = self.make_stream("public_stream")
+ self.subscribe(test_bot, "private_stream")
+ self.subscribe(self.example_user("hamlet"), "private_stream")
+ self.subscribe(test_bot, "public_stream")
+ self.subscribe(self.example_user("hamlet"), "public_stream")
+
+ private_stream_test = self.make_stream("private_stream_test", invite_only=True)
+ self.subscribe(self.example_user("iago"), "private_stream_test")
+ self.subscribe(test_bot, "private_stream_test")
+
do_deactivate_user(test_bot, acting_user=None)
# Deactivate the bot owner
@@ -873,6 +884,22 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
assert test_bot.bot_owner is not None
self.assertEqual(test_bot.bot_owner.id, self.example_user("iago").id)
+ self.assertFalse(
+ Subscription.objects.filter(
+ user_profile=test_bot, recipient__type_id=private_stream.id, active=True
+ ).exists()
+ )
+ self.assertTrue(
+ Subscription.objects.filter(
+ user_profile=test_bot, recipient__type_id=private_stream_test.id, active=True
+ ).exists()
+ )
+ self.assertTrue(
+ Subscription.objects.filter(
+ user_profile=test_bot, recipient__type_id=public_stream.id, active=True
+ ).exists()
+ )
+
def test_patch_bot_full_name(self) -> None:
self.login("hamlet")
bot_info = {
diff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.py
index 403c5819773..d1b5f598861 100644
--- a/zerver/tests/test_events.py
+++ b/zerver/tests/test_events.py
@@ -2476,10 +2476,12 @@ class NormalActionsTest(BaseAction):
self.user_profile = self.example_user("iago")
action = lambda: do_reactivate_user(bot, acting_user=self.example_user("iago"))
- events = self.verify_action(action, num_events=6)
+ events = self.verify_action(action, num_events=7)
check_realm_bot_add("events[1]", events[1])
check_realm_bot_update("events[2]", events[2], "owner_id")
check_realm_user_update("events[3]", events[3], "bot_owner_id")
+ check_subscription_peer_remove("events[4]", events[4])
+ check_stream_delete("events[5]", events[5])
def test_do_deactivate_realm(self) -> None:
realm = self.user_profile.realm