From 62ba627bc9918104a76ca1c5d62353a8134efbc6 Mon Sep 17 00:00:00 2001 From: Ryan Barrett Date: Tue, 1 Aug 2023 07:52:36 -0700 Subject: [PATCH] bug fix for interactive follow with followee Object without as2 fixes https://console.cloud.google.com/errors/detail/CIeL-MyNk9z5LQ;time=P30D?project=bridgy-federated , https://console.cloud.google.com/errors/detail/COLs4IvxloWVUg;time=P30D?project=bridgy-federated --- follow.py | 5 +++-- tests/test_follow.py | 44 ++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/follow.py b/follow.py index 21f9283..d2df4db 100644 --- a/follow.py +++ b/follow.py @@ -121,7 +121,8 @@ class FollowCallback(indieauth.Callback): return redirect(g.user.user_page_path('following')) followee_id = followee.as1.get('id') - inbox = followee.as2.get('inbox') + followee_as2 = followee.as_as2() + inbox = followee_as2.get('inbox') if not followee_id or not inbox: flash(f"AS2 profile {as2_url} missing id or inbox") return redirect(g.user.user_page_path('following')) @@ -132,7 +133,7 @@ class FollowCallback(indieauth.Callback): '@context': 'https://www.w3.org/ns/activitystreams', 'type': 'Follow', 'id': follow_id, - 'object': followee.as2, + 'object': followee_as2, 'actor': g.user.ap_actor(), 'to': [as2.PUBLIC_AUDIENCE], } diff --git a/tests/test_follow.py b/tests/test_follow.py index 07440b7..4b04afe 100644 --- a/tests/test_follow.py +++ b/tests/test_follow.py @@ -186,6 +186,39 @@ class FollowTest(TestCase): resp = self.client.get(f'/follow/callback?code=my_code&state={state}') self.check('https://bar/actor', resp, FOLLOW_URL, mock_get, mock_post) + def test_callback_stored_followee_with_our_as1(self, mock_get, mock_post): + self.store_object(id='https://bar/id', our_as1=as2.to_as1({ + **FOLLOWEE, + # 'id': 'https://bar/actor', + })) + + mock_get.side_effect = ( + requests_response(''), + ) + mock_post.side_effect = ( + requests_response('me=https://alice.com'), + requests_response('OK'), # AP Follow to inbox + ) + + self.state['state'] = 'https://bar/id' + state = util.encode_oauth_state(self.state) + resp = self.client.get(f'/follow/callback?code=my_code&state={state}') + + follow_with_profile_link = { + **FOLLOW_URL, + 'id': f'http://localhost/web/alice.com/following#2022-01-02T03:04:05-https://bar/id', + 'object': { + **FOLLOWEE, + 'attachment': [{ + 'type': 'PropertyValue', + 'name': 'Link', + 'value': 'bar/url', + }], + }, + } + self.check('https://bar/id', resp, follow_with_profile_link, mock_get, + mock_post, fetched_followee=False) + def test_callback_composite_url_field(self, mock_get, mock_post): """https://console.cloud.google.com/errors/detail/CKmLytj-nPv9RQ;time=P30D?project=bridgy-federated""" followee = { @@ -215,15 +248,18 @@ class FollowTest(TestCase): expected_follow['object'] = followee self.check('https://bar/actor', resp, expected_follow, mock_get, mock_post) - def check(self, input, resp, expected_follow, mock_get, mock_post): + def check(self, input, resp, expected_follow, mock_get, mock_post, + fetched_followee=True): self.assertEqual(302, resp.status_code) self.assertEqual('/web/alice.com/following', resp.headers['Location']) self.assertEqual([f'Followed {input}.'], get_flashed_messages()) - mock_get.assert_has_calls(( - self.as2_req('https://bar/actor'), - )) + if fetched_followee: + mock_get.assert_has_calls(( + self.as2_req('https://bar/actor'), + )) + inbox_args, inbox_kwargs = mock_post.call_args self.assertEqual(('http://bar/inbox',), inbox_args) self.assert_equals(expected_follow, json_loads(inbox_kwargs['data']))