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.

outgoing_webhook: modify outgoing_webhook's 407 error message.

Siddharth Asthanaec28a7555c27
diff --git a/zerver/lib/outgoing_webhook.py b/zerver/lib/outgoing_webhook.py
index b4a6be1fcd4..dbcd1cf33c8 100644
--- a/zerver/lib/outgoing_webhook.py
+++ b/zerver/lib/outgoing_webhook.py
@@ -243,9 +243,13 @@ def notify_bot_owner(
notification_message = f"[A message]({message_url}) to your bot @_**{bot.full_name}** triggered an outgoing webhook."
if failure_message:
notification_message += "\n" + failure_message
- if status_code:
+ if status_code == 407:
+ notification_message += (
+ "\nThe URL configured for the webhook is for a private or disallowed network."
+ )
+ elif status_code:
notification_message += f"\nThe webhook got a response with status code *{status_code}*."
- if response_content:
+ if response_content and status_code != 407:
notification_message += (
f"\nThe response contains the following payload:\n```\n{response_content!r}\n```"
)
diff --git a/zerver/tests/test_outgoing_webhook_system.py b/zerver/tests/test_outgoing_webhook_system.py
index 4705b79fb82..02c3bd91ad6 100644
--- a/zerver/tests/test_outgoing_webhook_system.py
+++ b/zerver/tests/test_outgoing_webhook_system.py
@@ -13,8 +13,9 @@ from zerver.lib.outgoing_webhook import (
)
from zerver.lib.test_classes import ZulipTestCase
from zerver.lib.topic import TOPIC_NAME
+from zerver.lib.url_encoding import near_message_url
from zerver.lib.users import add_service
-from zerver.models import Recipient, Service, UserProfile, get_display_recipient
+from zerver.models import Recipient, Service, UserProfile, get_display_recipient, get_realm
class ResponseMock:
@@ -316,6 +317,46 @@ class TestOutgoingWebhookMessaging(ZulipTestCase):
Recipient.PERSONAL,
)
+ def test_pm_to_outgoing_webhook_bot_for_407_error_code(self) -> None:
+ bot_owner = self.example_user("othello")
+ bot = self.create_outgoing_bot(bot_owner)
+ sender = self.example_user("hamlet")
+ realm = get_realm("zulip")
+
+ response = ResponseMock(407)
+ expect_407 = mock.patch("requests.request", return_value=response)
+ expect_fail = mock.patch("zerver.lib.outgoing_webhook.fail_with_message")
+
+ with expect_407, expect_fail as mock_fail, self.assertLogs(level="WARNING"):
+ message_id = self.send_personal_message(sender, bot, content="foo")
+
+ # create message dict to get the message url
+ message = {
+ "display_recipient": [{"id": bot.id}, {"id": sender.id}],
+ "stream_id": 999,
+ TOPIC_NAME: "Foo",
+ "id": message_id,
+ "type": "",
+ }
+ message_url = near_message_url(realm, message)
+
+ last_message = self.get_last_message()
+ self.assertEqual(
+ last_message.content,
+ f"[A message]({message_url}) to your bot @_**{bot.full_name}** triggered an outgoing webhook.\n"
+ "The URL configured for the webhook is for a private or disallowed network.",
+ )
+ self.assertEqual(last_message.sender_id, bot.id)
+ self.assertEqual(
+ last_message.recipient.type_id,
+ bot_owner.id,
+ )
+ self.assertEqual(
+ last_message.recipient.type,
+ Recipient.PERSONAL,
+ )
+ self.assertTrue(mock_fail.called)
+
@mock.patch(
"requests.request",
return_value=ResponseMock(