comparison tests/unit/test_ap-gateway.py @ 3890:c129234a5d0b

tests (AP gateway): fix tests following changes in gateway: - `shareInbox` is now used when available, thus tests now check for that - `client._ap_storage` is used, thus is added to the mockClient `ap_const.NS_AP_PUBLIC` is now in `cc` instead of `to` field rel 371
author Goffi <goffi@goffi.org>
date Wed, 31 Aug 2022 17:07:03 +0200
parents 8c01d8ab9447
children 989df1047c3c
comparison
equal deleted inserted replaced
3889:1ab5fb468a41 3890:c129234a5d0b
410 fake_cached_node = MagicMock() 410 fake_cached_node = MagicMock()
411 fake_cached_node.subscriptions = [] 411 fake_cached_node.subscriptions = []
412 return fake_cached_node 412 return fake_cached_node
413 413
414 414
415 def getVirtualClient(jid): 415 def mockClient(jid):
416 client = MagicMock() 416 client = MagicMock()
417 client.jid = jid 417 client.jid = jid
418 client.host = "test.example"
419 client._ap_storage.get = AsyncMock()
420 client._ap_storage.aset = AsyncMock()
418 return client 421 return client
422
423
424 def getVirtualClient(jid):
425 return mockClient(jid)
419 426
420 427
421 class FakeTReqPostResponse: 428 class FakeTReqPostResponse:
422 code = 202 429 code = 202
423 430
426 def ap_gateway(host): 433 def ap_gateway(host):
427 gateway = plugin_comp_ap_gateway.APGateway(host) 434 gateway = plugin_comp_ap_gateway.APGateway(host)
428 gateway.initialised = True 435 gateway.initialised = True
429 gateway.isPubsub = AsyncMock() 436 gateway.isPubsub = AsyncMock()
430 gateway.isPubsub.return_value = False 437 gateway.isPubsub.return_value = False
431 client = MagicMock() 438 client = mockClient(jid.JID("ap.test.example"))
432 client.jid = jid.JID("ap.test.example")
433 client.host = "test.example"
434 client.getVirtualClient = getVirtualClient 439 client.getVirtualClient = getVirtualClient
435 gateway.client = client 440 gateway.client = client
436 gateway.local_only = True 441 gateway.local_only = True
437 gateway.public_url = PUBLIC_URL 442 gateway.public_url = PUBLIC_URL
438 gateway.ap_path = '_ap' 443 gateway.ap_path = '_ap'
965 # we simulate the reception of a retract event 970 # we simulate the reception of a retract event
966 await ap_gateway._itemsReceived(ap_gateway.client, items_event) 971 await ap_gateway._itemsReceived(ap_gateway.client, items_event)
967 url, actor_id, doc = signAndPost.call_args[0] 972 url, actor_id, doc = signAndPost.call_args[0]
968 jid_account = await ap_gateway.getAPAccountFromJidAndNode(TEST_JID, None) 973 jid_account = await ap_gateway.getAPAccountFromJidAndNode(TEST_JID, None)
969 jid_actor_id = ap_gateway.buildAPURL(ap_const.TYPE_ACTOR, jid_account) 974 jid_actor_id = ap_gateway.buildAPURL(ap_const.TYPE_ACTOR, jid_account)
970 assert url == f"{TEST_BASE_URL}/users/{TEST_USER}/inbox" 975 assert url == f"{TEST_BASE_URL}/inbox"
971 assert actor_id == jid_actor_id 976 assert actor_id == jid_actor_id
972 assert doc["type"] == "Delete" 977 assert doc["type"] == "Delete"
973 assert doc["actor"] == jid_actor_id 978 assert doc["actor"] == jid_actor_id
974 obj = doc["object"] 979 obj = doc["object"]
975 assert obj["type"] == ap_const.TYPE_TOMBSTONE 980 assert obj["type"] == ap_const.TYPE_TOMBSTONE
1422 [item_elt] 1427 [item_elt]
1423 ) 1428 )
1424 1429
1425 assert signAndPost.called 1430 assert signAndPost.called
1426 url, actor_id, doc = signAndPost.call_args.args 1431 url, actor_id, doc = signAndPost.call_args.args
1427 assert url == TEST_USER_DATA["inbox"] 1432 assert url == TEST_USER_DATA["endpoints"]["sharedInbox"]
1428 assert actor_id == ap_gateway.buildAPURL(ap_const.TYPE_ACTOR, TEST_JID.userhost()) 1433 assert actor_id == ap_gateway.buildAPURL(ap_const.TYPE_ACTOR, TEST_JID.userhost())
1429 assert doc["type"] == "Announce" 1434 assert doc["type"] == "Announce"
1430 assert ap_const.NS_AP_PUBLIC in doc["to"] 1435 assert ap_const.NS_AP_PUBLIC in doc["to"]
1431 assert doc["object"] == ap_item["id"] 1436 assert doc["object"] == ap_item["id"]
1432 1437
1496 monkeypatch.setattr(ap_gateway, "apGet", mock_ap_get) 1501 monkeypatch.setattr(ap_gateway, "apGet", mock_ap_get)
1497 1502
1498 recipient_jid = ap_gateway.getLocalJIDFromAccount(TEST_AP_ACCOUNT) 1503 recipient_jid = ap_gateway.getLocalJIDFromAccount(TEST_AP_ACCOUNT)
1499 # noticed item 1504 # noticed item
1500 ap_item = TEST_AP_ITEMS[0] 1505 ap_item = TEST_AP_ITEMS[0]
1501 ap_item_url = xmpp_uri.buildXMPPUri(
1502 "pubsub",
1503 path=recipient_jid.full(),
1504 node=ap_gateway._m.namespace,
1505 item=ap_item["id"]
1506 )
1507 attachment_node = ap_gateway._pa.getAttachmentNodeName( 1506 attachment_node = ap_gateway._pa.getAttachmentNodeName(
1508 recipient_jid, 1507 recipient_jid,
1509 ap_gateway._m.namespace, 1508 ap_gateway._m.namespace,
1510 ap_item["id"] 1509 ap_item["id"]
1511 ) 1510 )
1512 item_elt = xml_tools.parse(f""" 1511 item_elt = xml_tools.parse(f"""
1513 <item id="{TEST_JID.userhost()}" published="{TEST_JID.userhostJID()}"> 1512 <item id="{TEST_JID.userhost()}" published="{TEST_JID.userhostJID()}">
1514 <attachments xmlns="urn:xmpp:pubsub-attachments:0"> 1513 <attachments xmlns="urn:xmpp:pubsub-attachments:1">
1515 <noticed timestamp="2022-07-22T12:29:45Z"/> 1514 <noticed timestamp="2022-07-22T12:29:45Z"/>
1516 </attachments> 1515 </attachments>
1517 </item> 1516 </item>
1518 """) 1517 """)
1519 item_elt.uri = pubsub.NS_PUBSUB_EVENT 1518 item_elt.uri = pubsub.NS_PUBSUB_EVENT
1532 items_event 1531 items_event
1533 ) 1532 )
1534 1533
1535 assert signAndPost.called 1534 assert signAndPost.called
1536 url, actor_id, doc = signAndPost.call_args.args 1535 url, actor_id, doc = signAndPost.call_args.args
1537 assert url == TEST_USER_DATA["inbox"] 1536 assert url == TEST_USER_DATA["endpoints"]["sharedInbox"]
1538 assert actor_id == ap_gateway.buildAPURL(ap_const.TYPE_ACTOR, TEST_JID.userhost()) 1537 assert actor_id == ap_gateway.buildAPURL(ap_const.TYPE_ACTOR, TEST_JID.userhost())
1539 assert doc["type"] == "Like" 1538 assert doc["type"] == "Like"
1540 assert ap_const.NS_AP_PUBLIC in doc["to"] 1539 assert ap_const.NS_AP_PUBLIC in doc["cc"]
1541 assert doc["object"] == ap_item["id"] 1540 assert doc["object"] == ap_item["id"]
1542 1541
1543 @ed 1542 @ed
1544 async def test_ap_like_to_xmpp_noticed_attachment(self, ap_gateway, monkeypatch): 1543 async def test_ap_like_to_xmpp_noticed_attachment(self, ap_gateway, monkeypatch):
1545 """AP Like activity is converted to ``noticed`` attachment""" 1544 """AP Like activity is converted to ``noticed`` attachment"""