bot tests: Use StubBotTestCase in test_giphy.py.

For three of the tests we use the simpler verify_reply()
API.  For the 403 test, we don't need to rely on setUp
any more to simulate everything for us, and we do more
surgical patching.
This commit is contained in:
Steve Howell 2017-12-06 19:43:52 -08:00 committed by showell
parent 87662da139
commit 7a963916f2

View file

@ -4,9 +4,9 @@ from unittest.mock import patch
from requests.exceptions import HTTPError, ConnectionError
from typing import Any, Union
from zulip_bots.test_lib import BotTestCase
from zulip_bots.test_lib import StubBotHandler, StubBotTestCase, get_bot_message_handler
class TestGiphyBot(BotTestCase):
class TestGiphyBot(StubBotTestCase):
bot_name = "giphy"
def test_normal(self: Any) -> None:
@ -16,43 +16,38 @@ class TestGiphyBot(BotTestCase):
with self.mock_config_info({'key': '12345678'}), \
self.mock_http_conversation('test_normal'):
self.initialize_bot()
self.assert_bot_response(
message = {'content': 'Hello'},
response = {'content': bot_response},
expected_method='send_reply'
)
self.verify_reply('Hello', bot_response)
def test_no_result(self: Any) -> None:
with self.mock_config_info({'key': '12345678'}), \
self.mock_http_conversation('test_no_result'):
self.initialize_bot()
self.assert_bot_response(
message = {'content': 'world without zulip'},
response = {'content': 'Sorry, I don\'t have a GIF for "world without zulip"! :astonished:'},
expected_method='send_reply'
self.verify_reply(
'world without zulip',
'Sorry, I don\'t have a GIF for "world without zulip"! :astonished:',
)
def test_403(self: Any) -> None:
with self.mock_config_info({'key': '12345678'}), \
self.mock_http_conversation('test_403'), \
self.assertRaises(HTTPError):
self.initialize_bot()
mock_message = {'content': 'Hello'}
# Call the native handle_message here, since we don't want to assert a response,
# but an exception.
self.message_handler.handle_message(message={'content': 'Hello'},
bot_handler=self.mock_bot_handler)
bot = get_bot_message_handler(self.bot_name)
bot_handler = StubBotHandler()
with self.mock_config_info({'key': '12345678'}):
bot.initialize(bot_handler)
mock_message = {'content': 'Hello'}
with self.mock_http_conversation('test_403'):
with self.assertRaises(HTTPError):
# Call the native handle_message here,
# since we don't want to assert a response,
# but an exception.
bot.handle_message(mock_message, bot_handler)
def test_connection_error(self: Any) -> None:
with self.mock_config_info({'key': '12345678'}), \
patch('requests.get', side_effect=ConnectionError()), \
patch('logging.warning'):
self.initialize_bot()
self.assert_bot_response(
message = {'content': 'world without chocolate'},
response = {'content': ('Uh oh, sorry :slightly_frowning_face:, I '
'cannot process your request right now. But, '
'let\'s try again later! :grin:')},
expected_method='send_reply'
)
self.verify_reply(
'world without chocolate',
'Uh oh, sorry :slightly_frowning_face:, I '
'cannot process your request right now. But, '
'let\'s try again later! :grin:')