From 8e978a0845ae2905e66b2eaf9ce85a96b4b30c63 Mon Sep 17 00:00:00 2001 From: Dhruv Thakker Date: Thu, 8 Mar 2018 18:41:17 +0530 Subject: [PATCH] link_shortner bot : Add test for bad requests. (This module now has 100% coverage.) --- .../fixtures/test_invalid_key.json | 27 +++++++++++++++++++ .../link_shortener/test_link_shortener.py | 10 +++++++ 2 files changed, 37 insertions(+) create mode 100644 zulip_bots/zulip_bots/bots/link_shortener/fixtures/test_invalid_key.json diff --git a/zulip_bots/zulip_bots/bots/link_shortener/fixtures/test_invalid_key.json b/zulip_bots/zulip_bots/bots/link_shortener/fixtures/test_invalid_key.json new file mode 100644 index 0000000..6ce938f --- /dev/null +++ b/zulip_bots/zulip_bots/bots/link_shortener/fixtures/test_invalid_key.json @@ -0,0 +1,27 @@ +{ + "request": { + "api_url": "https://www.googleapis.com/urlshortener/v1/url", + "method": "POST", + "params": { + "key": "qwertyuiopx" + }, + "json": { + "longUrl": "www.youtube.com/watch" + } + }, + "response": { + "error":{ + "errors":[ + { + "reason":"keyInvalid" + } + ] + }, + "code": 400, + "message": "Bad Request" + }, + "response-headers": { + "status": 400, + "content-type": "application/json; charset=utf-8" + } +} diff --git a/zulip_bots/zulip_bots/bots/link_shortener/test_link_shortener.py b/zulip_bots/zulip_bots/bots/link_shortener/test_link_shortener.py index 5259422..d1a141a 100644 --- a/zulip_bots/zulip_bots/bots/link_shortener/test_link_shortener.py +++ b/zulip_bots/zulip_bots/bots/link_shortener/test_link_shortener.py @@ -1,5 +1,8 @@ from unittest.mock import patch from zulip_bots.test_lib import BotTestCase +from zulip_bots.test_lib import StubBotHandler +from zulip_bots.bots.link_shortener.link_shortener import LinkShortenerHandler + class TestLinkShortenerBot(BotTestCase): bot_name = "link_shortener" @@ -33,3 +36,10 @@ class TestLinkShortenerBot(BotTestCase): 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.')) + + def test_exception_when_api_key_is_invalid(self)-> None: + bot_test_instance = LinkShortenerHandler() + with self.mock_config_info({'key': 'qwertyuiopx'}): + with self.mock_http_conversation('test_invalid_key'): + with self.assertRaises(StubBotHandler.BotQuitException): + bot_test_instance.initialize(StubBotHandler())