From f6b08cb26bc28e4c456bb5aaa97cae6e35d290da Mon Sep 17 00:00:00 2001 From: Thorsten Date: Thu, 26 Mar 2020 21:21:21 +0100 Subject: [PATCH] fix stupid bugs --- .gitignore | 4 ++-- distbot/bot/bot.py | 15 ++++++++------ distbot/common/action.py | 6 ++++++ tests/test_integration/test_plugins.py | 9 +++++++++ tests/test_unit/test_amqp_routing_key.py | 23 ++++++++++++++++++++++ tests/{ => test_unit}/test_urlresolver.py | 0 tests/test_unit/test_urlresolver_plugin.py | 23 ++++++++++++++++++++++ 7 files changed, 72 insertions(+), 8 deletions(-) create mode 100644 tests/test_integration/test_plugins.py create mode 100644 tests/test_unit/test_amqp_routing_key.py rename tests/{ => test_unit}/test_urlresolver.py (100%) create mode 100644 tests/test_unit/test_urlresolver_plugin.py diff --git a/.gitignore b/.gitignore index 1991284..b6e5ed3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -local_config.ini -persistent_config.ini +local_config.ini +persistent_config.ini .idea distbot.egg-info diff --git a/distbot/bot/bot.py b/distbot/bot/bot.py index e2ce043..f47e8c9 100644 --- a/distbot/bot/bot.py +++ b/distbot/bot/bot.py @@ -107,23 +107,27 @@ class Bot(sleekxmpp.ClientXMPP): }) ) - def get_amqp_routing_key(self, msg): + @staticmethod + def get_amqp_routing_key(nick, msg): # simplify the key significantly + offset = 0 key = shlex.split(re.sub(r'[^a-zäöüß0-9 "\']', '', msg["body"].lower())) if not key: # simple dots, smilies, emoji shit, pass through key = shlex.split(msg["body"].lower()) # cut the nick from the message - if self.nick.lower() in key[0].lower(): + if nick.lower() in key[0].lower(): if msg["body"][0] == '>': key.pop(0) key.insert(0, "quoted_nick") else: key.pop(0) key.insert(0, "nick") + offset = msg["body"].find(nick) + len(nick) + 1 routing_key = '.'.join(key).encode("UTF-8")[:255] - return routing_key + + return offset, routing_key def message(self, msg): logger.debug("msg is " + str(msg)) @@ -141,20 +145,19 @@ class Bot(sleekxmpp.ClientXMPP): self.initialize_actionthreads() self.echo("Worker One available for master.") - routing_key = self.get_amqp_routing_key(msg) + nick_offset, routing_key = self.get_amqp_routing_key(self.nick, msg) if msg["type"] == "groupchat": recipient = msg["mucroom"] else: recipient = msg["from"].jid - offset = msg["body"].find(self.nick) + len(self.nick) + 1 process_message( routing_key=routing_key, body=json.dumps({ "from": msg["from"].jid, "to": recipient, - "body": msg["body"][offset:].strip() + "body": msg["body"][nick_offset:].strip() }) ) diff --git a/distbot/common/action.py b/distbot/common/action.py index c754c0a..cb4af52 100644 --- a/distbot/common/action.py +++ b/distbot/common/action.py @@ -61,6 +61,12 @@ class Action: action.event = Action.deserialize(action.event) return action + def __eq__(self, other): + if isinstance(other, Action): + return self.serializable == other.serializable + else: + return False + def send_action(actionqueue, action): connection = pika.BlockingConnection( diff --git a/tests/test_integration/test_plugins.py b/tests/test_integration/test_plugins.py new file mode 100644 index 0000000..3227ca2 --- /dev/null +++ b/tests/test_integration/test_plugins.py @@ -0,0 +1,9 @@ +# -*- coding: utf-8 -*- + + +def message(): + return {"body": "message in a bottle"} + + +def test_pipeline(): + pass diff --git a/tests/test_unit/test_amqp_routing_key.py b/tests/test_unit/test_amqp_routing_key.py new file mode 100644 index 0000000..87d67f5 --- /dev/null +++ b/tests/test_unit/test_amqp_routing_key.py @@ -0,0 +1,23 @@ +# -*- coding: utf-8 -*- +import pytest + +from distbot.bot.bot import Bot + + +@pytest.mark.parametrize( + argnames='nick, message, expected_key, expected_offset', + argvalues=[ + ("schrottbot", "schrottbot make me happy", b"nick.make.me.happy", 11), + ("schrottbot", "schrottbot, make me happy", b"nick.make.me.happy", 11), + ("schrottbot", "schrottbot: make me happy", b"nick.make.me.happy", 11), + ("schrottbot", "> schrottbot: make me happy", b"quoted_nick.make.me.happy", 13), + ("schrottbot", "> schrottbot, make me happy", b"quoted_nick.make.me.happy", 13), + ("schrottbot", "> schrottbot make me happy", b"quoted_nick.make.me.happy", 13), + ("schrottbot", "https://docs.pytest.org/en/latest/warnings.html", b"httpsdocspytestorgenlatestwarningshtml", 0), + ] +) +def test_get_amqp_routing_key(nick, message, expected_key, expected_offset): + msgobj = {"body": message} + offset, key = Bot.get_amqp_routing_key(nick, msgobj) + assert offset == expected_offset + assert key == expected_key diff --git a/tests/test_urlresolver.py b/tests/test_unit/test_urlresolver.py similarity index 100% rename from tests/test_urlresolver.py rename to tests/test_unit/test_urlresolver.py diff --git a/tests/test_unit/test_urlresolver_plugin.py b/tests/test_unit/test_urlresolver_plugin.py new file mode 100644 index 0000000..8b4fa97 --- /dev/null +++ b/tests/test_unit/test_urlresolver_plugin.py @@ -0,0 +1,23 @@ +# -*- coding: utf-8 -*- +from distbot.bot.worker import Worker + +import pytest + +from distbot.common.action import Action + + +@pytest.mark.parametrize( + argnames='message, expected_action', + argvalues=[ + ('http://debianforum.de', Action(msg="Title: Trollspielwiese")), + ] +) +def test_urlresolver(monkeypatch, message, expected_action): + monkeypatch.setattr(Worker, "register_plugin", lambda x: None) + import distbot.plugins.url as url + monkeypatch.setattr(url, "extract_title", lambda x: "Trollspielwiese") + + worker = url.URLResolver("_") + + result = worker.parse_body(msg={"body": message}) + assert result == expected_action -- 2.39.2