From 0d82d490341ba3152c30d529b8ca1b6facd7a7b7 Mon Sep 17 00:00:00 2001 From: Ryan Barrett Date: Fri, 20 Dec 2024 10:07:39 -0800 Subject: [PATCH] make remote follow handle non-web users for #1640. thanks for reporting @mistrk7! --- follow.py | 6 +++--- pages.py | 3 +++ templates/followers.html | 4 ++-- tests/test_follow.py | 33 +++++++++++++++++++++++---------- 4 files changed, 31 insertions(+), 15 deletions(-) diff --git a/follow.py b/follow.py index 16bd0ffa..a0382c00 100644 --- a/follow.py +++ b/follow.py @@ -32,10 +32,10 @@ def remote_follow(): if not cls: error(f'Unknown protocol {request.values["protocol"]}') - domain = request.values['domain'] - user = cls.get_by_id(domain) + id = request.values['id'] + user = cls.get_by_id(id) if not user: - error(f'No web user found for domain {domain}') + error(f'No {cls.LABEL} user found for {id}') addr = request.values['address'] resp = webfinger.fetch(addr) diff --git a/pages.py b/pages.py index 3e37fb71..0aeea8a1 100644 --- a/pages.py +++ b/pages.py @@ -204,6 +204,9 @@ def update_profile(protocol, id): @canonicalize_request_domain(common.PROTOCOL_DOMAINS, common.PRIMARY_DOMAIN) def followers_or_following(protocol, id, collection): user = load_user(protocol, id) + id = user.key.id() + handle = user.handle + followers, before, after = Follower.fetch_page(collection, user) num_followers, num_following = user.count_followers() return render_template( diff --git a/templates/followers.html b/templates/followers.html index b05990e0..7017c2ce 100644 --- a/templates/followers.html +++ b/templates/followers.html @@ -6,11 +6,11 @@
- + - + diff --git a/tests/test_follow.py b/tests/test_follow.py index 0274cad4..b9155858 100644 --- a/tests/test_follow.py +++ b/tests/test_follow.py @@ -70,29 +70,29 @@ def setUp(self): super().setUp() self.make_user('user.com', cls=Web, has_redirects=True) - def test_no_domain(self, _): + def test_no_id(self, _): got = self.client.post('/remote-follow?address=@foo@ba.r&protocol=web') self.assertEqual(400, got.status_code) def test_no_address(self, _): - got = self.client.post('/remote-follow?domain=baz.com&protocol=web') + got = self.client.post('/remote-follow?id=baz.com&protocol=web') self.assertEqual(400, got.status_code) def test_no_protocol(self, _): - got = self.client.post('/remote-follow?address=@foo@ba.r&domain=user.com') + got = self.client.post('/remote-follow?address=@foo@ba.r&id=user.com') self.assertEqual(400, got.status_code) def test_unknown_protocol(self, _): - got = self.client.post('/remote-follow?address=@foo@ba.r&domain=user.com&protocol=foo') + got = self.client.post('/remote-follow?address=@foo@ba.r&id=user.com&protocol=foo') self.assertEqual(400, got.status_code) def test_no_user(self, _): - got = self.client.post('/remote-follow?address=@foo@ba.r&domain=baz.com') + got = self.client.post('/remote-follow?address=@foo@ba.r&id=baz.com') self.assertEqual(400, got.status_code) def test_addr(self, mock_get): mock_get.return_value = WEBFINGER - got = self.client.post('/remote-follow?address=@foo@ba.r&domain=user.com&protocol=web') + got = self.client.post('/remote-follow?address=@foo@ba.r&id=user.com&protocol=web') self.assertEqual(302, got.status_code) self.assertEqual('https://ba.r/follow?uri=@user.com@user.com', got.headers['Location']) @@ -103,7 +103,7 @@ def test_addr(self, mock_get): def test_url(self, mock_get): mock_get.return_value = WEBFINGER - got = self.client.post('/remote-follow?address=https://ba.r/foo&domain=user.com&protocol=web') + got = self.client.post('/remote-follow?address=https://ba.r/foo&id=user.com&protocol=web') self.assertEqual(302, got.status_code) self.assertEqual('https://ba.r/follow?uri=@user.com@user.com', got.headers['Location']) @@ -111,27 +111,40 @@ def test_url(self, mock_get): self.req('https://ba.r/.well-known/webfinger?resource=https://ba.r/foo'), )) + def test_non_web_user(self, mock_get): + user = self.make_user('fake:user', cls=Fake) + + mock_get.return_value = WEBFINGER + got = self.client.post('/remote-follow?address=@foo@ba.r&id=fake:user&protocol=fake') + self.assertEqual(302, got.status_code) + self.assertEqual('https://ba.r/follow?uri=@fake:handle:user@fa.brid.gy', + got.headers['Location']) + + mock_get.assert_has_calls(( + self.req('https://ba.r/.well-known/webfinger?resource=acct:foo@ba.r'), + )) + def test_no_webfinger_subscribe_link(self, mock_get): mock_get.return_value = requests_response({ 'subject': 'acct:foo@ba.r', 'links': [{'rel': 'other', 'template': 'meh'}], }) - got = self.client.post('/remote-follow?address=https://ba.r/foo&domain=user.com&protocol=web') + got = self.client.post('/remote-follow?address=https://ba.r/foo&id=user.com&protocol=web') self.assertEqual(302, got.status_code) self.assertEqual('/web/user.com', got.headers['Location']) def test_webfinger_error(self, mock_get): mock_get.return_value = requests_response(status=500) - got = self.client.post('/remote-follow?address=https://ba.r/foo&domain=user.com&protocol=web') + got = self.client.post('/remote-follow?address=https://ba.r/foo&id=user.com&protocol=web') self.assertEqual(302, got.status_code) self.assertEqual('/web/user.com', got.headers['Location']) def test_webfinger_returns_not_json(self, mock_get): mock_get.return_value = requests_response('not json') - got = self.client.post('/remote-follow?address=https://ba.r/foo&domain=user.com&protocol=web') + got = self.client.post('/remote-follow?address=https://ba.r/foo&id=user.com&protocol=web') self.assertEqual(302, got.status_code) self.assertEqual('/web/user.com', got.headers['Location'])