From f67d7549de5403cc9423ca4480e67e0c8a9c9697 Mon Sep 17 00:00:00 2001 From: Prakhar Pratyush Date: Tue, 24 Sep 2024 14:18:39 +0530 Subject: [PATCH] message: Add test coverage for 'set_visibility_policy_possible'. This commit adds test coverage for the 'if user_profile.realm != message.get_realm():' block in 'set_visibility_policy_possible' function. Currently, 'test_command_with_consented_message_id' adds the test coverage for this code block. But we plan to remove that test in the next commit as a part of removing 'react on consent message' approach. So, this commit explicitly adds code for the test coverage of the concerned code block. --- zerver/tests/test_user_topics.py | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/zerver/tests/test_user_topics.py b/zerver/tests/test_user_topics.py index 9d316d6a87..e5c4a6c035 100644 --- a/zerver/tests/test_user_topics.py +++ b/zerver/tests/test_user_topics.py @@ -5,14 +5,14 @@ import orjson import time_machine from django.utils.timezone import now as timezone_now -from zerver.actions.reactions import check_add_reaction +from zerver.actions.reactions import check_add_reaction, do_add_reaction from zerver.actions.user_settings import do_change_user_setting from zerver.actions.user_topics import do_set_user_topic_visibility_policy from zerver.lib.stream_topic import StreamTopicTarget from zerver.lib.test_classes import ZulipTestCase from zerver.lib.test_helpers import get_subscription from zerver.lib.user_topics import get_topic_mutes, topic_has_visibility_policy -from zerver.models import UserProfile, UserTopic +from zerver.models import Message, Reaction, UserProfile, UserTopic from zerver.models.constants import MAX_TOPIC_NAME_LENGTH from zerver.models.streams import get_stream @@ -1025,6 +1025,28 @@ class AutomaticallyFollowTopicsTests(ZulipTestCase): ) self.assertEqual(user_ids, {cordelia.id}) + # Add test coverage for 'should_change_visibility_policy' when + # user from a different realm reacted to the message. + starnine_mit = self.mit_user("starnine") + do_change_user_setting( + starnine_mit, + "automatically_follow_topics_policy", + UserProfile.AUTOMATICALLY_CHANGE_VISIBILITY_POLICY_ON_PARTICIPATION, + acting_user=None, + ) + do_add_reaction( + user_profile=starnine_mit, + message=Message.objects.get(id=message_id), + emoji_name="outbox", + emoji_code="1f4e4", + reaction_type=Reaction.UNICODE_EMOJI, + ) + + user_ids = stream_topic_target.user_ids_with_visibility_policy( + UserTopic.VisibilityPolicy.FOLLOWED + ) + self.assertNotIn(starnine_mit.id, user_ids) + def test_automatically_follow_topic_on_participation_participate_in_poll(self) -> None: iago = self.example_user("iago") hamlet = self.example_user("hamlet")