Mercurial > libervia-backend
comparison tests/unit/test_ap-gateway.py @ 4213:716dd791be46
tests (unit): fix tests
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 23 Feb 2024 15:42:02 +0100 |
parents | 4b842c1fb686 |
children | 49019947cc76 |
comparison
equal
deleted
inserted
replaced
4212:5f2d496c633f | 4213:716dd791be46 |
---|---|
40 from libervia.backend.plugins import plugin_comp_ap_gateway | 40 from libervia.backend.plugins import plugin_comp_ap_gateway |
41 from libervia.backend.plugins.plugin_comp_ap_gateway import constants as ap_const | 41 from libervia.backend.plugins.plugin_comp_ap_gateway import constants as ap_const |
42 from libervia.backend.plugins.plugin_comp_ap_gateway import TYPE_ACTOR | 42 from libervia.backend.plugins.plugin_comp_ap_gateway import TYPE_ACTOR |
43 from libervia.backend.plugins.plugin_comp_ap_gateway.http_server import HTTPServer | 43 from libervia.backend.plugins.plugin_comp_ap_gateway.http_server import HTTPServer |
44 from libervia.backend.plugins.plugin_xep_0277 import NS_ATOM | 44 from libervia.backend.plugins.plugin_xep_0277 import NS_ATOM |
45 from libervia.backend.plugins.plugin_xep_0422 import NS_FASTEN | |
46 from libervia.backend.plugins.plugin_xep_0424 import NS_MESSAGE_RETRACT | 45 from libervia.backend.plugins.plugin_xep_0424 import NS_MESSAGE_RETRACT |
47 from libervia.backend.plugins.plugin_xep_0465 import NS_PPS | 46 from libervia.backend.plugins.plugin_xep_0465 import NS_PPS |
48 from libervia.backend.tools import xml_tools | 47 from libervia.backend.tools import xml_tools |
49 from libervia.backend.tools.common import uri as xmpp_uri | 48 from libervia.backend.tools.common import uri as xmpp_uri |
50 from libervia.backend.tools.utils import xmpp_date | 49 from libervia.backend.tools.utils import xmpp_date |
1009 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "json_content", mock_treq_json) | 1008 monkeypatch.setattr(plugin_comp_ap_gateway.treq, "json_content", mock_treq_json) |
1010 monkeypatch.setattr(ap_gateway, "ap_get", mock_ap_get) | 1009 monkeypatch.setattr(ap_gateway, "ap_get", mock_ap_get) |
1011 # origin ID is the ID of the message to retract | 1010 # origin ID is the ID of the message to retract |
1012 origin_id = "mess_retract_123" | 1011 origin_id = "mess_retract_123" |
1013 | 1012 |
1014 # we call retract_by_origin_id to get the message element of a retraction request | 1013 # we call send_retract to get the message element of a retraction request |
1015 fake_client = MagicMock() | 1014 fake_client = MagicMock() |
1016 fake_client.jid = TEST_JID | 1015 fake_client.jid = TEST_JID |
1017 dest_jid = ap_gateway.get_local_jid_from_account(TEST_AP_ACCOUNT) | 1016 dest_jid = ap_gateway.get_local_jid_from_account(TEST_AP_ACCOUNT) |
1018 ap_gateway._r.retract_by_origin_id(fake_client, dest_jid, origin_id) | 1017 fake_history = MagicMock() |
1018 fake_history.type = C.MESS_TYPE_CHAT | |
1019 ap_gateway._r.send_retract(fake_client, dest_jid, origin_id, fake_history) | |
1019 # message_retract_elt is the message which would be sent for a retraction | 1020 # message_retract_elt is the message which would be sent for a retraction |
1020 message_retract_elt = fake_client.send.call_args.args[0] | 1021 message_retract_elt = fake_client.send.call_args.args[0] |
1021 apply_to_elt = next(message_retract_elt.elements(NS_FASTEN, "apply-to")) | 1022 retract_elt = next(message_retract_elt.elements(NS_MESSAGE_RETRACT, "retract")) |
1022 retract_elt = apply_to_elt.retract | |
1023 | 1023 |
1024 with patch.object(ap_gateway, "sign_and_post") as sign_and_post: | 1024 with patch.object(ap_gateway, "sign_and_post") as sign_and_post: |
1025 sign_and_post.return_value = FakeTReqPostResponse() | 1025 sign_and_post.return_value = FakeTReqPostResponse() |
1026 fake_fastened_elts = MagicMock() | 1026 fake_fastened_elts = MagicMock() |
1027 fake_fastened_elts.id = origin_id | 1027 fake_fastened_elts.id = origin_id |
1071 "to": ["https://www.w3.org/ns/activitystreams#Public"], | 1071 "to": ["https://www.w3.org/ns/activitystreams#Public"], |
1072 } | 1072 } |
1073 with patch.object(ap_gateway.host.memory.storage, "get") as storage_get: | 1073 with patch.object(ap_gateway.host.memory.storage, "get") as storage_get: |
1074 fake_history = MagicMock() | 1074 fake_history = MagicMock() |
1075 fake_history.source_jid = client.jid | 1075 fake_history.source_jid = client.jid |
1076 fake_history.dest = str(TEST_JID) | |
1076 fake_history.dest_jid = TEST_JID | 1077 fake_history.dest_jid = TEST_JID |
1077 fake_history.origin_id = ap_item["id"] | 1078 fake_history.origin_id = ap_item["id"] |
1078 storage_get.return_value = fake_history | 1079 storage_get.return_value = fake_history |
1079 # we simulate a received Delete activity | 1080 # we simulate a received Delete activity |
1080 await ap_gateway.new_ap_delete_item( | 1081 await ap_gateway.new_ap_delete_item( |
1090 assert fake_send.call_count == 1 | 1091 assert fake_send.call_count == 1 |
1091 sent_elt = fake_send.call_args.args[0] | 1092 sent_elt = fake_send.call_args.args[0] |
1092 assert sent_elt.name == "message" | 1093 assert sent_elt.name == "message" |
1093 assert sent_elt["from"] == client.jid.full() | 1094 assert sent_elt["from"] == client.jid.full() |
1094 assert sent_elt["to"] == TEST_JID.full() | 1095 assert sent_elt["to"] == TEST_JID.full() |
1095 apply_to_elt = next(sent_elt.elements(NS_FASTEN, "apply-to")) | 1096 retract_elt = next(sent_elt.elements(NS_MESSAGE_RETRACT, "retract")) |
1096 assert apply_to_elt["id"] == ap_item["id"] | 1097 assert retract_elt["id"] == ap_item["id"] |
1097 retract_elt = apply_to_elt.retract | |
1098 assert retract_elt is not None | 1098 assert retract_elt is not None |
1099 assert retract_elt.uri == NS_MESSAGE_RETRACT | 1099 assert retract_elt.uri == NS_MESSAGE_RETRACT |
1100 | 1100 |
1101 @ed | 1101 @ed |
1102 async def test_ap_actor_metadata_to_vcard(self, ap_gateway, monkeypatch): | 1102 async def test_ap_actor_metadata_to_vcard(self, ap_gateway, monkeypatch): |