Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
outgoing_webhook: Serialize widget_content to JSON string.
Tim Abbottf70684aa462a
diff --git a/zerver/lib/outgoing_webhook.py b/zerver/lib/outgoing_webhook.pyindex fe9e288c001..4d480d10a8b 100644--- a/zerver/lib/outgoing_webhook.py+++ b/zerver/lib/outgoing_webhook.py@@ -101,7 +101,10 @@ class GenericOutgoingWebhookService(OutgoingWebhookServiceInterface):if "content" in response_json:content = str(response_json["content"])- widget_content = response_json.get("widget_content")+ widget_content_raw = response_json.get("widget_content")+ widget_content = (+ json.dumps(widget_content_raw) if widget_content_raw is not None else None+ )return OutgoingWebhookResult(content=content, widget_content=widget_content)return Nonediff --git a/zerver/tests/test_outgoing_webhook_interfaces.py b/zerver/tests/test_outgoing_webhook_interfaces.pyindex aa03ad49060..ac129cb0083 100644--- a/zerver/tests/test_outgoing_webhook_interfaces.py+++ b/zerver/tests/test_outgoing_webhook_interfaces.py@@ -139,13 +139,15 @@ class TestGenericOutgoingWebhookService(ZulipTestCase):response = dict(content="test_content",- widget_content="test_widget_content",+ widget_content={"widget_type": "zform", "extra_data": {}},red_herring="whatever",)success_response = self.handler.process_success(response)self.assertEqual(success_response,- OutgoingWebhookResult(content="test_content", widget_content="test_widget_content"),+ OutgoingWebhookResult(+ content="test_content", widget_content='{"widget_type": "zform", "extra_data": {}}'+ ),)response = {}diff --git a/zerver/tests/test_outgoing_webhook_system.py b/zerver/tests/test_outgoing_webhook_system.pyindex f1425b19362..ae14cbac413 100644--- a/zerver/tests/test_outgoing_webhook_system.py+++ b/zerver/tests/test_outgoing_webhook_system.py@@ -19,7 +19,7 @@ from zerver.lib.test_classes import ZulipTestCasefrom zerver.lib.topic import TOPIC_NAMEfrom zerver.lib.url_encoding import message_link_urlfrom zerver.lib.users import add_service-from zerver.models import Recipient, Service, UserProfile+from zerver.models import Recipient, Service, SubMessage, UserProfilefrom zerver.models.realms import get_realmfrom zerver.models.recipients import get_or_create_direct_message_groupfrom zerver.models.streams import get_stream@@ -319,9 +319,8 @@ I'm a generic exception :(mock_event = self.mock_event(bot_user)service_handler = GenericOutgoingWebhookService("token", bot_user, "service")- # The "widget_content" key is required to be a string which is- # itself JSON-encoded; passing arbitrary text data in it will- # cause the hook to fail.+ # The "widget_content" value must be a valid widget_content+ # dict; passing a plain string will cause validation to fail.response = {"content": "whatever", "widget_content": "test"}expect_logging_info = self.assertLogs(level="INFO")expect_fail = mock.patch("zerver.lib.outgoing_webhook.fail_with_message")@@ -338,7 +337,7 @@ I'm a generic exception :(bot_owner_notification.content,"""[A message](http://zulip.testserver/#narrow/channel/999-Verona/topic/Foo/near/) to your bot @_**Outgoing Webhook** triggered an outgoing webhook.The outgoing webhook server attempted to send a message in Zulip, but that request resulted in the following error:-> Widgets: API programmer sent invalid JSON content\nThe response contains the following payload:\n```\n'{"content": "whatever", "widget_content": "test"}'\n```""",+> Widgets: widget_content is not a dict\nThe response contains the following payload:\n```\n'{"content": "whatever", "widget_content": "test"}'\n```""",)assert bot_user.bot_owner is not Noneself.assertEqual(@@ -585,6 +584,44 @@ class TestOutgoingWebhookMessaging(ZulipTestCase):self.assertEqual(last_message.topic_name(), "bar")self.assert_message_stream_name(last_message, "Denmark")+ @responses.activate+ def test_stream_message_to_outgoing_webhook_bot_with_widget(self) -> None:+ bot_owner = self.example_user("othello")+ bot = self.create_outgoing_bot(bot_owner)++ widget_content = {+ "widget_type": "zform",+ "extra_data": {+ "type": "choices",+ "heading": "Pick a color",+ "choices": [+ {"type": "choice", "short_name": "r", "long_name": "Red", "reply": "red"},+ {"type": "choice", "short_name": "b", "long_name": "Blue", "reply": "blue"},+ ],+ },+ }+ responses.add(+ responses.POST,+ "https://bot.example.com/",+ json={"content": "Choose a color", "widget_content": widget_content},+ )++ with self.assertLogs(level="INFO") as logs:+ self.send_stream_message(+ bot_owner, "Denmark", content=f"@**{bot.full_name}** foo", topic_name="bar"+ )++ self.assert_length(responses.calls, 1)+ self.assert_length(logs.output, 1)++ last_message = self.get_last_message()+ self.assertEqual(last_message.content, "Choose a color")+ self.assertEqual(last_message.sender_id, bot.id)++ submessage = SubMessage.objects.get(message_id=last_message.id)+ self.assertEqual(submessage.msg_type, "widget")+ self.assertEqual(orjson.loads(submessage.content), widget_content)+@responses.activatedef test_stream_message_failure_to_outgoing_webhook_bot(self) -> None:realm = get_realm("zulip")