2016-07-23 08:13:33 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2017-11-03 13:38:49 +01:00
|
|
|
import ujson
|
2017-08-29 15:21:25 +02:00
|
|
|
import logging
|
2016-07-23 08:13:33 +02:00
|
|
|
import mock
|
2017-05-25 19:48:36 +02:00
|
|
|
import requests
|
2017-10-02 07:46:47 +02:00
|
|
|
|
2019-02-02 23:53:44 +01:00
|
|
|
from typing import Any, Optional
|
2016-07-23 07:51:30 +02:00
|
|
|
|
2018-10-11 14:11:52 +02:00
|
|
|
from zerver.lib.outgoing_webhook import (
|
|
|
|
do_rest_call,
|
|
|
|
GenericOutgoingWebhookService,
|
|
|
|
SlackOutgoingWebhookService,
|
|
|
|
)
|
|
|
|
|
2017-05-25 19:48:36 +02:00
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
2018-11-10 16:43:59 +01:00
|
|
|
from zerver.lib.topic import TOPIC_NAME
|
2017-11-03 13:38:49 +01:00
|
|
|
from zerver.models import get_realm, get_user, UserProfile, get_display_recipient
|
2016-07-23 08:13:33 +02:00
|
|
|
|
2019-01-09 15:29:17 +01:00
|
|
|
from version import ZULIP_VERSION
|
|
|
|
|
2017-11-05 11:49:43 +01:00
|
|
|
class ResponseMock:
|
2017-11-05 10:51:25 +01:00
|
|
|
def __init__(self, status_code: int, content: Optional[Any]=None) -> None:
|
2016-07-23 08:13:33 +02:00
|
|
|
self.status_code = status_code
|
|
|
|
self.content = content
|
2017-11-03 13:38:49 +01:00
|
|
|
self.text = ujson.dumps(content)
|
2016-07-23 08:13:33 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def request_exception_error(http_method: Any, final_url: Any, data: Any, **request_kwargs: Any) -> Any:
|
2017-08-29 15:21:25 +02:00
|
|
|
raise requests.exceptions.RequestException("I'm a generic exception :(")
|
2016-07-23 08:13:33 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def timeout_error(http_method: Any, final_url: Any, data: Any, **request_kwargs: Any) -> Any:
|
2017-08-16 13:30:47 +02:00
|
|
|
raise requests.exceptions.Timeout("Time is up!")
|
2016-07-23 08:13:33 +02:00
|
|
|
|
2018-10-10 19:52:32 +02:00
|
|
|
def connection_error(http_method: Any, final_url: Any, data: Any, **request_kwargs: Any) -> Any:
|
|
|
|
raise requests.exceptions.ConnectionError()
|
|
|
|
|
2018-10-11 15:44:50 +02:00
|
|
|
service_handler = GenericOutgoingWebhookService(None, None, None)
|
2017-05-26 16:37:45 +02:00
|
|
|
|
2016-07-23 08:13:33 +02:00
|
|
|
class DoRestCallTests(ZulipTestCase):
|
2017-11-05 10:51:25 +01:00
|
|
|
def setUp(self) -> None:
|
2019-10-19 20:47:00 +02:00
|
|
|
super().setUp()
|
2017-09-25 16:03:35 +02:00
|
|
|
realm = get_realm("zulip")
|
|
|
|
user_profile = get_user("outgoing-webhook@zulip.com", realm)
|
2017-08-29 15:21:25 +02:00
|
|
|
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',
|
2018-02-15 21:02:47 +01:00
|
|
|
'stream_id': 999,
|
2018-11-10 16:43:59 +01:00
|
|
|
TOPIC_NAME: 'Foo',
|
2017-08-29 15:21:25 +02:00
|
|
|
'id': '',
|
|
|
|
'type': 'stream'},
|
|
|
|
'user_profile_id': user_profile.id,
|
|
|
|
'command': '',
|
|
|
|
'service_name': ''}
|
2017-09-25 16:03:35 +02:00
|
|
|
|
2017-08-29 15:21:25 +02:00
|
|
|
self.bot_user = self.example_user('outgoing_webhook_bot')
|
|
|
|
logging.disable(logging.WARNING)
|
2017-09-25 16:03:35 +02:00
|
|
|
|
2018-10-09 16:25:18 +02:00
|
|
|
@mock.patch('zerver.lib.outgoing_webhook.send_response_message')
|
|
|
|
def test_successful_request(self, mock_send: mock.Mock) -> None:
|
2018-10-11 15:44:50 +02:00
|
|
|
response = ResponseMock(200, dict(content='whatever'))
|
2016-07-23 08:13:33 +02:00
|
|
|
with mock.patch('requests.request', return_value=response):
|
2018-10-11 14:38:08 +02:00
|
|
|
do_rest_call('', None, self.mock_event, service_handler)
|
2018-10-09 16:25:18 +02:00
|
|
|
self.assertTrue(mock_send.called)
|
2016-07-23 08:13:33 +02:00
|
|
|
|
2018-10-11 14:11:52 +02:00
|
|
|
for service_class in [GenericOutgoingWebhookService, SlackOutgoingWebhookService]:
|
|
|
|
handler = service_class(None, None, None)
|
|
|
|
with mock.patch('requests.request', return_value=response):
|
|
|
|
do_rest_call('', None, self.mock_event, handler)
|
|
|
|
self.assertTrue(mock_send.called)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_retry_request(self: mock.Mock) -> None:
|
2017-11-03 13:12:59 +01:00
|
|
|
response = ResponseMock(500)
|
2017-08-29 15:21:25 +02:00
|
|
|
|
|
|
|
self.mock_event['failed_tries'] = 3
|
2016-07-23 08:13:33 +02:00
|
|
|
with mock.patch('requests.request', return_value=response):
|
2018-10-11 14:38:08 +02:00
|
|
|
do_rest_call('', None, self.mock_event, service_handler)
|
2017-08-29 15:21:25 +02:00
|
|
|
bot_owner_notification = self.get_last_message()
|
|
|
|
self.assertEqual(bot_owner_notification.content,
|
2018-10-27 16:37:29 +02:00
|
|
|
'''[A message](http://zulip.testserver/#narrow/stream/999-Verona/topic/Foo/near/) triggered an outgoing webhook.
|
2017-08-16 13:30:47 +02:00
|
|
|
The webhook got a response with status code *500*.''')
|
2017-08-29 15:21:25 +02:00
|
|
|
self.assertEqual(bot_owner_notification.recipient_id, self.bot_user.bot_owner.id)
|
|
|
|
self.mock_event['failed_tries'] = 0
|
2016-07-23 08:13:33 +02:00
|
|
|
|
|
|
|
@mock.patch('zerver.lib.outgoing_webhook.fail_with_message')
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_fail_request(self, mock_fail_with_message: mock.Mock) -> None:
|
2017-11-03 13:12:59 +01:00
|
|
|
response = ResponseMock(400)
|
2016-07-23 08:13:33 +02:00
|
|
|
with mock.patch('requests.request', return_value=response):
|
2018-10-11 14:38:08 +02:00
|
|
|
do_rest_call('', None, self.mock_event, service_handler)
|
2017-08-29 15:21:25 +02:00
|
|
|
bot_owner_notification = self.get_last_message()
|
2016-07-23 08:13:33 +02:00
|
|
|
self.assertTrue(mock_fail_with_message.called)
|
2017-08-29 15:21:25 +02:00
|
|
|
self.assertEqual(bot_owner_notification.content,
|
2018-10-27 16:37:29 +02:00
|
|
|
'''[A message](http://zulip.testserver/#narrow/stream/999-Verona/topic/Foo/near/) triggered an outgoing webhook.
|
2017-08-29 15:21:25 +02:00
|
|
|
The webhook got a response with status code *400*.''')
|
|
|
|
self.assertEqual(bot_owner_notification.recipient_id, self.bot_user.bot_owner.id)
|
2016-07-23 08:13:33 +02:00
|
|
|
|
2019-01-09 15:29:17 +01:00
|
|
|
def test_headers(self) -> None:
|
|
|
|
with mock.patch('requests.request') as mock_request:
|
|
|
|
do_rest_call('', 'payload-stub', self.mock_event, service_handler)
|
|
|
|
kwargs = mock_request.call_args[1]
|
|
|
|
self.assertEqual(kwargs['data'], 'payload-stub')
|
|
|
|
|
|
|
|
user_agent = 'ZulipOutgoingWebhook/' + ZULIP_VERSION
|
|
|
|
headers = {
|
|
|
|
'content-type': 'application/json',
|
|
|
|
'User-Agent': user_agent,
|
|
|
|
}
|
|
|
|
self.assertEqual(kwargs['headers'], headers)
|
|
|
|
|
2018-10-10 19:52:32 +02:00
|
|
|
def test_error_handling(self) -> None:
|
|
|
|
def helper(side_effect: Any, error_text: str) -> None:
|
|
|
|
with mock.patch('logging.info'):
|
|
|
|
with mock.patch('requests.request', side_effect=side_effect):
|
2018-10-11 14:38:08 +02:00
|
|
|
do_rest_call('', None, self.mock_event, service_handler)
|
2018-10-10 19:52:32 +02:00
|
|
|
bot_owner_notification = self.get_last_message()
|
|
|
|
self.assertIn(error_text, bot_owner_notification.content)
|
|
|
|
self.assertIn('triggered', bot_owner_notification.content)
|
|
|
|
self.assertEqual(bot_owner_notification.recipient_id, self.bot_user.bot_owner.id)
|
|
|
|
|
|
|
|
helper(side_effect=timeout_error, error_text='A timeout occurred.')
|
|
|
|
helper(side_effect=connection_error, error_text='A connection error occurred.')
|
2016-07-23 08:13:33 +02:00
|
|
|
|
|
|
|
@mock.patch('logging.exception')
|
|
|
|
@mock.patch('requests.request', side_effect=request_exception_error)
|
|
|
|
@mock.patch('zerver.lib.outgoing_webhook.fail_with_message')
|
2017-11-17 07:00:53 +01:00
|
|
|
def test_request_exception(self, mock_fail_with_message: mock.Mock,
|
|
|
|
mock_requests_request: mock.Mock, mock_logger: mock.Mock) -> None:
|
2018-10-11 14:38:08 +02:00
|
|
|
do_rest_call('', None, self.mock_event, service_handler)
|
2017-08-29 15:21:25 +02:00
|
|
|
bot_owner_notification = self.get_last_message()
|
2016-07-23 08:13:33 +02:00
|
|
|
self.assertTrue(mock_fail_with_message.called)
|
2017-08-29 15:21:25 +02:00
|
|
|
self.assertEqual(bot_owner_notification.content,
|
2018-10-27 16:37:29 +02:00
|
|
|
'''[A message](http://zulip.testserver/#narrow/stream/999-Verona/topic/Foo/near/) triggered an outgoing webhook.
|
2017-11-09 16:26:38 +01:00
|
|
|
When trying to send a request to the webhook service, an exception of type RequestException occurred:
|
2017-08-29 15:21:25 +02:00
|
|
|
```
|
|
|
|
I'm a generic exception :(
|
|
|
|
```''')
|
|
|
|
self.assertEqual(bot_owner_notification.recipient_id, self.bot_user.bot_owner.id)
|
2017-11-03 13:38:49 +01:00
|
|
|
|
|
|
|
class TestOutgoingWebhookMessaging(ZulipTestCase):
|
2017-11-05 10:51:25 +01:00
|
|
|
def setUp(self) -> None:
|
2019-10-19 20:47:00 +02:00
|
|
|
super().setUp()
|
2017-11-03 13:38:49 +01:00
|
|
|
self.user_profile = self.example_user("othello")
|
2018-01-30 17:05:14 +01:00
|
|
|
self.bot_profile = self.create_test_bot('outgoing-webhook', self.user_profile,
|
|
|
|
full_name='Outgoing Webhook bot',
|
|
|
|
bot_type=UserProfile.OUTGOING_WEBHOOK_BOT,
|
|
|
|
service_name='foo-service')
|
2017-11-03 13:38:49 +01:00
|
|
|
|
|
|
|
@mock.patch('requests.request', return_value=ResponseMock(200, {"response_string": "Hidley ho, I'm a webhook responding!"}))
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_pm_to_outgoing_webhook_bot(self, mock_requests_request: mock.Mock) -> None:
|
2020-03-07 11:43:05 +01:00
|
|
|
self.send_personal_message(self.user_profile, self.bot_profile,
|
2017-11-03 13:38:49 +01:00
|
|
|
content="foo")
|
|
|
|
last_message = self.get_last_message()
|
2018-10-10 01:36:31 +02:00
|
|
|
self.assertEqual(last_message.content, "Hidley ho, I'm a webhook responding!")
|
2017-11-03 13:38:49 +01:00
|
|
|
self.assertEqual(last_message.sender_id, self.bot_profile.id)
|
|
|
|
display_recipient = get_display_recipient(last_message.recipient)
|
2018-05-11 01:39:38 +02:00
|
|
|
# The next two lines error on mypy because the display_recipient is of type Union[str, List[Dict[str, Any]]].
|
2017-11-03 13:38:49 +01:00
|
|
|
# In this case, we know that display_recipient will be of type List[Dict[str, Any]].
|
|
|
|
# Otherwise this test will error, which is wanted behavior anyway.
|
|
|
|
self.assert_length(display_recipient, 1) # type: ignore
|
|
|
|
self.assertEqual(display_recipient[0]['email'], self.user_profile.email) # type: ignore
|
|
|
|
|
|
|
|
@mock.patch('requests.request', return_value=ResponseMock(200, {"response_string": "Hidley ho, I'm a webhook responding!"}))
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_stream_message_to_outgoing_webhook_bot(self, mock_requests_request: mock.Mock) -> None:
|
2020-03-07 11:43:05 +01:00
|
|
|
self.send_stream_message(self.user_profile, "Denmark",
|
2017-11-03 13:38:49 +01:00
|
|
|
content="@**{}** foo".format(self.bot_profile.full_name),
|
|
|
|
topic_name="bar")
|
|
|
|
last_message = self.get_last_message()
|
2018-10-10 01:36:31 +02:00
|
|
|
self.assertEqual(last_message.content, "Hidley ho, I'm a webhook responding!")
|
2017-11-03 13:38:49 +01:00
|
|
|
self.assertEqual(last_message.sender_id, self.bot_profile.id)
|
2018-11-10 16:11:12 +01:00
|
|
|
self.assertEqual(last_message.topic_name(), "bar")
|
2017-11-03 13:38:49 +01:00
|
|
|
display_recipient = get_display_recipient(last_message.recipient)
|
|
|
|
self.assertEqual(display_recipient, "Denmark")
|