diff --git a/plugins/answer.py b/plugins/answer.py index 1cd66d9e..34c107de 100644 --- a/plugins/answer.py +++ b/plugins/answer.py @@ -21,15 +21,16 @@ def construct_link(text): text = text.split('documentation/')[-1] return 'https://docs.coala.io/en/latest/' + text + raise ValueError('Unrecognised answer: {}'.format(text)) + @botcmd def answer(self, msg, arg): try: answers = requests.get(urljoin(os.environ['ANSWER_END'], 'answer'), params={'question': arg}).json() - except json.JSONDecodeError: # pragma: no cover # for logging + except json.JSONDecodeError: self.log.exception('something went wrong while fetching answer for' 'question: {}'.format(arg)) - yield 'Something went wrong, please check logs'.format() if answers: reply = 'Please checkout the following links: \n- ' reply += '- '.join(map(lambda x: diff --git a/tests/answer_test.py b/tests/answer_test.py index 8ad64043..d6b67290 100644 --- a/tests/answer_test.py +++ b/tests/answer_test.py @@ -3,6 +3,7 @@ from errbot.backends.test import FullStackTest import vcr +import requests_mock import plugins.answer @@ -26,3 +27,23 @@ def test_answer(self): self.assertIn('Please checkout the following links', self.pop_message()) self.push_message('!answer shell autocompletion') self.assertIn('Please checkout the following links', self.pop_message()) + + def test_invalid_json(self): + os.environ['ANSWER_END'] = 'http://0.0.0.0:8000' + with requests_mock.Mocker() as m: + m.get('http://0.0.0.0:8000/answer?question=foo', text='invalid') + self.assertCommand( + '!answer foo', + 'Computer says nooo. See logs for details:\n' + 'Expecting value: line 1 column 1 (char 0)') + + def test_invalid_repo(self): + os.environ['ANSWER_END'] = 'http://0.0.0.0:8000' + with requests_mock.Mocker() as m: + m.get('http://0.0.0.0:8000/answer?question=foo', + text='[["Wrong answer\\n/wrong/link\\n"]]') + self.assertCommand( + '!answer foo', + 'Computer says nooo. See logs for details:\n' + 'Unrecognised answer: /wrong/link') +