giphy bot: Fix check for invalid API key.
The previous check didn't notice invalid API keys. This commit also makes giphy quit on any connectivity issues during initialization.
This commit is contained in:
parent
ba530e2341
commit
5d4a352e2c
|
@ -26,18 +26,19 @@ class GiphyHandler(object):
|
||||||
|
|
||||||
def initialize(self, bot_handler: Any) -> None:
|
def initialize(self, bot_handler: Any) -> None:
|
||||||
self.config_info = bot_handler.get_config_info('giphy')
|
self.config_info = bot_handler.get_config_info('giphy')
|
||||||
query = {'test': 'keyword',
|
query = {'s': 'Hello',
|
||||||
'api_key': self.config_info['key']}
|
'api_key': self.config_info['key']}
|
||||||
try:
|
try:
|
||||||
data = requests.get(GIPHY_TRANSLATE_API, params=query)
|
data = requests.get(GIPHY_TRANSLATE_API, params=query)
|
||||||
|
data.raise_for_status()
|
||||||
|
except ConnectionError as e:
|
||||||
|
bot_handler.quit(str(e))
|
||||||
except HTTPError as e:
|
except HTTPError as e:
|
||||||
if (e.response.json()['error']['errors'][0]['reason'] == 'keyInvalid'):
|
error_message = str(e)
|
||||||
bot_handler.quit('Invalid key.'
|
if (data.status_code == 403):
|
||||||
'Follow the instructions in doc.md for setting API key.')
|
error_message += ('This is likely due to an invalid key.\n'
|
||||||
else:
|
'Follow the instructions in doc.md for setting an API key.')
|
||||||
raise
|
bot_handler.quit(error_message)
|
||||||
except ConnectionError:
|
|
||||||
logging.warning('Bad connection')
|
|
||||||
|
|
||||||
def handle_message(self, message: Dict[str, str], bot_handler: Any) -> None:
|
def handle_message(self, message: Dict[str, str], bot_handler: Any) -> None:
|
||||||
bot_response = get_bot_giphy_response(
|
bot_response = get_bot_giphy_response(
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch, MagicMock
|
||||||
from requests.exceptions import HTTPError, ConnectionError
|
from requests.exceptions import HTTPError, ConnectionError
|
||||||
|
|
||||||
from typing import Any, Union
|
from typing import Any, Union
|
||||||
|
@ -33,21 +33,14 @@ class TestGiphyBot(BotTestCase):
|
||||||
bot = get_bot_message_handler(self.bot_name)
|
bot = get_bot_message_handler(self.bot_name)
|
||||||
bot_handler = StubBotHandler()
|
bot_handler = StubBotHandler()
|
||||||
|
|
||||||
with self.mock_config_info({'key': '12345678'}):
|
with self.mock_config_info({'key': '12345678'}), \
|
||||||
|
self.mock_http_conversation('test_403'), \
|
||||||
|
self.assertRaises(bot_handler.BotQuitException):
|
||||||
bot.initialize(bot_handler)
|
bot.initialize(bot_handler)
|
||||||
|
|
||||||
mock_message = {'content': 'Hello'}
|
def test_connection_error_while_running(self) -> None:
|
||||||
|
|
||||||
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) -> 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=[MagicMock(), ConnectionError()]), \
|
||||||
patch('logging.exception'):
|
patch('logging.exception'):
|
||||||
self.verify_reply(
|
self.verify_reply(
|
||||||
'world without chocolate',
|
'world without chocolate',
|
||||||
|
|
Loading…
Reference in a new issue