From bed2c6c9ea26026ca2bce8f0b81164d6afa6df50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20H=C3=B6nig?= Date: Thu, 4 Jan 2018 15:39:55 +0100 Subject: [PATCH] mention bot: Quit on invalid API key. --- zulip_bots/zulip_bots/bots/mention/mention.py | 6 +++--- zulip_bots/zulip_bots/bots/mention/test_mention.py | 7 +++++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/zulip_bots/zulip_bots/bots/mention/mention.py b/zulip_bots/zulip_bots/bots/mention/mention.py index 5121ad8..3039d82 100644 --- a/zulip_bots/zulip_bots/bots/mention/mention.py +++ b/zulip_bots/zulip_bots/bots/mention/mention.py @@ -10,9 +10,9 @@ class MentionHandler(object): self.access_token = self.config_info['access_token'] self.account_id = '' - self.check_access_token() + self.check_access_token(bot_handler) - def check_access_token(self) -> None: + def check_access_token(self, bot_handler) -> None: test_query_header = { 'Authorization': 'Bearer ' + self.access_token, 'Accept-Version': '1.15', @@ -23,7 +23,7 @@ class MentionHandler(object): test_query_data = test_query_response.json() if test_query_data['error'] == 'invalid_grant' and \ test_query_data['error_description'] == 'The access token provided is invalid.': - logging.error('Access Token Invalid. Please see doc.md to find out how to get it.') + bot_handler.quit('Access Token Invalid. Please see doc.md to find out how to get it.') except KeyError: pass diff --git a/zulip_bots/zulip_bots/bots/mention/test_mention.py b/zulip_bots/zulip_bots/bots/mention/test_mention.py index 65ad5da..ac5a4bf 100644 --- a/zulip_bots/zulip_bots/bots/mention/test_mention.py +++ b/zulip_bots/zulip_bots/bots/mention/test_mention.py @@ -1,15 +1,18 @@ from zulip_bots.bots.mention.mention import MentionHandler +from unittest.mock import patch from zulip_bots.test_lib import BotTestCase class TestMentionBot(BotTestCase): bot_name = "mention" def test_bot_responds_to_empty_message(self) -> None: - with self.mock_config_info({'access_token': '12345'}): + with self.mock_config_info({'access_token': '12345'}), \ + patch('requests.get'): self.verify_reply('', 'Empty Mention Query') def test_help_query(self) -> None: - with self.mock_config_info({'access_token': '12345'}): + with self.mock_config_info({'access_token': '12345'}), \ + patch('requests.get'): self.verify_reply('help', ''' This is a Mention API Bot which will find mentions of the given keyword throughout the web.