black: Reformat skipping string normalization.

This commit is contained in:
PIG208 2021-05-28 17:03:46 +08:00 committed by Tim Abbott
parent 5580c68ae5
commit fba21bb00d
178 changed files with 6562 additions and 4469 deletions

View file

@ -9,7 +9,6 @@ from zulip_botserver import server
class BotServerTestCase(TestCase):
def setUp(self) -> None:
server.app.testing = True
self.app = server.app.test_client()
@ -31,8 +30,12 @@ class BotServerTestCase(TestCase):
bots_lib_modules = server.load_lib_modules(available_bots)
server.app.config["BOTS_LIB_MODULES"] = bots_lib_modules
if bot_handlers is None:
bot_handlers = server.load_bot_handlers(available_bots, bots_config, third_party_bot_conf)
message_handlers = server.init_message_handlers(available_bots, bots_lib_modules, bot_handlers)
bot_handlers = server.load_bot_handlers(
available_bots, bots_config, third_party_bot_conf
)
message_handlers = server.init_message_handlers(
available_bots, bots_lib_modules, bot_handlers
)
server.app.config["BOT_HANDLERS"] = bot_handlers
server.app.config["MESSAGE_HANDLERS"] = message_handlers

View file

@ -35,14 +35,18 @@ class BotServerTests(BotServerTestCase):
'token': 'abcd1234',
}
}
self.assert_bot_server_response(available_bots=available_bots,
bots_config=bots_config,
event=dict(message={'content': "@**test** test message"},
bot_email='helloworld-bot@zulip.com',
trigger='mention',
token='abcd1234'),
expected_response="beep boop",
check_success=True)
self.assert_bot_server_response(
available_bots=available_bots,
bots_config=bots_config,
event=dict(
message={'content': "@**test** test message"},
bot_email='helloworld-bot@zulip.com',
trigger='mention',
token='abcd1234',
),
expected_response="beep boop",
check_success=True,
)
def test_successful_request_from_two_bots(self) -> None:
available_bots = ['helloworld', 'help']
@ -58,16 +62,20 @@ class BotServerTests(BotServerTestCase):
'key': '123456789qwertyuiop',
'site': 'http://localhost',
'token': 'abcd1234',
}
},
}
self.assert_bot_server_response(available_bots=available_bots,
event=dict(message={'content': "@**test** test message"},
bot_email='helloworld-bot@zulip.com',
trigger='mention',
token='abcd1234'),
expected_response="beep boop",
bots_config=bots_config,
check_success=True)
self.assert_bot_server_response(
available_bots=available_bots,
event=dict(
message={'content': "@**test** test message"},
bot_email='helloworld-bot@zulip.com',
trigger='mention',
token='abcd1234',
),
expected_response="beep boop",
bots_config=bots_config,
check_success=True,
)
def test_request_for_unkown_bot(self) -> None:
bots_config = {
@ -78,11 +86,12 @@ class BotServerTests(BotServerTestCase):
'token': 'abcd1234',
},
}
self.assert_bot_server_response(available_bots=['helloworld'],
event=dict(message={'content': "test message"},
bot_email='unknown-bot@zulip.com'),
bots_config=bots_config,
check_success=False)
self.assert_bot_server_response(
available_bots=['helloworld'],
event=dict(message={'content': "test message"}, bot_email='unknown-bot@zulip.com'),
bots_config=bots_config,
check_success=False,
)
def test_wrong_bot_token(self) -> None:
available_bots = ['helloworld']
@ -94,17 +103,23 @@ class BotServerTests(BotServerTestCase):
'token': 'abcd1234',
}
}
self.assert_bot_server_response(available_bots=available_bots,
bots_config=bots_config,
event=dict(message={'content': "@**test** test message"},
bot_email='helloworld-bot@zulip.com',
trigger='mention',
token='wrongtoken'),
check_success=False)
self.assert_bot_server_response(
available_bots=available_bots,
bots_config=bots_config,
event=dict(
message={'content': "@**test** test message"},
bot_email='helloworld-bot@zulip.com',
trigger='mention',
token='wrongtoken',
),
check_success=False,
)
@mock.patch('logging.error')
@mock.patch('zulip_bots.lib.StateHandler')
def test_wrong_bot_credentials(self, mock_StateHandler: mock.Mock, mock_LoggingError: mock.Mock) -> None:
def test_wrong_bot_credentials(
self, mock_StateHandler: mock.Mock, mock_LoggingError: mock.Mock
) -> None:
available_bots = ['nonexistent-bot']
bots_config = {
'nonexistent-bot': {
@ -117,16 +132,21 @@ class BotServerTests(BotServerTestCase):
# This works, but mypy still complains:
# error: No overload variant of "assertRaisesRegexp" of "TestCase" matches argument types
# [def (*args: builtins.object, **kwargs: builtins.object) -> builtins.SystemExit, builtins.str]
with self.assertRaisesRegexp(SystemExit,
'Error: Bot "nonexistent-bot" doesn\'t exist. Please make '
'sure you have set up the botserverrc file correctly.'):
with self.assertRaisesRegexp(
SystemExit,
'Error: Bot "nonexistent-bot" doesn\'t exist. Please make '
'sure you have set up the botserverrc file correctly.',
):
self.assert_bot_server_response(
available_bots=available_bots,
event=dict(message={'content': "@**test** test message"},
bot_email='helloworld-bot@zulip.com',
trigger='mention',
token='abcd1234'),
bots_config=bots_config)
event=dict(
message={'content': "@**test** test message"},
bot_email='helloworld-bot@zulip.com',
trigger='mention',
token='abcd1234',
),
bots_config=bots_config,
)
@mock.patch('sys.argv', ['zulip-botserver', '--config-file', '/foo/bar/baz.conf'])
def test_argument_parsing_defaults(self) -> None:
@ -163,7 +183,9 @@ class BotServerTests(BotServerTestCase):
assert server.read_config_from_env_vars("giphy") == {'giphy': bots_config['giphy']}
# Specified bot doesn't exist; should read the first section of the config.
assert server.read_config_from_env_vars("redefined_bot") == {'redefined_bot': bots_config['hello_world']}
assert server.read_config_from_env_vars("redefined_bot") == {
'redefined_bot': bots_config['hello_world']
}
def test_read_config_file(self) -> None:
with self.assertRaises(IOError):
@ -184,7 +206,7 @@ class BotServerTests(BotServerTestCase):
'key': 'value2',
'site': 'http://localhost',
'token': 'abcd1234',
}
},
}
assert json.dumps(bot_conf1, sort_keys=True) == json.dumps(expected_config1, sort_keys=True)
@ -223,24 +245,35 @@ class BotServerTests(BotServerTestCase):
assert module == helloworld
# load valid file path
path = Path(root_dir, 'zulip_bots/zulip_bots/bots/{bot}/{bot}.py'.format(bot='helloworld')).as_posix()
path = Path(
root_dir, 'zulip_bots/zulip_bots/bots/{bot}/{bot}.py'.format(bot='helloworld')
).as_posix()
module = server.load_lib_modules([path])[path]
assert module.__name__ == 'custom_bot_module'
assert module.__file__ == path
assert isinstance(module, ModuleType)
# load invalid module name
with self.assertRaisesRegexp(SystemExit,
'Error: Bot "botserver-test-case-random-bot" doesn\'t exist. '
'Please make sure you have set up the botserverrc file correctly.'):
module = server.load_lib_modules(['botserver-test-case-random-bot'])['botserver-test-case-random-bot']
with self.assertRaisesRegexp(
SystemExit,
'Error: Bot "botserver-test-case-random-bot" doesn\'t exist. '
'Please make sure you have set up the botserverrc file correctly.',
):
module = server.load_lib_modules(['botserver-test-case-random-bot'])[
'botserver-test-case-random-bot'
]
# load invalid file path
with self.assertRaisesRegexp(SystemExit,
'Error: Bot "{}/zulip_bots/zulip_bots/bots/helloworld.py" doesn\'t exist. '
'Please make sure you have set up the botserverrc file correctly.'.format(root_dir)):
path = Path(root_dir, 'zulip_bots/zulip_bots/bots/{bot}.py'.format(bot='helloworld')).as_posix()
with self.assertRaisesRegexp(
SystemExit,
'Error: Bot "{}/zulip_bots/zulip_bots/bots/helloworld.py" doesn\'t exist. '
'Please make sure you have set up the botserverrc file correctly.'.format(root_dir),
):
path = Path(
root_dir, 'zulip_bots/zulip_bots/bots/{bot}.py'.format(bot='helloworld')
).as_posix()
module = server.load_lib_modules([path])[path]
if __name__ == '__main__':
unittest.main()