comparison tests/unit/test_ap-gateway.py @ 3826:81c79b7cafa7

tests (unit/ap-gateway): tests for XMPP identity/vCard4 <=> AP actor data conversion: rel 368
author Goffi <goffi@goffi.org>
date Wed, 29 Jun 2022 14:06:33 +0200
parents 04b57c0b2278
children 56720561f45f
comparison
equal deleted inserted replaced
3825:10a4846818e5 3826:81c79b7cafa7
23 23
24 import pytest 24 import pytest
25 from pytest_twisted import ensureDeferred as ed 25 from pytest_twisted import ensureDeferred as ed
26 from twisted.internet import defer 26 from twisted.internet import defer
27 from twisted.words.protocols.jabber import jid 27 from twisted.words.protocols.jabber import jid
28 from twisted.words.protocols.jabber.error import StanzaError
28 from twisted.web.server import Request 29 from twisted.web.server import Request
29 from twisted.words.xish import domish 30 from twisted.words.xish import domish
30 from wokkel import rsm, pubsub 31 from wokkel import rsm, pubsub
31 from treq.response import _Response as TReqResponse 32 from treq.response import _Response as TReqResponse
32 33
78 }, 79 },
79 "followers": f"{TEST_BASE_URL}/users/{TEST_USER}/followers", 80 "followers": f"{TEST_BASE_URL}/users/{TEST_USER}/followers",
80 "following": f"{TEST_BASE_URL}/users/{TEST_USER}/following", 81 "following": f"{TEST_BASE_URL}/users/{TEST_USER}/following",
81 "id": f"{TEST_BASE_URL}/users/{TEST_USER}", 82 "id": f"{TEST_BASE_URL}/users/{TEST_USER}",
82 "inbox": f"{TEST_BASE_URL}/users/{TEST_USER}/inbox", 83 "inbox": f"{TEST_BASE_URL}/users/{TEST_USER}/inbox",
83 "name": "", 84 "name": "test_user nickname",
85 "summary": "<p>test account</p>",
84 "outbox": f"{TEST_BASE_URL}/users/{TEST_USER}/outbox", 86 "outbox": f"{TEST_BASE_URL}/users/{TEST_USER}/outbox",
85 "preferredUsername": f"{TEST_USER}", 87 "preferredUsername": f"{TEST_USER}",
86 "type": "Person", 88 "type": "Person",
87 "url": f"{TEST_BASE_URL}/@{TEST_USER}" 89 "url": f"{TEST_BASE_URL}/@{TEST_USER}"
88 }, 90 },
365 367
366 async def mock_treq_json(data): 368 async def mock_treq_json(data):
367 return dict(data) 369 return dict(data)
368 370
369 371
370 async def mock_getItems(*args, **kwargs): 372 async def mock_getItems(client, service, node, *args, **kwargs):
373 """Mock getItems
374
375 special kwargs can be used:
376 ret_items (List[Domish.Element]): items to be returned, by default XMPP_ITEMS are
377 returned
378 tested_node (str): node for which items must be returned. If specified and a
379 different node is requested, "item-not-found" StanzaError will be raised
380 """
381 tested_node = kwargs.pop("tested_node", None)
382 if tested_node is not None and node != tested_node:
383 raise StanzaError("item-not-found")
371 ret_items = kwargs.pop("ret_items", XMPP_ITEMS) 384 ret_items = kwargs.pop("ret_items", XMPP_ITEMS)
372 rsm_resp = rsm.RSMResponse( 385 rsm_resp = rsm.RSMResponse(
373 first=ret_items[0]["id"], 386 first=ret_items[0]["id"],
374 last=ret_items[-1]["id"], 387 last=ret_items[-1]["id"],
375 index=0, 388 index=0,
405 gateway.base_ap_url = parse.urljoin( 418 gateway.base_ap_url = parse.urljoin(
406 f"https://{gateway.public_url}", 419 f"https://{gateway.public_url}",
407 f"{gateway.ap_path}/" 420 f"{gateway.ap_path}/"
408 ) 421 )
409 gateway.server = HTTPServer(gateway) 422 gateway.server = HTTPServer(gateway)
423 gateway.public_key_pem = None
410 return gateway 424 return gateway
411 425
412 426
413 class TestActivityPubGateway: 427 class TestActivityPubGateway:
414 428
1086 apply_to_elt = next(sent_elt.elements(NS_FASTEN, "apply-to")) 1100 apply_to_elt = next(sent_elt.elements(NS_FASTEN, "apply-to"))
1087 assert apply_to_elt["id"] == ap_item["id"] 1101 assert apply_to_elt["id"] == ap_item["id"]
1088 retract_elt = apply_to_elt.retract 1102 retract_elt = apply_to_elt.retract
1089 assert retract_elt is not None 1103 assert retract_elt is not None
1090 assert retract_elt.uri == NS_MESSAGE_RETRACT 1104 assert retract_elt.uri == NS_MESSAGE_RETRACT
1105
1106 @ed
1107 async def test_ap_actor_metadata_to_vcard(self, ap_gateway, monkeypatch):
1108 """AP actor metadata are converted to XMPP/vCard4"""
1109 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "get", mock_ap_get)
1110 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "json_content", mock_treq_json)
1111 monkeypatch.setattr(ap_gateway, "apGet", mock_ap_get)
1112
1113 items, __ = await ap_gateway.pubsub_service.items(
1114 jid.JID("toto@example.org"),
1115 ap_gateway.getLocalJIDFromAccount(TEST_AP_ACCOUNT),
1116 # VCard4 node
1117 ap_gateway._v.node,
1118 None,
1119 None,
1120 None
1121 )
1122 assert len(items) == 1
1123 vcard_elt = next(items[0].elements(ap_gateway._v.namespace, "vcard"))
1124 vcard = ap_gateway._v.vcard2Dict(vcard_elt)
1125 assert "test_user nickname" in vcard["nicknames"]
1126 assert vcard["description"] == "test account"
1127
1128 @ed
1129 async def test_identity_data_to_ap_actor_metadata(self, ap_gateway):
1130 """XMPP identity is converted to AP actor metadata"""
1131 # XXX: XMPP identity is normally an amalgam of metadata from several
1132 # XEPs/locations (vCard4, vcard-tmp, etc)
1133 with patch.object(ap_gateway._i, "getIdentity") as getIdentity:
1134 getIdentity.return_value = {
1135 "nicknames": ["nick1", "nick2"],
1136 "description": "test description"
1137 }
1138 actor_data = await ap_gateway.server.resource.APActorRequest(
1139 **self.ap_request_params(ap_gateway, ap_const.TYPE_ACTOR)
1140 )
1141
1142 # only the first nickname should be used
1143 assert actor_data["name"] == "nick1"
1144 assert actor_data["summary"] == "test description"