link_shortener bot: Quit on invalid API key.

This commit is contained in:
Robert Hönig 2018-01-04 15:58:13 +01:00 committed by showell
parent bed2c6c9ea
commit c86e62a06a
2 changed files with 13 additions and 9 deletions

View file

@ -17,9 +17,9 @@ class LinkShortenerHandler(object):
def initialize(self, bot_handler: Any) -> None: def initialize(self, bot_handler: Any) -> None:
self.config_info = bot_handler.get_config_info('link_shortener') 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( test_request = requests.post(
'https://www.googleapis.com/urlshortener/v1/url', 'https://www.googleapis.com/urlshortener/v1/url',
json={'longUrl': 'www.youtube.com/watch'}, json={'longUrl': 'www.youtube.com/watch'},
@ -28,7 +28,7 @@ class LinkShortenerHandler(object):
test_request_data = test_request.json() test_request_data = test_request.json()
try: try:
if test_request_data['error']['errors'][0]['reason'] == 'keyInvalid': 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: except KeyError:
pass pass

View file

@ -1,3 +1,4 @@
from unittest.mock import patch
from zulip_bots.test_lib import BotTestCase from zulip_bots.test_lib import BotTestCase
class TestLinkShortenerBot(BotTestCase): class TestLinkShortenerBot(BotTestCase):
@ -8,6 +9,7 @@ class TestLinkShortenerBot(BotTestCase):
self.verify_reply(message, response) self.verify_reply(message, response)
def test_bot_responds_to_empty_message(self) -> None: def test_bot_responds_to_empty_message(self) -> None:
with patch('requests.post'):
self._test('', 'No links found. Send "help" to see usage instructions.') self._test('', 'No links found. Send "help" to see usage instructions.')
def test_normal(self) -> None: def test_normal(self) -> None:
@ -19,6 +21,7 @@ class TestLinkShortenerBot(BotTestCase):
# No `mock_http_conversation` is necessary because the bot will # No `mock_http_conversation` is necessary because the bot will
# recognize that no links are in the message and won't make any HTTP # recognize that no links are in the message and won't make any HTTP
# requests. # requests.
with patch('requests.post'):
self._test('Shorten nothing please.', self._test('Shorten nothing please.',
'No links found. Send "help" to see usage instructions.') '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 # No `mock_http_conversation` is necessary because the bot will
# recognize that the message is 'help' and won't make any HTTP # recognize that the message is 'help' and won't make any HTTP
# requests. # requests.
with patch('requests.post'):
self._test('help', self._test('help',
('Mention the link shortener bot in a conversation and then ' ('Mention the link shortener bot in a conversation and then '
'enter any URLs you want to shorten in the body of the message.')) 'enter any URLs you want to shorten in the body of the message.'))