From cb28c19abc36636ff0ecadaeebff0cbaf5a3549c Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sat, 12 Dec 2020 15:19:57 -0800 Subject: [PATCH] Use get_data in resolving remote id --- bookwyrm/outgoing.py | 12 +--- .../tests/outgoing/test_remote_webfinger.py | 61 +++++++++++++++++++ bookwyrm/utils/regex.py | 2 +- 3 files changed, 65 insertions(+), 10 deletions(-) create mode 100644 bookwyrm/tests/outgoing/test_remote_webfinger.py diff --git a/bookwyrm/outgoing.py b/bookwyrm/outgoing.py index 545ac4911..2f94d2c05 100644 --- a/bookwyrm/outgoing.py +++ b/bookwyrm/outgoing.py @@ -4,10 +4,10 @@ import re from django.db import IntegrityError, transaction from django.http import HttpResponseNotFound, JsonResponse from django.views.decorators.csrf import csrf_exempt -import requests from bookwyrm import activitypub from bookwyrm import models +from bookwyrm.connectors import get_data from bookwyrm.broadcast import broadcast from bookwyrm.status import create_notification from bookwyrm.status import create_generated_note @@ -52,14 +52,8 @@ def handle_remote_webfinger(query): except models.User.DoesNotExist: url = 'https://%s/.well-known/webfinger?resource=acct:%s' % \ (domain, query) - try: - response = requests.get(url) - except requests.exceptions.ConnectionError: - return None - if not response.ok: - return None - data = response.json() - for link in data['links']: + data = get_data(url) + for link in data.get('links'): if link.get('rel') == 'self': try: user = activitypub.resolve_remote_id( diff --git a/bookwyrm/tests/outgoing/test_remote_webfinger.py b/bookwyrm/tests/outgoing/test_remote_webfinger.py new file mode 100644 index 000000000..1bf884a6e --- /dev/null +++ b/bookwyrm/tests/outgoing/test_remote_webfinger.py @@ -0,0 +1,61 @@ +''' testing user lookup ''' +import json +import pathlib +from unittest.mock import patch + +from django.test import TestCase +import responses + +from bookwyrm import models, outgoing +from bookwyrm.settings import DOMAIN + +class TestOutgoingRemoteWebfinger(TestCase): + ''' overwrites standard model feilds to work with activitypub ''' + def setUp(self): + ''' get user data ready ''' + datafile = pathlib.Path(__file__).parent.joinpath( + '../data/ap_user.json' + ) + self.userdata = json.loads(datafile.read_bytes()) + del self.userdata['icon'] + + def test_existing_user(self): + ''' simple database lookup by username ''' + user = models.User.objects.create_user( + 'mouse', 'mouse@mouse.mouse', 'mouseword', local=True) + + result = outgoing.handle_remote_webfinger('@mouse@%s' % DOMAIN) + self.assertEqual(result, user) + + result = outgoing.handle_remote_webfinger('mouse@%s' % DOMAIN) + self.assertEqual(result, user) + + + @responses.activate + def test_load_user(self): + username = 'mouse@example.com' + wellknown = { + "subject": "acct:mouse@example.com", + "links": [ + { + "rel": "self", + "type": "application/activity+json", + "href": "https://example.com/user/mouse" + } + ] + } + responses.add( + responses.GET, + 'https://example.com/.well-known/webfinger?resource=acct:%s' \ + % username, + json=wellknown, + status=200) + responses.add( + responses.GET, + 'https://example.com/user/mouse', + json=self.userdata, + status=200) + with patch('bookwyrm.models.user.set_remote_server.delay'): + result = outgoing.handle_remote_webfinger('@mouse@example.com') + self.assertIsInstance(result, models.User) + self.assertEqual(result.username, 'mouse@example.com') diff --git a/bookwyrm/utils/regex.py b/bookwyrm/utils/regex.py index 36e211d98..70a43b849 100644 --- a/bookwyrm/utils/regex.py +++ b/bookwyrm/utils/regex.py @@ -2,4 +2,4 @@ domain = r'[a-z-A-Z0-9_\-]+\.[a-z]+' username = r'@[a-zA-Z_\-\.0-9]+(@%s)?' % domain -full_username = r'@[a-zA-Z_\-\.0-9]+@%s' % domain +full_username = r'@?[a-zA-Z_\-\.0-9]+@%s' % domain