]> git.aero2k.de Git - urlbot-v3.git/commitdiff
fix stupid bugs
authorThorsten <mail@aero2k.de>
Thu, 26 Mar 2020 20:21:21 +0000 (21:21 +0100)
committerThorsten <mail@aero2k.de>
Thu, 26 Mar 2020 20:21:21 +0000 (21:21 +0100)
.gitignore
distbot/bot/bot.py
distbot/common/action.py
tests/test_integration/test_plugins.py [new file with mode: 0644]
tests/test_unit/test_amqp_routing_key.py [new file with mode: 0644]
tests/test_unit/test_urlresolver.py [moved from tests/test_urlresolver.py with 100% similarity]
tests/test_unit/test_urlresolver_plugin.py [new file with mode: 0644]

index 1991284eea58e186a0af6664a009cc89b3c1481c..b6e5ed3d5bf14e770657879293f3e5682625ef18 100644 (file)
@@ -1,4 +1,4 @@
-local_config.ini\r
-persistent_config.ini\r
+local_config.ini
+persistent_config.ini
 .idea
 distbot.egg-info
index e2ce04300a48b03361c4185089aada804e3fcc60..f47e8c9256e915e0894d432df29d0376774603f9 100644 (file)
@@ -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()
                 })
             )
 
index c754c0ab3e45da1b5021a91b5a656c83b6d083c4..cb4af523ab4db62f04b32d7fdc7aab9006148eb1 100644 (file)
@@ -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 (file)
index 0000000..3227ca2
--- /dev/null
@@ -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 (file)
index 0000000..87d67f5
--- /dev/null
@@ -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_unit/test_urlresolver_plugin.py b/tests/test_unit/test_urlresolver_plugin.py
new file mode 100644 (file)
index 0000000..8b4fa97
--- /dev/null
@@ -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