diff tests/unit/test_ap-gateway.py @ 3891:989df1047c3c

tests (AP gateway): reactions tests: 2 new tests check XMPP <=> AP reactions conversion rel 371
author Goffi <goffi@goffi.org>
date Wed, 31 Aug 2022 17:07:03 +0200
parents c129234a5d0b
children 755920bd30da
line wrap: on
line diff
--- a/tests/unit/test_ap-gateway.py	Wed Aug 31 17:07:03 2022 +0200
+++ b/tests/unit/test_ap-gateway.py	Wed Aug 31 17:07:03 2022 +0200
@@ -1588,3 +1588,120 @@
         assert parsed_item["from"] == si_client.jid.full()
         assert "noticed" in parsed_item
         assert parsed_item["noticed"]["noticed"] == True
+
+    @ed
+    async def test_xmpp_pubsub_reactions_to_ap(self, ap_gateway, monkeypatch):
+        """Pubsub-attachments ``reactions`` is converted to AP EmojiReact activity"""
+        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)
+
+        recipient_jid = ap_gateway.getLocalJIDFromAccount(TEST_AP_ACCOUNT)
+        # noticed item
+        ap_item = TEST_AP_ITEMS[0]
+        ap_item_url = xmpp_uri.buildXMPPUri(
+            "pubsub",
+            path=recipient_jid.full(),
+            node=ap_gateway._m.namespace,
+            item=ap_item["id"]
+        )
+        attachment_node = ap_gateway._pa.getAttachmentNodeName(
+            recipient_jid,
+            ap_gateway._m.namespace,
+            ap_item["id"]
+        )
+        reactions = ["🦁", "🥜", "🎻"]
+        item_elt =  xml_tools.parse(f"""
+            <item id="{TEST_JID.userhost()}" published="{TEST_JID.userhostJID()}">
+              <attachments xmlns="urn:xmpp:pubsub-attachments:1">
+                <reactions timestamp="2022-08-31T12:17:23Z">
+                    <reaction>{reactions[0]}</reaction>
+                    <reaction>{reactions[1]}</reaction>
+                    <reaction>{reactions[2]}</reaction>
+                </reactions>
+              </attachments>
+            </item>
+        """)
+        item_elt.uri = pubsub.NS_PUBSUB_EVENT
+        items_event = pubsub.ItemsEvent(
+            TEST_JID,
+            recipient_jid,
+            attachment_node,
+            [item_elt],
+            {}
+        )
+
+        with patch.object(ap_gateway, "signAndPost") as signAndPost:
+            signAndPost.return_value.code = 202
+            await ap_gateway._itemsReceived(
+                ap_gateway.client,
+                items_event
+            )
+
+        assert signAndPost.call_count == 3
+        for idx, call_args in enumerate(signAndPost.call_args_list):
+            url, actor_id, doc = call_args.args
+            assert url == TEST_USER_DATA["endpoints"]["sharedInbox"]
+            assert actor_id == ap_gateway.buildAPURL(
+                ap_const.TYPE_ACTOR, TEST_JID.userhost()
+            )
+            assert doc["type"] == "EmojiReact"
+            assert ap_const.NS_AP_PUBLIC in doc["cc"]
+            assert doc["object"] == ap_item["id"]
+            # reactions can be sent in random order (due to the use of set), thus we check
+            # if each reaction appear once, and that nothing is left after all calls are
+            # checked.
+            assert doc["content"] in reactions
+            reactions.remove(doc["content"])
+        assert len(reactions) == 0
+
+    @ed
+    async def test_ap_reactions_to_xmpp(self, ap_gateway, monkeypatch):
+        """AP EmojiReact activity is converted to ``reactions`` attachment"""
+        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)
+
+        xmpp_actor_id = ap_gateway.buildAPURL(ap_const.TYPE_ACTOR, TEST_JID.userhost())
+        # item on which reaction is attached
+        xmpp_item = XMPP_ITEMS[0]
+        xmpp_item_url = ap_gateway.buildAPURL(
+            ap_const.TYPE_ITEM, TEST_JID.userhost(), xmpp_item["id"]
+        )
+        like = {
+            "@context": "https://www.w3.org/ns/activitystreams",
+            "type": "EmojiReact",
+            "actor": TEST_AP_ACTOR_ID,
+            "cc": [
+                xmpp_actor_id,
+                TEST_USER_DATA["followers"]
+            ],
+            "id": "https://example.org/like/123",
+            "object": xmpp_item_url,
+            "content": "🐅",
+            "published": "2022-07-22T09:24:12Z",
+            "to": [ap_const.NS_AP_PUBLIC]
+        }
+        with patch.object(ap_gateway.host.memory.storage, "getItems") as getItems:
+            getItems.return_value = ([], {})
+            with patch.object(ap_gateway._p, "sendItems") as sendItems:
+                await ap_gateway.server.resource.APInboxRequest(
+                    **self.ap_request_params(
+                        ap_gateway,
+                        "inbox",
+                        doc=like,
+                        signing_actor=TEST_AP_ACTOR_ID
+                    )
+                )
+
+        assert sendItems.called
+        si_client, si_service, si_node, [si_item] = sendItems.call_args.args
+        assert si_client.jid == ap_gateway.getLocalJIDFromAccount(TEST_AP_ACCOUNT)
+        assert si_service == TEST_JID
+        assert si_node == ap_gateway._pa.getAttachmentNodeName(
+            TEST_JID, ap_gateway._m.namespace, xmpp_item["id"]
+        )
+        [parsed_item] = ap_gateway._pa.items2attachmentData(si_client, [si_item])
+        assert parsed_item["from"] == si_client.jid.full()
+        assert "reactions" in parsed_item
+        assert parsed_item["reactions"]["reactions"] == ["🐅"]