-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
zulip-bot-server: Add test suite for zulip bot server.
Fixes: #5358.
- Loading branch information
Showing
4 changed files
with
102 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |