Esempio n. 1
0
    def test_get_deactivated_muted_topic(self) -> None:
        user = self.example_user("hamlet")
        self.login_user(user)

        stream = get_stream("Verona", user.realm)
        recipient = stream.recipient

        mock_date_muted = datetime(2020, 1, 1, tzinfo=timezone.utc).timestamp()

        assert recipient is not None
        add_topic_mute(
            user_profile=user,
            stream_id=stream.id,
            recipient_id=recipient.id,
            topic_name="Verona3",
            date_muted=datetime(2020, 1, 1, tzinfo=timezone.utc),
        )

        stream.deactivated = True
        stream.save()

        self.assertNotIn((stream.name, "Verona3", mock_date_muted),
                         get_topic_mutes(user))
        self.assertIn((stream.name, "Verona3", mock_date_muted),
                      get_topic_mutes(user, True))
Esempio n. 2
0
    def test_add_muted_topic(self) -> None:
        user = self.example_user("hamlet")
        self.login_user(user)

        stream = get_stream("Verona", user.realm)

        url = "/api/v1/users/me/subscriptions/muted_topics"

        payloads: List[Dict[str, object]] = [
            {
                "stream": stream.name,
                "topic": "Verona3",
                "op": "add"
            },
            {
                "stream_id": stream.id,
                "topic": "Verona3",
                "op": "add"
            },
        ]

        mock_date_muted = datetime(2020, 1, 1, tzinfo=timezone.utc).timestamp()
        for data in payloads:
            with mock.patch(
                    "zerver.views.muting.timezone_now",
                    return_value=datetime(2020, 1, 1, tzinfo=timezone.utc),
            ):
                result = self.api_patch(user, url, data)
                self.assert_json_success(result)

            self.assertIn((stream.name, "Verona3", mock_date_muted),
                          get_topic_mutes(user))
            self.assertTrue(topic_is_muted(user, stream.id, "verona3"))

            remove_topic_mute(
                user_profile=user,
                stream_id=stream.id,
                topic_name="Verona3",
            )

        # Verify the error handling for the database level
        # IntegrityError we'll get with a race between two processes
        # trying to mute the topic.  To do this, we patch the
        # topic_is_muted function to always return False when trying
        # to mute a topic that is already muted.
        assert stream.recipient is not None
        add_topic_mute(
            user_profile=user,
            stream_id=stream.id,
            recipient_id=stream.recipient.id,
            topic_name="Verona3",
            date_muted=datetime(2020, 1, 1, tzinfo=timezone.utc),
        )

        with mock.patch("zerver.views.muting.topic_is_muted",
                        return_value=False):
            result = self.api_patch(user, url, data)
            self.assert_json_error(result, "Topic already muted")
Esempio n. 3
0
 def mute_topic_for_user(user: UserProfile) -> None:
     assert recipient is not None
     add_topic_mute(
         user_profile=user,
         stream_id=stream.id,
         recipient_id=recipient.id,
         topic_name="test TOPIC",
         date_muted=timezone_now(),
     )
Esempio n. 4
0
    def test_muted_topic_add_invalid(self) -> None:
        user = self.example_user("hamlet")
        realm = user.realm
        self.login_user(user)

        stream = get_stream("Verona", realm)
        recipient = stream.recipient
        assert recipient is not None
        add_topic_mute(
            user_profile=user,
            stream_id=stream.id,
            recipient_id=recipient.id,
            topic_name="Verona3",
            date_muted=timezone_now(),
        )

        url = "/api/v1/users/me/subscriptions/muted_topics"

        data: Dict[str, Any] = {
            "stream": stream.name,
            "topic": "Verona3",
            "op": "add"
        }
        result = self.api_patch(user, url, data)
        self.assert_json_error(result, "Topic already muted")

        data = {"stream_id": 999999999, "topic": "Verona3", "op": "add"}
        result = self.api_patch(user, url, data)
        self.assert_json_error(result, "Invalid stream ID")

        data = {"topic": "Verona3", "op": "add"}
        result = self.api_patch(user, url, data)
        self.assert_json_error(result, "Please supply 'stream'.")

        data = {
            "stream": stream.name,
            "stream_id": stream.id,
            "topic": "Verona3",
            "op": "add"
        }
        result = self.api_patch(user, url, data)
        self.assert_json_error(result,
                               "Please choose one: 'stream' or 'stream_id'.")
Esempio n. 5
0
def do_mute_topic(
    user_profile: UserProfile,
    stream: Stream,
    topic: str,
    date_muted: Optional[datetime.datetime] = None,
    ignore_duplicate: bool = False,
) -> None:
    if date_muted is None:
        date_muted = timezone_now()
    add_topic_mute(
        user_profile,
        stream.id,
        stream.recipient_id,
        topic,
        date_muted,
        ignore_duplicate=ignore_duplicate,
    )
    event = dict(type="muted_topics",
                 muted_topics=get_topic_mutes(user_profile))
    send_event(user_profile.realm, event, [user_profile.id])
Esempio n. 6
0
    def test_remove_muted_topic(self) -> None:
        user = self.example_user("hamlet")
        realm = user.realm
        self.login_user(user)

        stream = get_stream("Verona", realm)
        recipient = stream.recipient

        url = "/api/v1/users/me/subscriptions/muted_topics"
        payloads: List[Dict[str, object]] = [
            {
                "stream": stream.name,
                "topic": "vERONA3",
                "op": "remove"
            },
            {
                "stream_id": stream.id,
                "topic": "vEroNA3",
                "op": "remove"
            },
        ]
        mock_date_muted = datetime(2020, 1, 1, tzinfo=timezone.utc).timestamp()

        assert recipient is not None
        for data in payloads:
            add_topic_mute(
                user_profile=user,
                stream_id=stream.id,
                recipient_id=recipient.id,
                topic_name="Verona3",
                date_muted=datetime(2020, 1, 1, tzinfo=timezone.utc),
            )
            self.assertIn((stream.name, "Verona3", mock_date_muted),
                          get_topic_mutes(user))

            result = self.api_patch(user, url, data)

            self.assert_json_success(result)
            self.assertNotIn((stream.name, "Verona3", mock_date_muted),
                             get_topic_mutes(user))
            self.assertFalse(topic_is_muted(user, stream.id, "verona3"))