comparison tests/unit/test_ap-gateway.py @ 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 fedbf7aade11
children 39fc2e1b3793
comparison
equal deleted inserted replaced
3784:efc34a89e70b 3785:0b54be42d0aa
814 first_page = followers["first"] 814 first_page = followers["first"]
815 assert first_page["type"] == "OrderedCollectionPage" 815 assert first_page["type"] == "OrderedCollectionPage"
816 assert len(first_page["orderedItems"]) == len(subscribers) 816 assert len(first_page["orderedItems"]) == len(subscribers)
817 items = first_page["orderedItems"] 817 items = first_page["orderedItems"]
818 assert items == ['local_user@test.example', 'ext_user@example.org'] 818 assert items == ['local_user@test.example', 'ext_user@example.org']
819
820 @ed
821 async def test_xmpp_message_to_ap_direct_message(self, ap_gateway, monkeypatch):
822 """XMPP message are sent as AP direct message"""
823 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "get", mock_ap_get)
824 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "json_content", mock_treq_json)
825 monkeypatch.setattr(ap_gateway, "apGet", mock_ap_get)
826 message_elt = domish.Element((None, "message"))
827 message_elt.addElement("body", content="This is a test message.")
828 message_elt["from"] = TEST_JID.full()
829 message_elt["to"] = ap_gateway.getLocalJIDFromAccount(TEST_AP_ACCOUNT).full()
830 with patch.object(ap_gateway, "signAndPost") as signAndPost:
831 await ap_gateway.onMessage(message_elt)
832 url, actor_id, doc = signAndPost.call_args[0]
833 assert url == "https://example.org/users/test_user/inbox"
834 assert actor_id == "https://test.example/_ap/actor/some_user%40test.example"
835 obj = doc["object"]
836 assert doc["@context"] == "https://www.w3.org/ns/activitystreams"
837 assert doc["actor"] == "https://test.example/_ap/actor/some_user%40test.example"
838 assert obj["type"] == "Note"
839 assert obj["content"] == "This is a test message."
840 assert obj["attributedTo"] == (
841 "https://test.example/_ap/actor/some_user%40test.example"
842 )
843 # we must have a direct message, thus the item must be only addressed to destinee
844 # ("to" attribute of the message), and the "Public" namespace must not be set
845 assert doc["to"] == ["https://example.org/users/test_user"]
846 assert obj["to"] == ["https://example.org/users/test_user"]
847 for field in ("bto", "cc", "bcc", "audience"):
848 assert field not in doc
849 assert field not in obj
850
851 @ed
852 async def test_ap_direct_message_to_xmpp_message(self, ap_gateway, monkeypatch):
853 """AP direct message are sent as XMPP message (not Pubsub)"""
854 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "get", mock_ap_get)
855 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "json_content", mock_treq_json)
856 monkeypatch.setattr(ap_gateway, "apGet", mock_ap_get)
857 # we have to patch DeferredList to not wait forever
858 monkeypatch.setattr(defer, "DeferredList", AsyncMock())
859
860 xmpp_actor_id = ap_gateway.buildAPURL(ap_const.TYPE_ACTOR, TEST_JID.userhost())
861 direct_ap_message = {
862 'attributedTo': TEST_AP_ACTOR_ID,
863 'cc': [],
864 'content': '<p>test direct message</p>',
865 'contentMap': {'en': '<p>test direct message</p>'},
866 'id': f'{TEST_AP_ACTOR_ID}/statuses/123',
867 'published': '2022-05-20T08:14:39Z',
868 'to': [xmpp_actor_id],
869 'type': 'Note',
870 }
871 client = ap_gateway.client.getVirtualClient(
872 ap_gateway.getLocalJIDFromAccount(TEST_AP_ACCOUNT)
873 )
874 with patch.object(client, "sendMessage") as sendMessage:
875 await ap_gateway.newAPItem(
876 client, None, ap_gateway._m.namespace, direct_ap_message
877 )
878
879 # sendMessage must be called for <message> stanza, and the "message" argument must
880 # be set to the content of the original AP message
881 assert sendMessage.called
882 assert sendMessage.call_args.args[0] == TEST_JID
883 assert sendMessage.call_args.args[1] == {"": "test direct message"}