link_shortener bot: Quit on invalid API key.
MODIFY
zulip_bots/zulip_bots/bots/link_shortener/link_shortener.py
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

MODIFY
zulip_bots/zulip_bots/bots/link_shortener/test_link_shortener.py
zulip_bots/zulip_bots/bots/link_shortener/test_link_shortener.py
+from unittest.mock import patch
from zulip_bots.test_lib import BotTestCase
class TestLinkShortenerBot(BotTestCase):
@@ -8,6 +9,7 @@ class TestLinkShortenerBot(BotTestCase):
self.verify_reply(message, response)
def test_bot_responds_to_empty_message(self) -> None:
+ with patch('requests.post'):
self._test('', 'No links found. Send "help" to see usage instructions.')
def test_normal(self) -> None:
@@ -19,6 +21,7 @@ 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.
+ with patch('requests.post'):
self._test('Shorten nothing please.',
'No links found. Send "help" to see usage instructions.')
@@ -26,6 +29,7 @@ class TestLinkShortenerBot(BotTestCase):
# No `mock_http_conversation` is necessary because the bot will
# recognize that the message is 'help' and won't make any HTTP
# requests.
+ 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.'))

link_shortener bot: Quit on invalid API key.