2016-07-23 08:13:33 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from __future__ import absolute_import
|
|
|
|
from __future__ import print_function
|
|
|
|
|
|
|
|
import mock
|
2017-05-25 19:48:36 +02:00
|
|
|
import requests
|
2017-07-21 06:49:00 +02:00
|
|
|
from typing import Any, Dict, Tuple, Text, Optional
|
2017-05-26 16:37:45 +02:00
|
|
|
from requests import Response
|
2016-07-23 07:51:30 +02:00
|
|
|
|
2017-05-26 16:37:45 +02:00
|
|
|
from zerver.lib.outgoing_webhook import do_rest_call, OutgoingWebhookServiceInterface
|
2017-05-25 19:48:36 +02:00
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
2017-05-26 16:37:45 +02:00
|
|
|
from builtins import object
|
2016-07-23 08:13:33 +02:00
|
|
|
|
2017-08-28 18:51:13 +02:00
|
|
|
mock_event = {'message': {'display_recipient': '',
|
|
|
|
'subject': '',
|
|
|
|
'id': ''},
|
|
|
|
'command': '',
|
|
|
|
'service_name': ''}
|
|
|
|
|
2016-07-23 08:13:33 +02:00
|
|
|
rest_operation = {'method': "POST",
|
|
|
|
'relative_url_path': "",
|
|
|
|
'request_kwargs': {},
|
|
|
|
'base_url': ""}
|
|
|
|
|
|
|
|
class ResponseMock(object):
|
|
|
|
def __init__(self, status_code, data, content):
|
|
|
|
# type: (int, Any, str) -> None
|
|
|
|
self.status_code = status_code
|
|
|
|
self.data = data
|
|
|
|
self.content = content
|
|
|
|
|
|
|
|
def request_exception_error(http_method, final_url, data, **request_kwargs):
|
2017-07-06 08:21:11 +02:00
|
|
|
# type: (Any, Any, Any, **Any) -> Any
|
2016-07-23 08:13:33 +02:00
|
|
|
raise requests.exceptions.RequestException
|
|
|
|
|
|
|
|
def timeout_error(http_method, final_url, data, **request_kwargs):
|
2017-07-06 08:21:11 +02:00
|
|
|
# type: (Any, Any, Any, **Any) -> Any
|
2016-07-23 08:13:33 +02:00
|
|
|
raise requests.exceptions.Timeout
|
|
|
|
|
2017-05-26 16:37:45 +02:00
|
|
|
class MockServiceHandler(OutgoingWebhookServiceInterface):
|
|
|
|
def process_success(self, response, event):
|
2017-07-21 06:49:00 +02:00
|
|
|
# type: (Response, Dict[Text, Any]) -> Optional[str]
|
2017-07-24 09:08:20 +02:00
|
|
|
return "Success!"
|
2017-05-26 16:37:45 +02:00
|
|
|
|
|
|
|
service_handler = MockServiceHandler(None, None, None, None)
|
|
|
|
|
2016-07-23 08:13:33 +02:00
|
|
|
class DoRestCallTests(ZulipTestCase):
|
|
|
|
@mock.patch('zerver.lib.outgoing_webhook.succeed_with_message')
|
|
|
|
def test_successful_request(self, mock_succeed_with_message):
|
|
|
|
# type: (mock.Mock) -> None
|
|
|
|
response = ResponseMock(200, {"message": "testing"}, '')
|
|
|
|
with mock.patch('requests.request', return_value=response):
|
2017-05-26 16:37:45 +02:00
|
|
|
do_rest_call(rest_operation, None, None, service_handler, None)
|
2016-07-23 08:13:33 +02:00
|
|
|
self.assertTrue(mock_succeed_with_message.called)
|
|
|
|
|
|
|
|
@mock.patch('zerver.lib.outgoing_webhook.request_retry')
|
|
|
|
def test_retry_request(self, mock_request_retry):
|
|
|
|
# type: (mock.Mock) -> None
|
|
|
|
response = ResponseMock(500, {"message": "testing"}, '')
|
|
|
|
with mock.patch('requests.request', return_value=response):
|
2017-08-28 18:51:13 +02:00
|
|
|
do_rest_call(rest_operation, None, mock_event, service_handler, None)
|
2016-07-23 08:13:33 +02:00
|
|
|
self.assertTrue(mock_request_retry.called)
|
|
|
|
|
|
|
|
@mock.patch('zerver.lib.outgoing_webhook.fail_with_message')
|
|
|
|
def test_fail_request(self, mock_fail_with_message):
|
|
|
|
# type: (mock.Mock) -> None
|
|
|
|
response = ResponseMock(400, {"message": "testing"}, '')
|
|
|
|
with mock.patch('requests.request', return_value=response):
|
2017-08-28 18:51:13 +02:00
|
|
|
do_rest_call(rest_operation, None, mock_event, service_handler, None)
|
2016-07-23 08:13:33 +02:00
|
|
|
self.assertTrue(mock_fail_with_message.called)
|
|
|
|
|
|
|
|
@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):
|
|
|
|
# type: (mock.Mock, mock.Mock, mock.Mock) -> None
|
2017-08-28 18:51:13 +02:00
|
|
|
do_rest_call(rest_operation, None, mock_event, service_handler, None)
|
2016-07-23 08:13:33 +02:00
|
|
|
self.assertTrue(mock_request_retry.called)
|
|
|
|
|
|
|
|
@mock.patch('logging.exception')
|
|
|
|
@mock.patch('requests.request', side_effect=request_exception_error)
|
|
|
|
@mock.patch('zerver.lib.outgoing_webhook.fail_with_message')
|
|
|
|
def test_request_exception(self, mock_fail_with_message, mock_requests_request, mock_logger):
|
|
|
|
# type: (mock.Mock, mock.Mock, mock.Mock) -> None
|
2017-08-28 18:51:13 +02:00
|
|
|
do_rest_call(rest_operation, None, mock_event, service_handler, None)
|
2016-07-23 08:13:33 +02:00
|
|
|
self.assertTrue(mock_fail_with_message.called)
|