Fixes broadcast tests
This commit is contained in:
parent
e9be31e9c1
commit
9ddd60ce16
|
@ -15,6 +15,7 @@ from django.db.models.query_utils import DeferredAttribute
|
|||
from django.utils import timezone
|
||||
import requests
|
||||
|
||||
from bookwyrm.connectors import ConnectorException, get_data, get_image
|
||||
|
||||
class ActivitySerializerError(ValueError):
|
||||
''' 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
|
||||
try:
|
||||
response = requests.get(
|
||||
remote_id,
|
||||
headers={'Accept': 'application/json; charset=utf-8'},
|
||||
)
|
||||
except ConnectionError:
|
||||
data = get_data(remote_id)
|
||||
except (ConnectorException, ConnectionError):
|
||||
raise ActivitySerializerError(
|
||||
'Could not connect to host for remote_id in %s model: %s' % \
|
||||
(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
|
||||
return item.to_model(model, instance=result)
|
||||
|
||||
|
@ -272,11 +266,9 @@ def image_formatter(image_slug):
|
|||
return None
|
||||
if not url:
|
||||
return None
|
||||
try:
|
||||
response = requests.get(url)
|
||||
except ConnectionError:
|
||||
return None
|
||||
if not response.ok:
|
||||
|
||||
response = get_image(url)
|
||||
if not response:
|
||||
return None
|
||||
|
||||
image_name = str(uuid4()) + '.' + url.split('.')[-1]
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
''' bring connectors into the namespace '''
|
||||
from .settings import CONNECTORS
|
||||
from .abstract_connector import ConnectorException
|
||||
from .abstract_connector import get_data, get_image
|
||||
|
|
|
@ -318,6 +318,17 @@ def get_data(url):
|
|||
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
|
||||
class SearchResult:
|
||||
''' standardized search result object '''
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
from unittest.mock import patch
|
||||
from django.test import TestCase
|
||||
|
||||
from bookwyrm import models, broadcast
|
||||
|
@ -37,6 +38,7 @@ class Book(TestCase):
|
|||
'joe', 'joe@mouse.mouse', 'jeoword')
|
||||
self.user.followers.add(local_follower)
|
||||
|
||||
with patch('bookwyrm.models.user.get_remote_reviews.delay'):
|
||||
models.User.objects.create_user(
|
||||
'nutria', 'nutria@mouse.mouse', 'nuword',
|
||||
remote_id='http://example.com/u/4',
|
||||
|
|
Loading…
Reference in New Issue