zulip-bot-server: Add test suite for zulip bot server.
Fixes:
MODIFY
setup.py
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

ADD
tests/__init__.py
tests/__init__.py
ADD
tests/bot_server_test_lib.py
+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

ADD
tests/test_bot_server.py
+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()

zulip-bot-server: Add test suite for zulip bot server.

Fixes: #5358.