comparison tests/unit/test_ap-gateway.py @ 4259:49019947cc76

component AP Gateway: implement HTTP GET signature.
author Goffi <goffi@goffi.org>
date Wed, 05 Jun 2024 22:34:09 +0200
parents 716dd791be46
children
comparison
equal deleted inserted replaced
4258:ba28ca268f4a 4259:49019947cc76
337 # request on an item ID must return the ID 337 # request on an item ID must return the ID
338 for item in TEST_AP_ITEMS: 338 for item in TEST_AP_ITEMS:
339 AP_REQUESTS[item["id"]] = item 339 AP_REQUESTS[item["id"]] = item
340 340
341 341
342 async def mock_ap_get(url): 342 async def mock_treq_get(url):
343 return deepcopy(AP_REQUESTS[url])
344
345
346 async def mock_ap_get(url, requestor_actor_id):
343 return deepcopy(AP_REQUESTS[url]) 347 return deepcopy(AP_REQUESTS[url])
344 348
345 349
346 async def mock_treq_json(data): 350 async def mock_treq_json(data):
347 return dict(data) 351 return dict(data)
512 ) == (jid.JID("pubsub.test.example"), "urn:xmpp:microblog:0") 516 ) == (jid.JID("pubsub.test.example"), "urn:xmpp:microblog:0")
513 517
514 @ed 518 @ed
515 async def test_ap_to_pubsub_conversion(self, ap_gateway, monkeypatch): 519 async def test_ap_to_pubsub_conversion(self, ap_gateway, monkeypatch):
516 """AP requests are converted to pubsub""" 520 """AP requests are converted to pubsub"""
517 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "get", mock_ap_get) 521 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "get", mock_treq_get)
518 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "json_content", mock_treq_json) 522 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "json_content", mock_treq_json)
519 monkeypatch.setattr(ap_gateway, "ap_get", mock_ap_get) 523 monkeypatch.setattr(ap_gateway, "ap_get", mock_ap_get)
520 524
521 actor_data = await ap_gateway.get_ap_actor_data_from_account(TEST_AP_ACCOUNT) 525 actor_data = await ap_gateway.get_ap_actor_data_from_account(
522 outbox = await ap_gateway.ap_get_object(actor_data, "outbox") 526 TEST_AP_ACTOR_ID, TEST_AP_ACCOUNT
523 items, rsm_resp = await ap_gateway.get_ap_items(outbox, 2) 527 )
528 outbox = await ap_gateway.ap_get_object(TEST_AP_ACTOR_ID, actor_data, "outbox")
529 items, rsm_resp = await ap_gateway.get_ap_items(TEST_AP_ACTOR_ID, outbox, 2)
524 530
525 assert rsm_resp.count == 4 531 assert rsm_resp.count == 4
526 assert rsm_resp.index == 0 532 assert rsm_resp.index == 0
527 assert rsm_resp.first == "https://example.org/users/test_user/statuses/4" 533 assert rsm_resp.first == "https://example.org/users/test_user/statuses/4"
528 assert rsm_resp.last == "https://example.org/users/test_user/statuses/3" 534 assert rsm_resp.last == "https://example.org/users/test_user/statuses/3"
550 ) 556 )
551 assert author_uri == "xmpp:test_user\\40example.org@ap.test.example" 557 assert author_uri == "xmpp:test_user\\40example.org@ap.test.example"
552 assert str(items[1].entry.published) == "2021-12-16T17:26:03Z" 558 assert str(items[1].entry.published) == "2021-12-16T17:26:03Z"
553 559
554 items, rsm_resp = await ap_gateway.get_ap_items( 560 items, rsm_resp = await ap_gateway.get_ap_items(
561 TEST_AP_ACTOR_ID,
555 outbox, 562 outbox,
556 max_items=2, 563 max_items=2,
557 after_id="https://example.org/users/test_user/statuses/3", 564 after_id="https://example.org/users/test_user/statuses/3",
558 ) 565 )
559 566
584 [e for e in items[1].entry.author.elements() if e.name == "uri"][0] 591 [e for e in items[1].entry.author.elements() if e.name == "uri"][0]
585 ) 592 )
586 assert author_uri == "xmpp:test_user\\40example.org@ap.test.example" 593 assert author_uri == "xmpp:test_user\\40example.org@ap.test.example"
587 assert str(items[1].entry.published) == "2021-12-16T17:28:03Z" 594 assert str(items[1].entry.published) == "2021-12-16T17:28:03Z"
588 595
589 items, rsm_resp = await ap_gateway.get_ap_items(outbox, max_items=1, start_index=2) 596 items, rsm_resp = await ap_gateway.get_ap_items(
597 TEST_AP_ACTOR_ID, outbox, max_items=1, start_index=2
598 )
590 599
591 assert rsm_resp.count == 4 600 assert rsm_resp.count == 4
592 assert rsm_resp.index == 2 601 assert rsm_resp.index == 2
593 assert rsm_resp.first == "https://example.org/users/test_user/statuses/2" 602 assert rsm_resp.first == "https://example.org/users/test_user/statuses/2"
594 assert rsm_resp.last == "https://example.org/users/test_user/statuses/2" 603 assert rsm_resp.last == "https://example.org/users/test_user/statuses/2"
601 "</title>" 610 "</title>"
602 ) 611 )
603 assert str(items[0].entry.published) == "2021-12-16T17:27:03Z" 612 assert str(items[0].entry.published) == "2021-12-16T17:27:03Z"
604 613
605 items, rsm_resp = await ap_gateway.get_ap_items( 614 items, rsm_resp = await ap_gateway.get_ap_items(
606 outbox, max_items=3, chronological_pagination=False 615 TEST_AP_ACTOR_ID, outbox, max_items=3, chronological_pagination=False
607 ) 616 )
608 assert rsm_resp.count == 4 617 assert rsm_resp.count == 4
609 assert rsm_resp.index == 1 618 assert rsm_resp.index == 1
610 assert rsm_resp.first == "https://example.org/users/test_user/statuses/3" 619 assert rsm_resp.first == "https://example.org/users/test_user/statuses/3"
611 assert rsm_resp.last == "https://example.org/users/test_user/statuses/1" 620 assert rsm_resp.last == "https://example.org/users/test_user/statuses/1"
668 "node": None, 677 "node": None,
669 "ap_account": test_jid.full(), 678 "ap_account": test_jid.full(),
670 "ap_url": ap_url, 679 "ap_url": ap_url,
671 "signing_actor": signing_actor, 680 "signing_actor": signing_actor,
672 } 681 }
682 if type_ not in (ap_const.TYPE_ACTOR, "outbox", "following", "followers"):
683 kwargs["requestor_actor_id"] = TEST_AP_ACTOR_ID
673 if type_ == "outbox" and query_data: 684 if type_ == "outbox" and query_data:
674 kwargs["query_data"] = query_data 685 kwargs["query_data"] = query_data
675 # signing_actor is not used for page requests 686 # signing_actor is not used for page requests
676 del kwargs["signing_actor"] 687 del kwargs["signing_actor"]
677 return kwargs 688 return kwargs
720 assert first_item_obj["to"] == ["https://www.w3.org/ns/activitystreams#Public"] 731 assert first_item_obj["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
721 732
722 @ed 733 @ed
723 async def test_following_to_pps(self, ap_gateway, monkeypatch): 734 async def test_following_to_pps(self, ap_gateway, monkeypatch):
724 """AP following items are converted to Public Pubsub Subscription subscriptions""" 735 """AP following items are converted to Public Pubsub Subscription subscriptions"""
725 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "get", mock_ap_get) 736 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "get", mock_treq_get)
726 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "json_content", mock_treq_json) 737 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "json_content", mock_treq_json)
727 monkeypatch.setattr(ap_gateway, "ap_get", mock_ap_get) 738 monkeypatch.setattr(ap_gateway, "ap_get", mock_ap_get)
728 739
729 items, __ = await ap_gateway.pubsub_service.items( 740 items, __ = await ap_gateway.pubsub_service.items(
730 jid.JID("toto@example.org"), 741 jid.JID("toto@example.org"),
744 assert subscription_elt["service"] == entity 755 assert subscription_elt["service"] == entity
745 756
746 @ed 757 @ed
747 async def test_followers_to_pps(self, ap_gateway, monkeypatch): 758 async def test_followers_to_pps(self, ap_gateway, monkeypatch):
748 """AP followers items are converted to Public Pubsub Subscription subscribers""" 759 """AP followers items are converted to Public Pubsub Subscription subscribers"""
749 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "get", mock_ap_get) 760 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "get", mock_treq_get)
750 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "json_content", mock_treq_json) 761 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "json_content", mock_treq_json)
751 monkeypatch.setattr(ap_gateway, "ap_get", mock_ap_get) 762 monkeypatch.setattr(ap_gateway, "ap_get", mock_ap_get)
752 763
753 items, __ = await ap_gateway.pubsub_service.items( 764 items, __ = await ap_gateway.pubsub_service.items(
754 jid.JID("toto@example.org"), 765 jid.JID("toto@example.org"),
844 assert items == ["local_user@test.example", "ext_user@example.org"] 855 assert items == ["local_user@test.example", "ext_user@example.org"]
845 856
846 @ed 857 @ed
847 async def test_xmpp_message_to_ap_direct_message(self, ap_gateway, monkeypatch): 858 async def test_xmpp_message_to_ap_direct_message(self, ap_gateway, monkeypatch):
848 """XMPP message are sent as AP direct message""" 859 """XMPP message are sent as AP direct message"""
849 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "get", mock_ap_get) 860 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "get", mock_treq_get)
850 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "json_content", mock_treq_json) 861 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "json_content", mock_treq_json)
851 monkeypatch.setattr(ap_gateway, "ap_get", mock_ap_get) 862 monkeypatch.setattr(ap_gateway, "ap_get", mock_ap_get)
852 mess_data = { 863 mess_data = {
853 "from": TEST_JID, 864 "from": TEST_JID,
854 "to": ap_gateway.get_local_jid_from_account(TEST_AP_ACCOUNT), 865 "to": ap_gateway.get_local_jid_from_account(TEST_AP_ACCOUNT),
855 "type": "chat", 866 "type": "chat",
856 "message": {"": "This is a test message."}, 867 "message": {"": "This is a test message."},
857 "extra": {"origin-id": "123"}, 868 "extra": {"origin-id": "123"},
858 } 869 }
859 with patch.object(ap_gateway, "sign_and_post") as sign_and_post: 870 with patch.object(ap_gateway, "ap_post") as ap_post:
860 await ap_gateway.onMessage(ap_gateway.client, mess_data) 871 await ap_gateway.on_message(ap_gateway.client, mess_data)
861 url, actor_id, doc = sign_and_post.call_args[0] 872 url, actor_id, doc = ap_post.call_args[0]
862 assert url == "https://example.org/users/test_user/inbox" 873 assert url == "https://example.org/users/test_user/inbox"
863 assert actor_id == "https://test.example/_ap/actor/some_user@test.example" 874 assert actor_id == "https://test.example/_ap/actor/some_user@test.example"
864 obj = doc["object"] 875 obj = doc["object"]
865 assert doc["@context"] == ["https://www.w3.org/ns/activitystreams"] 876 assert doc["@context"] == ["https://www.w3.org/ns/activitystreams"]
866 assert doc["actor"] == "https://test.example/_ap/actor/some_user@test.example" 877 assert doc["actor"] == "https://test.example/_ap/actor/some_user@test.example"
878 assert field not in obj 889 assert field not in obj
879 890
880 @ed 891 @ed
881 async def test_ap_direct_message_to_xmpp_message(self, ap_gateway, monkeypatch): 892 async def test_ap_direct_message_to_xmpp_message(self, ap_gateway, monkeypatch):
882 """AP direct message are sent as XMPP message (not Pubsub)""" 893 """AP direct message are sent as XMPP message (not Pubsub)"""
883 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "get", mock_ap_get) 894 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "get", mock_treq_get)
884 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "json_content", mock_treq_json) 895 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "json_content", mock_treq_json)
885 monkeypatch.setattr(ap_gateway, "ap_get", mock_ap_get) 896 monkeypatch.setattr(ap_gateway, "ap_get", mock_ap_get)
886 # we have to patch DeferredList to not wait forever 897 # we have to patch DeferredList to not wait forever
887 monkeypatch.setattr(defer, "DeferredList", AsyncMock()) 898 monkeypatch.setattr(defer, "DeferredList", AsyncMock())
888 899
912 assert sendMessage.call_args.args[1] == {"": "test direct message"} 923 assert sendMessage.call_args.args[1] == {"": "test direct message"}
913 924
914 @ed 925 @ed
915 async def test_pubsub_retract_to_ap_delete(self, ap_gateway, monkeypatch): 926 async def test_pubsub_retract_to_ap_delete(self, ap_gateway, monkeypatch):
916 """Pubsub retract requests are converted to AP delete activity""" 927 """Pubsub retract requests are converted to AP delete activity"""
917 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "get", mock_ap_get) 928 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "get", mock_treq_get)
918 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "json_content", mock_treq_json) 929 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "json_content", mock_treq_json)
919 monkeypatch.setattr(ap_gateway, "ap_get", mock_ap_get) 930 monkeypatch.setattr(ap_gateway, "ap_get", mock_ap_get)
920 retract_id = "retract_123" 931 retract_id = "retract_123"
921 retract_elt = domish.Element((pubsub.NS_PUBSUB_EVENT, "retract")) 932 retract_elt = domish.Element((pubsub.NS_PUBSUB_EVENT, "retract"))
922 retract_elt["id"] = retract_id 933 retract_elt["id"] = retract_id
925 recipient=ap_gateway.get_local_jid_from_account(TEST_AP_ACCOUNT), 936 recipient=ap_gateway.get_local_jid_from_account(TEST_AP_ACCOUNT),
926 nodeIdentifier=ap_gateway._m.namespace, 937 nodeIdentifier=ap_gateway._m.namespace,
927 items=[retract_elt], 938 items=[retract_elt],
928 headers={}, 939 headers={},
929 ) 940 )
930 with patch.object(ap_gateway, "sign_and_post") as sign_and_post: 941 with patch.object(ap_gateway, "ap_post") as ap_post:
931 sign_and_post.return_value = FakeTReqPostResponse() 942 ap_post.return_value = FakeTReqPostResponse()
932 # we simulate the reception of a retract event 943 # we simulate the reception of a retract event
933 await ap_gateway._items_received(ap_gateway.client, items_event) 944 await ap_gateway._items_received(ap_gateway.client, items_event)
934 url, actor_id, doc = sign_and_post.call_args[0] 945 url, actor_id, doc = ap_post.call_args[0]
935 jid_account = await ap_gateway.get_ap_account_from_jid_and_node(TEST_JID, None) 946 jid_account = await ap_gateway.get_ap_account_from_jid_and_node(TEST_JID, None)
936 jid_actor_id = ap_gateway.build_apurl(ap_const.TYPE_ACTOR, jid_account) 947 jid_actor_id = ap_gateway.build_apurl(ap_const.TYPE_ACTOR, jid_account)
937 assert url == f"{TEST_BASE_URL}/inbox" 948 assert url == f"{TEST_BASE_URL}/inbox"
938 assert actor_id == jid_actor_id 949 assert actor_id == jid_actor_id
939 assert doc["type"] == "Delete" 950 assert doc["type"] == "Delete"
1002 assert item_elt["id"] == ap_item["id"] 1013 assert item_elt["id"] == ap_item["id"]
1003 1014
1004 @ed 1015 @ed
1005 async def test_message_retract_to_ap_delete(self, ap_gateway, monkeypatch): 1016 async def test_message_retract_to_ap_delete(self, ap_gateway, monkeypatch):
1006 """Message retract requests are converted to AP delete activity""" 1017 """Message retract requests are converted to AP delete activity"""
1007 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "get", mock_ap_get) 1018 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "get", mock_treq_get)
1008 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "json_content", mock_treq_json) 1019 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "json_content", mock_treq_json)
1009 monkeypatch.setattr(ap_gateway, "ap_get", mock_ap_get) 1020 monkeypatch.setattr(ap_gateway, "ap_get", mock_ap_get)
1010 # origin ID is the ID of the message to retract 1021 # origin ID is the ID of the message to retract
1011 origin_id = "mess_retract_123" 1022 origin_id = "mess_retract_123"
1012 1023
1019 ap_gateway._r.send_retract(fake_client, dest_jid, origin_id, fake_history) 1030 ap_gateway._r.send_retract(fake_client, dest_jid, origin_id, fake_history)
1020 # message_retract_elt is the message which would be sent for a retraction 1031 # message_retract_elt is the message which would be sent for a retraction
1021 message_retract_elt = fake_client.send.call_args.args[0] 1032 message_retract_elt = fake_client.send.call_args.args[0]
1022 retract_elt = next(message_retract_elt.elements(NS_MESSAGE_RETRACT, "retract")) 1033 retract_elt = next(message_retract_elt.elements(NS_MESSAGE_RETRACT, "retract"))
1023 1034
1024 with patch.object(ap_gateway, "sign_and_post") as sign_and_post: 1035 with patch.object(ap_gateway, "ap_post") as ap_post:
1025 sign_and_post.return_value = FakeTReqPostResponse() 1036 ap_post.return_value = FakeTReqPostResponse()
1026 fake_fastened_elts = MagicMock() 1037 fake_fastened_elts = MagicMock()
1027 fake_fastened_elts.id = origin_id 1038 fake_fastened_elts.id = origin_id
1028 # we simulate the reception of a retract event using the message element that 1039 # we simulate the reception of a retract event using the message element that
1029 # we generated above 1040 # we generated above
1030 await ap_gateway._on_message_retract( 1041 await ap_gateway._on_message_retract(
1031 ap_gateway.client, message_retract_elt, retract_elt, fake_fastened_elts 1042 ap_gateway.client, message_retract_elt, retract_elt, fake_fastened_elts
1032 ) 1043 )
1033 url, actor_id, doc = sign_and_post.call_args[0] 1044 url, actor_id, doc = ap_post.call_args[0]
1034 1045
1035 # the AP delete activity must have been sent through sign_and_post 1046 # the AP delete activity must have been sent through ap_post
1036 # we check its values 1047 # we check its values
1037 jid_account = await ap_gateway.get_ap_account_from_jid_and_node(TEST_JID, None) 1048 jid_account = await ap_gateway.get_ap_account_from_jid_and_node(TEST_JID, None)
1038 jid_actor_id = ap_gateway.build_apurl(ap_const.TYPE_ACTOR, jid_account) 1049 jid_actor_id = ap_gateway.build_apurl(ap_const.TYPE_ACTOR, jid_account)
1039 assert url == f"{TEST_BASE_URL}/users/{TEST_USER}/inbox" 1050 assert url == f"{TEST_BASE_URL}/users/{TEST_USER}/inbox"
1040 assert actor_id == jid_actor_id 1051 assert actor_id == jid_actor_id
1099 assert retract_elt.uri == NS_MESSAGE_RETRACT 1110 assert retract_elt.uri == NS_MESSAGE_RETRACT
1100 1111
1101 @ed 1112 @ed
1102 async def test_ap_actor_metadata_to_vcard(self, ap_gateway, monkeypatch): 1113 async def test_ap_actor_metadata_to_vcard(self, ap_gateway, monkeypatch):
1103 """AP actor metadata are converted to XMPP/vCard4""" 1114 """AP actor metadata are converted to XMPP/vCard4"""
1104 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "get", mock_ap_get) 1115 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "get", mock_treq_get)
1105 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "json_content", mock_treq_json) 1116 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "json_content", mock_treq_json)
1106 monkeypatch.setattr(ap_gateway, "ap_get", mock_ap_get) 1117 monkeypatch.setattr(ap_gateway, "ap_get", mock_ap_get)
1107 1118
1108 items, __ = await ap_gateway.pubsub_service.items( 1119 items, __ = await ap_gateway.pubsub_service.items(
1109 jid.JID("toto@example.org"), 1120 jid.JID("toto@example.org"),
1139 assert actor_data["summary"] == "test description" 1150 assert actor_data["summary"] == "test description"
1140 1151
1141 @ed 1152 @ed
1142 async def test_direct_addressing_mention_to_reference(self, ap_gateway, monkeypatch): 1153 async def test_direct_addressing_mention_to_reference(self, ap_gateway, monkeypatch):
1143 """AP mentions by direct addressing are converted to XEP-0372 references""" 1154 """AP mentions by direct addressing are converted to XEP-0372 references"""
1144 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "get", mock_ap_get) 1155 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "get", mock_treq_get)
1145 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "json_content", mock_treq_json) 1156 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "json_content", mock_treq_json)
1146 monkeypatch.setattr(ap_gateway, "ap_get", mock_ap_get) 1157 monkeypatch.setattr(ap_gateway, "ap_get", mock_ap_get)
1147 1158
1148 xmpp_actor_id = ap_gateway.build_apurl(ap_const.TYPE_ACTOR, TEST_JID.userhost()) 1159 xmpp_actor_id = ap_gateway.build_apurl(ap_const.TYPE_ACTOR, TEST_JID.userhost())
1149 1160
1180 assert send_reference.call_args.kwargs["anchor"] == expected_anchor 1191 assert send_reference.call_args.kwargs["anchor"] == expected_anchor
1181 1192
1182 @ed 1193 @ed
1183 async def test_tag_mention_to_reference(self, ap_gateway, monkeypatch): 1194 async def test_tag_mention_to_reference(self, ap_gateway, monkeypatch):
1184 """AP mentions in "tag" field are converted to XEP-0372 references""" 1195 """AP mentions in "tag" field are converted to XEP-0372 references"""
1185 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "get", mock_ap_get) 1196 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "get", mock_treq_get)
1186 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "json_content", mock_treq_json) 1197 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "json_content", mock_treq_json)
1187 monkeypatch.setattr(ap_gateway, "ap_get", mock_ap_get) 1198 monkeypatch.setattr(ap_gateway, "ap_get", mock_ap_get)
1188 1199
1189 xmpp_actor_id = ap_gateway.build_apurl(ap_const.TYPE_ACTOR, TEST_JID.userhost()) 1200 xmpp_actor_id = ap_gateway.build_apurl(ap_const.TYPE_ACTOR, TEST_JID.userhost())
1190 1201
1222 assert send_reference.call_args.kwargs["anchor"] == expected_anchor 1233 assert send_reference.call_args.kwargs["anchor"] == expected_anchor
1223 1234
1224 @ed 1235 @ed
1225 async def test_auto_mentions(self, ap_gateway, monkeypatch): 1236 async def test_auto_mentions(self, ap_gateway, monkeypatch):
1226 """Check that mentions in body are converted to AP mentions""" 1237 """Check that mentions in body are converted to AP mentions"""
1227 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "get", mock_ap_get) 1238 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "get", mock_treq_get)
1228 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "json_content", mock_treq_json) 1239 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "json_content", mock_treq_json)
1229 monkeypatch.setattr(ap_gateway, "ap_get", mock_ap_get) 1240 monkeypatch.setattr(ap_gateway, "ap_get", mock_ap_get)
1230 1241
1231 mb_data = { 1242 mb_data = {
1232 "author_jid": TEST_JID.full(), 1243 "author_jid": TEST_JID.full(),
1250 @ed 1261 @ed
1251 async def test_no_auto_mentions_when_not_public(self, ap_gateway, monkeypatch): 1262 async def test_no_auto_mentions_when_not_public(self, ap_gateway, monkeypatch):
1252 """Check that no mention is send when the message is not public""" 1263 """Check that no mention is send when the message is not public"""
1253 # this is the same test as test_auto_mentions above, except that public is not set 1264 # this is the same test as test_auto_mentions above, except that public is not set
1254 # in mb_data_2_ap_item 1265 # in mb_data_2_ap_item
1255 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "get", mock_ap_get) 1266 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "get", mock_treq_get)
1256 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "json_content", mock_treq_json) 1267 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "json_content", mock_treq_json)
1257 monkeypatch.setattr(ap_gateway, "ap_get", mock_ap_get) 1268 monkeypatch.setattr(ap_gateway, "ap_get", mock_ap_get)
1258 1269
1259 mb_data = { 1270 mb_data = {
1260 "author_jid": TEST_JID.full(), 1271 "author_jid": TEST_JID.full(),
1271 assert "tag" not in ap_object 1282 assert "tag" not in ap_object
1272 1283
1273 @ed 1284 @ed
1274 async def test_xmpp_reference_to_ap_mention(self, ap_gateway, monkeypatch): 1285 async def test_xmpp_reference_to_ap_mention(self, ap_gateway, monkeypatch):
1275 """Check that XEP-0372 references are converted to AP mention""" 1286 """Check that XEP-0372 references are converted to AP mention"""
1276 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "get", mock_ap_get) 1287 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "get", mock_treq_get)
1277 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "json_content", mock_treq_json) 1288 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "json_content", mock_treq_json)
1278 monkeypatch.setattr(ap_gateway, "ap_get", mock_ap_get) 1289 monkeypatch.setattr(ap_gateway, "ap_get", mock_ap_get)
1279 1290
1280 local_actor_jid = ap_gateway.get_local_jid_from_account(TEST_AP_ACCOUNT) 1291 local_actor_jid = ap_gateway.get_local_jid_from_account(TEST_AP_ACCOUNT)
1281 item_elt = XMPP_ITEMS[0] 1292 item_elt = XMPP_ITEMS[0]
1310 # get_items returns a sqla_mapping.PubsubItem, thus we need to fake it and set 1321 # get_items returns a sqla_mapping.PubsubItem, thus we need to fake it and set
1311 # the item_elt we want to use in its "data" attribute 1322 # the item_elt we want to use in its "data" attribute
1312 mock_pubsub_item = MagicMock 1323 mock_pubsub_item = MagicMock
1313 mock_pubsub_item.data = item_elt 1324 mock_pubsub_item.data = item_elt
1314 get_items.return_value = ([mock_pubsub_item], {}) 1325 get_items.return_value = ([mock_pubsub_item], {})
1315 with patch.object(ap_gateway, "sign_and_post") as sign_and_post: 1326 with patch.object(ap_gateway, "ap_post") as ap_post:
1316 sign_and_post.return_value.code = 202 1327 ap_post.return_value.code = 202
1317 await ap_gateway._on_reference_received( 1328 await ap_gateway._on_reference_received(
1318 ap_gateway.client, message_elt, ref_data 1329 ap_gateway.client, message_elt, ref_data
1319 ) 1330 )
1320 1331
1321 # when reference is received, the referencing item must be sent to referenced 1332 # when reference is received, the referencing item must be sent to referenced
1322 # actor, and they must be in "to" field and in "tag" 1333 # actor, and they must be in "to" field and in "tag"
1323 assert sign_and_post.call_count == 1 1334 assert ap_post.call_count == 1
1324 send_ap_item = sign_and_post.call_args.args[-1] 1335 send_ap_item = ap_post.call_args.args[-1]
1325 ap_object = send_ap_item["object"] 1336 ap_object = send_ap_item["object"]
1326 assert TEST_AP_ACTOR_ID in ap_object["to"] 1337 assert TEST_AP_ACTOR_ID in ap_object["to"]
1327 expected_mention = { 1338 expected_mention = {
1328 "type": ap_const.TYPE_MENTION, 1339 "type": ap_const.TYPE_MENTION,
1329 "href": TEST_AP_ACTOR_ID, 1340 "href": TEST_AP_ACTOR_ID,
1334 assert expected_mention in ap_object["tag"] 1345 assert expected_mention in ap_object["tag"]
1335 1346
1336 @ed 1347 @ed
1337 async def test_xmpp_repeat_to_ap_announce(self, ap_gateway, monkeypatch): 1348 async def test_xmpp_repeat_to_ap_announce(self, ap_gateway, monkeypatch):
1338 """XEP-0272 post repeat is converted to AP Announce activity""" 1349 """XEP-0272 post repeat is converted to AP Announce activity"""
1339 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "get", mock_ap_get) 1350 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "get", mock_treq_get)
1340 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "json_content", mock_treq_json) 1351 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "json_content", mock_treq_json)
1341 monkeypatch.setattr(ap_gateway, "ap_get", mock_ap_get) 1352 monkeypatch.setattr(ap_gateway, "ap_get", mock_ap_get)
1342 1353
1343 # JID repeated AP actor (also the recipient of the message) 1354 # JID repeated AP actor (also the recipient of the message)
1344 recipient_jid = ap_gateway.get_local_jid_from_account(TEST_AP_ACCOUNT) 1355 recipient_jid = ap_gateway.get_local_jid_from_account(TEST_AP_ACCOUNT)
1372 </item> 1383 </item>
1373 """ 1384 """
1374 ) 1385 )
1375 item_elt.uri = pubsub.NS_PUBSUB_EVENT 1386 item_elt.uri = pubsub.NS_PUBSUB_EVENT
1376 1387
1377 with patch.object(ap_gateway, "sign_and_post") as sign_and_post: 1388 with patch.object(ap_gateway, "ap_post") as ap_post:
1378 sign_and_post.return_value.code = 202 1389 ap_post.return_value.code = 202
1379 await ap_gateway.convert_and_post_items( 1390 await ap_gateway.convert_and_post_items(
1380 ap_gateway.client, 1391 ap_gateway.client,
1381 TEST_AP_ACCOUNT, 1392 TEST_AP_ACCOUNT,
1382 TEST_JID, 1393 TEST_JID,
1383 ap_gateway._m.namespace, 1394 ap_gateway._m.namespace,
1384 [item_elt], 1395 [item_elt],
1385 ) 1396 )
1386 1397
1387 assert sign_and_post.called 1398 assert ap_post.called
1388 url, actor_id, doc = sign_and_post.call_args.args 1399 url, actor_id, doc = ap_post.call_args.args
1389 assert url == TEST_USER_DATA["endpoints"]["sharedInbox"] 1400 assert url == TEST_USER_DATA["endpoints"]["sharedInbox"]
1390 assert actor_id == ap_gateway.build_apurl(ap_const.TYPE_ACTOR, TEST_JID.userhost()) 1401 assert actor_id == ap_gateway.build_apurl(ap_const.TYPE_ACTOR, TEST_JID.userhost())
1391 assert doc["type"] == "Announce" 1402 assert doc["type"] == "Announce"
1392 assert ap_const.NS_AP_PUBLIC in doc["to"] 1403 assert ap_const.NS_AP_PUBLIC in doc["to"]
1393 assert doc["object"] == ap_item["id"] 1404 assert doc["object"] == ap_item["id"]
1394 1405
1395 @ed 1406 @ed
1396 async def test_ap_announce_to_xmpp_repeat(self, ap_gateway, monkeypatch): 1407 async def test_ap_announce_to_xmpp_repeat(self, ap_gateway, monkeypatch):
1397 """AP Announce activity is converted to XEP-0272 post repeat""" 1408 """AP Announce activity is converted to XEP-0272 post repeat"""
1398 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "get", mock_ap_get) 1409 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "get", mock_treq_get)
1399 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "json_content", mock_treq_json) 1410 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "json_content", mock_treq_json)
1400 monkeypatch.setattr(ap_gateway, "ap_get", mock_ap_get) 1411 monkeypatch.setattr(ap_gateway, "ap_get", mock_ap_get)
1401 1412
1402 xmpp_actor_id = ap_gateway.build_apurl(ap_const.TYPE_ACTOR, TEST_JID.userhost()) 1413 xmpp_actor_id = ap_gateway.build_apurl(ap_const.TYPE_ACTOR, TEST_JID.userhost())
1403 # announced item 1414 # announced item
1421 get_items.return_value = ([mock_pubsub_item], {}) 1432 get_items.return_value = ([mock_pubsub_item], {})
1422 with patch.object( 1433 with patch.object(
1423 ap_gateway.host.memory.storage, "cache_pubsub_items" 1434 ap_gateway.host.memory.storage, "cache_pubsub_items"
1424 ) as cache_pubsub_items: 1435 ) as cache_pubsub_items:
1425 await ap_gateway.server.resource.handle_announce_activity( 1436 await ap_gateway.server.resource.handle_announce_activity(
1426 Request(MagicMock()), announce, None, None, None, "", TEST_AP_ACTOR_ID 1437 TEST_AP_ACTOR_ID, Request(MagicMock()), announce, None, None, None,
1438 "", TEST_AP_ACTOR_ID
1427 ) 1439 )
1428 1440
1429 assert cache_pubsub_items.called 1441 assert cache_pubsub_items.called
1430 # the microblog data put in cache correspond to the item sent to subscribers 1442 # the microblog data put in cache correspond to the item sent to subscribers
1431 __, __, __, [mb_data] = cache_pubsub_items.call_args.args 1443 __, __, __, [mb_data] = cache_pubsub_items.call_args.args
1442 assert repeated["uri"] == xmpp_item_xmpp_url 1454 assert repeated["uri"] == xmpp_item_xmpp_url
1443 1455
1444 @ed 1456 @ed
1445 async def test_xmpp_attachment_noticed_to_ap_like(self, ap_gateway, monkeypatch): 1457 async def test_xmpp_attachment_noticed_to_ap_like(self, ap_gateway, monkeypatch):
1446 """Pubsub-attachments ``noticed`` is converted to AP Like activity""" 1458 """Pubsub-attachments ``noticed`` is converted to AP Like activity"""
1447 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "get", mock_ap_get) 1459 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "get", mock_treq_get)
1448 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "json_content", mock_treq_json) 1460 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "json_content", mock_treq_json)
1449 monkeypatch.setattr(ap_gateway, "ap_get", mock_ap_get) 1461 monkeypatch.setattr(ap_gateway, "ap_get", mock_ap_get)
1450 1462
1451 recipient_jid = ap_gateway.get_local_jid_from_account(TEST_AP_ACCOUNT) 1463 recipient_jid = ap_gateway.get_local_jid_from_account(TEST_AP_ACCOUNT)
1452 # noticed item 1464 # noticed item
1466 item_elt.uri = pubsub.NS_PUBSUB_EVENT 1478 item_elt.uri = pubsub.NS_PUBSUB_EVENT
1467 items_event = pubsub.ItemsEvent( 1479 items_event = pubsub.ItemsEvent(
1468 TEST_JID, recipient_jid, attachment_node, [item_elt], {} 1480 TEST_JID, recipient_jid, attachment_node, [item_elt], {}
1469 ) 1481 )
1470 1482
1471 with patch.object(ap_gateway, "sign_and_post") as sign_and_post: 1483 with patch.object(ap_gateway, "ap_post") as ap_post:
1472 sign_and_post.return_value.code = 202 1484 ap_post.return_value.code = 202
1473 await ap_gateway._items_received(ap_gateway.client, items_event) 1485 await ap_gateway._items_received(ap_gateway.client, items_event)
1474 1486
1475 assert sign_and_post.called 1487 assert ap_post.called
1476 url, actor_id, doc = sign_and_post.call_args.args 1488 url, actor_id, doc = ap_post.call_args.args
1477 assert url == TEST_USER_DATA["endpoints"]["sharedInbox"] 1489 assert url == TEST_USER_DATA["endpoints"]["sharedInbox"]
1478 assert actor_id == ap_gateway.build_apurl(ap_const.TYPE_ACTOR, TEST_JID.userhost()) 1490 assert actor_id == ap_gateway.build_apurl(ap_const.TYPE_ACTOR, TEST_JID.userhost())
1479 assert doc["type"] == "Like" 1491 assert doc["type"] == "Like"
1480 assert ap_const.NS_AP_PUBLIC in doc["cc"] 1492 assert ap_const.NS_AP_PUBLIC in doc["cc"]
1481 assert doc["object"] == ap_item["id"] 1493 assert doc["object"] == ap_item["id"]
1482 1494
1483 @ed 1495 @ed
1484 async def test_ap_like_to_xmpp_noticed_attachment(self, ap_gateway, monkeypatch): 1496 async def test_ap_like_to_xmpp_noticed_attachment(self, ap_gateway, monkeypatch):
1485 """AP Like activity is converted to ``noticed`` attachment""" 1497 """AP Like activity is converted to ``noticed`` attachment"""
1486 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "get", mock_ap_get) 1498 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "get", mock_treq_get)
1487 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "json_content", mock_treq_json) 1499 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "json_content", mock_treq_json)
1488 monkeypatch.setattr(ap_gateway, "ap_get", mock_ap_get) 1500 monkeypatch.setattr(ap_gateway, "ap_get", mock_ap_get)
1489 1501
1490 xmpp_actor_id = ap_gateway.build_apurl(ap_const.TYPE_ACTOR, TEST_JID.userhost()) 1502 xmpp_actor_id = ap_gateway.build_apurl(ap_const.TYPE_ACTOR, TEST_JID.userhost())
1491 # liked item 1503 # liked item
1525 assert parsed_item["noticed"]["noticed"] == True 1537 assert parsed_item["noticed"]["noticed"] == True
1526 1538
1527 @ed 1539 @ed
1528 async def test_xmpp_pubsub_reactions_to_ap(self, ap_gateway, monkeypatch): 1540 async def test_xmpp_pubsub_reactions_to_ap(self, ap_gateway, monkeypatch):
1529 """Pubsub-attachments ``reactions`` is converted to AP EmojiReact activity""" 1541 """Pubsub-attachments ``reactions`` is converted to AP EmojiReact activity"""
1530 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "get", mock_ap_get) 1542 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "get", mock_treq_get)
1531 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "json_content", mock_treq_json) 1543 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "json_content", mock_treq_json)
1532 monkeypatch.setattr(ap_gateway, "ap_get", mock_ap_get) 1544 monkeypatch.setattr(ap_gateway, "ap_get", mock_ap_get)
1533 1545
1534 recipient_jid = ap_gateway.get_local_jid_from_account(TEST_AP_ACCOUNT) 1546 recipient_jid = ap_gateway.get_local_jid_from_account(TEST_AP_ACCOUNT)
1535 # noticed item 1547 # noticed item
1560 item_elt.uri = pubsub.NS_PUBSUB_EVENT 1572 item_elt.uri = pubsub.NS_PUBSUB_EVENT
1561 items_event = pubsub.ItemsEvent( 1573 items_event = pubsub.ItemsEvent(
1562 TEST_JID, recipient_jid, attachment_node, [item_elt], {} 1574 TEST_JID, recipient_jid, attachment_node, [item_elt], {}
1563 ) 1575 )
1564 1576
1565 with patch.object(ap_gateway, "sign_and_post") as sign_and_post: 1577 with patch.object(ap_gateway, "ap_post") as ap_post:
1566 sign_and_post.return_value.code = 202 1578 ap_post.return_value.code = 202
1567 await ap_gateway._items_received(ap_gateway.client, items_event) 1579 await ap_gateway._items_received(ap_gateway.client, items_event)
1568 1580
1569 assert sign_and_post.call_count == 3 1581 assert ap_post.call_count == 3
1570 for idx, call_args in enumerate(sign_and_post.call_args_list): 1582 for idx, call_args in enumerate(ap_post.call_args_list):
1571 url, actor_id, doc = call_args.args 1583 url, actor_id, doc = call_args.args
1572 assert url == TEST_USER_DATA["endpoints"]["sharedInbox"] 1584 assert url == TEST_USER_DATA["endpoints"]["sharedInbox"]
1573 assert actor_id == ap_gateway.build_apurl( 1585 assert actor_id == ap_gateway.build_apurl(
1574 ap_const.TYPE_ACTOR, TEST_JID.userhost() 1586 ap_const.TYPE_ACTOR, TEST_JID.userhost()
1575 ) 1587 )
1584 assert len(reactions) == 0 1596 assert len(reactions) == 0
1585 1597
1586 @ed 1598 @ed
1587 async def test_ap_reactions_to_xmpp(self, ap_gateway, monkeypatch): 1599 async def test_ap_reactions_to_xmpp(self, ap_gateway, monkeypatch):
1588 """AP EmojiReact activity is converted to ``reactions`` attachment""" 1600 """AP EmojiReact activity is converted to ``reactions`` attachment"""
1589 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "get", mock_ap_get) 1601 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "get", mock_treq_get)
1590 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "json_content", mock_treq_json) 1602 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "json_content", mock_treq_json)
1591 monkeypatch.setattr(ap_gateway, "ap_get", mock_ap_get) 1603 monkeypatch.setattr(ap_gateway, "ap_get", mock_ap_get)
1592 1604
1593 xmpp_actor_id = ap_gateway.build_apurl(ap_const.TYPE_ACTOR, TEST_JID.userhost()) 1605 xmpp_actor_id = ap_gateway.build_apurl(ap_const.TYPE_ACTOR, TEST_JID.userhost())
1594 # item on which reaction is attached 1606 # item on which reaction is attached
1723 "to": ["https://www.w3.org/ns/activitystreams#Public"], 1735 "to": ["https://www.w3.org/ns/activitystreams#Public"],
1724 "type": "Event", 1736 "type": "Event",
1725 "url": f"https://test.example/_ap/item/{test_actor}/event_123", 1737 "url": f"https://test.example/_ap/item/{test_actor}/event_123",
1726 } 1738 }
1727 1739
1728 event_data = await ap_gateway.ap_events.ap_item_2_event_data(ap_object) 1740 event_data = await ap_gateway.ap_events.ap_item_2_event_data(
1741 TEST_AP_ACTOR_ID, ap_object
1742 )
1729 1743
1730 assert event_data["id"] == ap_object["id"] 1744 assert event_data["id"] == ap_object["id"]
1731 assert event_data["name"] == {"": "test event"} 1745 assert event_data["name"] == {"": "test event"}
1732 assert event_data["start"] == 1664222400 1746 assert event_data["start"] == 1664222400
1733 assert event_data["end"] == 1664226000 1747 assert event_data["end"] == 1664226000