def test_plugin_logs(): tfh = tempfile.NamedTemporaryFile() limbo.init_log(config={ "logfile": tfh.name, "loglevel": logging.DEBUG, }) limbo.init_plugins("test/plugins") tfh.seek(0) log = tfh.read() assert "DEBUG:plugin: test/plugins/echo.py" in log assert "DEBUG:attaching" in log
def test_config_plugin_empty_string_success(): hooks = limbo.init_plugins("test/plugins", "") assert len(hooks) == 9 assert "message" in hooks assert isinstance(hooks, dict) assert isinstance(hooks["message"], list) assert len(hooks["message"]) == 2
def test_handle_member_left(): event = {"type": "member_left_channel", "user": "******"} hooks = limbo.init_plugins("test/plugins") server = limbo.FakeServer(hooks=hooks) assert limbo.handle_event(event, server) == "user 2 left"
def test_config_plugin_success(): hooks = limbo.init_plugins("test/plugins", "echo,loop") assert len(hooks) == 5 assert "message" in hooks assert isinstance(hooks, dict) assert isinstance(hooks["message"], list) assert len(hooks["message"]) == 1
def test_loop_hook(): hooks = limbo.init_plugins("test/plugins") server = limbo.FakeServer(hooks=hooks) slack = limbo.FakeSlack() limbo.loop(server, test_loop=1) eq_(server._loop_plugin_ran, True)
def test_plugin_success(): hooks = limbo.init_plugins("test/plugins") eq_(len(hooks), 4) assert "message" in hooks assert isinstance(hooks, dict) assert isinstance(hooks["message"], list) eq_(len(hooks["message"]), 2)
def test_plugin_success(): hooks = limbo.init_plugins("test/plugins") eq_(len(hooks), 2) assert "message" in hooks assert isinstance(hooks, dict) assert isinstance(hooks["message"], list) eq_(len(hooks["message"]), 2)
def test_loop_hook(): hooks = limbo.init_plugins("test/plugins") server = limbo.FakeServer(hooks=hooks) slack = limbo.FakeSlack() limbo.loop(server, test_loop=1) assert server._loop_plugin_ran == True
def test_config_plugin_none_success(): hooks = limbo.init_plugins("test/plugins", None) assert len(hooks) == 7 assert "message" in hooks assert isinstance(hooks, dict) assert isinstance(hooks["message"], list) assert len(hooks["message"]) == 2
def test_handle_message_basic(): msg = u"!echo Iñtërnâtiônàlizætiøn" event = {"user": "******", "text": msg} hooks = limbo.init_plugins("test/plugins") server = limbo.FakeServer(hooks=hooks) eq_(limbo.handle_message(event, server), msg)
def test_handle_bot_message(): msg = u"!echo Iñtërnâtiônàlizætiøn bot" event = {"bot_id": "1", "text": msg, "subtype": "bot_message"} hooks = limbo.init_plugins("test/plugins") server = limbo.FakeServer(hooks=hooks) eq_(limbo.handle_message(event, server), msg)
def test_handle_bot_message(): msg = u"!echo Iñtërnâtiônàlizætiøn bot" event = {"bot_id": "2", "text": msg, "subtype": "bot_message"} hooks = limbo.init_plugins("test/plugins") server = limbo.FakeServer(hooks=hooks) assert limbo.handle_message(event, server) == msg
def test_loop_hook(): hooks = limbo.init_plugins("test/plugins") server = limbo.FakeServer(hooks=hooks) slack = limbo.FakeSlack() metrics = limbo.NullMetrics() limbo.loop(server, metrics, test_loop=1) assert server._loop_plugin_ran == True assert metrics.count == 0
def test_handle_message_slack_user_nil(): msg = u"!echo Iñtërnâtiônàlizætiøn" event = {"user": "******", "text": msg} users = {"0": User("nobody", 0, "", 0)} hooks = limbo.init_plugins("test/plugins") slack = limbo.FakeSlack(users=users) server = limbo.FakeServer(slack=slack, hooks=hooks) assert limbo.handle_message(event, server) == u"!echo Iñtërnâtiônàlizætiøn"
def test_handle_message_handles_mention(): msg = u"!echo Iñtërnâtiônàlizætiøn <@1>" event = {"user": "******", "text": msg} config = {"needmention": "true"} slack = limbo.FakeSlack() hooks = limbo.init_plugins("test/plugins") server = limbo.FakeServer(config=config, slack=slack, hooks=hooks) assert limbo.handle_message(event, server) == msg
def test_handle_message_ignores_wrongmention(): msg = u"!echo Iñtërnâtiônàlizætiøn bot <@3>" event = {"bot_id": "2", "text": msg, "subtype": "bot_message"} config = {"needmention": "true"} slack = limbo.FakeSlack() hooks = limbo.init_plugins("test/plugins") server = limbo.FakeServer(config=config, slack=slack, hooks=hooks) assert limbo.handle_message(event, server) == None
def test_handle_delete_message(): oldmsg = u"!echo old message" event = {"type": "message", "subtype": "message_deleted", "previous_message": {"text": oldmsg, "user": "******"}} hooks = limbo.init_plugins("test/plugins") server = limbo.FakeServer(hooks=hooks) assert limbo.handle_message(event, server) == "Deleted: {}".format(oldmsg)
def test_handle_message_slack_user_nil(): msg = u"!echo Iñtërnâtiônàlizætiøn" event = {"user": "******", "text": msg} users = SearchList([User(None, "nobody", 0, "", 0)]) hooks = limbo.init_plugins("test/plugins") slack = limbo.FakeSlack(users=users) server = limbo.FakeServer(slack=slack, hooks=hooks) eq_(limbo.handle_message(event, server), None)
def test_handle_member_left(): event = { "type": "member_left_channel", "user": "******" } hooks = limbo.init_plugins("test/plugins") server = limbo.FakeServer(hooks=hooks) assert limbo.handle_event(event, server) == "user 2 left"
def test_handle_message_slack_user_nil(): msg = u"!echo Iñtërnâtiônàlizætiøn" event = {"user": "******", "text": msg} users = {"0": User(None, "nobody", 0, "", 0)} hooks = limbo.init_plugins("test/plugins") slack = limbo.FakeSlack(users=users) server = limbo.FakeServer(slack=slack, hooks=hooks) eq_(limbo.handle_message(event, server), None)
def test_handle_channel_join(): event = { "user": "******", "type": "message", "subtype": "channel_join", "text": "User has joined" } hooks = limbo.init_plugins("test/plugins") server = limbo.FakeServer(hooks=hooks) assert limbo.handle_message(event, server) == "saw user 2 join"
def test_handle_edit_message(): oldmsg = u"old message" newmsg = u"!echo new message" event = {"type": "message", "subtype": "message_changed", "previous_message": {"text": oldmsg}, "message": {"text": newmsg, "user": "******"}} hooks = limbo.init_plugins("test/plugins") server = limbo.FakeServer(hooks=hooks) assert limbo.handle_message(event, server) == "Changed: {}".format(newmsg)
def test_event_count(): msg = u"!echo Iñtërnâtiônàlizætiøn bot" oldmsg = u"old message" newmsg = u"!echo new message" events = [[{ "channel": "none", "user": "******", "text": msg }, { "channel": "none", "user": "******", "type": "message", "subtype": "channel_join", "text": "User has joined" }, { "channel": "none", "user": "******", "type": "member_left_channel" }, { "channel": "none", "type": "message", "subtype": "message_changed", "previous_message": { "text": oldmsg }, "message": { "text": newmsg, "user": "******" } }]] slack = limbo.FakeSlack(events=events) hooks = limbo.init_plugins("test/plugins") server = limbo.FakeServer(hooks=hooks, slack=slack) metrics = limbo.NullMetrics() limbo.loop(server, metrics, test_loop=1) assert metrics.count == 4
def test_plugin_invalid_dir(): try: limbo.init_plugins("invalid/package") except limbo.InvalidPluginDir: return 1 / 0
def test_missing_hook(): hooks = limbo.init_plugins("test/plugins") eq_(limbo.run_hook(hooks, "nonexistant", {"text": u"!echo bananas"}, None), [])
def test_plugin_logs(): mhdr = MockHandler() logging.getLogger("limbo.limbo").addHandler(mhdr) limbo.init_plugins("test/plugins") mhdr.check("debug", "attaching message hook for echo")
def test_config_plugin_doesnt_exist(): hooks = limbo.init_plugins("test/plugins", "doesnotexist") assert len(hooks) == 0
def test_config_plugin_doesnt_exist(): hooks = limbo.init_plugins("test/plugins", "doesnotexist") eq_(len(hooks), 0)
def test_run_hook(): hooks = limbo.init_plugins("test/plugins") eq_(limbo.run_hook(hooks, "message", {"text": u"!echo bananas"}, None), [u"!echo bananas"])