link_shortener bot: Quit on invalid API key.
This commit is contained in:
parent
bed2c6c9ea
commit
c86e62a06a
|
@ -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
|
||||
|
||||
|
|
|
@ -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.'))
|
||||
|
|
Loading…
Reference in a new issue