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