From c86e62a06aa2ba9bae1e17e3cafad569810f18a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20H=C3=B6nig?= Date: Thu, 4 Jan 2018 15:58:13 +0100 Subject: [PATCH] link_shortener bot: Quit on invalid API key. --- .../bots/link_shortener/link_shortener.py | 6 +++--- .../bots/link_shortener/test_link_shortener.py | 16 ++++++++++------ 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/zulip_bots/zulip_bots/bots/link_shortener/link_shortener.py b/zulip_bots/zulip_bots/bots/link_shortener/link_shortener.py index 62e03e5..a0e13dd 100644 --- a/zulip_bots/zulip_bots/bots/link_shortener/link_shortener.py +++ b/zulip_bots/zulip_bots/bots/link_shortener/link_shortener.py @@ -17,9 +17,9 @@ class LinkShortenerHandler(object): def initialize(self, bot_handler: Any) -> None: self.config_info = bot_handler.get_config_info('link_shortener') - self.check_api_key() + self.check_api_key(bot_handler) - def check_api_key(self) -> None: + def check_api_key(self, bot_handler) -> None: test_request = requests.post( 'https://www.googleapis.com/urlshortener/v1/url', json={'longUrl': 'www.youtube.com/watch'}, @@ -28,7 +28,7 @@ class LinkShortenerHandler(object): test_request_data = test_request.json() try: if test_request_data['error']['errors'][0]['reason'] == 'keyInvalid': - logging.error('Invalid key. Follow the instructions in doc.md for setting API key.') + bot_handler.quit('Invalid key. Follow the instructions in doc.md for setting API key.') except KeyError: pass diff --git a/zulip_bots/zulip_bots/bots/link_shortener/test_link_shortener.py b/zulip_bots/zulip_bots/bots/link_shortener/test_link_shortener.py index 893024b..5259422 100644 --- a/zulip_bots/zulip_bots/bots/link_shortener/test_link_shortener.py +++ b/zulip_bots/zulip_bots/bots/link_shortener/test_link_shortener.py @@ -1,3 +1,4 @@ +from unittest.mock import patch from zulip_bots.test_lib import BotTestCase class TestLinkShortenerBot(BotTestCase): @@ -8,7 +9,8 @@ class TestLinkShortenerBot(BotTestCase): self.verify_reply(message, response) def test_bot_responds_to_empty_message(self) -> None: - self._test('', 'No links found. Send "help" to see usage instructions.') + with patch('requests.post'): + self._test('', 'No links found. Send "help" to see usage instructions.') def test_normal(self) -> None: with self.mock_http_conversation('test_normal'): @@ -19,13 +21,15 @@ class TestLinkShortenerBot(BotTestCase): # No `mock_http_conversation` is necessary because the bot will # recognize that no links are in the message and won't make any HTTP # requests. - self._test('Shorten nothing please.', - 'No links found. Send "help" to see usage instructions.') + with patch('requests.post'): + self._test('Shorten nothing please.', + 'No links found. Send "help" to see usage instructions.') def test_help(self) -> None: # No `mock_http_conversation` is necessary because the bot will # recognize that the message is 'help' and won't make any HTTP # requests. - self._test('help', - ('Mention the link shortener bot in a conversation and then ' - 'enter any URLs you want to shorten in the body of the message.')) + with patch('requests.post'): + self._test('help', + ('Mention the link shortener bot in a conversation and then ' + 'enter any URLs you want to shorten in the body of the message.'))