zulip_bots: Get 100% test coverage for giphy bot.
This commit is contained in:
parent
93edededdd
commit
2cb43256e0
25
zulip_bots/zulip_bots/bots/giphy/fixtures/test_403.json
Normal file
25
zulip_bots/zulip_bots/bots/giphy/fixtures/test_403.json
Normal 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"
|
||||||
|
}
|
||||||
|
}
|
|
@ -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"
|
||||||
|
}
|
||||||
|
}
|
|
@ -20,7 +20,6 @@
|
||||||
},
|
},
|
||||||
"response-headers": {
|
"response-headers": {
|
||||||
"status": 200,
|
"status": 200,
|
||||||
"ok": true,
|
|
||||||
"content-type": "application/json; charset=utf-8"
|
"content-type": "application/json; charset=utf-8"
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -54,11 +54,7 @@ def get_url_gif_giphy(keyword, api_key):
|
||||||
except requests.exceptions.ConnectionError as e: # Usually triggered by bad connection.
|
except requests.exceptions.ConnectionError as e: # Usually triggered by bad connection.
|
||||||
logging.warning(e)
|
logging.warning(e)
|
||||||
raise
|
raise
|
||||||
|
data.raise_for_status()
|
||||||
search_status = data.json()['meta']['status']
|
|
||||||
if search_status != 200 or not data.ok:
|
|
||||||
raise requests.exceptions.ConnectionError
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
gif_url = data.json()['data']['images']['original']['url']
|
gif_url = data.json()['data']['images']['original']['url']
|
||||||
except (TypeError, KeyError): # Usually triggered by no result in Giphy.
|
except (TypeError, KeyError): # Usually triggered by no result in Giphy.
|
||||||
|
|
|
@ -5,21 +5,58 @@ from __future__ import print_function
|
||||||
|
|
||||||
import json
|
import json
|
||||||
|
|
||||||
|
from unittest.mock import patch
|
||||||
|
from requests.exceptions import HTTPError, ConnectionError
|
||||||
|
|
||||||
from zulip_bots.test_lib import BotTestCase
|
from zulip_bots.test_lib import BotTestCase
|
||||||
|
|
||||||
class TestGiphyBot(BotTestCase):
|
class TestGiphyBot(BotTestCase):
|
||||||
bot_name = "giphy"
|
bot_name = "giphy"
|
||||||
|
|
||||||
def test_bot(self):
|
def test_normal(self):
|
||||||
bot_response = '[Click to enlarge]' \
|
bot_response = '[Click to enlarge]' \
|
||||||
'(https://media4.giphy.com/media/3o6ZtpxSZbQRRnwCKQ/giphy.gif)' \
|
'(https://media4.giphy.com/media/3o6ZtpxSZbQRRnwCKQ/giphy.gif)' \
|
||||||
'[](/static/images/interactive-bot/giphy/powered-by-giphy.png)'
|
'[](/static/images/interactive-bot/giphy/powered-by-giphy.png)'
|
||||||
|
|
||||||
with self.mock_config_info({'key': '12345678'}), \
|
with self.mock_config_info({'key': '12345678'}), \
|
||||||
self.mock_http_conversation('test_1'):
|
self.mock_http_conversation('test_normal'):
|
||||||
self.initialize_bot()
|
self.initialize_bot()
|
||||||
self.assert_bot_response(
|
self.assert_bot_response(
|
||||||
message = {'content': 'Hello'},
|
message = {'content': 'Hello'},
|
||||||
response = {'content': bot_response},
|
response = {'content': bot_response},
|
||||||
expected_method='send_reply'
|
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'
|
||||||
|
)
|
||||||
|
|
|
@ -137,7 +137,7 @@ class BotTestCaseBase(TestCase):
|
||||||
http_headers = http_data.get('response-headers')
|
http_headers = http_data.get('response-headers')
|
||||||
with patch('requests.get') as mock_get:
|
with patch('requests.get') as mock_get:
|
||||||
mock_result = requests.Response()
|
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_result.status_code = http_headers.get('status', 200)
|
||||||
mock_get.return_value = mock_result
|
mock_get.return_value = mock_result
|
||||||
yield
|
yield
|
||||||
|
|
Loading…
Reference in a new issue