massive refactoring to switch from camelCase to snake_case:

historically, Libervia (SàT before) was using camelCase as allowed by PEP8 when using a
pre-PEP8 code, to use the same coding style as in Twisted.

However, snake_case is more readable and it's better to follow PEP8 best practices, so it
has been decided to move on full snake_case. Because Libervia has a huge codebase, this
ended with a ugly mix of camelCase and snake_case.

To fix that, this patch does a big refactoring by renaming every function and method
(including bridge) that are not coming from Twisted or Wokkel, to use fully snake_case.

This is a massive change, and may result in some bugs.
This commit is contained in:
Goffi 2023-04-08 13:54:42 +02:00
commit 6a6f15707a
3 changed files with 9945 additions and 9945 deletions

View file

@ -29,8 +29,8 @@ from sat.core import xmpp
@fixture(scope="session")
def bridge():
bridge = AsyncMock()
bridge.addSignal = MagicMock()
bridge.addMethod = MagicMock()
bridge.add_signal = MagicMock()
bridge.add_method = MagicMock()
return bridge
@ -49,21 +49,21 @@ class MockSAT(SAT):
self.profiles = {}
self.plugins = {}
# map for short name to whole namespace,
# extended by plugins with registerNamespace
# extended by plugins with register_namespace
self.ns_map = {
"x-data": xmpp.NS_X_DATA,
"disco#info": xmpp.NS_DISCO_INFO,
}
self.memory = MagicMock()
self.memory.storage = storage
self.memory.getConfig.side_effect = self.get_test_config
self.memory.config_get.side_effect = self.get_test_config
self.trigger = trigger.TriggerManager()
self.bridge = bridge
defer.ensureDeferred(self._post_init())
self.common_cache = AsyncMock()
self._import_plugins()
self._addBaseMenus()
self._add_base_menus()
self.initialised = defer.Deferred()
self.initialised.callback(None)

View file

@ -348,8 +348,8 @@ async def mock_treq_json(data):
return dict(data)
async def mock_getItems(client, service, node, *args, **kwargs):
"""Mock getItems
async def mock_get_items(client, service, node, *args, **kwargs):
"""Mock get_items
special kwargs can be used:
ret_items (List[Domish.Element]): items to be returned, by default XMPP_ITEMS are
@ -367,8 +367,8 @@ async def mock_getItems(client, service, node, *args, **kwargs):
return ret_items, {"rsm": rsm_resp.toDict(), "complete": True}
async def mock_getPubsubNode(client, service, node, with_subscriptions=False, **kwargs):
"""Mock storage's getPubsubNode
async def mock_get_pubsub_node(client, service, node, with_subscriptions=False, **kwargs):
"""Mock storage's get_pubsub_node
return an MagicMock with subscription attribute set to empty list
"""
@ -377,7 +377,7 @@ async def mock_getPubsubNode(client, service, node, with_subscriptions=False, **
return fake_cached_node
def mockClient(jid):
def mock_client(jid):
client = MagicMock()
client.jid = jid
client.host = "test.example"
@ -386,8 +386,8 @@ def mockClient(jid):
return client
def getVirtualClient(jid):
return mockClient(jid)
def get_virtual_client(jid):
return mock_client(jid)
class FakeTReqPostResponse:
@ -398,10 +398,10 @@ class FakeTReqPostResponse:
def ap_gateway(host):
gateway = plugin_comp_ap_gateway.APGateway(host)
gateway.initialised = True
gateway.isPubsub = AsyncMock()
gateway.isPubsub.return_value = False
client = mockClient(jid.JID("ap.test.example"))
client.getVirtualClient = getVirtualClient
gateway.is_pubsub = AsyncMock()
gateway.is_pubsub.return_value = False
client = mock_client(jid.JID("ap.test.example"))
client.get_virtual_client = get_virtual_client
gateway.client = client
gateway.local_only = True
gateway.public_url = PUBLIC_URL
@ -416,7 +416,7 @@ def ap_gateway(host):
class TestActivityPubGateway:
def getTitleXHTML(self, item_elt: domish.Element) -> domish.Element:
def get_title_xhtml(self, item_elt: domish.Element) -> domish.Element:
return next(
t
for t in item_elt.entry.elements(NS_ATOM, "title")
@ -426,7 +426,7 @@ class TestActivityPubGateway:
@ed
async def test_jid_and_node_convert_to_ap_handle(self, ap_gateway):
"""JID and pubsub node are converted correctly to an AP actor handle"""
get_account = ap_gateway.getAPAccountFromJidAndNode
get_account = ap_gateway.get_ap_account_from_jid_and_node
# local jid
assert (
@ -447,16 +447,16 @@ class TestActivityPubGateway:
)
# local pubsub node
with patch.object(ap_gateway, "isPubsub") as isPubsub:
isPubsub.return_value = True
with patch.object(ap_gateway, "is_pubsub") as is_pubsub:
is_pubsub.return_value = True
assert (
await get_account(jid_=jid.JID("pubsub.test.example"), node="some_node")
== "some_node@pubsub.test.example"
)
# non local pubsub node
with patch.object(ap_gateway, "isPubsub") as isPubsub:
isPubsub.return_value = True
with patch.object(ap_gateway, "is_pubsub") as is_pubsub:
is_pubsub.return_value = True
assert (
await get_account(jid_=jid.JID("pubsub.example.org"), node="some_node")
== "___some_node.40pubsub.2eexample.2eorg@ap.test.example"
@ -465,11 +465,11 @@ class TestActivityPubGateway:
@ed
async def test_ap_handle_convert_to_jid_and_node(self, ap_gateway, monkeypatch):
"""AP actor handle convert correctly to JID and pubsub node"""
get_jid_node = ap_gateway.getJIDAndNode
get_jid_node = ap_gateway.get_jid_and_node
# for following assertion, host is not a pubsub service
with patch.object(ap_gateway, "isPubsub") as isPubsub:
isPubsub.return_value = False
with patch.object(ap_gateway, "is_pubsub") as is_pubsub:
is_pubsub.return_value = False
# simple local jid
assert await get_jid_node("toto@test.example") == (
@ -498,8 +498,8 @@ class TestActivityPubGateway:
)
# for following assertion, host is a pubsub service
with patch.object(ap_gateway, "isPubsub") as isPubsub:
isPubsub.return_value = True
with patch.object(ap_gateway, "is_pubsub") as is_pubsub:
is_pubsub.return_value = True
# simple local node
assert await get_jid_node("toto@pubsub.test.example") == (
@ -517,18 +517,18 @@ class TestActivityPubGateway:
"""AP requests are converted to 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)
monkeypatch.setattr(ap_gateway, "ap_get", mock_ap_get)
actor_data = await ap_gateway.getAPActorDataFromAccount(TEST_AP_ACCOUNT)
outbox = await ap_gateway.apGetObject(actor_data, "outbox")
items, rsm_resp = await ap_gateway.getAPItems(outbox, 2)
actor_data = await ap_gateway.get_ap_actor_data_from_account(TEST_AP_ACCOUNT)
outbox = await ap_gateway.ap_get_object(actor_data, "outbox")
items, rsm_resp = await ap_gateway.get_ap_items(outbox, 2)
assert rsm_resp.count == 4
assert rsm_resp.index == 0
assert rsm_resp.first == "https://example.org/users/test_user/statuses/4"
assert rsm_resp.last == "https://example.org/users/test_user/statuses/3"
title_xhtml = self.getTitleXHTML(items[0])
title_xhtml = self.get_title_xhtml(items[0])
assert title_xhtml.toXml() == (
"<title xmlns='http://www.w3.org/2005/Atom' type='xhtml'>"
"<div xmlns='http://www.w3.org/1999/xhtml'><p>test message 4</p></div>"
@ -540,7 +540,7 @@ class TestActivityPubGateway:
assert author_uri == "xmpp:test_user\\40example.org@ap.test.example"
assert str(items[0].entry.published) == "2021-12-16T17:25:03Z"
title_xhtml = self.getTitleXHTML(items[1])
title_xhtml = self.get_title_xhtml(items[1])
assert title_xhtml.toXml() == (
"<title xmlns='http://www.w3.org/2005/Atom' type='xhtml'>"
"<div xmlns='http://www.w3.org/1999/xhtml'><p>test message 3</p></div>"
@ -552,7 +552,7 @@ class TestActivityPubGateway:
assert author_uri == "xmpp:test_user\\40example.org@ap.test.example"
assert str(items[1].entry.published) == "2021-12-16T17:26:03Z"
items, rsm_resp = await ap_gateway.getAPItems(
items, rsm_resp = await ap_gateway.get_ap_items(
outbox,
max_items=2,
after_id="https://example.org/users/test_user/statuses/3",
@ -563,7 +563,7 @@ class TestActivityPubGateway:
assert rsm_resp.first == "https://example.org/users/test_user/statuses/2"
assert rsm_resp.last == "https://example.org/users/test_user/statuses/1"
title_xhtml = self.getTitleXHTML(items[0])
title_xhtml = self.get_title_xhtml(items[0])
assert title_xhtml.toXml() == (
"<title xmlns='http://www.w3.org/2005/Atom' type='xhtml'>"
"<div xmlns='http://www.w3.org/1999/xhtml'><p>test message 2</p></div>"
@ -575,7 +575,7 @@ class TestActivityPubGateway:
assert author_uri == "xmpp:test_user\\40example.org@ap.test.example"
assert str(items[0].entry.published) == "2021-12-16T17:27:03Z"
title_xhtml = self.getTitleXHTML(items[1])
title_xhtml = self.get_title_xhtml(items[1])
assert title_xhtml.toXml() == (
"<title xmlns='http://www.w3.org/2005/Atom' type='xhtml'>"
"<div xmlns='http://www.w3.org/1999/xhtml'><p>test message 1</p></div>"
@ -587,7 +587,7 @@ class TestActivityPubGateway:
assert author_uri == "xmpp:test_user\\40example.org@ap.test.example"
assert str(items[1].entry.published) == "2021-12-16T17:28:03Z"
items, rsm_resp = await ap_gateway.getAPItems(outbox, max_items=1, start_index=2)
items, rsm_resp = await ap_gateway.get_ap_items(outbox, max_items=1, start_index=2)
assert rsm_resp.count == 4
assert rsm_resp.index == 2
@ -595,7 +595,7 @@ class TestActivityPubGateway:
assert rsm_resp.last == "https://example.org/users/test_user/statuses/2"
assert len(items) == 1
title_xhtml = self.getTitleXHTML(items[0])
title_xhtml = self.get_title_xhtml(items[0])
assert title_xhtml.toXml() == (
"<title xmlns='http://www.w3.org/2005/Atom' type='xhtml'>"
"<div xmlns='http://www.w3.org/1999/xhtml'><p>test message 2</p></div>"
@ -603,7 +603,7 @@ class TestActivityPubGateway:
)
assert str(items[0].entry.published) == "2021-12-16T17:27:03Z"
items, rsm_resp = await ap_gateway.getAPItems(
items, rsm_resp = await ap_gateway.get_ap_items(
outbox, max_items=3, chronological_pagination=False
)
assert rsm_resp.count == 4
@ -611,13 +611,13 @@ class TestActivityPubGateway:
assert rsm_resp.first == "https://example.org/users/test_user/statuses/3"
assert rsm_resp.last == "https://example.org/users/test_user/statuses/1"
assert len(items) == 3
title_xhtml = self.getTitleXHTML(items[0])
title_xhtml = self.get_title_xhtml(items[0])
assert title_xhtml.toXml() == (
"<title xmlns='http://www.w3.org/2005/Atom' type='xhtml'>"
"<div xmlns='http://www.w3.org/1999/xhtml'><p>test message 3</p></div>"
"</title>"
)
title_xhtml = self.getTitleXHTML(items[2])
title_xhtml = self.get_title_xhtml(items[2])
assert title_xhtml.toXml() == (
"<title xmlns='http://www.w3.org/2005/Atom' type='xhtml'>"
"<div xmlns='http://www.w3.org/1999/xhtml'><p>test message 1</p></div>"
@ -680,8 +680,8 @@ class TestActivityPubGateway:
@ed
async def test_pubsub_to_ap_conversion(self, ap_gateway, monkeypatch):
"""Pubsub nodes are converted to AP collections"""
monkeypatch.setattr(ap_gateway._p, "getItems", mock_getItems)
outbox = await ap_gateway.server.resource.APOutboxRequest(
monkeypatch.setattr(ap_gateway._p, "get_items", mock_get_items)
outbox = await ap_gateway.server.resource.ap_outbox_request(
**self.ap_request_params(ap_gateway, "outbox")
)
assert outbox["@context"] == ["https://www.w3.org/ns/activitystreams"]
@ -691,7 +691,7 @@ class TestActivityPubGateway:
assert outbox["first"]
assert outbox["last"]
first_page = await ap_gateway.server.resource.APOutboxPageRequest(
first_page = await ap_gateway.server.resource.ap_outbox_page_request(
**self.ap_request_params(ap_gateway, url=outbox["first"])
)
assert first_page["@context"] == ["https://www.w3.org/ns/activitystreams"]
@ -725,11 +725,11 @@ class TestActivityPubGateway:
"""AP following items are converted to Public Pubsub Subscription subscriptions"""
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)
monkeypatch.setattr(ap_gateway, "ap_get", mock_ap_get)
items, __ = await ap_gateway.pubsub_service.items(
jid.JID("toto@example.org"),
ap_gateway.getLocalJIDFromAccount(TEST_AP_ACCOUNT),
ap_gateway.get_local_jid_from_account(TEST_AP_ACCOUNT),
ap_gateway._pps.subscriptions_node,
None,
None,
@ -749,12 +749,12 @@ class TestActivityPubGateway:
"""AP followers items are converted to Public Pubsub Subscription subscribers"""
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)
monkeypatch.setattr(ap_gateway, "ap_get", mock_ap_get)
items, __ = await ap_gateway.pubsub_service.items(
jid.JID("toto@example.org"),
ap_gateway.getLocalJIDFromAccount(TEST_AP_ACCOUNT),
ap_gateway._pps.getPublicSubscribersNode(ap_gateway._m.namespace),
ap_gateway.get_local_jid_from_account(TEST_AP_ACCOUNT),
ap_gateway._pps.get_public_subscribers_node(ap_gateway._m.namespace),
None,
None,
None,
@ -773,22 +773,22 @@ class TestActivityPubGateway:
subscriptions = [
pubsub.Item(
id="subscription_1",
payload=ap_gateway._pps.buildSubscriptionElt(
payload=ap_gateway._pps.build_subscription_elt(
ap_gateway._m.namespace, jid.JID("local_user@test.example")
),
),
pubsub.Item(
id="subscription_2",
payload=ap_gateway._pps.buildSubscriptionElt(
payload=ap_gateway._pps.build_subscription_elt(
ap_gateway._m.namespace,
jid.JID("ext_user\\40example.org@ap.test.example"),
),
),
]
monkeypatch.setattr(
ap_gateway._p, "getItems", partial(mock_getItems, ret_items=subscriptions)
ap_gateway._p, "get_items", partial(mock_get_items, ret_items=subscriptions)
)
following = await ap_gateway.server.resource.APFollowingRequest(
following = await ap_gateway.server.resource.ap_following_request(
**self.ap_request_params(ap_gateway, "following")
)
assert following["@context"] == ["https://www.w3.org/ns/activitystreams"]
@ -812,21 +812,21 @@ class TestActivityPubGateway:
subscribers = [
pubsub.Item(
id="subscriber_1",
payload=ap_gateway._pps.buildSubscriberElt(
payload=ap_gateway._pps.build_subscriber_elt(
jid.JID("local_user@test.example")
),
),
pubsub.Item(
id="subscriber_2",
payload=ap_gateway._pps.buildSubscriberElt(
payload=ap_gateway._pps.build_subscriber_elt(
jid.JID("ext_user\\40example.org@ap.test.example")
),
),
]
monkeypatch.setattr(
ap_gateway._p, "getItems", partial(mock_getItems, ret_items=subscribers)
ap_gateway._p, "get_items", partial(mock_get_items, ret_items=subscribers)
)
followers = await ap_gateway.server.resource.APFollowersRequest(
followers = await ap_gateway.server.resource.ap_followers_request(
**self.ap_request_params(ap_gateway, "followers")
)
assert followers["@context"] == ["https://www.w3.org/ns/activitystreams"]
@ -849,17 +849,17 @@ class TestActivityPubGateway:
"""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)
monkeypatch.setattr(ap_gateway, "ap_get", mock_ap_get)
mess_data = {
"from": TEST_JID,
"to": ap_gateway.getLocalJIDFromAccount(TEST_AP_ACCOUNT),
"to": ap_gateway.get_local_jid_from_account(TEST_AP_ACCOUNT),
"type": "chat",
"message": {"": "This is a test message."},
"extra": {"origin-id": "123"},
}
with patch.object(ap_gateway, "signAndPost") as signAndPost:
with patch.object(ap_gateway, "sign_and_post") as sign_and_post:
await ap_gateway.onMessage(ap_gateway.client, mess_data)
url, actor_id, doc = signAndPost.call_args[0]
url, actor_id, doc = sign_and_post.call_args[0]
assert url == "https://example.org/users/test_user/inbox"
assert actor_id == "https://test.example/_ap/actor/some_user@test.example"
obj = doc["object"]
@ -883,11 +883,11 @@ class TestActivityPubGateway:
"""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)
monkeypatch.setattr(ap_gateway, "ap_get", 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())
xmpp_actor_id = ap_gateway.build_apurl(ap_const.TYPE_ACTOR, TEST_JID.userhost())
direct_ap_message = {
"attributedTo": TEST_AP_ACTOR_ID,
"cc": [],
@ -898,11 +898,11 @@ class TestActivityPubGateway:
"to": [xmpp_actor_id],
"type": "Note",
}
client = ap_gateway.client.getVirtualClient(
ap_gateway.getLocalJIDFromAccount(TEST_AP_ACCOUNT)
client = ap_gateway.client.get_virtual_client(
ap_gateway.get_local_jid_from_account(TEST_AP_ACCOUNT)
)
with patch.object(client, "sendMessage") as sendMessage:
await ap_gateway.newAPItem(
await ap_gateway.new_ap_item(
client, None, ap_gateway._m.namespace, direct_ap_message
)
@ -917,38 +917,38 @@ class TestActivityPubGateway:
"""Pubsub retract requests are converted to AP delete activity"""
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)
monkeypatch.setattr(ap_gateway, "ap_get", mock_ap_get)
retract_id = "retract_123"
retract_elt = domish.Element((pubsub.NS_PUBSUB_EVENT, "retract"))
retract_elt["id"] = retract_id
items_event = pubsub.ItemsEvent(
sender=TEST_JID,
recipient=ap_gateway.getLocalJIDFromAccount(TEST_AP_ACCOUNT),
recipient=ap_gateway.get_local_jid_from_account(TEST_AP_ACCOUNT),
nodeIdentifier=ap_gateway._m.namespace,
items=[retract_elt],
headers={},
)
with patch.object(ap_gateway, "signAndPost") as signAndPost:
signAndPost.return_value = FakeTReqPostResponse()
with patch.object(ap_gateway, "sign_and_post") as sign_and_post:
sign_and_post.return_value = FakeTReqPostResponse()
# we simulate the reception of a retract event
await ap_gateway._itemsReceived(ap_gateway.client, items_event)
url, actor_id, doc = signAndPost.call_args[0]
jid_account = await ap_gateway.getAPAccountFromJidAndNode(TEST_JID, None)
jid_actor_id = ap_gateway.buildAPURL(ap_const.TYPE_ACTOR, jid_account)
await ap_gateway._items_received(ap_gateway.client, items_event)
url, actor_id, doc = sign_and_post.call_args[0]
jid_account = await ap_gateway.get_ap_account_from_jid_and_node(TEST_JID, None)
jid_actor_id = ap_gateway.build_apurl(ap_const.TYPE_ACTOR, jid_account)
assert url == f"{TEST_BASE_URL}/inbox"
assert actor_id == jid_actor_id
assert doc["type"] == "Delete"
assert doc["actor"] == jid_actor_id
obj = doc["object"]
assert obj["type"] == ap_const.TYPE_TOMBSTONE
url_item_id = ap_gateway.buildAPURL(ap_const.TYPE_ITEM, jid_account, retract_id)
url_item_id = ap_gateway.build_apurl(ap_const.TYPE_ITEM, jid_account, retract_id)
assert obj["id"] == url_item_id
@ed
async def test_ap_delete_to_pubsub_retract(self, ap_gateway):
"""AP delete activity is converted to pubsub retract"""
client = ap_gateway.client.getVirtualClient(
ap_gateway.getLocalJIDFromAccount(TEST_AP_ACCOUNT)
client = ap_gateway.client.get_virtual_client(
ap_gateway.get_local_jid_from_account(TEST_AP_ACCOUNT)
)
ap_item = {
@ -962,12 +962,12 @@ class TestActivityPubGateway:
with patch.multiple(
ap_gateway.host.memory.storage,
get=DEFAULT,
getPubsubNode=DEFAULT,
deletePubsubItems=DEFAULT,
get_pubsub_node=DEFAULT,
delete_pubsub_items=DEFAULT,
) as mock_objs:
mock_objs["get"].return_value = None
cached_node = MagicMock()
mock_objs["getPubsubNode"].return_value = cached_node
mock_objs["get_pubsub_node"].return_value = cached_node
subscription = MagicMock()
subscription.state = SubscriptionState.SUBSCRIBED
subscription.subscriber = TEST_JID
@ -976,7 +976,7 @@ class TestActivityPubGateway:
ap_gateway.pubsub_service, "notifyRetract"
) as notifyRetract:
# we simulate a received Delete activity
await ap_gateway.newAPDeleteItem(
await ap_gateway.new_ap_delete_item(
client=client,
destinee=None,
node=ap_gateway._m.namespace,
@ -984,9 +984,9 @@ class TestActivityPubGateway:
)
# item is deleted from database
deletePubsubItems = mock_objs["deletePubsubItems"]
assert deletePubsubItems.call_count == 1
assert deletePubsubItems.call_args.args[1] == [ap_item["id"]]
delete_pubsub_items = mock_objs["delete_pubsub_items"]
assert delete_pubsub_items.call_count == 1
assert delete_pubsub_items.call_args.args[1] == [ap_item["id"]]
# retraction notification is sent to subscribers
assert notifyRetract.call_count == 1
@ -1007,42 +1007,42 @@ class TestActivityPubGateway:
"""Message retract requests are converted to AP delete activity"""
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)
monkeypatch.setattr(ap_gateway, "ap_get", mock_ap_get)
# origin ID is the ID of the message to retract
origin_id = "mess_retract_123"
# we call retractByOriginId to get the message element of a retraction request
# we call retract_by_origin_id to get the message element of a retraction request
fake_client = MagicMock()
fake_client.jid = TEST_JID
dest_jid = ap_gateway.getLocalJIDFromAccount(TEST_AP_ACCOUNT)
ap_gateway._r.retractByOriginId(fake_client, dest_jid, origin_id)
dest_jid = ap_gateway.get_local_jid_from_account(TEST_AP_ACCOUNT)
ap_gateway._r.retract_by_origin_id(fake_client, dest_jid, origin_id)
# message_retract_elt is the message which would be sent for a retraction
message_retract_elt = fake_client.send.call_args.args[0]
apply_to_elt = next(message_retract_elt.elements(NS_FASTEN, "apply-to"))
retract_elt = apply_to_elt.retract
with patch.object(ap_gateway, "signAndPost") as signAndPost:
signAndPost.return_value = FakeTReqPostResponse()
with patch.object(ap_gateway, "sign_and_post") as sign_and_post:
sign_and_post.return_value = FakeTReqPostResponse()
fake_fastened_elts = MagicMock()
fake_fastened_elts.id = origin_id
# we simulate the reception of a retract event using the message element that
# we generated above
await ap_gateway._onMessageRetract(
await ap_gateway._on_message_retract(
ap_gateway.client, message_retract_elt, retract_elt, fake_fastened_elts
)
url, actor_id, doc = signAndPost.call_args[0]
url, actor_id, doc = sign_and_post.call_args[0]
# the AP delete activity must have been sent through signAndPost
# the AP delete activity must have been sent through sign_and_post
# we check its values
jid_account = await ap_gateway.getAPAccountFromJidAndNode(TEST_JID, None)
jid_actor_id = ap_gateway.buildAPURL(ap_const.TYPE_ACTOR, jid_account)
jid_account = await ap_gateway.get_ap_account_from_jid_and_node(TEST_JID, None)
jid_actor_id = ap_gateway.build_apurl(ap_const.TYPE_ACTOR, jid_account)
assert url == f"{TEST_BASE_URL}/users/{TEST_USER}/inbox"
assert actor_id == jid_actor_id
assert doc["type"] == "Delete"
assert doc["actor"] == jid_actor_id
obj = doc["object"]
assert obj["type"] == ap_const.TYPE_TOMBSTONE
url_item_id = ap_gateway.buildAPURL(ap_const.TYPE_ITEM, jid_account, origin_id)
url_item_id = ap_gateway.build_apurl(ap_const.TYPE_ITEM, jid_account, origin_id)
assert obj["id"] == url_item_id
@ed
@ -1053,11 +1053,11 @@ class TestActivityPubGateway:
# by ``test_ap_delete_to_pubsub_retract``)
# we don't want actual queries in database
retractDBHistory = AsyncMock()
monkeypatch.setattr(ap_gateway._r, "retractDBHistory", retractDBHistory)
retract_db_history = AsyncMock()
monkeypatch.setattr(ap_gateway._r, "retract_db_history", retract_db_history)
client = ap_gateway.client.getVirtualClient(
ap_gateway.getLocalJIDFromAccount(TEST_AP_ACCOUNT)
client = ap_gateway.client.get_virtual_client(
ap_gateway.get_local_jid_from_account(TEST_AP_ACCOUNT)
)
fake_send = MagicMock()
monkeypatch.setattr(client, "send", fake_send)
@ -1077,14 +1077,14 @@ class TestActivityPubGateway:
fake_history.origin_id = ap_item["id"]
storage_get.return_value = fake_history
# we simulate a received Delete activity
await ap_gateway.newAPDeleteItem(
await ap_gateway.new_ap_delete_item(
client=client, destinee=None, node=ap_gateway._m.namespace, item=ap_item
)
# item is deleted from database
assert retractDBHistory.call_count == 1
assert retractDBHistory.call_args.args[0] == client
assert retractDBHistory.call_args.args[1] == fake_history
assert retract_db_history.call_count == 1
assert retract_db_history.call_args.args[0] == client
assert retract_db_history.call_args.args[1] == fake_history
# retraction notification is sent to destinee
assert fake_send.call_count == 1
@ -1103,11 +1103,11 @@ class TestActivityPubGateway:
"""AP actor metadata are converted to XMPP/vCard4"""
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)
monkeypatch.setattr(ap_gateway, "ap_get", mock_ap_get)
items, __ = await ap_gateway.pubsub_service.items(
jid.JID("toto@example.org"),
ap_gateway.getLocalJIDFromAccount(TEST_AP_ACCOUNT),
ap_gateway.get_local_jid_from_account(TEST_AP_ACCOUNT),
# VCard4 node
ap_gateway._v.node,
None,
@ -1116,7 +1116,7 @@ class TestActivityPubGateway:
)
assert len(items) == 1
vcard_elt = next(items[0].elements(ap_gateway._v.namespace, "vcard"))
vcard = ap_gateway._v.vcard2Dict(vcard_elt)
vcard = ap_gateway._v.vcard_2_dict(vcard_elt)
assert "test_user nickname" in vcard["nicknames"]
assert vcard["description"] == "test account"
@ -1125,12 +1125,12 @@ class TestActivityPubGateway:
"""XMPP identity is converted to AP actor metadata"""
# XXX: XMPP identity is normally an amalgam of metadata from several
# XEPs/locations (vCard4, vcard-tmp, etc)
with patch.object(ap_gateway._i, "getIdentity") as getIdentity:
getIdentity.return_value = {
with patch.object(ap_gateway._i, "get_identity") as get_identity:
get_identity.return_value = {
"nicknames": ["nick1", "nick2"],
"description": "test description",
}
actor_data = await ap_gateway.server.resource.APActorRequest(
actor_data = await ap_gateway.server.resource.ap_actor_request(
**self.ap_request_params(ap_gateway, ap_const.TYPE_ACTOR)
)
@ -1143,9 +1143,9 @@ class TestActivityPubGateway:
"""AP mentions by direct addressing are converted to XEP-0372 references"""
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)
monkeypatch.setattr(ap_gateway, "ap_get", mock_ap_get)
xmpp_actor_id = ap_gateway.buildAPURL(ap_const.TYPE_ACTOR, TEST_JID.userhost())
xmpp_actor_id = ap_gateway.build_apurl(ap_const.TYPE_ACTOR, TEST_JID.userhost())
direct_addr_mention = {
"attributedTo": TEST_AP_ACTOR_ID,
@ -1156,37 +1156,37 @@ class TestActivityPubGateway:
"to": [ap_const.NS_AP_PUBLIC, xmpp_actor_id],
"type": "Note",
}
client = ap_gateway.client.getVirtualClient(
ap_gateway.getLocalJIDFromAccount(TEST_AP_ACCOUNT)
client = ap_gateway.client.get_virtual_client(
ap_gateway.get_local_jid_from_account(TEST_AP_ACCOUNT)
)
monkeypatch.setattr(client, "sendMessage", MagicMock())
with patch.object(ap_gateway._refs, "sendReference") as sendReference:
await ap_gateway.newAPItem(
with patch.object(ap_gateway._refs, "send_reference") as send_reference:
await ap_gateway.new_ap_item(
client, None, ap_gateway._m.namespace, direct_addr_mention
)
assert sendReference.call_count == 1
assert sendReference.call_args.kwargs["to_jid"] == TEST_JID
assert send_reference.call_count == 1
assert send_reference.call_args.kwargs["to_jid"] == TEST_JID
local_actor_jid = ap_gateway.getLocalJIDFromAccount(TEST_AP_ACCOUNT)
expected_anchor = xmpp_uri.buildXMPPUri(
local_actor_jid = ap_gateway.get_local_jid_from_account(TEST_AP_ACCOUNT)
expected_anchor = xmpp_uri.build_xmpp_uri(
"pubsub",
path=local_actor_jid.full(),
node=ap_gateway._m.namespace,
item=direct_addr_mention["id"],
)
assert sendReference.call_args.kwargs["anchor"] == expected_anchor
assert send_reference.call_args.kwargs["anchor"] == expected_anchor
@ed
async def test_tag_mention_to_reference(self, ap_gateway, monkeypatch):
"""AP mentions in "tag" field are converted to XEP-0372 references"""
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)
monkeypatch.setattr(ap_gateway, "ap_get", mock_ap_get)
xmpp_actor_id = ap_gateway.buildAPURL(ap_const.TYPE_ACTOR, TEST_JID.userhost())
xmpp_actor_id = ap_gateway.build_apurl(ap_const.TYPE_ACTOR, TEST_JID.userhost())
direct_addr_mention = {
"attributedTo": TEST_AP_ACTOR_ID,
@ -1198,35 +1198,35 @@ class TestActivityPubGateway:
"tag": [{"type": "Mention", "href": xmpp_actor_id, "name": f"@{TEST_JID}'"}],
"type": "Note",
}
client = ap_gateway.client.getVirtualClient(
ap_gateway.getLocalJIDFromAccount(TEST_AP_ACCOUNT)
client = ap_gateway.client.get_virtual_client(
ap_gateway.get_local_jid_from_account(TEST_AP_ACCOUNT)
)
monkeypatch.setattr(client, "sendMessage", MagicMock())
with patch.object(ap_gateway._refs, "sendReference") as sendReference:
await ap_gateway.newAPItem(
with patch.object(ap_gateway._refs, "send_reference") as send_reference:
await ap_gateway.new_ap_item(
client, None, ap_gateway._m.namespace, direct_addr_mention
)
assert sendReference.call_count == 1
assert sendReference.call_args.kwargs["to_jid"] == TEST_JID
assert send_reference.call_count == 1
assert send_reference.call_args.kwargs["to_jid"] == TEST_JID
local_actor_jid = ap_gateway.getLocalJIDFromAccount(TEST_AP_ACCOUNT)
expected_anchor = xmpp_uri.buildXMPPUri(
local_actor_jid = ap_gateway.get_local_jid_from_account(TEST_AP_ACCOUNT)
expected_anchor = xmpp_uri.build_xmpp_uri(
"pubsub",
path=local_actor_jid.full(),
node=ap_gateway._m.namespace,
item=direct_addr_mention["id"],
)
assert sendReference.call_args.kwargs["anchor"] == expected_anchor
assert send_reference.call_args.kwargs["anchor"] == expected_anchor
@ed
async def test_auto_mentions(self, ap_gateway, monkeypatch):
"""Check that mentions in body are converted to AP mentions"""
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)
monkeypatch.setattr(ap_gateway, "ap_get", mock_ap_get)
mb_data = {
"author_jid": TEST_JID.full(),
@ -1254,7 +1254,7 @@ class TestActivityPubGateway:
# in mb_data_2_ap_item
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)
monkeypatch.setattr(ap_gateway, "ap_get", mock_ap_get)
mb_data = {
"author_jid": TEST_JID.full(),
@ -1275,11 +1275,11 @@ class TestActivityPubGateway:
"""Check that XEP-0372 references are converted to AP mention"""
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)
monkeypatch.setattr(ap_gateway, "ap_get", mock_ap_get)
local_actor_jid = ap_gateway.getLocalJIDFromAccount(TEST_AP_ACCOUNT)
local_actor_jid = ap_gateway.get_local_jid_from_account(TEST_AP_ACCOUNT)
item_elt = XMPP_ITEMS[0]
anchor = xmpp_uri.buildXMPPUri(
anchor = xmpp_uri.build_xmpp_uri(
"pubsub",
path=TEST_JID.full(),
node=ap_gateway._m.namespace,
@ -1287,18 +1287,18 @@ class TestActivityPubGateway:
)
ref_data: Dict[str, Union[str, int, dict]] = {
"uri": xmpp_uri.buildXMPPUri(None, path=local_actor_jid.full()),
"uri": xmpp_uri.build_xmpp_uri(None, path=local_actor_jid.full()),
"type_": "mention",
"anchor": anchor,
}
reference_elt = ap_gateway._refs.buildRefElement(**ref_data)
reference_elt = ap_gateway._refs.build_ref_element(**ref_data)
# we now update ref_data to look like what is received in the trigger
ref_data["parsed_uri"] = xmpp_uri.parseXMPPUri(ref_data["uri"])
ref_data["parsed_anchor"] = xmpp_uri.parseXMPPUri(ref_data["anchor"])
ref_data["parsed_uri"] = xmpp_uri.parse_xmpp_uri(ref_data["uri"])
ref_data["parsed_anchor"] = xmpp_uri.parse_xmpp_uri(ref_data["anchor"])
# "type" is a builtin function, thus "type_" is used in buildRefElement, but in
# "type" is a builtin function, thus "type_" is used in build_ref_element, but in
# ref_data is "type" without underscore
ref_data["type"] = ref_data["type_"]
del ref_data["type_"]
@ -1306,22 +1306,22 @@ class TestActivityPubGateway:
message_elt = domish.Element((None, "message"))
message_elt.addChild(reference_elt)
with patch.object(ap_gateway.host.memory.storage, "getItems") as getItems:
# getItems returns a sqla_mapping.PubsubItem, thus we need to fake it and set
with patch.object(ap_gateway.host.memory.storage, "get_items") as get_items:
# get_items returns a sqla_mapping.PubsubItem, thus we need to fake it and set
# the item_elt we want to use in its "data" attribute
mock_pubsub_item = MagicMock
mock_pubsub_item.data = item_elt
getItems.return_value = ([mock_pubsub_item], {})
with patch.object(ap_gateway, "signAndPost") as signAndPost:
signAndPost.return_value.code = 202
await ap_gateway._onReferenceReceived(
get_items.return_value = ([mock_pubsub_item], {})
with patch.object(ap_gateway, "sign_and_post") as sign_and_post:
sign_and_post.return_value.code = 202
await ap_gateway._on_reference_received(
ap_gateway.client, message_elt, ref_data
)
# when reference is received, the referencing item must be sent to referenced
# actor, and they must be in "to" field and in "tag"
assert signAndPost.call_count == 1
send_ap_item = signAndPost.call_args.args[-1]
assert sign_and_post.call_count == 1
send_ap_item = sign_and_post.call_args.args[-1]
ap_object = send_ap_item["object"]
assert TEST_AP_ACTOR_ID in ap_object["to"]
expected_mention = {
@ -1338,13 +1338,13 @@ class TestActivityPubGateway:
"""XEP-0272 post repeat is converted to AP Announce activity"""
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)
monkeypatch.setattr(ap_gateway, "ap_get", mock_ap_get)
# JID repeated AP actor (also the recipient of the message)
recipient_jid = ap_gateway.getLocalJIDFromAccount(TEST_AP_ACCOUNT)
recipient_jid = ap_gateway.get_local_jid_from_account(TEST_AP_ACCOUNT)
# repeated item
ap_item = TEST_AP_ITEMS[0]
ap_item_url = xmpp_uri.buildXMPPUri(
ap_item_url = xmpp_uri.build_xmpp_uri(
"pubsub",
path=recipient_jid.full(),
node=ap_gateway._m.namespace,
@ -1374,9 +1374,9 @@ class TestActivityPubGateway:
)
item_elt.uri = pubsub.NS_PUBSUB_EVENT
with patch.object(ap_gateway, "signAndPost") as signAndPost:
signAndPost.return_value.code = 202
await ap_gateway.convertAndPostItems(
with patch.object(ap_gateway, "sign_and_post") as sign_and_post:
sign_and_post.return_value.code = 202
await ap_gateway.convert_and_post_items(
ap_gateway.client,
TEST_AP_ACCOUNT,
TEST_JID,
@ -1384,10 +1384,10 @@ class TestActivityPubGateway:
[item_elt],
)
assert signAndPost.called
url, actor_id, doc = signAndPost.call_args.args
assert sign_and_post.called
url, actor_id, doc = sign_and_post.call_args.args
assert url == TEST_USER_DATA["endpoints"]["sharedInbox"]
assert actor_id == ap_gateway.buildAPURL(ap_const.TYPE_ACTOR, TEST_JID.userhost())
assert actor_id == ap_gateway.build_apurl(ap_const.TYPE_ACTOR, TEST_JID.userhost())
assert doc["type"] == "Announce"
assert ap_const.NS_AP_PUBLIC in doc["to"]
assert doc["object"] == ap_item["id"]
@ -1397,12 +1397,12 @@ class TestActivityPubGateway:
"""AP Announce activity is converted to XEP-0272 post repeat"""
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)
monkeypatch.setattr(ap_gateway, "ap_get", mock_ap_get)
xmpp_actor_id = ap_gateway.buildAPURL(ap_const.TYPE_ACTOR, TEST_JID.userhost())
xmpp_actor_id = ap_gateway.build_apurl(ap_const.TYPE_ACTOR, TEST_JID.userhost())
# announced item
xmpp_item = XMPP_ITEMS[0]
xmpp_item_url = ap_gateway.buildAPURL(
xmpp_item_url = ap_gateway.build_apurl(
ap_const.TYPE_ITEM, TEST_JID.userhost(), xmpp_item["id"]
)
announce = {
@ -1415,25 +1415,25 @@ class TestActivityPubGateway:
"published": "2022-07-22T09:24:12Z",
"to": [ap_const.NS_AP_PUBLIC],
}
with patch.object(ap_gateway.host.memory.storage, "getItems") as getItems:
with patch.object(ap_gateway.host.memory.storage, "get_items") as get_items:
mock_pubsub_item = MagicMock
mock_pubsub_item.data = xmpp_item
getItems.return_value = ([mock_pubsub_item], {})
get_items.return_value = ([mock_pubsub_item], {})
with patch.object(
ap_gateway.host.memory.storage, "cachePubsubItems"
) as cachePubsubItems:
await ap_gateway.server.resource.handleAnnounceActivity(
ap_gateway.host.memory.storage, "cache_pubsub_items"
) as cache_pubsub_items:
await ap_gateway.server.resource.handle_announce_activity(
Request(MagicMock()), announce, None, None, None, "", TEST_AP_ACTOR_ID
)
assert cachePubsubItems.called
assert cache_pubsub_items.called
# the microblog data put in cache correspond to the item sent to subscribers
__, __, __, [mb_data] = cachePubsubItems.call_args.args
__, __, __, [mb_data] = cache_pubsub_items.call_args.args
extra = mb_data["extra"]
assert "repeated" in extra
repeated = extra["repeated"]
assert repeated["by"] == ap_gateway.getLocalJIDFromAccount(TEST_AP_ACCOUNT).full()
xmpp_item_xmpp_url = xmpp_uri.buildXMPPUri(
assert repeated["by"] == ap_gateway.get_local_jid_from_account(TEST_AP_ACCOUNT).full()
xmpp_item_xmpp_url = xmpp_uri.build_xmpp_uri(
"pubsub",
path=TEST_JID.full(),
node=ap_gateway._m.namespace,
@ -1446,12 +1446,12 @@ class TestActivityPubGateway:
"""Pubsub-attachments ``noticed`` is converted to AP Like activity"""
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)
monkeypatch.setattr(ap_gateway, "ap_get", mock_ap_get)
recipient_jid = ap_gateway.getLocalJIDFromAccount(TEST_AP_ACCOUNT)
recipient_jid = ap_gateway.get_local_jid_from_account(TEST_AP_ACCOUNT)
# noticed item
ap_item = TEST_AP_ITEMS[0]
attachment_node = ap_gateway._pa.getAttachmentNodeName(
attachment_node = ap_gateway._pa.get_attachment_node_name(
recipient_jid, ap_gateway._m.namespace, ap_item["id"]
)
item_elt = xml_tools.parse(
@ -1468,14 +1468,14 @@ class TestActivityPubGateway:
TEST_JID, recipient_jid, attachment_node, [item_elt], {}
)
with patch.object(ap_gateway, "signAndPost") as signAndPost:
signAndPost.return_value.code = 202
await ap_gateway._itemsReceived(ap_gateway.client, items_event)
with patch.object(ap_gateway, "sign_and_post") as sign_and_post:
sign_and_post.return_value.code = 202
await ap_gateway._items_received(ap_gateway.client, items_event)
assert signAndPost.called
url, actor_id, doc = signAndPost.call_args.args
assert sign_and_post.called
url, actor_id, doc = sign_and_post.call_args.args
assert url == TEST_USER_DATA["endpoints"]["sharedInbox"]
assert actor_id == ap_gateway.buildAPURL(ap_const.TYPE_ACTOR, TEST_JID.userhost())
assert actor_id == ap_gateway.build_apurl(ap_const.TYPE_ACTOR, TEST_JID.userhost())
assert doc["type"] == "Like"
assert ap_const.NS_AP_PUBLIC in doc["cc"]
assert doc["object"] == ap_item["id"]
@ -1485,12 +1485,12 @@ class TestActivityPubGateway:
"""AP Like activity is converted to ``noticed`` attachment"""
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)
monkeypatch.setattr(ap_gateway, "ap_get", mock_ap_get)
xmpp_actor_id = ap_gateway.buildAPURL(ap_const.TYPE_ACTOR, TEST_JID.userhost())
xmpp_actor_id = ap_gateway.build_apurl(ap_const.TYPE_ACTOR, TEST_JID.userhost())
# liked item
xmpp_item = XMPP_ITEMS[0]
xmpp_item_url = ap_gateway.buildAPURL(
xmpp_item_url = ap_gateway.build_apurl(
ap_const.TYPE_ITEM, TEST_JID.userhost(), xmpp_item["id"]
)
like = {
@ -1503,23 +1503,23 @@ class TestActivityPubGateway:
"published": "2022-07-22T09:24:12Z",
"to": [ap_const.NS_AP_PUBLIC],
}
with patch.object(ap_gateway.host.memory.storage, "getItems") as getItems:
getItems.return_value = ([], {})
with patch.object(ap_gateway._p, "sendItems") as sendItems:
await ap_gateway.server.resource.APInboxRequest(
with patch.object(ap_gateway.host.memory.storage, "get_items") as get_items:
get_items.return_value = ([], {})
with patch.object(ap_gateway._p, "send_items") as send_items:
await ap_gateway.server.resource.ap_inbox_request(
**self.ap_request_params(
ap_gateway, "inbox", data=like, signing_actor=TEST_AP_ACTOR_ID
)
)
assert sendItems.called
si_client, si_service, si_node, [si_item] = sendItems.call_args.args
assert si_client.jid == ap_gateway.getLocalJIDFromAccount(TEST_AP_ACCOUNT)
assert send_items.called
si_client, si_service, si_node, [si_item] = send_items.call_args.args
assert si_client.jid == ap_gateway.get_local_jid_from_account(TEST_AP_ACCOUNT)
assert si_service == TEST_JID
assert si_node == ap_gateway._pa.getAttachmentNodeName(
assert si_node == ap_gateway._pa.get_attachment_node_name(
TEST_JID, ap_gateway._m.namespace, xmpp_item["id"]
)
[parsed_item] = ap_gateway._pa.items2attachmentData(si_client, [si_item])
[parsed_item] = ap_gateway._pa.items_2_attachment_data(si_client, [si_item])
assert parsed_item["from"] == si_client.jid.full()
assert "noticed" in parsed_item
assert parsed_item["noticed"]["noticed"] == True
@ -1529,18 +1529,18 @@ class TestActivityPubGateway:
"""Pubsub-attachments ``reactions`` is converted to AP EmojiReact activity"""
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)
monkeypatch.setattr(ap_gateway, "ap_get", mock_ap_get)
recipient_jid = ap_gateway.getLocalJIDFromAccount(TEST_AP_ACCOUNT)
recipient_jid = ap_gateway.get_local_jid_from_account(TEST_AP_ACCOUNT)
# noticed item
ap_item = TEST_AP_ITEMS[0]
ap_item_url = xmpp_uri.buildXMPPUri(
ap_item_url = xmpp_uri.build_xmpp_uri(
"pubsub",
path=recipient_jid.full(),
node=ap_gateway._m.namespace,
item=ap_item["id"],
)
attachment_node = ap_gateway._pa.getAttachmentNodeName(
attachment_node = ap_gateway._pa.get_attachment_node_name(
recipient_jid, ap_gateway._m.namespace, ap_item["id"]
)
reactions = ["🦁", "🥜", "🎻"]
@ -1562,15 +1562,15 @@ class TestActivityPubGateway:
TEST_JID, recipient_jid, attachment_node, [item_elt], {}
)
with patch.object(ap_gateway, "signAndPost") as signAndPost:
signAndPost.return_value.code = 202
await ap_gateway._itemsReceived(ap_gateway.client, items_event)
with patch.object(ap_gateway, "sign_and_post") as sign_and_post:
sign_and_post.return_value.code = 202
await ap_gateway._items_received(ap_gateway.client, items_event)
assert signAndPost.call_count == 3
for idx, call_args in enumerate(signAndPost.call_args_list):
assert sign_and_post.call_count == 3
for idx, call_args in enumerate(sign_and_post.call_args_list):
url, actor_id, doc = call_args.args
assert url == TEST_USER_DATA["endpoints"]["sharedInbox"]
assert actor_id == ap_gateway.buildAPURL(
assert actor_id == ap_gateway.build_apurl(
ap_const.TYPE_ACTOR, TEST_JID.userhost()
)
assert doc["type"] == "EmojiReact"
@ -1588,12 +1588,12 @@ class TestActivityPubGateway:
"""AP EmojiReact activity is converted to ``reactions`` attachment"""
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)
monkeypatch.setattr(ap_gateway, "ap_get", mock_ap_get)
xmpp_actor_id = ap_gateway.buildAPURL(ap_const.TYPE_ACTOR, TEST_JID.userhost())
xmpp_actor_id = ap_gateway.build_apurl(ap_const.TYPE_ACTOR, TEST_JID.userhost())
# item on which reaction is attached
xmpp_item = XMPP_ITEMS[0]
xmpp_item_url = ap_gateway.buildAPURL(
xmpp_item_url = ap_gateway.build_apurl(
ap_const.TYPE_ITEM, TEST_JID.userhost(), xmpp_item["id"]
)
like = {
@ -1607,23 +1607,23 @@ class TestActivityPubGateway:
"published": "2022-07-22T09:24:12Z",
"to": [ap_const.NS_AP_PUBLIC],
}
with patch.object(ap_gateway.host.memory.storage, "getItems") as getItems:
getItems.return_value = ([], {})
with patch.object(ap_gateway._p, "sendItems") as sendItems:
await ap_gateway.server.resource.APInboxRequest(
with patch.object(ap_gateway.host.memory.storage, "get_items") as get_items:
get_items.return_value = ([], {})
with patch.object(ap_gateway._p, "send_items") as send_items:
await ap_gateway.server.resource.ap_inbox_request(
**self.ap_request_params(
ap_gateway, "inbox", data=like, signing_actor=TEST_AP_ACTOR_ID
)
)
assert sendItems.called
si_client, si_service, si_node, [si_item] = sendItems.call_args.args
assert si_client.jid == ap_gateway.getLocalJIDFromAccount(TEST_AP_ACCOUNT)
assert send_items.called
si_client, si_service, si_node, [si_item] = send_items.call_args.args
assert si_client.jid == ap_gateway.get_local_jid_from_account(TEST_AP_ACCOUNT)
assert si_service == TEST_JID
assert si_node == ap_gateway._pa.getAttachmentNodeName(
assert si_node == ap_gateway._pa.get_attachment_node_name(
TEST_JID, ap_gateway._m.namespace, xmpp_item["id"]
)
[parsed_item] = ap_gateway._pa.items2attachmentData(si_client, [si_item])
[parsed_item] = ap_gateway._pa.items_2_attachment_data(si_client, [si_item])
assert parsed_item["from"] == si_client.jid.full()
assert "reactions" in parsed_item
assert parsed_item["reactions"]["reactions"] == ["🐅"]

View file

@ -27,22 +27,22 @@ class TestPubsubCache:
@ed
async def test_cache_is_used_transparently(self, host, client):
"""Cache is used when a pubsub getItems operation is done"""
"""Cache is used when a pubsub get_items operation is done"""
items_ret = defer.Deferred()
items_ret.callback(([], {}))
client.pubsub_client.items = MagicMock(return_value=items_ret)
host.memory.storage.getPubsubNode.return_value = None
pubsub_node = host.memory.storage.setPubsubNode.return_value = PubsubNode(
host.memory.storage.get_pubsub_node.return_value = None
pubsub_node = host.memory.storage.set_pubsub_node.return_value = PubsubNode(
sync_state = None
)
with patch.object(host.plugins["PUBSUB_CACHE"], "cacheNode") as cacheNode:
await host.plugins["XEP-0060"].getItems(
with patch.object(host.plugins["PUBSUB_CACHE"], "cache_node") as cache_node:
await host.plugins["XEP-0060"].get_items(
client,
None,
"urn:xmpp:microblog:0",
)
assert cacheNode.call_count == 1
assert cacheNode.call_args.args[-1] == pubsub_node
assert cache_node.call_count == 1
assert cache_node.call_args.args[-1] == pubsub_node
@ed
async def test_cache_is_skipped_with_use_cache_false(self, host, client):
@ -50,18 +50,18 @@ class TestPubsubCache:
items_ret = defer.Deferred()
items_ret.callback(([], {}))
client.pubsub_client.items = MagicMock(return_value=items_ret)
host.memory.storage.getPubsubNode.return_value = None
host.memory.storage.setPubsubNode.return_value = PubsubNode(
host.memory.storage.get_pubsub_node.return_value = None
host.memory.storage.set_pubsub_node.return_value = PubsubNode(
sync_state = None
)
with patch.object(host.plugins["PUBSUB_CACHE"], "cacheNode") as cacheNode:
await host.plugins["XEP-0060"].getItems(
with patch.object(host.plugins["PUBSUB_CACHE"], "cache_node") as cache_node:
await host.plugins["XEP-0060"].get_items(
client,
None,
"urn:xmpp:microblog:0",
extra = {C.KEY_USE_CACHE: False}
)
assert not cacheNode.called
assert not cache_node.called
@ed
async def test_cache_is_not_used_when_no_cache(self, host, client):
@ -70,17 +70,17 @@ class TestPubsubCache:
items_ret = defer.Deferred()
items_ret.callback(([], {}))
client.pubsub_client.items = MagicMock(return_value=items_ret)
host.memory.storage.getPubsubNode.return_value = None
host.memory.storage.setPubsubNode.return_value = PubsubNode(
host.memory.storage.get_pubsub_node.return_value = None
host.memory.storage.set_pubsub_node.return_value = PubsubNode(
sync_state = None
)
with patch.object(host.plugins["PUBSUB_CACHE"], "cacheNode") as cacheNode:
await host.plugins["XEP-0060"].getItems(
with patch.object(host.plugins["PUBSUB_CACHE"], "cache_node") as cache_node:
await host.plugins["XEP-0060"].get_items(
client,
None,
"urn:xmpp:microblog:0",
)
assert not cacheNode.called
assert not cache_node.called
@ed
@ -89,20 +89,20 @@ class TestPubsubCache:
items_ret = defer.Deferred()
items_ret.callback(([], {}))
client.pubsub_client.items = MagicMock(return_value=items_ret)
host.memory.storage.getPubsubNode.return_value = PubsubNode(
host.memory.storage.get_pubsub_node.return_value = PubsubNode(
sync_state = SyncState.COMPLETED
)
with patch.object(
host.plugins["PUBSUB_CACHE"],
"getItemsFromCache"
) as getItemsFromCache:
getItemsFromCache.return_value = ([], {})
await host.plugins["XEP-0060"].getItems(
"get_items_from_cache"
) as get_items_from_cache:
get_items_from_cache.return_value = ([], {})
await host.plugins["XEP-0060"].get_items(
client,
None,
"urn:xmpp:microblog:0",
)
assert getItemsFromCache.call_count == 1
assert get_items_from_cache.call_count == 1
assert not client.pubsub_client.items.called
@ed
@ -111,21 +111,21 @@ class TestPubsubCache:
items_ret = defer.Deferred()
items_ret.callback(([], {}))
client.pubsub_client.items = MagicMock(return_value=items_ret)
host.memory.storage.getPubsubNode.return_value = PubsubNode(
host.memory.storage.get_pubsub_node.return_value = PubsubNode(
sync_state = SyncState.IN_PROGRESS
)
with patch.object(host.plugins["PUBSUB_CACHE"], "analyseNode") as analyseNode:
analyseNode.return_value = {"to_sync": True}
with patch.object(host.plugins["PUBSUB_CACHE"], "analyse_node") as analyse_node:
analyse_node.return_value = {"to_sync": True}
with patch.object(
host.plugins["PUBSUB_CACHE"],
"getItemsFromCache"
) as getItemsFromCache:
getItemsFromCache.return_value = ([], {})
"get_items_from_cache"
) as get_items_from_cache:
get_items_from_cache.return_value = ([], {})
assert client.pubsub_client.items.call_count == 0
await host.plugins["XEP-0060"].getItems(
await host.plugins["XEP-0060"].get_items(
client,
None,
"urn:xmpp:microblog:0",
)
assert not getItemsFromCache.called
assert not get_items_from_cache.called
assert client.pubsub_client.items.call_count == 1