outgoing_webhook: modify outgoing_webhook's 407 error message.

The message from the bot which triggered the 407 error message notifies
the bot owner about the exceptions as well in the error message. This
commit handles it more gracefully and shows a generic message.
This commit is contained in:
Siddharth Asthana
2021-03-16 12:24:33 +05:30
committed by Tim Abbott
parent 5ec0860a2f
commit ec28a7555c
2 changed files with 48 additions and 3 deletions

View File

@@ -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```"
)

View File

@@ -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(