comparison 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
comparison
equal deleted inserted replaced
3890:c129234a5d0b 3891:989df1047c3c
1586 ) 1586 )
1587 [parsed_item] = ap_gateway._pa.items2attachmentData(si_client, [si_item]) 1587 [parsed_item] = ap_gateway._pa.items2attachmentData(si_client, [si_item])
1588 assert parsed_item["from"] == si_client.jid.full() 1588 assert parsed_item["from"] == si_client.jid.full()
1589 assert "noticed" in parsed_item 1589 assert "noticed" in parsed_item
1590 assert parsed_item["noticed"]["noticed"] == True 1590 assert parsed_item["noticed"]["noticed"] == True
1591
1592 @ed
1593 async def test_xmpp_pubsub_reactions_to_ap(self, ap_gateway, monkeypatch):
1594 """Pubsub-attachments ``reactions`` is converted to AP EmojiReact activity"""
1595 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "get", mock_ap_get)
1596 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "json_content", mock_treq_json)
1597 monkeypatch.setattr(ap_gateway, "apGet", mock_ap_get)
1598
1599 recipient_jid = ap_gateway.getLocalJIDFromAccount(TEST_AP_ACCOUNT)
1600 # noticed item
1601 ap_item = TEST_AP_ITEMS[0]
1602 ap_item_url = xmpp_uri.buildXMPPUri(
1603 "pubsub",
1604 path=recipient_jid.full(),
1605 node=ap_gateway._m.namespace,
1606 item=ap_item["id"]
1607 )
1608 attachment_node = ap_gateway._pa.getAttachmentNodeName(
1609 recipient_jid,
1610 ap_gateway._m.namespace,
1611 ap_item["id"]
1612 )
1613 reactions = ["🦁", "🥜", "🎻"]
1614 item_elt = xml_tools.parse(f"""
1615 <item id="{TEST_JID.userhost()}" published="{TEST_JID.userhostJID()}">
1616 <attachments xmlns="urn:xmpp:pubsub-attachments:1">
1617 <reactions timestamp="2022-08-31T12:17:23Z">
1618 <reaction>{reactions[0]}</reaction>
1619 <reaction>{reactions[1]}</reaction>
1620 <reaction>{reactions[2]}</reaction>
1621 </reactions>
1622 </attachments>
1623 </item>
1624 """)
1625 item_elt.uri = pubsub.NS_PUBSUB_EVENT
1626 items_event = pubsub.ItemsEvent(
1627 TEST_JID,
1628 recipient_jid,
1629 attachment_node,
1630 [item_elt],
1631 {}
1632 )
1633
1634 with patch.object(ap_gateway, "signAndPost") as signAndPost:
1635 signAndPost.return_value.code = 202
1636 await ap_gateway._itemsReceived(
1637 ap_gateway.client,
1638 items_event
1639 )
1640
1641 assert signAndPost.call_count == 3
1642 for idx, call_args in enumerate(signAndPost.call_args_list):
1643 url, actor_id, doc = call_args.args
1644 assert url == TEST_USER_DATA["endpoints"]["sharedInbox"]
1645 assert actor_id == ap_gateway.buildAPURL(
1646 ap_const.TYPE_ACTOR, TEST_JID.userhost()
1647 )
1648 assert doc["type"] == "EmojiReact"
1649 assert ap_const.NS_AP_PUBLIC in doc["cc"]
1650 assert doc["object"] == ap_item["id"]
1651 # reactions can be sent in random order (due to the use of set), thus we check
1652 # if each reaction appear once, and that nothing is left after all calls are
1653 # checked.
1654 assert doc["content"] in reactions
1655 reactions.remove(doc["content"])
1656 assert len(reactions) == 0
1657
1658 @ed
1659 async def test_ap_reactions_to_xmpp(self, ap_gateway, monkeypatch):
1660 """AP EmojiReact activity is converted to ``reactions`` attachment"""
1661 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "get", mock_ap_get)
1662 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "json_content", mock_treq_json)
1663 monkeypatch.setattr(ap_gateway, "apGet", mock_ap_get)
1664
1665 xmpp_actor_id = ap_gateway.buildAPURL(ap_const.TYPE_ACTOR, TEST_JID.userhost())
1666 # item on which reaction is attached
1667 xmpp_item = XMPP_ITEMS[0]
1668 xmpp_item_url = ap_gateway.buildAPURL(
1669 ap_const.TYPE_ITEM, TEST_JID.userhost(), xmpp_item["id"]
1670 )
1671 like = {
1672 "@context": "https://www.w3.org/ns/activitystreams",
1673 "type": "EmojiReact",
1674 "actor": TEST_AP_ACTOR_ID,
1675 "cc": [
1676 xmpp_actor_id,
1677 TEST_USER_DATA["followers"]
1678 ],
1679 "id": "https://example.org/like/123",
1680 "object": xmpp_item_url,
1681 "content": "🐅",
1682 "published": "2022-07-22T09:24:12Z",
1683 "to": [ap_const.NS_AP_PUBLIC]
1684 }
1685 with patch.object(ap_gateway.host.memory.storage, "getItems") as getItems:
1686 getItems.return_value = ([], {})
1687 with patch.object(ap_gateway._p, "sendItems") as sendItems:
1688 await ap_gateway.server.resource.APInboxRequest(
1689 **self.ap_request_params(
1690 ap_gateway,
1691 "inbox",
1692 doc=like,
1693 signing_actor=TEST_AP_ACTOR_ID
1694 )
1695 )
1696
1697 assert sendItems.called
1698 si_client, si_service, si_node, [si_item] = sendItems.call_args.args
1699 assert si_client.jid == ap_gateway.getLocalJIDFromAccount(TEST_AP_ACCOUNT)
1700 assert si_service == TEST_JID
1701 assert si_node == ap_gateway._pa.getAttachmentNodeName(
1702 TEST_JID, ap_gateway._m.namespace, xmpp_item["id"]
1703 )
1704 [parsed_item] = ap_gateway._pa.items2attachmentData(si_client, [si_item])
1705 assert parsed_item["from"] == si_client.jid.full()
1706 assert "reactions" in parsed_item
1707 assert parsed_item["reactions"]["reactions"] == ["🐅"]