Mercurial > libervia-backend
changeset 3907:755920bd30da
tests (unit/AP gateway): fix tests:
rel 372
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 22 Sep 2022 00:01:48 +0200 |
parents | d8baf92cb921 |
children | d43b197735d1 |
files | tests/unit/test_ap-gateway.py |
diffstat | 1 files changed, 227 insertions(+), 303 deletions(-) [+] |
line wrap: on
line diff
--- a/tests/unit/test_ap-gateway.py Thu Sep 22 00:01:48 2022 +0200 +++ b/tests/unit/test_ap-gateway.py Thu Sep 22 00:01:48 2022 +0200 @@ -62,25 +62,22 @@ f"resource=acct:{parse.quote(TEST_AP_ACCOUNT)}": { "aliases": [ f"{TEST_BASE_URL}/@{TEST_USER}", - f"{TEST_BASE_URL}/users/{TEST_USER}" + f"{TEST_BASE_URL}/users/{TEST_USER}", ], "links": [ { "href": f"{TEST_BASE_URL}/users/{TEST_USER}", "rel": "self", - "type": "application/activity+json" + "type": "application/activity+json", }, ], - "subject": f"acct:{TEST_AP_ACCOUNT}" + "subject": f"acct:{TEST_AP_ACCOUNT}", }, - f"{TEST_BASE_URL}/users/{TEST_USER}": { "@context": [ "https://www.w3.org/ns/activitystreams", ], - "endpoints": { - "sharedInbox": f"{TEST_BASE_URL}/inbox" - }, + "endpoints": {"sharedInbox": f"{TEST_BASE_URL}/inbox"}, "followers": f"{TEST_BASE_URL}/users/{TEST_USER}/followers", "following": f"{TEST_BASE_URL}/users/{TEST_USER}/following", "id": f"{TEST_BASE_URL}/users/{TEST_USER}", @@ -90,30 +87,25 @@ "outbox": f"{TEST_BASE_URL}/users/{TEST_USER}/outbox", "preferredUsername": f"{TEST_USER}", "type": "Person", - "url": f"{TEST_BASE_URL}/@{TEST_USER}" + "url": f"{TEST_BASE_URL}/@{TEST_USER}", }, f"{TEST_BASE_URL}/.well-known/webfinger?" f"resource=acct:{parse.quote('ext_user@example.org')}": { - "aliases": [ - f"{TEST_BASE_URL}/@ext_user", - f"{TEST_BASE_URL}/users/ext_user" - ], + "aliases": [f"{TEST_BASE_URL}/@ext_user", f"{TEST_BASE_URL}/users/ext_user"], "links": [ { "href": f"{TEST_BASE_URL}/users/ext_user", "rel": "self", - "type": "application/activity+json" + "type": "application/activity+json", }, ], - "subject": f"acct:ext_user@example.org" + "subject": f"acct:ext_user@example.org", }, f"{TEST_BASE_URL}/users/ext_user": { "@context": [ "https://www.w3.org/ns/activitystreams", ], - "endpoints": { - "sharedInbox": f"{TEST_BASE_URL}/inbox" - }, + "endpoints": {"sharedInbox": f"{TEST_BASE_URL}/inbox"}, "followers": f"{TEST_BASE_URL}/users/ext_user/followers", "following": f"{TEST_BASE_URL}/users/ext_user/following", "id": f"{TEST_BASE_URL}/users/ext_user", @@ -122,7 +114,7 @@ "outbox": f"{TEST_BASE_URL}/users/ext_user/outbox", "preferredUsername": f"ext_user", "type": "Person", - "url": f"{TEST_BASE_URL}/@ext_user" + "url": f"{TEST_BASE_URL}/@ext_user", }, f"{TEST_BASE_URL}/users/{TEST_USER}/outbox": { "@context": "https://www.w3.org/ns/activitystreams", @@ -130,7 +122,7 @@ "id": f"{TEST_BASE_URL}/users/{TEST_USER}/outbox", "last": f"{TEST_BASE_URL}/users/{TEST_USER}/outbox?page=true", "totalItems": 4, - "type": "OrderedCollection" + "type": "OrderedCollection", }, f"{TEST_BASE_URL}/users/{TEST_USER}/outbox?page=true": { "@context": [ @@ -150,26 +142,20 @@ f"{TEST_BASE_URL}/users/{TEST_USER}/followers", ], "content": "<p>test message 1</p>", - "contentMap": { - "en": "<p>test message 1</p>" - }, + "contentMap": {"en": "<p>test message 1</p>"}, "id": f"{TEST_BASE_URL}/users/{TEST_USER}/statuses/1", "inReplyTo": None, "published": "2021-12-16T17:28:03Z", "sensitive": False, "summary": None, "tag": [], - "to": [ - "https://www.w3.org/ns/activitystreams#Public" - ], + "to": ["https://www.w3.org/ns/activitystreams#Public"], "type": "Note", - "url": f"{TEST_BASE_URL}/@{TEST_USER}/1" + "url": f"{TEST_BASE_URL}/@{TEST_USER}/1", }, "published": "2021-12-16T17:28:03Z", - "to": [ - "https://www.w3.org/ns/activitystreams#Public" - ], - "type": "Create" + "to": ["https://www.w3.org/ns/activitystreams#Public"], + "type": "Create", }, { "actor": f"{TEST_BASE_URL}/users/{TEST_USER}", @@ -183,26 +169,20 @@ f"{TEST_BASE_URL}/users/{TEST_USER}/followers", ], "content": "<p>test message 2</p>", - "contentMap": { - "en": "<p>test message 2</p>" - }, + "contentMap": {"en": "<p>test message 2</p>"}, "id": f"{TEST_BASE_URL}/users/{TEST_USER}/statuses/2", "inReplyTo": None, "published": "2021-12-16T17:27:03Z", "sensitive": False, "summary": None, "tag": [], - "to": [ - "https://www.w3.org/ns/activitystreams#Public" - ], + "to": ["https://www.w3.org/ns/activitystreams#Public"], "type": "Note", - "url": f"{TEST_BASE_URL}/@{TEST_USER}/2" + "url": f"{TEST_BASE_URL}/@{TEST_USER}/2", }, "published": "2021-12-16T17:27:03Z", - "to": [ - "https://www.w3.org/ns/activitystreams#Public" - ], - "type": "Create" + "to": ["https://www.w3.org/ns/activitystreams#Public"], + "type": "Create", }, { "actor": f"{TEST_BASE_URL}/users/{TEST_USER}", @@ -216,26 +196,20 @@ f"{TEST_BASE_URL}/users/{TEST_USER}/followers", ], "content": "<p>test message 3</p>", - "contentMap": { - "en": "<p>test message 3</p>" - }, + "contentMap": {"en": "<p>test message 3</p>"}, "id": f"{TEST_BASE_URL}/users/{TEST_USER}/statuses/3", "inReplyTo": None, "published": "2021-12-16T17:26:03Z", "sensitive": False, "summary": None, "tag": [], - "to": [ - "https://www.w3.org/ns/activitystreams#Public" - ], + "to": ["https://www.w3.org/ns/activitystreams#Public"], "type": "Note", - "url": f"{TEST_BASE_URL}/@{TEST_USER}/3" + "url": f"{TEST_BASE_URL}/@{TEST_USER}/3", }, "published": "2021-12-16T17:26:03Z", - "to": [ - "https://www.w3.org/ns/activitystreams#Public" - ], - "type": "Create" + "to": ["https://www.w3.org/ns/activitystreams#Public"], + "type": "Create", }, { "actor": f"{TEST_BASE_URL}/users/{TEST_USER}", @@ -249,38 +223,32 @@ f"{TEST_BASE_URL}/users/{TEST_USER}/followers", ], "content": "<p>test message 4</p>", - "contentMap": { - "en": "<p>test message 4</p>" - }, + "contentMap": {"en": "<p>test message 4</p>"}, "id": f"{TEST_BASE_URL}/users/{TEST_USER}/statuses/4", "inReplyTo": None, "published": "2021-12-16T17:25:03Z", "sensitive": False, "summary": None, "tag": [], - "to": [ - "https://www.w3.org/ns/activitystreams#Public" - ], + "to": ["https://www.w3.org/ns/activitystreams#Public"], "type": "Note", - "url": f"{TEST_BASE_URL}/@{TEST_USER}/4" + "url": f"{TEST_BASE_URL}/@{TEST_USER}/4", }, "published": "2021-12-16T17:25:03Z", - "to": [ - "https://www.w3.org/ns/activitystreams#Public" - ], - "type": "Create" + "to": ["https://www.w3.org/ns/activitystreams#Public"], + "type": "Create", }, - ], + ], "partOf": f"{TEST_BASE_URL}/users/{TEST_USER}/outbox", "prev": None, - "type": "OrderedCollectionPage" + "type": "OrderedCollectionPage", }, f"{TEST_BASE_URL}/users/{TEST_USER}/following": { "@context": "https://www.w3.org/ns/activitystreams", "first": f"{TEST_BASE_URL}/users/{TEST_USER}/following?page=1", "id": f"{TEST_BASE_URL}/users/{TEST_USER}/following", "totalItems": 2, - "type": "OrderedCollection" + "type": "OrderedCollection", }, f"{TEST_BASE_URL}/users/{TEST_USER}/following?page=1": { "@context": "https://www.w3.org/ns/activitystreams", @@ -291,14 +259,14 @@ ], "partOf": "{TEST_BASE_URL}/users/{TEST_USER}/following", "totalItems": 2, - "type": "OrderedCollectionPage" + "type": "OrderedCollectionPage", }, f"{TEST_BASE_URL}/users/{TEST_USER}/followers": { "@context": "https://www.w3.org/ns/activitystreams", "first": f"{TEST_BASE_URL}/users/{TEST_USER}/followers?page=1", "id": f"{TEST_BASE_URL}/users/{TEST_USER}/followers", "totalItems": 2, - "type": "OrderedCollection" + "type": "OrderedCollection", }, f"{TEST_BASE_URL}/users/{TEST_USER}/followers?page=1": { "@context": "https://www.w3.org/ns/activitystreams", @@ -309,9 +277,8 @@ ], "partOf": "{TEST_BASE_URL}/users/{TEST_USER}/followers", "totalItems": 2, - "type": "OrderedCollectionPage" + "type": "OrderedCollectionPage", }, - } XMPP_ITEM_TPL = """ @@ -352,14 +319,15 @@ XMPP_ITEMS = [ xml_tools.parse( "".join( - l.strip() for l in XMPP_ITEM_TPL.format( + l.strip() + for l in XMPP_ITEM_TPL.format( id=i, publisher_jid="some_user@test.example", updated=xmpp_date(ITEM_BASE_TS + i * 60), published=xmpp_date(ITEM_BASE_TS + i * 60), ).split("\n") ), - namespace=pubsub.NS_PUBSUB + namespace=pubsub.NS_PUBSUB, ) for i in range(1, 5) ] @@ -394,10 +362,7 @@ raise StanzaError("item-not-found") ret_items = kwargs.pop("ret_items", XMPP_ITEMS) rsm_resp = rsm.RSMResponse( - first=ret_items[0]["id"], - last=ret_items[-1]["id"], - index=0, - count=len(ret_items) + first=ret_items[0]["id"], last=ret_items[-1]["id"], index=0, count=len(ret_items) ) return ret_items, {"rsm": rsm_resp.toDict(), "complete": True} @@ -440,11 +405,10 @@ gateway.client = client gateway.local_only = True gateway.public_url = PUBLIC_URL - gateway.ap_path = '_ap' + gateway.ap_path = "_ap" gateway.auto_mentions = True gateway.base_ap_url = parse.urljoin( - f"https://{gateway.public_url}", - f"{gateway.ap_path}/" + f"https://{gateway.public_url}", f"{gateway.ap_path}/" ) gateway.server = HTTPServer(gateway) gateway.public_key_pem = None @@ -452,52 +416,51 @@ class TestActivityPubGateway: - def getTitleXHTML(self, item_elt: domish.Element) -> domish.Element: return next( - t for t in item_elt.entry.elements(NS_ATOM, "title") + t + for t in item_elt.entry.elements(NS_ATOM, "title") if t.getAttribute("type") == "xhtml" ) - @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 # local jid - assert await get_account( - jid_ = jid.JID("simple@test.example"), - node = None - ) == "simple@test.example" + assert ( + await get_account(jid_=jid.JID("simple@test.example"), node=None) + == "simple@test.example" + ) # non local jid - assert await get_account( - jid_ = jid.JID("simple@example.org"), - node = None - ) == "___simple.40example.2eorg@ap.test.example" + assert ( + await get_account(jid_=jid.JID("simple@example.org"), node=None) + == "___simple.40example.2eorg@ap.test.example" + ) # local jid with non microblog node - assert await get_account( - jid_ = jid.JID("simple@test.example"), - node = "some_other_node" - ) == "some_other_node---simple@test.example" + assert ( + await get_account(jid_=jid.JID("simple@test.example"), node="some_other_node") + == "some_other_node---simple@test.example" + ) # local pubsub node with patch.object(ap_gateway, "isPubsub") as isPubsub: isPubsub.return_value = True - assert await get_account( - jid_ = jid.JID("pubsub.test.example"), - node = "some_node" - ) == "some_node@pubsub.test.example" + 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 - assert await get_account( - jid_ = jid.JID("pubsub.example.org"), - node = "some_node" - ) == "___some_node.40pubsub.2eexample.2eorg@ap.test.example" + assert ( + await get_account(jid_=jid.JID("pubsub.example.org"), node="some_node") + == "___some_node.40pubsub.2eexample.2eorg@ap.test.example" + ) @ed async def test_ap_handle_convert_to_jid_and_node(self, ap_gateway, monkeypatch): @@ -509,9 +472,10 @@ isPubsub.return_value = False # simple local jid - assert await get_jid_node( - "toto@test.example" - ) == (jid.JID("toto@test.example"), None) + assert await get_jid_node("toto@test.example") == ( + jid.JID("toto@test.example"), + None, + ) # simple external jid @@ -522,23 +486,26 @@ ## with "local_only" unset, it should work with monkeypatch.context() as m: m.setattr(ap_gateway, "local_only", False, raising=True) - assert await get_jid_node( - "toto@example.org" - ) == (jid.JID("toto@example.org"), None) + assert await get_jid_node("toto@example.org") == ( + jid.JID("toto@example.org"), + None, + ) # explicit node - assert await get_jid_node( - "tata---toto@test.example" - ) == (jid.JID("toto@test.example"), "tata") + assert await get_jid_node("tata---toto@test.example") == ( + jid.JID("toto@test.example"), + "tata", + ) # for following assertion, host is a pubsub service with patch.object(ap_gateway, "isPubsub") as isPubsub: isPubsub.return_value = True # simple local node - assert await get_jid_node( - "toto@pubsub.test.example" - ) == (jid.JID("pubsub.test.example"), "toto") + assert await get_jid_node("toto@pubsub.test.example") == ( + jid.JID("pubsub.test.example"), + "toto", + ) # encoded local node assert await get_jid_node( @@ -620,11 +587,7 @@ 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.getAPItems(outbox, max_items=1, start_index=2) assert rsm_resp.count == 4 assert rsm_resp.index == 2 @@ -641,9 +604,7 @@ assert str(items[0].entry.published) == "2021-12-16T17:27:03Z" items, rsm_resp = await ap_gateway.getAPItems( - outbox, - max_items=3, - chronological_pagination=False + outbox, max_items=3, chronological_pagination=False ) assert rsm_resp.count == 4 assert rsm_resp.index == 1 @@ -670,7 +631,7 @@ url: Optional[str] = None, doc: Optional[Any] = None, query_data: Optional[dict] = None, - signing_actor: Optional[str] = None + signing_actor: Optional[str] = None, ) -> Dict[str, Any]: """Generate parameters for HTTPAPGServer's AP*Request @@ -703,17 +664,14 @@ if doc is not None: request.content = io.BytesIO(json.dumps(doc).encode()) - ap_url = parse.urljoin( - f"https://{ap_gateway.public_url}", - path - ) + ap_url = parse.urljoin(f"https://{ap_gateway.public_url}", path) kwargs = { "request": request, "account_jid": test_jid, "node": None, "ap_account": test_jid.full(), "ap_url": ap_url, - "signing_actor": signing_actor + "signing_actor": signing_actor, } if type_ == "outbox" and query_data: kwargs["query_data"] = query_data @@ -728,7 +686,7 @@ outbox = await ap_gateway.server.resource.APOutboxRequest( **self.ap_request_params(ap_gateway, "outbox") ) - assert outbox["@context"] == "https://www.w3.org/ns/activitystreams" + assert outbox["@context"] == ["https://www.w3.org/ns/activitystreams"] assert outbox["id"] == "https://test.example/_ap/outbox/some_user%40test.example" assert outbox["totalItems"] == len(XMPP_ITEMS) assert outbox["type"] == "OrderedCollection" @@ -738,15 +696,23 @@ first_page = await ap_gateway.server.resource.APOutboxPageRequest( **self.ap_request_params(ap_gateway, url=outbox["first"]) ) - assert first_page["@context"] == "https://www.w3.org/ns/activitystreams" - assert first_page["id"] == "https://test.example/_ap/outbox/some_user%40test.example?page=first" + assert first_page["@context"] == ["https://www.w3.org/ns/activitystreams"] + assert ( + first_page["id"] + == "https://test.example/_ap/outbox/some_user%40test.example?page=first" + ) assert first_page["type"] == "OrderedCollectionPage" assert first_page["partOf"] == outbox["id"] assert len(first_page["orderedItems"]) == len(XMPP_ITEMS) first_item = first_page["orderedItems"][0] - assert first_item["@context"] == "https://www.w3.org/ns/activitystreams" - assert first_item["id"] == "https://test.example/_ap/item/some_user%40test.example/4" - assert first_item["actor"] == "https://test.example/_ap/actor/some_user%40test.example" + assert first_item["@context"] == ["https://www.w3.org/ns/activitystreams"] + assert ( + first_item["id"] == "https://test.example/_ap/item/some_user%40test.example/4" + ) + assert ( + first_item["actor"] + == "https://test.example/_ap/actor/some_user%40test.example" + ) assert first_item["type"] == "Create" first_item_obj = first_item["object"] assert first_item_obj["id"] == first_item["id"] @@ -769,13 +735,12 @@ ap_gateway._pps.subscriptions_node, None, None, - None + None, ) assert len(items) == 2 - for idx, entity in enumerate(( - "local_user@test.example", - "ext_user\\40example.org@ap.test.example" - )): + for idx, entity in enumerate( + ("local_user@test.example", "ext_user\\40example.org@ap.test.example") + ): subscription_elt = next(items[idx].elements(NS_PPS, "subscription"), None) assert subscription_elt is not None assert subscription_elt["node"] == ap_gateway._m.namespace @@ -794,13 +759,12 @@ ap_gateway._pps.getPublicSubscribersNode(ap_gateway._m.namespace), None, None, - None + None, ) assert len(items) == 2 - for idx, entity in enumerate(( - "local_user@test.example", - "ext_user\\40example.org@ap.test.example" - )): + for idx, entity in enumerate( + ("local_user@test.example", "ext_user\\40example.org@ap.test.example") + ): subscriber_elt = next(items[idx].elements(NS_PPS, "subscriber"), None) assert subscriber_elt is not None assert subscriber_elt["jid"] == entity @@ -811,28 +775,29 @@ subscriptions = [ pubsub.Item( id="subscription_1", - payload = ap_gateway._pps.buildSubscriptionElt( - ap_gateway._m.namespace, - jid.JID("local_user@test.example") - ) + payload=ap_gateway._pps.buildSubscriptionElt( + ap_gateway._m.namespace, jid.JID("local_user@test.example") + ), ), pubsub.Item( id="subscription_2", - payload = ap_gateway._pps.buildSubscriptionElt( + payload=ap_gateway._pps.buildSubscriptionElt( ap_gateway._m.namespace, - jid.JID("ext_user\\40example.org@ap.test.example") - ) - ) + jid.JID("ext_user\\40example.org@ap.test.example"), + ), + ), ] - monkeypatch.setattr(ap_gateway._p, "getItems", partial( - mock_getItems, - ret_items=subscriptions - )) + monkeypatch.setattr( + ap_gateway._p, "getItems", partial(mock_getItems, ret_items=subscriptions) + ) following = await ap_gateway.server.resource.APFollowingRequest( **self.ap_request_params(ap_gateway, "following") ) - assert following["@context"] == "https://www.w3.org/ns/activitystreams" - assert following["id"] == "https://test.example/_ap/following/some_user%40test.example" + assert following["@context"] == ["https://www.w3.org/ns/activitystreams"] + assert ( + following["id"] + == "https://test.example/_ap/following/some_user%40test.example" + ) assert following["totalItems"] == len(subscriptions) assert following["type"] == "OrderedCollection" assert following.get("first") @@ -841,7 +806,7 @@ assert first_page["type"] == "OrderedCollectionPage" assert len(first_page["orderedItems"]) == len(subscriptions) items = first_page["orderedItems"] - assert items == ['local_user@test.example', 'ext_user@example.org'] + assert items == ["local_user@test.example", "ext_user@example.org"] @ed async def test_pps_to_followers(self, ap_gateway, monkeypatch): @@ -849,26 +814,28 @@ subscribers = [ pubsub.Item( id="subscriber_1", - payload = ap_gateway._pps.buildSubscriberElt( + payload=ap_gateway._pps.buildSubscriberElt( jid.JID("local_user@test.example") - ) + ), ), pubsub.Item( id="subscriber_2", - payload = ap_gateway._pps.buildSubscriberElt( + payload=ap_gateway._pps.buildSubscriberElt( jid.JID("ext_user\\40example.org@ap.test.example") - ) - ) + ), + ), ] - monkeypatch.setattr(ap_gateway._p, "getItems", partial( - mock_getItems, - ret_items=subscribers - )) + monkeypatch.setattr( + ap_gateway._p, "getItems", partial(mock_getItems, ret_items=subscribers) + ) followers = await ap_gateway.server.resource.APFollowersRequest( **self.ap_request_params(ap_gateway, "followers") ) - assert followers["@context"] == "https://www.w3.org/ns/activitystreams" - assert followers["id"] == "https://test.example/_ap/followers/some_user%40test.example" + assert followers["@context"] == ["https://www.w3.org/ns/activitystreams"] + assert ( + followers["id"] + == "https://test.example/_ap/followers/some_user%40test.example" + ) assert followers["totalItems"] == len(subscribers) assert followers["type"] == "OrderedCollection" assert followers.get("first") @@ -877,7 +844,7 @@ assert first_page["type"] == "OrderedCollectionPage" assert len(first_page["orderedItems"]) == len(subscribers) items = first_page["orderedItems"] - assert items == ['local_user@test.example', 'ext_user@example.org'] + assert items == ["local_user@test.example", "ext_user@example.org"] @ed async def test_xmpp_message_to_ap_direct_message(self, ap_gateway, monkeypatch): @@ -890,9 +857,7 @@ "to": ap_gateway.getLocalJIDFromAccount(TEST_AP_ACCOUNT), "type": "chat", "message": {"": "This is a test message."}, - "extra": { - "origin-id": "123" - } + "extra": {"origin-id": "123"}, } with patch.object(ap_gateway, "signAndPost") as signAndPost: await ap_gateway.onMessage(ap_gateway.client, mess_data) @@ -900,7 +865,7 @@ assert url == "https://example.org/users/test_user/inbox" assert actor_id == "https://test.example/_ap/actor/some_user%40test.example" obj = doc["object"] - assert doc["@context"] == "https://www.w3.org/ns/activitystreams" + assert doc["@context"] == ["https://www.w3.org/ns/activitystreams"] assert doc["actor"] == "https://test.example/_ap/actor/some_user%40test.example" assert obj["type"] == "Note" assert obj["content"] == "This is a test message." @@ -926,14 +891,14 @@ xmpp_actor_id = ap_gateway.buildAPURL(ap_const.TYPE_ACTOR, TEST_JID.userhost()) direct_ap_message = { - 'attributedTo': TEST_AP_ACTOR_ID, - 'cc': [], - 'content': '<p>test direct message</p>', - 'contentMap': {'en': '<p>test direct message</p>'}, - 'id': f'{TEST_AP_ACTOR_ID}/statuses/123', - 'published': '2022-05-20T08:14:39Z', - 'to': [xmpp_actor_id], - 'type': 'Note', + "attributedTo": TEST_AP_ACTOR_ID, + "cc": [], + "content": "<p>test direct message</p>", + "contentMap": {"en": "<p>test direct message</p>"}, + "id": f"{TEST_AP_ACTOR_ID}/statuses/123", + "published": "2022-05-20T08:14:39Z", + "to": [xmpp_actor_id], + "type": "Note", } client = ap_gateway.client.getVirtualClient( ap_gateway.getLocalJIDFromAccount(TEST_AP_ACCOUNT) @@ -963,7 +928,7 @@ recipient=ap_gateway.getLocalJIDFromAccount(TEST_AP_ACCOUNT), nodeIdentifier=ap_gateway._m.namespace, items=[retract_elt], - headers={} + headers={}, ) with patch.object(ap_gateway, "signAndPost") as signAndPost: signAndPost.return_value = FakeTReqPostResponse() @@ -993,9 +958,8 @@ "actor": TEST_AP_ACTOR_ID, "id": "https://test.example/retract_123", "type": "Delete", - "object": {"id": f"{TEST_AP_ACTOR_ID}/item/123", - "type": "Tombstone"}, - "to": ["https://www.w3.org/ns/activitystreams#Public"] + "object": {"id": f"{TEST_AP_ACTOR_ID}/item/123", "type": "Tombstone"}, + "to": ["https://www.w3.org/ns/activitystreams#Public"], } with patch.multiple( ap_gateway.host.memory.storage, @@ -1003,9 +967,9 @@ getPubsubNode=DEFAULT, deletePubsubItems=DEFAULT, ) as mock_objs: - mock_objs["get"].return_value=None + mock_objs["get"].return_value = None cached_node = MagicMock() - mock_objs["getPubsubNode"].return_value=cached_node + mock_objs["getPubsubNode"].return_value = cached_node subscription = MagicMock() subscription.state = SubscriptionState.SUBSCRIBED subscription.subscriber = TEST_JID @@ -1018,7 +982,7 @@ client=client, destinee=None, node=ap_gateway._m.namespace, - item=ap_item + item=ap_item, ) # item is deleted from database @@ -1066,10 +1030,7 @@ # we simulate the reception of a retract event using the message element that # we generated above await ap_gateway._onMessageRetract( - ap_gateway.client, - message_retract_elt, - retract_elt, - fake_fastened_elts + ap_gateway.client, message_retract_elt, retract_elt, fake_fastened_elts ) url, actor_id, doc = signAndPost.call_args[0] @@ -1108,9 +1069,8 @@ "actor": TEST_AP_ACTOR_ID, "id": "https://test.example/retract_123", "type": "Delete", - "object": {"id": f"{TEST_AP_ACTOR_ID}/item/123", - "type": "Tombstone"}, - "to": ["https://www.w3.org/ns/activitystreams#Public"] + "object": {"id": f"{TEST_AP_ACTOR_ID}/item/123", "type": "Tombstone"}, + "to": ["https://www.w3.org/ns/activitystreams#Public"], } with patch.object(ap_gateway.host.memory.storage, "get") as storage_get: fake_history = MagicMock() @@ -1120,10 +1080,7 @@ storage_get.return_value = fake_history # we simulate a received Delete activity await ap_gateway.newAPDeleteItem( - client=client, - destinee=None, - node=ap_gateway._m.namespace, - item=ap_item + client=client, destinee=None, node=ap_gateway._m.namespace, item=ap_item ) # item is deleted from database @@ -1157,7 +1114,7 @@ ap_gateway._v.node, None, None, - None + None, ) assert len(items) == 1 vcard_elt = next(items[0].elements(ap_gateway._v.namespace, "vcard")) @@ -1172,8 +1129,8 @@ # XEPs/locations (vCard4, vcard-tmp, etc) with patch.object(ap_gateway._i, "getIdentity") as getIdentity: getIdentity.return_value = { - "nicknames": ["nick1", "nick2"], - "description": "test description" + "nicknames": ["nick1", "nick2"], + "description": "test description", } actor_data = await ap_gateway.server.resource.APActorRequest( **self.ap_request_params(ap_gateway, ap_const.TYPE_ACTOR) @@ -1193,13 +1150,13 @@ xmpp_actor_id = ap_gateway.buildAPURL(ap_const.TYPE_ACTOR, TEST_JID.userhost()) direct_addr_mention = { - 'attributedTo': TEST_AP_ACTOR_ID, - 'cc': [], - 'content': '<p>test mention by direct addressing</p>', - 'id': f'{TEST_AP_ACTOR_ID}/statuses/direct_addr_123', - 'published': '2022-05-20T08:14:39Z', - 'to': [ap_const.NS_AP_PUBLIC, xmpp_actor_id], - 'type': 'Note' + "attributedTo": TEST_AP_ACTOR_ID, + "cc": [], + "content": "<p>test mention by direct addressing</p>", + "id": f"{TEST_AP_ACTOR_ID}/statuses/direct_addr_123", + "published": "2022-05-20T08:14:39Z", + "to": [ap_const.NS_AP_PUBLIC, xmpp_actor_id], + "type": "Note", } client = ap_gateway.client.getVirtualClient( ap_gateway.getLocalJIDFromAccount(TEST_AP_ACCOUNT) @@ -1219,7 +1176,7 @@ "pubsub", path=local_actor_jid.full(), node=ap_gateway._m.namespace, - item=direct_addr_mention["id"] + item=direct_addr_mention["id"], ) assert sendReference.call_args.kwargs["anchor"] == expected_anchor @@ -1234,20 +1191,14 @@ xmpp_actor_id = ap_gateway.buildAPURL(ap_const.TYPE_ACTOR, TEST_JID.userhost()) direct_addr_mention = { - 'attributedTo': TEST_AP_ACTOR_ID, - 'cc': [], - 'content': '<p>test mention by tag</p>', - 'id': f'{TEST_AP_ACTOR_ID}/statuses/tag_123', - 'published': '2022-05-20T08:14:39Z', - 'to': [ap_const.NS_AP_PUBLIC], - "tag": [ - { - "type": "Mention", - "href": xmpp_actor_id, - "name": f"@{TEST_JID}'" - } - ], - 'type': 'Note' + "attributedTo": TEST_AP_ACTOR_ID, + "cc": [], + "content": "<p>test mention by tag</p>", + "id": f"{TEST_AP_ACTOR_ID}/statuses/tag_123", + "published": "2022-05-20T08:14:39Z", + "to": [ap_const.NS_AP_PUBLIC], + "tag": [{"type": "Mention", "href": xmpp_actor_id, "name": f"@{TEST_JID}'"}], + "type": "Note", } client = ap_gateway.client.getVirtualClient( ap_gateway.getLocalJIDFromAccount(TEST_AP_ACCOUNT) @@ -1267,7 +1218,7 @@ "pubsub", path=local_actor_jid.full(), node=ap_gateway._m.namespace, - item=direct_addr_mention["id"] + item=direct_addr_mention["id"], ) assert sendReference.call_args.kwargs["anchor"] == expected_anchor @@ -1283,16 +1234,18 @@ "author_jid": TEST_JID.full(), "content": f"mention of @{TEST_AP_ACCOUNT}", "service": TEST_JID.full(), - "node": ap_gateway._m.namespace + "node": ap_gateway._m.namespace, } - ap_item = await ap_gateway.mbdata2APitem(ap_gateway.client, mb_data, public=True) + ap_item = await ap_gateway.mb_data_2_ap_item( + ap_gateway.client, mb_data, public=True + ) ap_object = ap_item["object"] assert TEST_AP_ACTOR_ID in ap_object["to"] expected_mention = { "type": ap_const.TYPE_MENTION, "href": TEST_AP_ACTOR_ID, - "name": f"@{TEST_AP_ACCOUNT}" + "name": f"@{TEST_AP_ACCOUNT}", } assert expected_mention in ap_object["tag"] @@ -1300,7 +1253,7 @@ async def test_no_auto_mentions_when_not_public(self, ap_gateway, monkeypatch): """Check that no mention is send when the message is not public""" # this is the same test as test_auto_mentions above, except that public is not set - # in mbdata2APitem + # 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) @@ -1309,9 +1262,11 @@ "author_jid": TEST_JID.full(), "content": f"mention of @{TEST_AP_ACCOUNT}", "service": TEST_JID.full(), - "node": ap_gateway._m.namespace + "node": ap_gateway._m.namespace, } - ap_item = await ap_gateway.mbdata2APitem(ap_gateway.client, mb_data, public=False) + ap_item = await ap_gateway.mb_data_2_ap_item( + ap_gateway.client, mb_data, public=False + ) ap_object = ap_item["object"] assert "to" not in ap_object @@ -1330,13 +1285,13 @@ "pubsub", path=TEST_JID.full(), node=ap_gateway._m.namespace, - item=item_elt["id"] + item=item_elt["id"], ) ref_data: Dict[str, Union[str, int, dict]] = { "uri": xmpp_uri.buildXMPPUri(None, path=local_actor_jid.full()), "type_": "mention", - "anchor": anchor + "anchor": anchor, } reference_elt = ap_gateway._refs.buildRefElement(**ref_data) @@ -1361,7 +1316,9 @@ getItems.return_value = ([mock_pubsub_item], {}) with patch.object(ap_gateway, "signAndPost") as signAndPost: signAndPost.return_value.code = 202 - await ap_gateway._onReferenceReceived(ap_gateway.client, message_elt, ref_data) + await ap_gateway._onReferenceReceived( + 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" @@ -1374,7 +1331,7 @@ "href": TEST_AP_ACTOR_ID, # we don't have a prefixing "@" here, because it's not needed in referencing # item with XMPP - "name": f"{TEST_AP_ACCOUNT}" + "name": f"{TEST_AP_ACCOUNT}", } assert expected_mention in ap_object["tag"] @@ -1393,9 +1350,10 @@ "pubsub", path=recipient_jid.full(), node=ap_gateway._m.namespace, - item=ap_item["id"] + item=ap_item["id"], ) - item_elt = xml_tools.parse(f""" + item_elt = xml_tools.parse( + f""" <item id="123" publisher="{TEST_JID}/res.123"> <entry xmlns="http://www.w3.org/2005/Atom"> <title type="text">test message 1</title> @@ -1414,7 +1372,8 @@ <link href="{ap_item_url}" rel="via"/> </entry> </item> - """) + """ + ) item_elt.uri = pubsub.NS_PUBSUB_EVENT with patch.object(ap_gateway, "signAndPost") as signAndPost: @@ -1424,7 +1383,7 @@ TEST_AP_ACCOUNT, TEST_JID, ap_gateway._m.namespace, - [item_elt] + [item_elt], ) assert signAndPost.called @@ -1452,30 +1411,21 @@ "@context": "https://www.w3.org/ns/activitystreams", "type": "Announce", "actor": TEST_AP_ACTOR_ID, - "cc": [ - xmpp_actor_id, - TEST_USER_DATA["followers"] - ], + "cc": [xmpp_actor_id, TEST_USER_DATA["followers"]], "id": "https://example.org/announce/123", "object": xmpp_item_url, "published": "2022-07-22T09:24:12Z", - "to": [ap_const.NS_AP_PUBLIC] + "to": [ap_const.NS_AP_PUBLIC], } with patch.object(ap_gateway.host.memory.storage, "getItems") as getItems: mock_pubsub_item = MagicMock mock_pubsub_item.data = xmpp_item getItems.return_value = ([mock_pubsub_item], {}) with patch.object( - ap_gateway.host.memory.storage, - "cachePubsubItems") as cachePubsubItems: + ap_gateway.host.memory.storage, "cachePubsubItems" + ) as cachePubsubItems: await ap_gateway.server.resource.handleAnnounceActivity( - Request(MagicMock()), - announce, - None, - None, - None, - "", - TEST_AP_ACTOR_ID + Request(MagicMock()), announce, None, None, None, "", TEST_AP_ACTOR_ID ) assert cachePubsubItems.called @@ -1489,7 +1439,7 @@ "pubsub", path=TEST_JID.full(), node=ap_gateway._m.namespace, - item=xmpp_item["id"] + item=xmpp_item["id"], ) assert repeated["uri"] == xmpp_item_xmpp_url @@ -1504,32 +1454,25 @@ # noticed item ap_item = TEST_AP_ITEMS[0] attachment_node = ap_gateway._pa.getAttachmentNodeName( - recipient_jid, - ap_gateway._m.namespace, - ap_item["id"] + recipient_jid, ap_gateway._m.namespace, ap_item["id"] ) - item_elt = xml_tools.parse(f""" + item_elt = xml_tools.parse( + f""" <item id="{TEST_JID.userhost()}" published="{TEST_JID.userhostJID()}"> <attachments xmlns="urn:xmpp:pubsub-attachments:1"> <noticed timestamp="2022-07-22T12:29:45Z"/> </attachments> </item> - """) + """ + ) item_elt.uri = pubsub.NS_PUBSUB_EVENT items_event = pubsub.ItemsEvent( - TEST_JID, - recipient_jid, - attachment_node, - [item_elt], - {} + 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 - ) + await ap_gateway._itemsReceived(ap_gateway.client, items_event) assert signAndPost.called url, actor_id, doc = signAndPost.call_args.args @@ -1556,24 +1499,18 @@ "@context": "https://www.w3.org/ns/activitystreams", "type": "Like", "actor": TEST_AP_ACTOR_ID, - "cc": [ - xmpp_actor_id, - TEST_USER_DATA["followers"] - ], + "cc": [xmpp_actor_id, TEST_USER_DATA["followers"]], "id": "https://example.org/like/123", "object": xmpp_item_url, "published": "2022-07-22T09:24:12Z", - "to": [ap_const.NS_AP_PUBLIC] + "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( **self.ap_request_params( - ap_gateway, - "inbox", - doc=like, - signing_actor=TEST_AP_ACTOR_ID + ap_gateway, "inbox", doc=like, signing_actor=TEST_AP_ACTOR_ID ) ) @@ -1603,15 +1540,14 @@ "pubsub", path=recipient_jid.full(), node=ap_gateway._m.namespace, - item=ap_item["id"] + item=ap_item["id"], ) attachment_node = ap_gateway._pa.getAttachmentNodeName( - recipient_jid, - ap_gateway._m.namespace, - ap_item["id"] + recipient_jid, ap_gateway._m.namespace, ap_item["id"] ) reactions = ["🦁", "🥜", "🎻"] - item_elt = xml_tools.parse(f""" + item_elt = xml_tools.parse( + f""" <item id="{TEST_JID.userhost()}" published="{TEST_JID.userhostJID()}"> <attachments xmlns="urn:xmpp:pubsub-attachments:1"> <reactions timestamp="2022-08-31T12:17:23Z"> @@ -1621,22 +1557,16 @@ </reactions> </attachments> </item> - """) + """ + ) item_elt.uri = pubsub.NS_PUBSUB_EVENT items_event = pubsub.ItemsEvent( - TEST_JID, - recipient_jid, - attachment_node, - [item_elt], - {} + 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 - ) + await ap_gateway._itemsReceived(ap_gateway.client, items_event) assert signAndPost.call_count == 3 for idx, call_args in enumerate(signAndPost.call_args_list): @@ -1672,25 +1602,19 @@ "@context": "https://www.w3.org/ns/activitystreams", "type": "EmojiReact", "actor": TEST_AP_ACTOR_ID, - "cc": [ - xmpp_actor_id, - TEST_USER_DATA["followers"] - ], + "cc": [xmpp_actor_id, TEST_USER_DATA["followers"]], "id": "https://example.org/like/123", "object": xmpp_item_url, "content": "🐅", "published": "2022-07-22T09:24:12Z", - "to": [ap_const.NS_AP_PUBLIC] + "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( **self.ap_request_params( - ap_gateway, - "inbox", - doc=like, - signing_actor=TEST_AP_ACTOR_ID + ap_gateway, "inbox", doc=like, signing_actor=TEST_AP_ACTOR_ID ) )