Use get_data in resolving remote id

This commit is contained in:
Mouse Reeve 2020-12-12 15:19:57 -08:00
parent 49979fabef
commit cb28c19abc
3 changed files with 65 additions and 10 deletions

View file

@ -4,10 +4,10 @@ import re
from django.db import IntegrityError, transaction from django.db import IntegrityError, transaction
from django.http import HttpResponseNotFound, JsonResponse from django.http import HttpResponseNotFound, JsonResponse
from django.views.decorators.csrf import csrf_exempt from django.views.decorators.csrf import csrf_exempt
import requests
from bookwyrm import activitypub from bookwyrm import activitypub
from bookwyrm import models from bookwyrm import models
from bookwyrm.connectors import get_data
from bookwyrm.broadcast import broadcast from bookwyrm.broadcast import broadcast
from bookwyrm.status import create_notification from bookwyrm.status import create_notification
from bookwyrm.status import create_generated_note from bookwyrm.status import create_generated_note
@ -52,14 +52,8 @@ def handle_remote_webfinger(query):
except models.User.DoesNotExist: except models.User.DoesNotExist:
url = 'https://%s/.well-known/webfinger?resource=acct:%s' % \ url = 'https://%s/.well-known/webfinger?resource=acct:%s' % \
(domain, query) (domain, query)
try: data = get_data(url)
response = requests.get(url) for link in data.get('links'):
except requests.exceptions.ConnectionError:
return None
if not response.ok:
return None
data = response.json()
for link in data['links']:
if link.get('rel') == 'self': if link.get('rel') == 'self':
try: try:
user = activitypub.resolve_remote_id( user = activitypub.resolve_remote_id(

View file

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

View file

@ -2,4 +2,4 @@
domain = r'[a-z-A-Z0-9_\-]+\.[a-z]+' domain = r'[a-z-A-Z0-9_\-]+\.[a-z]+'
username = r'@[a-zA-Z_\-\.0-9]+(@%s)?' % domain 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