From eb872b5bcdb8ba7b7f9ea4ea5c3a30e6c1330ca2 Mon Sep 17 00:00:00 2001 From: Alex Vandiver Date: Tue, 11 Jan 2022 13:39:17 -0800 Subject: [PATCH] actions: Use check_stream_topic when editing message topics. --- zerver/lib/actions.py | 6 +++--- zerver/tests/test_message_edit.py | 16 +++++++++++++++- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py index ee4b639339..1e9b0198ec 100644 --- a/zerver/lib/actions.py +++ b/zerver/lib/actions.py @@ -155,7 +155,7 @@ from zerver.lib.streams import ( send_stream_creation_event, subscribed_to_stream, ) -from zerver.lib.string_validation import check_stream_name +from zerver.lib.string_validation import check_stream_name, check_stream_topic from zerver.lib.timestamp import datetime_to_timestamp, timestamp_to_datetime from zerver.lib.timezone import canonicalize_timezone from zerver.lib.topic import ( @@ -2975,8 +2975,8 @@ def validate_message_edit_payload( if propagate_mode != "change_one" and topic_name is None and stream_id is None: raise JsonableError(_("Invalid propagate_mode without topic edit")) - if topic_name == "": - raise JsonableError(_("Topic can't be empty")) + if topic_name is not None: + check_stream_topic(topic_name) if stream_id is not None and content is not None: raise JsonableError(_("Cannot change message content while changing stream")) diff --git a/zerver/tests/test_message_edit.py b/zerver/tests/test_message_edit.py index df15cd6b45..95a389df3c 100644 --- a/zerver/tests/test_message_edit.py +++ b/zerver/tests/test_message_edit.py @@ -192,7 +192,21 @@ class EditMessagePayloadTest(EditMessageTestCase): "topic": " ", }, ) - self.assert_json_error(result, "Topic can't be empty") + self.assert_json_error(result, "Topic can't be empty!") + + def test_edit_message_invalid_topic(self) -> None: + self.login("hamlet") + msg_id = self.send_stream_message( + self.example_user("hamlet"), "Denmark", topic_name="editing", content="before edit" + ) + result = self.client_patch( + "/json/messages/" + str(msg_id), + { + "message_id": msg_id, + "topic": "editing\nfun", + }, + ) + self.assert_json_error(result, "Invalid characters in topic!") def test_move_message_to_stream_with_content(self) -> None: (user_profile, old_stream, new_stream, msg_id, msg_id_later) = self.prepare_move_topics(