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
|
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]
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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 '''
|
||||||
|
|
|
@ -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,6 +38,7 @@ 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)
|
||||||
|
|
||||||
|
with patch('bookwyrm.models.user.get_remote_reviews.delay'):
|
||||||
models.User.objects.create_user(
|
models.User.objects.create_user(
|
||||||
'nutria', 'nutria@mouse.mouse', 'nuword',
|
'nutria', 'nutria@mouse.mouse', 'nuword',
|
||||||
remote_id='http://example.com/u/4',
|
remote_id='http://example.com/u/4',
|
||||||
|
|
Loading…
Reference in New Issue