changeset 3785:0b54be42d0aa

test (unit/AP gateway): AP direct message ↔ XMPP `<message>` conversion: test that AP direct messages are converted to XMPP `<message>` stanzas instead of pubsub items. test that XMPP `<message>` stanzas are converted to AP direct messages (i.e. items addressed solely to the recipient, without *followers* or *public* collections). rel 366
author Goffi <goffi@goffi.org>
date Tue, 24 May 2022 17:57:39 +0200
parents efc34a89e70b
children cebfdfff3e99
files tests/unit/test_ap-gateway.py
diffstat 1 files changed, 65 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/tests/unit/test_ap-gateway.py	Tue May 24 17:57:36 2022 +0200
+++ b/tests/unit/test_ap-gateway.py	Tue May 24 17:57:39 2022 +0200
@@ -816,3 +816,68 @@
         assert len(first_page["orderedItems"]) == len(subscribers)
         items = first_page["orderedItems"]
         assert items == ['local_user@test.example', 'ext_user@example.org']
+
+    @ed
+    async def test_xmpp_message_to_ap_direct_message(self, ap_gateway, monkeypatch):
+        """XMPP message are sent as AP direct message"""
+        monkeypatch.setattr(plugin_comp_ap_gateway.treq, "get", mock_ap_get)
+        monkeypatch.setattr(plugin_comp_ap_gateway.treq, "json_content", mock_treq_json)
+        monkeypatch.setattr(ap_gateway, "apGet", mock_ap_get)
+        message_elt = domish.Element((None, "message"))
+        message_elt.addElement("body", content="This is a test message.")
+        message_elt["from"] = TEST_JID.full()
+        message_elt["to"] = ap_gateway.getLocalJIDFromAccount(TEST_AP_ACCOUNT).full()
+        with patch.object(ap_gateway, "signAndPost") as signAndPost:
+            await ap_gateway.onMessage(message_elt)
+            url, actor_id, doc = signAndPost.call_args[0]
+        assert url == "https://example.org/users/test_user/inbox"
+        assert actor_id == "https://test.example/_ap/actor/some_user%40test.example"
+        obj = doc["object"]
+        assert doc["@context"] == "https://www.w3.org/ns/activitystreams"
+        assert doc["actor"] == "https://test.example/_ap/actor/some_user%40test.example"
+        assert obj["type"] == "Note"
+        assert obj["content"] == "This is a test message."
+        assert obj["attributedTo"] == (
+            "https://test.example/_ap/actor/some_user%40test.example"
+        )
+        # we must have a direct message, thus the item must be only addressed to destinee
+        # ("to" attribute of the message), and the "Public" namespace must not be set
+        assert doc["to"] == ["https://example.org/users/test_user"]
+        assert obj["to"] == ["https://example.org/users/test_user"]
+        for field in ("bto", "cc", "bcc", "audience"):
+            assert field not in doc
+            assert field not in obj
+
+    @ed
+    async def test_ap_direct_message_to_xmpp_message(self, ap_gateway, monkeypatch):
+        """AP direct message are sent as XMPP message (not Pubsub)"""
+        monkeypatch.setattr(plugin_comp_ap_gateway.treq, "get", mock_ap_get)
+        monkeypatch.setattr(plugin_comp_ap_gateway.treq, "json_content", mock_treq_json)
+        monkeypatch.setattr(ap_gateway, "apGet", mock_ap_get)
+        # we have to patch DeferredList to not wait forever
+        monkeypatch.setattr(defer, "DeferredList", AsyncMock())
+
+        xmpp_actor_id = ap_gateway.buildAPURL(ap_const.TYPE_ACTOR, TEST_JID.userhost())
+        direct_ap_message = {
+            'attributedTo': TEST_AP_ACTOR_ID,
+            'cc': [],
+            'content': '<p>test direct message</p>',
+            'contentMap': {'en': '<p>test direct message</p>'},
+            'id': f'{TEST_AP_ACTOR_ID}/statuses/123',
+            'published': '2022-05-20T08:14:39Z',
+            'to': [xmpp_actor_id],
+            'type': 'Note',
+        }
+        client = ap_gateway.client.getVirtualClient(
+            ap_gateway.getLocalJIDFromAccount(TEST_AP_ACCOUNT)
+        )
+        with patch.object(client, "sendMessage") as sendMessage:
+            await ap_gateway.newAPItem(
+                client, None, ap_gateway._m.namespace, direct_ap_message
+            )
+
+        # sendMessage must be called for <message> stanza, and the "message" argument must
+        # be set to the content of the original AP message
+        assert sendMessage.called
+        assert sendMessage.call_args.args[0] == TEST_JID
+        assert sendMessage.call_args.args[1] == {"": "test direct message"}