| 1 |
# coding=utf-8 |
| 2 |
import logging |
| 3 |
import os # noqa |
| 4 |
import re # noqa |
| 5 |
import sys |
| 6 |
from collections import OrderedDict |
| 7 |
from os.path import sep |
| 8 |
from pathlib import Path |
| 9 |
from queue import Empty, Queue # noqa |
| 10 |
from tempfile import mkdtemp |
| 11 |
|
| 12 |
import pytest |
| 13 |
|
| 14 |
from errbot import arg_botcmd, botcmd, re_botcmd, templating # noqa |
| 15 |
from errbot.backend_plugin_manager import BackendPluginManager |
| 16 |
from errbot.backends.base import ONLINE, Identifier, Message, Room |
| 17 |
from errbot.backends.test import ( |
| 18 |
ShallowConfig, |
| 19 |
TestOccupant, |
| 20 |
TestPerson, |
| 21 |
TestRoom, |
| 22 |
TestRoomAcl, |
| 23 |
) |
| 24 |
from errbot.bootstrap import CORE_STORAGE, bot_config_defaults |
| 25 |
from errbot.core import ErrBot |
| 26 |
from errbot.core_plugins.acls import ACLS |
| 27 |
from errbot.plugin_manager import BotPluginManager |
| 28 |
from errbot.rendering import text |
| 29 |
from errbot.repo_manager import BotRepoManager |
| 30 |
from errbot.storage.base import StoragePluginBase |
| 31 |
from errbot.utils import PLUGINS_SUBDIR |
| 32 |
|
| 33 |
LONG_TEXT_STRING = ( |
| 34 |
"This is a relatively long line of output, but I am repeated multiple times.\n" |
| 35 |
) |
| 36 |
|
| 37 |
logging.basicConfig(level=logging.DEBUG) |
| 38 |
|
| 39 |
SIMPLE_JSON_PLUGINS_INDEX = """ |
| 40 |
{"errbotio/err-helloworld": |
| 41 |
{"HelloWorld": |
| 42 |
{"path": "/helloWorld.plug", |
| 43 |
"documentation": "let's say hello!", |
| 44 |
"avatar_url": "https://avatars.githubusercontent.com/u/15802630?v=3", |
| 45 |
"name": "HelloWorld", |
| 46 |
"python": "2+", |
| 47 |
"repo": "https://github.com/errbotio/err-helloworld" |
| 48 |
} |
| 49 |
} |
| 50 |
} |
| 51 |
""" |
| 52 |
|
| 53 |
|
| 54 |
class DummyBackend(ErrBot): |
| 55 |
def change_presence(self, status: str = ONLINE, message: str = "") -> None: |
| 56 |
pass |
| 57 |
|
| 58 |
def prefix_groupchat_reply(self, message: Message, identifier: Identifier): |
| 59 |
pass |
| 60 |
|
| 61 |
def query_room(self, room: str) -> Room: |
| 62 |
pass |
| 63 |
|
| 64 |
def __init__(self, extra_config=None): |
| 65 |
self.outgoing_message_queue = Queue() |
| 66 |
if extra_config is None: |
| 67 |
extra_config = {} |
| 68 |
# make up a config. |
| 69 |
tempdir = mkdtemp() |
| 70 |
# reset the config every time |
| 71 |
sys.modules.pop("errbot.config-template", None) |
| 72 |
__import__("errbot.config-template") |
| 73 |
config = ShallowConfig() |
| 74 |
config.__dict__.update(sys.modules["errbot.config-template"].__dict__) |
| 75 |
bot_config_defaults(config) |
| 76 |
|
| 77 |
# It injects itself as a plugin. Changed the name to be sure we distinguish it. |
| 78 |
self.name = "DummyBackendRealName" |
| 79 |
|
| 80 |
config.BOT_DATA_DIR = tempdir |
| 81 |
config.BOT_LOG_FILE = tempdir + sep + "log.txt" |
| 82 |
config.BOT_PLUGIN_INDEXES = tempdir + sep + "repos.json" |
| 83 |
config.BOT_EXTRA_PLUGIN_DIR = [] |
| 84 |
config.BOT_LOG_LEVEL = logging.DEBUG |
| 85 |
config.BOT_IDENTITY = {"username": "err@localhost"} |
| 86 |
config.BOT_ASYNC = False |
| 87 |
config.BOT_PREFIX = "!" |
| 88 |
config.CHATROOM_FN = "blah" |
| 89 |
|
| 90 |
# Writeout the made up repos file |
| 91 |
with open(config.BOT_PLUGIN_INDEXES, "w") as index_file: |
| 92 |
index_file.write(SIMPLE_JSON_PLUGINS_INDEX) |
| 93 |
|
| 94 |
for key in extra_config: |
| 95 |
setattr(config, key, extra_config[key]) |
| 96 |
super().__init__(config) |
| 97 |
self.bot_identifier = self.build_identifier("err") |
| 98 |
self.md = text() # We just want simple text for testing purposes |
| 99 |
|
| 100 |
# setup a memory based storage |
| 101 |
spm = BackendPluginManager( |
| 102 |
config, "errbot.storage", "Memory", StoragePluginBase, CORE_STORAGE |
| 103 |
) |
| 104 |
storage_plugin = spm.load_plugin() |
| 105 |
|
| 106 |
# setup the plugin_manager just internally |
| 107 |
botplugins_dir = os.path.join(config.BOT_DATA_DIR, PLUGINS_SUBDIR) |
| 108 |
if not os.path.exists(botplugins_dir): |
| 109 |
os.makedirs(botplugins_dir, mode=0o755) |
| 110 |
|
| 111 |
# get it back from where we publish it. |
| 112 |
repo_index_paths = ( |
| 113 |
os.path.join( |
| 114 |
os.path.dirname(__file__), "..", "docs", "_extra", "repos.json" |
| 115 |
), |
| 116 |
) |
| 117 |
repo_manager = BotRepoManager(storage_plugin, botplugins_dir, repo_index_paths) |
| 118 |
self.attach_storage_plugin(storage_plugin) |
| 119 |
self.attach_repo_manager(repo_manager) |
| 120 |
self.attach_plugin_manager( |
| 121 |
BotPluginManager( |
| 122 |
storage_plugin, |
| 123 |
config.BOT_EXTRA_PLUGIN_DIR, |
| 124 |
config.AUTOINSTALL_DEPS, |
| 125 |
getattr(config, "CORE_PLUGINS", None), |
| 126 |
lambda name, clazz: clazz(self, name), |
| 127 |
getattr(config, "PLUGINS_CALLBACK_ORDER", (None,)), |
| 128 |
) |
| 129 |
) |
| 130 |
self.inject_commands_from(self) |
| 131 |
self.inject_command_filters_from(ACLS(self)) |
| 132 |
|
| 133 |
def build_identifier(self, text_representation): |
| 134 |
return TestPerson(text_representation) |
| 135 |
|
| 136 |
def build_reply(self, msg, text=None, private=False, threaded=False): |
| 137 |
reply = self.build_message(text) |
| 138 |
reply.frm = self.bot_identifier |
| 139 |
reply.to = msg.frm |
| 140 |
if threaded: |
| 141 |
reply.parent = msg |
| 142 |
return reply |
| 143 |
|
| 144 |
def send_message(self, msg): |
| 145 |
msg._body = self.md.convert(msg.body) |
| 146 |
self.outgoing_message_queue.put(msg) |
| 147 |
|
| 148 |
def pop_message(self, timeout=3, block=True): |
| 149 |
return self.outgoing_message_queue.get(timeout=timeout, block=block) |
| 150 |
|
| 151 |
@botcmd |
| 152 |
def command(self, msg, args): |
| 153 |
return "Regular command" |
| 154 |
|
| 155 |
@botcmd(admin_only=True) |
| 156 |
def admin_command(self, msg, args): |
| 157 |
return "Admin command" |
| 158 |
|
| 159 |
@re_botcmd(pattern=r"^regex command with prefix$", prefixed=True) |
| 160 |
def regex_command_with_prefix(self, msg, match): |
| 161 |
return "Regex command" |
| 162 |
|
| 163 |
@re_botcmd(pattern=r"^regex command without prefix$", prefixed=False) |
| 164 |
def regex_command_without_prefix(self, msg, match): |
| 165 |
return "Regex command" |
| 166 |
|
| 167 |
@re_botcmd( |
| 168 |
pattern=r"regex command with capture group: (?P<capture>.*)", prefixed=False |
| 169 |
) |
| 170 |
def regex_command_with_capture_group(self, msg, match): |
| 171 |
return match.group("capture") |
| 172 |
|
| 173 |
@re_botcmd(pattern=r"matched by two commands") |
| 174 |
def double_regex_command_one(self, msg, match): |
| 175 |
return "one" |
| 176 |
|
| 177 |
@re_botcmd(pattern=r"matched by two commands", flags=re.IGNORECASE) |
| 178 |
def double_regex_command_two(self, msg, match): |
| 179 |
return "two" |
| 180 |
|
| 181 |
@re_botcmd(pattern=r"match_here", matchall=True) |
| 182 |
def regex_command_with_matchall(self, msg, matches): |
| 183 |
return len(matches) |
| 184 |
|
| 185 |
@botcmd |
| 186 |
def return_args_as_str(self, msg, args): |
| 187 |
return "".join(args) |
| 188 |
|
| 189 |
@botcmd(template="args_as_md") |
| 190 |
def return_args_as_md(self, msg, args): |
| 191 |
return {"args": args} |
| 192 |
|
| 193 |
@botcmd |
| 194 |
def send_args_as_md(self, msg, args): |
| 195 |
self.send_templated(msg.frm, "args_as_md", {"args": args}) |
| 196 |
|
| 197 |
@botcmd |
| 198 |
def raises_exception(self, msg, args): |
| 199 |
raise Exception("Kaboom!") |
| 200 |
|
| 201 |
@botcmd |
| 202 |
def yield_args_as_str(self, msg, args): |
| 203 |
for arg in args: |
| 204 |
yield arg |
| 205 |
|
| 206 |
@botcmd(template="args_as_md") |
| 207 |
def yield_args_as_md(self, msg, args): |
| 208 |
for arg in args: |
| 209 |
yield {"args": [arg]} |
| 210 |
|
| 211 |
@botcmd |
| 212 |
def yields_str_then_raises_exception(self, msg, args): |
| 213 |
yield "foobar" |
| 214 |
raise Exception("Kaboom!") |
| 215 |
|
| 216 |
@botcmd |
| 217 |
def return_long_output(self, msg, args): |
| 218 |
return LONG_TEXT_STRING * 3 |
| 219 |
|
| 220 |
@botcmd |
| 221 |
def yield_long_output(self, msg, args): |
| 222 |
for i in range(2): |
| 223 |
yield LONG_TEXT_STRING * 3 |
| 224 |
|
| 225 |
## |
| 226 |
# arg_botcmd test commands |
| 227 |
## |
| 228 |
|
| 229 |
@arg_botcmd("--first-name", dest="first_name") |
| 230 |
@arg_botcmd("--last-name", dest="last_name") |
| 231 |
def yields_first_name_last_name(self, msg, first_name=None, last_name=None): |
| 232 |
yield "%s %s" % (first_name, last_name) |
| 233 |
|
| 234 |
@arg_botcmd("--first-name", dest="first_name") |
| 235 |
@arg_botcmd("--last-name", dest="last_name") |
| 236 |
def returns_first_name_last_name(self, msg, first_name=None, last_name=None): |
| 237 |
return "%s %s" % (first_name, last_name) |
| 238 |
|
| 239 |
@arg_botcmd("--first-name", dest="first_name") |
| 240 |
@arg_botcmd("--last-name", dest="last_name", unpack_args=False) |
| 241 |
def returns_first_name_last_name_without_unpacking(self, msg, args): |
| 242 |
return "%s %s" % (args.first_name, args.last_name) |
| 243 |
|
| 244 |
@arg_botcmd("value", type=str) |
| 245 |
@arg_botcmd("--count", dest="count", type=int) |
| 246 |
def returns_value_repeated_count_times(self, msg, value=None, count=None): |
| 247 |
# str * int gives a repeated string |
| 248 |
return value * count |
| 249 |
|
| 250 |
@property |
| 251 |
def mode(self): |
| 252 |
return "Dummy" |
| 253 |
|
| 254 |
@property |
| 255 |
def rooms(self): |
| 256 |
return [] |
| 257 |
|
| 258 |
|
| 259 |
@pytest.fixture |
| 260 |
def dummy_backend(): |
| 261 |
return DummyBackend() |
| 262 |
|
| 263 |
|
| 264 |
def test_buildreply(dummy_backend): |
| 265 |
m = dummy_backend.build_message("Content") |
| 266 |
m.frm = dummy_backend.build_identifier("user") |
| 267 |
m.to = dummy_backend.build_identifier("somewhere") |
| 268 |
resp = dummy_backend.build_reply(m, "Response") |
| 269 |
|
| 270 |
assert str(resp.to) == "user" |
| 271 |
assert str(resp.frm) == "err" |
| 272 |
assert str(resp.body) == "Response" |
| 273 |
assert resp.parent is None |
| 274 |
|
| 275 |
|
| 276 |
def test_buildreply_with_parent(dummy_backend): |
| 277 |
m = dummy_backend.build_message("Content") |
| 278 |
m.frm = dummy_backend.build_identifier("user") |
| 279 |
m.to = dummy_backend.build_identifier("somewhere") |
| 280 |
resp = dummy_backend.build_reply(m, "Response", threaded=True) |
| 281 |
|
| 282 |
assert resp.parent is not None |
| 283 |
|
| 284 |
|
| 285 |
def test_all_command_private(): |
| 286 |
dummy_backend = DummyBackend(extra_config={"DIVERT_TO_PRIVATE": ("ALL_COMMANDS",)}) |
| 287 |
m = dummy_backend.build_message("Content") |
| 288 |
m.frm = dummy_backend.build_identifier("user") |
| 289 |
m.to = dummy_backend.build_identifier("somewhere") |
| 290 |
resp = dummy_backend.build_reply(m, "Response", threaded=True) |
| 291 |
assert "ALL_COMMANDS" in dummy_backend.bot_config.DIVERT_TO_PRIVATE |
| 292 |
assert resp is not None |
| 293 |
|
| 294 |
|
| 295 |
def test_bot_admins_unique_string(): |
| 296 |
dummy = DummyBackend(extra_config={"BOT_ADMINS": "err@localhost"}) |
| 297 |
assert dummy.bot_config.BOT_ADMINS == ("err@localhost",) |
| 298 |
|
| 299 |
|
| 300 |
@pytest.fixture |
| 301 |
def dummy_execute_and_send(): |
| 302 |
dummy = DummyBackend() |
| 303 |
example_message = dummy.build_message("some_message") |
| 304 |
example_message.frm = dummy.build_identifier("noterr") |
| 305 |
example_message.to = dummy.build_identifier("err") |
| 306 |
|
| 307 |
assets_path = os.path.join(os.path.dirname(__file__), "assets") |
| 308 |
templating.template_path.append( |
| 309 |
str(templating.make_templates_path(Path(assets_path))) |
| 310 |
) |
| 311 |
templating.env = templating.Environment( |
| 312 |
loader=templating.FileSystemLoader(templating.template_path) |
| 313 |
) |
| 314 |
return dummy, example_message |
| 315 |
|
| 316 |
|
| 317 |
def test_commands_can_return_string(dummy_execute_and_send): |
| 318 |
dummy, m = dummy_execute_and_send |
| 319 |
|
| 320 |
dummy._execute_and_send( |
| 321 |
cmd="return_args_as_str", |
| 322 |
args=["foo", "bar"], |
| 323 |
match=None, |
| 324 |
msg=m, |
| 325 |
template_name=dummy.return_args_as_str._err_command_template, |
| 326 |
) |
| 327 |
assert "foobar" == dummy.pop_message().body |
| 328 |
|
| 329 |
|
| 330 |
def test_commands_can_return_md(dummy_execute_and_send): |
| 331 |
dummy, m = dummy_execute_and_send |
| 332 |
dummy._execute_and_send( |
| 333 |
cmd="return_args_as_md", |
| 334 |
args=["foo", "bar"], |
| 335 |
match=None, |
| 336 |
msg=m, |
| 337 |
template_name=dummy.return_args_as_md._err_command_template, |
| 338 |
) |
| 339 |
response = dummy.pop_message() |
| 340 |
assert "foobar" == response.body |
| 341 |
|
| 342 |
|
| 343 |
def test_commands_can_send_templated(dummy_execute_and_send): |
| 344 |
dummy, m = dummy_execute_and_send |
| 345 |
dummy._execute_and_send( |
| 346 |
cmd="send_args_as_md", |
| 347 |
args=["foo", "bar"], |
| 348 |
match=None, |
| 349 |
msg=m, |
| 350 |
template_name=dummy.return_args_as_md._err_command_template, |
| 351 |
) |
| 352 |
response = dummy.pop_message() |
| 353 |
assert "foobar" == response.body |
| 354 |
|
| 355 |
|
| 356 |
def test_exception_is_caught_and_shows_error_message(dummy_execute_and_send): |
| 357 |
dummy, m = dummy_execute_and_send |
| 358 |
dummy._execute_and_send( |
| 359 |
cmd="raises_exception", |
| 360 |
args=[], |
| 361 |
match=None, |
| 362 |
msg=m, |
| 363 |
template_name=dummy.raises_exception._err_command_template, |
| 364 |
) |
| 365 |
assert dummy.MSG_ERROR_OCCURRED in dummy.pop_message().body |
| 366 |
|
| 367 |
dummy._execute_and_send( |
| 368 |
cmd="yields_str_then_raises_exception", |
| 369 |
args=[], |
| 370 |
match=None, |
| 371 |
msg=m, |
| 372 |
template_name=dummy.yields_str_then_raises_exception._err_command_template, |
| 373 |
) |
| 374 |
assert "foobar" == dummy.pop_message().body |
| 375 |
assert dummy.MSG_ERROR_OCCURRED in dummy.pop_message().body |
| 376 |
|
| 377 |
|
| 378 |
def test_commands_can_yield_strings(dummy_execute_and_send): |
| 379 |
dummy, m = dummy_execute_and_send |
| 380 |
dummy._execute_and_send( |
| 381 |
cmd="yield_args_as_str", |
| 382 |
args=["foo", "bar"], |
| 383 |
match=None, |
| 384 |
msg=m, |
| 385 |
template_name=dummy.yield_args_as_str._err_command_template, |
| 386 |
) |
| 387 |
assert "foo" == dummy.pop_message().body |
| 388 |
assert "bar" == dummy.pop_message().body |
| 389 |
|
| 390 |
|
| 391 |
def test_commands_can_yield_md(dummy_execute_and_send): |
| 392 |
dummy, m = dummy_execute_and_send |
| 393 |
dummy._execute_and_send( |
| 394 |
cmd="yield_args_as_md", |
| 395 |
args=["foo", "bar"], |
| 396 |
match=None, |
| 397 |
msg=m, |
| 398 |
template_name=dummy.yield_args_as_md._err_command_template, |
| 399 |
) |
| 400 |
assert "foo" == dummy.pop_message().body |
| 401 |
assert "bar" == dummy.pop_message().body |
| 402 |
|
| 403 |
|
| 404 |
def test_output_longer_than_max_msg_size_is_split_into_multiple_msgs_when_returned( |
| 405 |
dummy_execute_and_send, |
| 406 |
): |
| 407 |
dummy, m = dummy_execute_and_send |
| 408 |
dummy.bot_config.MESSAGE_SIZE_LIMIT = len(LONG_TEXT_STRING) |
| 409 |
|
| 410 |
dummy._execute_and_send( |
| 411 |
cmd="return_long_output", |
| 412 |
args=["foo", "bar"], |
| 413 |
match=None, |
| 414 |
msg=m, |
| 415 |
template_name=dummy.return_long_output._err_command_template, |
| 416 |
) |
| 417 |
for i in range( |
| 418 |
3 |
| 419 |
): # return_long_output outputs a string that's 3x longer than the size limit |
| 420 |
assert LONG_TEXT_STRING.strip() == dummy.pop_message().body |
| 421 |
|
| 422 |
with pytest.raises(Empty): |
| 423 |
dummy.pop_message(block=False) |
| 424 |
|
| 425 |
|
| 426 |
def test_output_longer_than_max_msg_size_is_split_into_multiple_msgs_when_yielded( |
| 427 |
dummy_execute_and_send, |
| 428 |
): |
| 429 |
dummy, m = dummy_execute_and_send |
| 430 |
dummy.bot_config.MESSAGE_SIZE_LIMIT = len(LONG_TEXT_STRING) |
| 431 |
|
| 432 |
dummy._execute_and_send( |
| 433 |
cmd="yield_long_output", |
| 434 |
args=["foo", "bar"], |
| 435 |
match=None, |
| 436 |
msg=m, |
| 437 |
template_name=dummy.yield_long_output._err_command_template, |
| 438 |
) |
| 439 |
for i in range( |
| 440 |
6 |
| 441 |
): # yields_long_output yields 2 strings that are 3x longer than the size limit |
| 442 |
assert LONG_TEXT_STRING.strip() == dummy.pop_message().body |
| 443 |
with pytest.raises(Empty): |
| 444 |
dummy.pop_message(block=False) |
| 445 |
|
| 446 |
|
| 447 |
def makemessage(dummy, message, from_=None, to=None): |
| 448 |
if not from_: |
| 449 |
from_ = dummy.build_identifier("noterr") |
| 450 |
if not to: |
| 451 |
to = dummy.build_identifier("noterr") |
| 452 |
m = dummy.build_message(message) |
| 453 |
m.frm = from_ |
| 454 |
m.to = to |
| 455 |
return m |
| 456 |
|
| 457 |
|
| 458 |
def test_inject_skips_methods_without_botcmd_decorator(dummy_backend): |
| 459 |
assert "build_message" not in dummy_backend.commands |
| 460 |
|
| 461 |
|
| 462 |
def test_inject_and_remove_botcmd(dummy_backend): |
| 463 |
assert "command" in dummy_backend.commands |
| 464 |
dummy_backend.remove_commands_from(dummy_backend) |
| 465 |
assert len(dummy_backend.commands) == 0 |
| 466 |
|
| 467 |
|
| 468 |
def test_inject_and_remove_re_botcmd(dummy_backend): |
| 469 |
assert "regex_command_with_prefix" in dummy_backend.re_commands |
| 470 |
dummy_backend.remove_commands_from(dummy_backend) |
| 471 |
assert len(dummy_backend.re_commands) == 0 |
| 472 |
|
| 473 |
|
| 474 |
def test_callback_message(dummy_backend): |
| 475 |
dummy_backend.callback_message( |
| 476 |
makemessage(dummy_backend, "!return_args_as_str one two") |
| 477 |
) |
| 478 |
assert "one two" == dummy_backend.pop_message().body |
| 479 |
|
| 480 |
|
| 481 |
def test_callback_message_with_prefix_optional(): |
| 482 |
dummy = DummyBackend({"BOT_PREFIX_OPTIONAL_ON_CHAT": True}) |
| 483 |
m = makemessage(dummy, "return_args_as_str one two") |
| 484 |
dummy.callback_message(m) |
| 485 |
assert "one two" == dummy.pop_message().body |
| 486 |
|
| 487 |
# Groupchat should still require the prefix |
| 488 |
m.frm = TestOccupant("someone", "room") |
| 489 |
room = TestRoom("room", bot=dummy) |
| 490 |
m.to = room |
| 491 |
dummy.callback_message(m) |
| 492 |
|
| 493 |
with pytest.raises(Empty): |
| 494 |
dummy.pop_message(block=False) |
| 495 |
|
| 496 |
m = makemessage( |
| 497 |
dummy, |
| 498 |
"!return_args_as_str one two", |
| 499 |
from_=TestOccupant("someone", "room"), |
| 500 |
to=room, |
| 501 |
) |
| 502 |
dummy.callback_message(m) |
| 503 |
assert "one two" == dummy.pop_message().body |
| 504 |
|
| 505 |
|
| 506 |
def test_callback_message_with_bot_alt_prefixes(): |
| 507 |
dummy = DummyBackend( |
| 508 |
{"BOT_ALT_PREFIXES": ("Err",), "BOT_ALT_PREFIX_SEPARATORS": (",", ";")} |
| 509 |
) |
| 510 |
dummy.callback_message(makemessage(dummy, "Err return_args_as_str one two")) |
| 511 |
assert "one two" == dummy.pop_message().body |
| 512 |
dummy.callback_message(makemessage(dummy, "Err, return_args_as_str one two")) |
| 513 |
assert "one two" == dummy.pop_message().body |
| 514 |
|
| 515 |
|
| 516 |
def test_callback_message_with_re_botcmd(dummy_backend): |
| 517 |
dummy_backend.callback_message( |
| 518 |
makemessage(dummy_backend, "!regex command with prefix") |
| 519 |
) |
| 520 |
assert "Regex command" == dummy_backend.pop_message().body |
| 521 |
dummy_backend.callback_message( |
| 522 |
makemessage(dummy_backend, "regex command without prefix") |
| 523 |
) |
| 524 |
assert "Regex command" == dummy_backend.pop_message().body |
| 525 |
dummy_backend.callback_message( |
| 526 |
makemessage(dummy_backend, "!regex command with capture group: Captured text") |
| 527 |
) |
| 528 |
assert "Captured text" == dummy_backend.pop_message().body |
| 529 |
dummy_backend.callback_message( |
| 530 |
makemessage(dummy_backend, "regex command with capture group: Captured text") |
| 531 |
) |
| 532 |
assert "Captured text" == dummy_backend.pop_message().body |
| 533 |
dummy_backend.callback_message( |
| 534 |
makemessage( |
| 535 |
dummy_backend, |
| 536 |
"This command also allows extra text in front - regex " |
| 537 |
"command with capture group: Captured text", |
| 538 |
) |
| 539 |
) |
| 540 |
assert "Captured text" == dummy_backend.pop_message().body |
| 541 |
|
| 542 |
|
| 543 |
def test_callback_message_with_re_botcmd_and_alt_prefixes(): |
| 544 |
dummy_backend = DummyBackend( |
| 545 |
{"BOT_ALT_PREFIXES": ("Err",), "BOT_ALT_PREFIX_SEPARATORS": (",", ";")} |
| 546 |
) |
| 547 |
dummy_backend.callback_message( |
| 548 |
makemessage(dummy_backend, "!regex command with prefix") |
| 549 |
) |
| 550 |
assert "Regex command" == dummy_backend.pop_message().body |
| 551 |
dummy_backend.callback_message( |
| 552 |
makemessage(dummy_backend, "Err regex command with prefix") |
| 553 |
) |
| 554 |
assert "Regex command" == dummy_backend.pop_message().body |
| 555 |
dummy_backend.callback_message( |
| 556 |
makemessage(dummy_backend, "Err, regex command with prefix") |
| 557 |
) |
| 558 |
assert "Regex command" == dummy_backend.pop_message().body |
| 559 |
dummy_backend.callback_message( |
| 560 |
makemessage(dummy_backend, "regex command without prefix") |
| 561 |
) |
| 562 |
assert "Regex command" == dummy_backend.pop_message().body |
| 563 |
dummy_backend.callback_message( |
| 564 |
makemessage(dummy_backend, "!regex command with capture group: Captured text") |
| 565 |
) |
| 566 |
assert "Captured text" == dummy_backend.pop_message().body |
| 567 |
dummy_backend.callback_message( |
| 568 |
makemessage(dummy_backend, "regex command with capture group: Captured text") |
| 569 |
) |
| 570 |
assert "Captured text" == dummy_backend.pop_message().body |
| 571 |
dummy_backend.callback_message( |
| 572 |
makemessage( |
| 573 |
dummy_backend, |
| 574 |
"This command also allows extra text in front - " |
| 575 |
"regex command with capture group: Captured text", |
| 576 |
) |
| 577 |
) |
| 578 |
assert "Captured text" == dummy_backend.pop_message().body |
| 579 |
dummy_backend.callback_message( |
| 580 |
makemessage( |
| 581 |
dummy_backend, "Err, regex command with capture group: Captured text" |
| 582 |
) |
| 583 |
) |
| 584 |
assert "Captured text" == dummy_backend.pop_message().body |
| 585 |
dummy_backend.callback_message( |
| 586 |
makemessage( |
| 587 |
dummy_backend, |
| 588 |
"Err This command also allows extra text in front - " |
| 589 |
"regex command with capture group: Captured text", |
| 590 |
) |
| 591 |
) |
| 592 |
assert "Captured text" == dummy_backend.pop_message().body |
| 593 |
dummy_backend.callback_message(makemessage(dummy_backend, "!match_here")) |
| 594 |
assert "1" == dummy_backend.pop_message().body |
| 595 |
dummy_backend.callback_message( |
| 596 |
makemessage(dummy_backend, "!match_here match_here match_here") |
| 597 |
) |
| 598 |
assert "3" == dummy_backend.pop_message().body |
| 599 |
|
| 600 |
|
| 601 |
def test_regex_commands_can_overlap(dummy_backend): |
| 602 |
dummy_backend.callback_message( |
| 603 |
makemessage(dummy_backend, "!matched by two commands") |
| 604 |
) |
| 605 |
response = (dummy_backend.pop_message().body, dummy_backend.pop_message().body) |
| 606 |
assert response == ("one", "two") or response == ("two", "one") |
| 607 |
|
| 608 |
|
| 609 |
def test_regex_commands_allow_passing_re_flags(dummy_backend): |
| 610 |
dummy_backend.callback_message( |
| 611 |
makemessage(dummy_backend, "!MaTcHeD By TwO cOmMaNdS") |
| 612 |
) |
| 613 |
assert "two" == dummy_backend.pop_message().body |
| 614 |
with pytest.raises(Empty): |
| 615 |
dummy_backend.pop_message(timeout=1) |
| 616 |
|
| 617 |
|
| 618 |
def test_arg_botcmd_returns_first_name_last_name(dummy_backend): |
| 619 |
dummy_backend.callback_message( |
| 620 |
makemessage( |
| 621 |
dummy_backend, |
| 622 |
"!returns_first_name_last_name --first-name=Err --last-name=Bot", |
| 623 |
) |
| 624 |
) |
| 625 |
assert "Err Bot" |
| 626 |
|
| 627 |
|
| 628 |
def test_arg_botcmd_returns_with_escaping(dummy_backend): |
| 629 |
first_name = 'Err\\"' |
| 630 |
last_name = "Bot" |
| 631 |
dummy_backend.callback_message( |
| 632 |
makemessage( |
| 633 |
dummy_backend, |
| 634 |
"!returns_first_name_last_name --first-name=%s --last-name=%s" |
| 635 |
% (first_name, last_name), |
| 636 |
) |
| 637 |
) |
| 638 |
assert 'Err" Bot' == dummy_backend.pop_message().body |
| 639 |
|
| 640 |
|
| 641 |
def test_arg_botcmd_returns_with_incorrect_escaping(dummy_backend): |
| 642 |
first_name = 'Err"' |
| 643 |
last_name = "Bot" |
| 644 |
dummy_backend.callback_message( |
| 645 |
makemessage( |
| 646 |
dummy_backend, |
| 647 |
"!returns_first_name_last_name --first-name=%s --last-name=%s" |
| 648 |
% (first_name, last_name), |
| 649 |
) |
| 650 |
) |
| 651 |
assert ( |
| 652 |
"I couldn't parse this command; No closing quotation" |
| 653 |
in dummy_backend.pop_message().body |
| 654 |
) |
| 655 |
|
| 656 |
|
| 657 |
def test_arg_botcmd_yields_first_name_last_name(dummy_backend): |
| 658 |
dummy_backend.callback_message( |
| 659 |
makemessage( |
| 660 |
dummy_backend, |
| 661 |
"!yields_first_name_last_name --first-name=Err --last-name=Bot", |
| 662 |
) |
| 663 |
) |
| 664 |
assert "Err Bot" == dummy_backend.pop_message().body |
| 665 |
|
| 666 |
|
| 667 |
def test_arg_botcmd_returns_value_repeated_count_times(dummy_backend): |
| 668 |
dummy_backend.callback_message( |
| 669 |
makemessage(dummy_backend, "!returns_value_repeated_count_times Foo --count 5") |
| 670 |
) |
| 671 |
assert "FooFooFooFooFoo" == dummy_backend.pop_message().body |
| 672 |
|
| 673 |
|
| 674 |
def test_arg_botcmd_doesnt_raise_systemerror(dummy_backend): |
| 675 |
dummy_backend.callback_message( |
| 676 |
makemessage(dummy_backend, "!returns_first_name_last_name --invalid-parameter") |
| 677 |
) |
| 678 |
|
| 679 |
|
| 680 |
def test_arg_botcdm_returns_errors_as_chat(dummy_backend): |
| 681 |
dummy_backend.callback_message( |
| 682 |
makemessage(dummy_backend, "!returns_first_name_last_name --invalid-parameter") |
| 683 |
) |
| 684 |
assert ( |
| 685 |
"I couldn't parse the arguments; unrecognized arguments: --invalid-parameter" |
| 686 |
in dummy_backend.pop_message().body |
| 687 |
) |
| 688 |
|
| 689 |
|
| 690 |
def test_arg_botcmd_returns_help_message_as_chat(dummy_backend): |
| 691 |
dummy_backend.callback_message( |
| 692 |
makemessage(dummy_backend, "!returns_first_name_last_name --help") |
| 693 |
) |
| 694 |
assert ( |
| 695 |
"usage: returns_first_name_last_name [-h] [--last-name LAST_NAME]" |
| 696 |
in dummy_backend.pop_message().body |
| 697 |
) |
| 698 |
|
| 699 |
|
| 700 |
def test_arg_botcmd_undoes_fancy_unicode_dash_conversion(dummy_backend): |
| 701 |
dummy_backend.callback_message( |
| 702 |
makemessage( |
| 703 |
dummy_backend, |
| 704 |
"!returns_first_name_last_name —first-name=Err —last-name=Bot", |
| 705 |
) |
| 706 |
) |
| 707 |
assert "Err Bot" == dummy_backend.pop_message().body |
| 708 |
|
| 709 |
|
| 710 |
def test_arg_botcmd_without_argument_unpacking(dummy_backend): |
| 711 |
dummy_backend.callback_message( |
| 712 |
makemessage( |
| 713 |
dummy_backend, |
| 714 |
"!returns_first_name_last_name_without_unpacking --first-name=Err --last-name=Bot", |
| 715 |
) |
| 716 |
) |
| 717 |
assert "Err Bot" == dummy_backend.pop_message().body |
| 718 |
|
| 719 |
|
| 720 |
def test_access_controls(dummy_backend): |
| 721 |
testroom = TestRoom("room", bot=dummy_backend) |
| 722 |
tests = [ |
| 723 |
# BOT_ADMINS scenarios |
| 724 |
dict( |
| 725 |
message=makemessage(dummy_backend, "!admin_command"), |
| 726 |
bot_admins=("noterr",), |
| 727 |
expected_response="Admin command", |
| 728 |
), |
| 729 |
dict( |
| 730 |
message=makemessage(dummy_backend, "!admin_command"), |
| 731 |
bot_admins=(), |
| 732 |
expected_response="This command requires bot-admin privileges", |
| 733 |
), |
| 734 |
dict( |
| 735 |
message=makemessage(dummy_backend, "!admin_command"), |
| 736 |
bot_admins=("*err",), |
| 737 |
expected_response="Admin command", |
| 738 |
), |
| 739 |
# admin_only commands SHOULD be private-message only by default |
| 740 |
dict( |
| 741 |
message=makemessage( |
| 742 |
dummy_backend, |
| 743 |
"!admin_command", |
| 744 |
from_=TestOccupant("noterr", room=testroom), |
| 745 |
to=testroom, |
| 746 |
), |
| 747 |
bot_admins=("noterr",), |
| 748 |
expected_response="This command may only be issued through a direct message", |
| 749 |
), |
| 750 |
# But MAY be sent via groupchat IF 'allowmuc' is specifically set to True. |
| 751 |
dict( |
| 752 |
message=makemessage( |
| 753 |
dummy_backend, |
| 754 |
"!admin_command", |
| 755 |
from_=TestOccupant("noterr", room=testroom), |
| 756 |
to=testroom, |
| 757 |
), |
| 758 |
bot_admins=("noterr",), |
| 759 |
acl={"admin_command": {"allowmuc": True}}, |
| 760 |
expected_response="Admin command", |
| 761 |
), |
| 762 |
# ACCESS_CONTROLS scenarios WITHOUT wildcards (<4.0 format) |
| 763 |
dict( |
| 764 |
message=makemessage(dummy_backend, "!command"), |
| 765 |
expected_response="Regular command", |
| 766 |
), |
| 767 |
dict( |
| 768 |
message=makemessage(dummy_backend, "!regex command with prefix"), |
| 769 |
expected_response="Regex command", |
| 770 |
), |
| 771 |
dict( |
| 772 |
message=makemessage(dummy_backend, "!command"), |
| 773 |
acl_default={"allowmuc": False, "allowprivate": False}, |
| 774 |
expected_response="You're not allowed to access this command via private message to me", |
| 775 |
), |
| 776 |
dict( |
| 777 |
message=makemessage(dummy_backend, "regex command without prefix"), |
| 778 |
acl_default={"allowmuc": False, "allowprivate": False}, |
| 779 |
expected_response="You're not allowed to access this command via private message to me", |
| 780 |
), |
| 781 |
dict( |
| 782 |
message=makemessage(dummy_backend, "!command"), |
| 783 |
acl_default={"allowmuc": True, "allowprivate": False}, |
| 784 |
expected_response="You're not allowed to access this command via private message to me", |
| 785 |
), |
| 786 |
dict( |
| 787 |
message=makemessage(dummy_backend, "!command"), |
| 788 |
acl_default={"allowmuc": False, "allowprivate": True}, |
| 789 |
expected_response="Regular command", |
| 790 |
), |
| 791 |
dict( |
| 792 |
message=makemessage(dummy_backend, "!command"), |
| 793 |
acl={"command": {"allowprivate": False}}, |
| 794 |
acl_default={"allowmuc": False, "allowprivate": True}, |
| 795 |
expected_response="You're not allowed to access this command via private message to me", |
| 796 |
), |
| 797 |
dict( |
| 798 |
message=makemessage(dummy_backend, "!command"), |
| 799 |
acl={"command": {"allowmuc": True}}, |
| 800 |
acl_default={"allowmuc": True, "allowprivate": False}, |
| 801 |
expected_response="You're not allowed to access this command via private message to me", |
| 802 |
), |
| 803 |
dict( |
| 804 |
message=makemessage(dummy_backend, "!command"), |
| 805 |
acl={"command": {"allowprivate": True}}, |
| 806 |
acl_default={"allowmuc": False, "allowprivate": False}, |
| 807 |
expected_response="Regular command", |
| 808 |
), |
| 809 |
dict( |
| 810 |
message=makemessage( |
| 811 |
dummy_backend, |
| 812 |
"!command", |
| 813 |
from_=TestOccupant("someone", "room"), |
| 814 |
to=TestRoom("room", bot=dummy_backend), |
| 815 |
), |
| 816 |
acl={"command": {"allowrooms": ("room",)}}, |
| 817 |
expected_response="Regular command", |
| 818 |
), |
| 819 |
dict( |
| 820 |
message=makemessage( |
| 821 |
dummy_backend, |
| 822 |
"!command", |
| 823 |
from_=TestOccupant("someone", "room"), |
| 824 |
to=TestRoomAcl("room", bot=dummy_backend), |
| 825 |
), |
| 826 |
acl={"command": {"allowrooms": ("room",)}}, |
| 827 |
expected_response="Regular command", |
| 828 |
), |
| 829 |
dict( |
| 830 |
message=makemessage( |
| 831 |
dummy_backend, |
| 832 |
"!command", |
| 833 |
from_=TestOccupant("someone", "room_1"), |
| 834 |
to=TestRoom("room1", bot=dummy_backend), |
| 835 |
), |
| 836 |
acl={"command": {"allowrooms": ("room_*",)}}, |
| 837 |
expected_response="Regular command", |
| 838 |
), |
| 839 |
dict( |
| 840 |
message=makemessage( |
| 841 |
dummy_backend, |
| 842 |
"!command", |
| 843 |
from_=TestOccupant("someone", "room"), |
| 844 |
to=TestRoom("room", bot=dummy_backend), |
| 845 |
), |
| 846 |
acl={"command": {"allowrooms": ("anotherroom@localhost",)}}, |
| 847 |
expected_response="You're not allowed to access this command from this room", |
| 848 |
), |
| 849 |
dict( |
| 850 |
message=makemessage( |
| 851 |
dummy_backend, |
| 852 |
"!command", |
| 853 |
from_=TestOccupant("someone", "room"), |
| 854 |
to=TestRoomAcl("room", bot=dummy_backend), |
| 855 |
), |
| 856 |
acl={"command": {"allowrooms": ("anotherroom@localhost",)}}, |
| 857 |
expected_response="You're not allowed to access this command from this room", |
| 858 |
), |
| 859 |
dict( |
| 860 |
message=makemessage( |
| 861 |
dummy_backend, |
| 862 |
"!command", |
| 863 |
from_=TestOccupant("someone", "room"), |
| 864 |
to=TestRoom("room", bot=dummy_backend), |
| 865 |
), |
| 866 |
acl={"command": {"denyrooms": ("room",)}}, |
| 867 |
expected_response="You're not allowed to access this command from this room", |
| 868 |
), |
| 869 |
dict( |
| 870 |
message=makemessage( |
| 871 |
dummy_backend, |
| 872 |
"!command", |
| 873 |
from_=TestOccupant("someone", "room"), |
| 874 |
to=TestRoom("room", bot=dummy_backend), |
| 875 |
), |
| 876 |
acl={"command": {"denyrooms": ("*",)}}, |
| 877 |
expected_response="You're not allowed to access this command from this room", |
| 878 |
), |
| 879 |
dict( |
| 880 |
message=makemessage( |
| 881 |
dummy_backend, |
| 882 |
"!command", |
| 883 |
from_=TestOccupant("someone", "room"), |
| 884 |
to=TestRoom("room", bot=dummy_backend), |
| 885 |
), |
| 886 |
acl={"command": {"denyrooms": ("anotherroom",)}}, |
| 887 |
expected_response="Regular command", |
| 888 |
), |
| 889 |
dict( |
| 890 |
message=makemessage(dummy_backend, "!command"), |
| 891 |
acl={"command": {"allowusers": ("noterr",)}}, |
| 892 |
expected_response="Regular command", |
| 893 |
), |
| 894 |
dict( |
| 895 |
message=makemessage(dummy_backend, "!command"), |
| 896 |
acl={"command": {"allowusers": "noterr"}}, # simple string instead of tuple |
| 897 |
expected_response="Regular command", |
| 898 |
), |
| 899 |
dict( |
| 900 |
message=makemessage(dummy_backend, "!command"), |
| 901 |
acl={"command": {"allowusers": ("err",)}}, |
| 902 |
expected_response="You're not allowed to access this command from this user", |
| 903 |
), |
| 904 |
dict( |
| 905 |
message=makemessage(dummy_backend, "!command"), |
| 906 |
acl={"command": {"allowusers": ("*err",)}}, |
| 907 |
expected_response="Regular command", |
| 908 |
), |
| 909 |
dict( |
| 910 |
message=makemessage(dummy_backend, "!command"), |
| 911 |
acl={"command": {"denyusers": ("err",)}}, |
| 912 |
expected_response="Regular command", |
| 913 |
), |
| 914 |
dict( |
| 915 |
message=makemessage(dummy_backend, "!command"), |
| 916 |
acl={"command": {"denyusers": ("noterr",)}}, |
| 917 |
expected_response="You're not allowed to access this command from this user", |
| 918 |
), |
| 919 |
dict( |
| 920 |
message=makemessage(dummy_backend, "!command"), |
| 921 |
acl={"command": {"denyusers": "noterr"}}, # simple string instead of tuple |
| 922 |
expected_response="You're not allowed to access this command from this user", |
| 923 |
), |
| 924 |
dict( |
| 925 |
message=makemessage(dummy_backend, "!command"), |
| 926 |
acl={"command": {"denyusers": ("*err",)}}, |
| 927 |
expected_response="You're not allowed to access this command from this user", |
| 928 |
), |
| 929 |
dict( |
| 930 |
message=makemessage(dummy_backend, "!command echo"), |
| 931 |
acl={"command": {"allowargs": ("echo",)}}, |
| 932 |
expected_response="Regular command", |
| 933 |
), |
| 934 |
dict( |
| 935 |
message=makemessage(dummy_backend, "!command notallowed"), |
| 936 |
acl={"command": {"allowargs": ("echo",)}}, |
| 937 |
expected_response="You're not allowed to access this command using the provided arguments", |
| 938 |
), |
| 939 |
dict( |
| 940 |
message=makemessage(dummy_backend, "!command echodeny"), |
| 941 |
acl={"command": {"denyargs": ("echodeny",)}}, |
| 942 |
expected_response="You're not allowed to access this command using the provided arguments", |
| 943 |
), |
| 944 |
dict( |
| 945 |
message=makemessage(dummy_backend, "!command echo"), |
| 946 |
acl={"command": {"denyargs": ("echodeny",)}}, |
| 947 |
expected_response="Regular command", |
| 948 |
), |
| 949 |
# ACCESS_CONTROLS scenarios WITH wildcards (>=4.0 format) |
| 950 |
dict( |
| 951 |
message=makemessage(dummy_backend, "!command"), |
| 952 |
acl={"DummyBackendRealName:command": {"denyusers": ("noterr",)}}, |
| 953 |
expected_response="You're not allowed to access this command from this user", |
| 954 |
), |
| 955 |
dict( |
| 956 |
message=makemessage(dummy_backend, "!command"), |
| 957 |
acl={"*:command": {"denyusers": ("noterr",)}}, |
| 958 |
expected_response="You're not allowed to access this command from this user", |
| 959 |
), |
| 960 |
dict( |
| 961 |
message=makemessage(dummy_backend, "!command"), |
| 962 |
acl={"DummyBackendRealName:*": {"denyusers": ("noterr",)}}, |
| 963 |
expected_response="You're not allowed to access this command from this user", |
| 964 |
), |
| 965 |
dict( |
| 966 |
message=makemessage(dummy_backend, "!command echo"), |
| 967 |
acl={"command": {"allowargs": ("ec*",)}}, |
| 968 |
expected_response="Regular command", |
| 969 |
), |
| 970 |
dict( |
| 971 |
message=makemessage(dummy_backend, "!command notallowed"), |
| 972 |
acl={"command": {"allowargs": ("e*",)}}, |
| 973 |
expected_response="You're not allowed to access this command using the provided arguments", |
| 974 |
), |
| 975 |
dict( |
| 976 |
message=makemessage(dummy_backend, "!command denied"), |
| 977 |
acl={"command": {"denyargs": ("den*",)}}, |
| 978 |
expected_response="You're not allowed to access this command using the provided arguments", |
| 979 |
), |
| 980 |
# Overlapping globs should use first match |
| 981 |
dict( |
| 982 |
message=makemessage(dummy_backend, "!command"), |
| 983 |
acl=OrderedDict( |
| 984 |
[ |
| 985 |
("DummyBackendRealName:*", {"denyusers": ("noterr",)}), |
| 986 |
("DummyBackendRealName:command", {"denyusers": ()}), |
| 987 |
] |
| 988 |
), |
| 989 |
expected_response="You're not allowed to access this command from this user", |
| 990 |
), |
| 991 |
# ACCESS_CONTROLS with numeric username as in telegram |
| 992 |
dict( |
| 993 |
message=makemessage( |
| 994 |
dummy_backend, "!command", from_=dummy_backend.build_identifier(1234) |
| 995 |
), |
| 996 |
acl={"command": {"allowusers": (1234,)}}, |
| 997 |
expected_response="Regular command", |
| 998 |
), |
| 999 |
] |
| 1000 |
|
| 1001 |
for test in tests: |
| 1002 |
dummy_backend.bot_config.ACCESS_CONTROLS_DEFAULT = test.get("acl_default", {}) |
| 1003 |
dummy_backend.bot_config.ACCESS_CONTROLS = test.get("acl", {}) |
| 1004 |
dummy_backend.bot_config.BOT_ADMINS = test.get("bot_admins", ()) |
| 1005 |
logger = logging.getLogger(__name__) |
| 1006 |
logger.info(f"** message: {test['message'].body}") |
| 1007 |
logger.info(f"** bot_admins: {dummy_backend.bot_config.BOT_ADMINS}") |
| 1008 |
logger.info(f"** acl: {dummy_backend.bot_config.ACCESS_CONTROLS}") |
| 1009 |
logger.info( |
| 1010 |
"** acl_default: {!r}".format( |
| 1011 |
dummy_backend.bot_config.ACCESS_CONTROLS_DEFAULT |
| 1012 |
) |
| 1013 |
) |
| 1014 |
dummy_backend.callback_message(test["message"]) |
| 1015 |
assert test["expected_response"] == dummy_backend.pop_message().body |