Skip to content

Commit

Permalink
make remote follow handle non-web users
Browse files Browse the repository at this point in the history
for #1640. thanks for reporting @mistrk7!
  • Loading branch information
snarfed committed Dec 20, 2024
1 parent f7bf986 commit 0d82d49
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 15 deletions.
6 changes: 3 additions & 3 deletions follow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions templates/followers.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
<div class="row">
<form method="post" action="/remote-follow">
<nobr>
<label for="follow-address">Follow @{{ id }} with your fediverse address:</label>
<label for="follow-address">Follow @{{ handle }} with your fediverse address:</label>
<input id="follow-address" name="address" type="text" required
placeholder="@[email protected]" alt="fediverse address"
value="{{ follow_url or '' }}"></input>
<input name="domain" type="hidden" value="{{ id }}"></input>
<input name="id" type="hidden" value="{{ id }}"></input>
<input name="protocol" type="hidden" value="{{ user.LABEL }}"></input>
<button type="submit" class="btn btn-default">Follow</button>
</nobr>
Expand Down
33 changes: 23 additions & 10 deletions tests/test_follow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=@[email protected]&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=@[email protected]&domain=user.com')
got = self.client.post('/remote-follow?address=@[email protected]&id=user.com')
self.assertEqual(400, got.status_code)

def test_unknown_protocol(self, _):
got = self.client.post('/remote-follow?address=@[email protected]&domain=user.com&protocol=foo')
got = self.client.post('/remote-follow?address=@[email protected]&id=user.com&protocol=foo')
self.assertEqual(400, got.status_code)

def test_no_user(self, _):
got = self.client.post('/remote-follow?address=@[email protected]&domain=baz.com')
got = self.client.post('/remote-follow?address=@[email protected]&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=@[email protected]&domain=user.com&protocol=web')
got = self.client.post('/remote-follow?address=@[email protected]&id=user.com&protocol=web')
self.assertEqual(302, got.status_code)
self.assertEqual('https://ba.r/[email protected]@user.com',
got.headers['Location'])
Expand All @@ -103,35 +103,48 @@ 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/[email protected]@user.com', got.headers['Location'])

mock_get.assert_has_calls((
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=@[email protected]&id=fake:user&protocol=fake')
self.assertEqual(302, got.status_code)
self.assertEqual('https://ba.r/follow?uri=@fake:handle:[email protected]',
got.headers['Location'])

mock_get.assert_has_calls((
self.req('https://ba.r/.well-known/webfinger?resource=acct:[email protected]'),
))

def test_no_webfinger_subscribe_link(self, mock_get):
mock_get.return_value = requests_response({
'subject': 'acct:[email protected]',
'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('<html>not json</html>')

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'])

Expand Down

0 comments on commit 0d82d49

Please sign in to comment.