diff --git a/setup.py b/setup.py index 0014f1b..9b5b984 100755 --- a/setup.py +++ b/setup.py @@ -62,6 +62,7 @@ package_info = dict( 'zulip-bot-server=zulip.bot_server:main', ], }, + test_suite='tests', ) # type: Dict[str, Any] setuptools_info = dict( @@ -69,14 +70,15 @@ setuptools_info = dict( 'simplejson', 'six', 'typing>=3.5.2.2', - 'flask>=0.12.2' + 'flask>=0.12.2', + 'mock>=2.0.0', ], ) try: from setuptools import setup, find_packages package_info.update(setuptools_info) - package_info['packages'] = find_packages() + package_info['packages'] = find_packages(exclude=["tests"]) except ImportError: from distutils.core import setup diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/bot_server_test_lib.py b/tests/bot_server_test_lib.py new file mode 100644 index 0000000..4e8ce90 --- /dev/null +++ b/tests/bot_server_test_lib.py @@ -0,0 +1,37 @@ +from unittest import TestCase +import zulip.bot_server +import json +from typing import Any, List, Dict, Mapping + +class BotServerTestCase(TestCase): + + def setUp(self): + # type: () -> None + zulip.bot_server.app.testing = True + self.app = zulip.bot_server.app.test_client() + + def assert_bot_server_response(self, + available_bots=None, + bots_config=None, + bots_lib_module=None, + payload_url="/bots/testbot", + message=dict(message={'key': "test message"}), + check_success=False, + ): + # type: (List[str], Dict[str, Any], Dict[str, Any], str, Dict[str, Dict[str, Any]], bool) -> None + + if available_bots is not None: + zulip.bot_server.available_bots = available_bots + + if bots_config is not None: + zulip.bot_server.bots_config = bots_config + + if bots_lib_module is not None: + zulip.bot_server.bots_lib_module = bots_lib_module + + response = self.app.post(payload_url, data=json.dumps(message)) + + if check_success: + assert response.status_code >= 200 and response.status_code < 300 + else: + assert response.status_code >= 400 and response.status_code < 500 diff --git a/tests/test_bot_server.py b/tests/test_bot_server.py new file mode 100644 index 0000000..d0011d5 --- /dev/null +++ b/tests/test_bot_server.py @@ -0,0 +1,60 @@ +from __future__ import absolute_import +import mock +import unittest +from typing import Any +from .bot_server_test_lib import BotServerTestCase + +class BotServerTests(BotServerTestCase): + class MockMessageHandler(object): + def handle_message(self, message, bot_handler, state_handler): + # type: (Any, Any, Any) -> None + assert message == {'key': "test message"} + + class MockLibModule(object): + def handler_class(self): + # type: () -> Any + return BotServerTests.MockMessageHandler() + + @mock.patch('zulip.bot_server.ExternalBotHandler') + def test_successful_request(self, mock_ExternalBotHandler): + # type: (mock.Mock) -> None + available_bots = ['testbot'] + bots_config = { + 'testbot': { + 'email': 'testbot-bot@zulip.com', + 'key': '123456789qwertyuiop', + 'site': 'http://localhost', + } + } + bots_lib_module = { + 'testbot': BotServerTests.MockLibModule() + } + self.assert_bot_server_response(available_bots=available_bots, + bots_config=bots_config, + bots_lib_module=bots_lib_module, + check_success=True) + assert mock_ExternalBotHandler.called + + def test_bot_not_supported(self): + # type: () -> None + available_bots = ['testbot'] + self.assert_bot_server_response(available_bots=available_bots, + payload_url="/bots/not_supported_bot", + check_success=False) + + def test_wrong_bot_credentials(self): + # type: () -> None + available_bots = ['testbot'] + bots_config = { + 'testbot': { + 'email': 'testbot-bot@zulip.com', + 'key': '123456789qwertyuiop', + 'site': 'http://localhost', + } + } + self.assert_bot_server_response(available_bots=available_bots, + bots_config=bots_config, + check_success=False) + +if __name__ == '__main__': + unittest.main()