zulip_bots: Get 100% test coverage for giphy bot.

This commit is contained in:
Robert Hönig 2017-11-27 16:04:30 +01:00 committed by showell
parent 93edededdd
commit 2cb43256e0
6 changed files with 85 additions and 9 deletions

View file

@ -0,0 +1,25 @@
{
"request": {
"api_url": "http://api.giphy.com/v1/gifs/translate",
"params": {
"s": "Hello",
"api_key": "12345678"
}
},
"response": {
"meta": {
"status": 200
},
"data": {
"images": {
"original": {
"url": "https://media4.giphy.com/media/3o6ZtpxSZbQRRnwCKQ/giphy.gif"
}
}
}
},
"response-headers": {
"status": 403,
"content-type": "application/json; charset=utf-8"
}
}

View file

@ -0,0 +1,19 @@
{
"request": {
"api_url": "http://api.giphy.com/v1/gifs/translate",
"params": {
"s": "world without zulip",
"api_key": "12345678"
}
},
"response": {
"meta": {
"status": 200
},
"data": []
},
"response-headers": {
"status": 200,
"content-type": "application/json; charset=utf-8"
}
}

View file

@ -20,7 +20,6 @@
},
"response-headers": {
"status": 200,
"ok": true,
"content-type": "application/json; charset=utf-8"
}
}

View file

@ -54,11 +54,7 @@ def get_url_gif_giphy(keyword, api_key):
except requests.exceptions.ConnectionError as e: # Usually triggered by bad connection.
logging.warning(e)
raise
search_status = data.json()['meta']['status']
if search_status != 200 or not data.ok:
raise requests.exceptions.ConnectionError
data.raise_for_status()
try:
gif_url = data.json()['data']['images']['original']['url']
except (TypeError, KeyError): # Usually triggered by no result in Giphy.

View file

@ -5,21 +5,58 @@ from __future__ import print_function
import json
from unittest.mock import patch
from requests.exceptions import HTTPError, ConnectionError
from zulip_bots.test_lib import BotTestCase
class TestGiphyBot(BotTestCase):
bot_name = "giphy"
def test_bot(self):
def test_normal(self):
bot_response = '[Click to enlarge]' \
'(https://media4.giphy.com/media/3o6ZtpxSZbQRRnwCKQ/giphy.gif)' \
'[](/static/images/interactive-bot/giphy/powered-by-giphy.png)'
with self.mock_config_info({'key': '12345678'}), \
self.mock_http_conversation('test_1'):
self.mock_http_conversation('test_normal'):
self.initialize_bot()
self.assert_bot_response(
message = {'content': 'Hello'},
response = {'content': bot_response},
expected_method='send_reply'
)
def test_no_result(self):
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'
)
def test_403(self):
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)
def test_connection_error(self):
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'
)

View file

@ -137,7 +137,7 @@ class BotTestCaseBase(TestCase):
http_headers = http_data.get('response-headers')
with patch('requests.get') as mock_get:
mock_result = requests.Response()
mock_result._content = json.dumps(http_response).encode()
mock_result._content = json.dumps(http_response).encode() # type: ignore # We are modifying a "hidden" attribute.
mock_result.status_code = http_headers.get('status', 200)
mock_get.return_value = mock_result
yield