Skip to content

Commit

Permalink
zulip-bot-server: Add test suite for zulip bot server.
Browse files Browse the repository at this point in the history
Fixes: #5358.
  • Loading branch information
vabs22 committed Jun 20, 2017
1 parent 26bce90 commit de06721
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 2 deletions.
6 changes: 4 additions & 2 deletions api/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,23 @@ def recur_expand(target_root, dir):
'zulip-bot-server=zulip.bot_server:main',
],
},
test_suite='tests',
) # type: Dict[str, Any]

setuptools_info = dict(
install_requires=['requests>=0.12.1',
'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
Expand Down
Empty file added api/tests/__init__.py
Empty file.
39 changes: 39 additions & 0 deletions api/tests/bot_server_test_lib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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,
check_failure=False,
):
# type: (List[str], Dict[str, Mapping[str, str]], Dict[str, Any], str, Dict[str, Dict[str, Any]], bool, 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_failure:
assert response.status_code >= 400 and response.status_code < 500

if check_success:
assert response.status_code >= 200 and response.status_code < 300
59 changes: 59 additions & 0 deletions api/tests/test_bot_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
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.BotHandlerApi')
def test_successful_request(self, mock_BotHandlerApi):
# 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_BotHandlerApi.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_failure=True)

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_failure=True)

if __name__ == '__main__':
unittest.main()

0 comments on commit de06721

Please sign in to comment.