diff --git a/tests/unit/test_ap-gateway.py b/tests/unit/test_ap-gateway.py index e8cc3d3b..ce9598e0 100644 --- a/tests/unit/test_ap-gateway.py +++ b/tests/unit/test_ap-gateway.py @@ -816,3 +816,68 @@ class TestActivityPubGateway: 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