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

Fixes: #5358.
This commit is contained in:
vaibhav 2017-06-18 14:45:40 +05:30 committed by showell
parent 303daf074a
commit b5982794e7
4 changed files with 101 additions and 2 deletions

View file

@ -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

0
tests/__init__.py Normal file
View file

View file

@ -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

60
tests/test_bot_server.py Normal file
View file

@ -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()