outgoing webhook: Notify bot owner on failure response.

This commit is contained in:
Robert Hönig
2017-08-29 15:21:25 +02:00
committed by Tim Abbott
parent c77b245944
commit 43422fa6f2
2 changed files with 89 additions and 27 deletions

View File

@@ -2,6 +2,7 @@
from __future__ import absolute_import
from __future__ import print_function
import logging
import mock
import requests
from typing import Any, Dict, Tuple, Text, Optional
@@ -21,11 +22,11 @@ class ResponseMock(object):
def request_exception_error(http_method, final_url, data, **request_kwargs):
# type: (Any, Any, Any, **Any) -> Any
raise requests.exceptions.RequestException
raise requests.exceptions.RequestException("I'm a generic exception :(")
def timeout_error(http_method, final_url, data, **request_kwargs):
# type: (Any, Any, Any, **Any) -> Any
raise requests.exceptions.Timeout
raise requests.exceptions.Timeout()
class MockServiceHandler(OutgoingWebhookServiceInterface):
def process_success(self, response, event):
@@ -39,17 +40,25 @@ class DoRestCallTests(ZulipTestCase):
# type: () -> None
realm = get_realm("zulip")
user_profile = get_user("outgoing-webhook@zulip.com", realm)
self.mock_event = {'message': {'display_recipient': '',
'subject': '',
'id': ''},
'user_profile_id': user_profile.id,
'command': '',
'service_name': ''}
self.mock_event = {
# In the tests there is no active queue processor, so retries don't get processed.
# Therefore, we need to emulate `retry_event` in the last stage when the maximum
# retries have been exceeded.
'failed_tries': 3,
'message': {'display_recipient': 'Verona',
'subject': 'Foo',
'id': '',
'type': 'stream'},
'user_profile_id': user_profile.id,
'command': '',
'service_name': ''}
self.rest_operation = {'method': "POST",
'relative_url_path': "",
'request_kwargs': {},
'base_url': ""}
self.bot_user = self.example_user('outgoing_webhook_bot')
logging.disable(logging.WARNING)
@mock.patch('zerver.lib.outgoing_webhook.succeed_with_message')
def test_successful_request(self, mock_succeed_with_message):
@@ -59,13 +68,18 @@ class DoRestCallTests(ZulipTestCase):
do_rest_call(self.rest_operation, None, self.mock_event, service_handler, None)
self.assertTrue(mock_succeed_with_message.called)
@mock.patch('zerver.lib.outgoing_webhook.request_retry')
def test_retry_request(self, mock_request_retry):
def test_retry_request(self):
# type: (mock.Mock) -> None
response = ResponseMock(500, {"message": "testing"}, '')
self.mock_event['failed_tries'] = 3
with mock.patch('requests.request', return_value=response):
do_rest_call(self.rest_operation, None, self.mock_event, service_handler, None)
self.assertTrue(mock_request_retry.called)
bot_owner_notification = self.get_last_message()
self.assertEqual(bot_owner_notification.content,
"[A message](http://testserver/#narrow/stream/Verona/subject/Foo/near/) triggered an outgoing webhook.")
self.assertEqual(bot_owner_notification.recipient_id, self.bot_user.bot_owner.id)
self.mock_event['failed_tries'] = 0
@mock.patch('zerver.lib.outgoing_webhook.fail_with_message')
def test_fail_request(self, mock_fail_with_message):
@@ -73,15 +87,22 @@ class DoRestCallTests(ZulipTestCase):
response = ResponseMock(400, {"message": "testing"}, '')
with mock.patch('requests.request', return_value=response):
do_rest_call(self.rest_operation, None, self.mock_event, service_handler, None)
bot_owner_notification = self.get_last_message()
self.assertTrue(mock_fail_with_message.called)
self.assertEqual(bot_owner_notification.content,
'''[A message](http://testserver/#narrow/stream/Verona/subject/Foo/near/) triggered an outgoing webhook.
The webhook got a response with status code *400*.''')
self.assertEqual(bot_owner_notification.recipient_id, self.bot_user.bot_owner.id)
@mock.patch('logging.info')
@mock.patch('requests.request', side_effect=timeout_error)
@mock.patch('zerver.lib.outgoing_webhook.request_retry')
def test_timeout_request(self, mock_request_retry, mock_requests_request, mock_logger):
def test_timeout_request(self, mock_requests_request, mock_logger):
# type: (mock.Mock, mock.Mock, mock.Mock) -> None
do_rest_call(self.rest_operation, None, self.mock_event, service_handler, None)
self.assertTrue(mock_request_retry.called)
bot_owner_notification = self.get_last_message()
self.assertEqual(bot_owner_notification.content,
'''[A message](http://testserver/#narrow/stream/Verona/subject/Foo/near/) triggered an outgoing webhook.''')
self.assertEqual(bot_owner_notification.recipient_id, self.bot_user.bot_owner.id)
@mock.patch('logging.exception')
@mock.patch('requests.request', side_effect=request_exception_error)
@@ -89,4 +110,12 @@ class DoRestCallTests(ZulipTestCase):
def test_request_exception(self, mock_fail_with_message, mock_requests_request, mock_logger):
# type: (mock.Mock, mock.Mock, mock.Mock) -> None
do_rest_call(self.rest_operation, None, self.mock_event, service_handler, None)
bot_owner_notification = self.get_last_message()
self.assertTrue(mock_fail_with_message.called)
self.assertEqual(bot_owner_notification.content,
'''[A message](http://testserver/#narrow/stream/Verona/subject/Foo/near/) triggered an outgoing webhook.
When trying to send a request to the webhook service, an exception of type RequestException occured:
```
I'm a generic exception :(
```''')
self.assertEqual(bot_owner_notification.recipient_id, self.bot_user.bot_owner.id)