# HG changeset patch # User Goffi # Date 1653407859 -7200 # Node ID 0b54be42d0aaee684a35281dd887a79ad3f4d3b9 # Parent efc34a89e70bc52008abb6f29e467b6cdaaff5b3 test (unit/AP gateway): AP direct message ↔ XMPP `` conversion: test that AP direct messages are converted to XMPP `` stanzas instead of pubsub items. test that XMPP `` stanzas are converted to AP direct messages (i.e. items addressed solely to the recipient, without *followers* or *public* collections). rel 366 diff -r efc34a89e70b -r 0b54be42d0aa tests/unit/test_ap-gateway.py --- 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': '

test direct message

', + 'contentMap': {'en': '

test direct message

'}, + '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 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"}