Fixes broadcast tests

This commit is contained in:
Mouse Reeve 2020-11-29 09:40:15 -08:00
parent e9be31e9c1
commit 9ddd60ce16
4 changed files with 28 additions and 22 deletions

View File

@ -15,6 +15,7 @@ from django.db.models.query_utils import DeferredAttribute
from django.utils import timezone from django.utils import timezone
import requests import requests
from bookwyrm.connectors import ConnectorException, get_data, get_image
class ActivitySerializerError(ValueError): class ActivitySerializerError(ValueError):
''' routine problems serializing activitypub json ''' ''' routine problems serializing activitypub json '''
@ -242,20 +243,13 @@ def resolve_remote_id(model, remote_id, refresh=False):
# load the data and create the object # load the data and create the object
try: try:
response = requests.get( data = get_data(remote_id)
remote_id, except (ConnectorException, ConnectionError):
headers={'Accept': 'application/json; charset=utf-8'},
)
except ConnectionError:
raise ActivitySerializerError( raise ActivitySerializerError(
'Could not connect to host for remote_id in %s model: %s' % \ 'Could not connect to host for remote_id in %s model: %s' % \
(model.__name__, remote_id)) (model.__name__, remote_id))
if not response.ok:
raise ActivitySerializerError(
'Could not resolve remote_id in %s model: %s' % \
(model.__name__, remote_id))
item = model.activity_serializer(**response.json()) item = model.activity_serializer(**data)
# if we're refreshing, "result" will be set and we'll update it # if we're refreshing, "result" will be set and we'll update it
return item.to_model(model, instance=result) return item.to_model(model, instance=result)
@ -272,11 +266,9 @@ def image_formatter(image_slug):
return None return None
if not url: if not url:
return None return None
try:
response = requests.get(url) response = get_image(url)
except ConnectionError: if not response:
return None
if not response.ok:
return None return None
image_name = str(uuid4()) + '.' + url.split('.')[-1] image_name = str(uuid4()) + '.' + url.split('.')[-1]

View File

@ -1,3 +1,4 @@
''' bring connectors into the namespace ''' ''' bring connectors into the namespace '''
from .settings import CONNECTORS from .settings import CONNECTORS
from .abstract_connector import ConnectorException from .abstract_connector import ConnectorException
from .abstract_connector import get_data, get_image

View File

@ -318,6 +318,17 @@ def get_data(url):
return data return data
def get_image(url):
''' wrapper for requesting an image '''
try:
resp = requests.get(url)
except RequestError:
return None
if not resp.ok:
return None
return resp
@dataclass @dataclass
class SearchResult: class SearchResult:
''' standardized search result object ''' ''' standardized search result object '''

View File

@ -1,3 +1,4 @@
from unittest.mock import patch
from django.test import TestCase from django.test import TestCase
from bookwyrm import models, broadcast from bookwyrm import models, broadcast
@ -37,13 +38,14 @@ class Book(TestCase):
'joe', 'joe@mouse.mouse', 'jeoword') 'joe', 'joe@mouse.mouse', 'jeoword')
self.user.followers.add(local_follower) self.user.followers.add(local_follower)
models.User.objects.create_user( with patch('bookwyrm.models.user.get_remote_reviews.delay'):
'nutria', 'nutria@mouse.mouse', 'nuword', models.User.objects.create_user(
remote_id='http://example.com/u/4', 'nutria', 'nutria@mouse.mouse', 'nuword',
outbox='http://example.com/u/4/o', remote_id='http://example.com/u/4',
shared_inbox='http://example.com/inbox', outbox='http://example.com/u/4/o',
inbox='http://example.com/u/4/inbox', shared_inbox='http://example.com/inbox',
local=False) inbox='http://example.com/u/4/inbox',
local=False)
def test_get_public_recipients(self): def test_get_public_recipients(self):