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:
|
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
|
||||||
|
|
||||||
|
|
|
@ -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,7 +9,8 @@ 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:
|
||||||
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:
|
def test_normal(self) -> None:
|
||||||
with self.mock_http_conversation('test_normal'):
|
with self.mock_http_conversation('test_normal'):
|
||||||
|
@ -19,13 +21,15 @@ 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.
|
||||||
self._test('Shorten nothing please.',
|
with patch('requests.post'):
|
||||||
'No links found. Send "help" to see usage instructions.')
|
self._test('Shorten nothing please.',
|
||||||
|
'No links found. Send "help" to see usage instructions.')
|
||||||
|
|
||||||
def test_help(self) -> None:
|
def test_help(self) -> None:
|
||||||
# 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.
|
||||||
self._test('help',
|
with patch('requests.post'):
|
||||||
('Mention the link shortener bot in a conversation and then '
|
self._test('help',
|
||||||
'enter any URLs you want to shorten in the body of the message.'))
|
('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