diff --git a/zulip_bots/zulip_bots/bots/giphy/fixtures/test_403.json b/zulip_bots/zulip_bots/bots/giphy/fixtures/test_403.json new file mode 100644 index 0000000..cccc893 --- /dev/null +++ b/zulip_bots/zulip_bots/bots/giphy/fixtures/test_403.json @@ -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" + } +} diff --git a/zulip_bots/zulip_bots/bots/giphy/fixtures/test_no_result.json b/zulip_bots/zulip_bots/bots/giphy/fixtures/test_no_result.json new file mode 100644 index 0000000..14d72cc --- /dev/null +++ b/zulip_bots/zulip_bots/bots/giphy/fixtures/test_no_result.json @@ -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" + } +} diff --git a/zulip_bots/zulip_bots/bots/giphy/fixtures/test_1.json b/zulip_bots/zulip_bots/bots/giphy/fixtures/test_normal.json similarity index 96% rename from zulip_bots/zulip_bots/bots/giphy/fixtures/test_1.json rename to zulip_bots/zulip_bots/bots/giphy/fixtures/test_normal.json index 5a6eae8..d410824 100644 --- a/zulip_bots/zulip_bots/bots/giphy/fixtures/test_1.json +++ b/zulip_bots/zulip_bots/bots/giphy/fixtures/test_normal.json @@ -20,7 +20,6 @@ }, "response-headers": { "status": 200, - "ok": true, "content-type": "application/json; charset=utf-8" } } diff --git a/zulip_bots/zulip_bots/bots/giphy/giphy.py b/zulip_bots/zulip_bots/bots/giphy/giphy.py index cba1563..c9f932e 100644 --- a/zulip_bots/zulip_bots/bots/giphy/giphy.py +++ b/zulip_bots/zulip_bots/bots/giphy/giphy.py @@ -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. diff --git a/zulip_bots/zulip_bots/bots/giphy/test_giphy.py b/zulip_bots/zulip_bots/bots/giphy/test_giphy.py index 1f6d545..fb6aefd 100755 --- a/zulip_bots/zulip_bots/bots/giphy/test_giphy.py +++ b/zulip_bots/zulip_bots/bots/giphy/test_giphy.py @@ -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' + ) diff --git a/zulip_bots/zulip_bots/test_lib.py b/zulip_bots/zulip_bots/test_lib.py index 8daa8de..e844a30 100755 --- a/zulip_bots/zulip_bots/test_lib.py +++ b/zulip_bots/zulip_bots/test_lib.py @@ -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