Runs black
This commit is contained in:
@ -7,19 +7,18 @@ from bookwyrm import models
|
||||
class Author(TestCase):
|
||||
def setUp(self):
|
||||
self.book = models.Edition.objects.create(
|
||||
title='Example Edition',
|
||||
remote_id='https://example.com/book/1',
|
||||
title="Example Edition",
|
||||
remote_id="https://example.com/book/1",
|
||||
)
|
||||
self.author = models.Author.objects.create(
|
||||
name='Author fullname',
|
||||
aliases=['One', 'Two'],
|
||||
bio='bio bio bio',
|
||||
name="Author fullname",
|
||||
aliases=["One", "Two"],
|
||||
bio="bio bio bio",
|
||||
)
|
||||
|
||||
|
||||
def test_serialize_model(self):
|
||||
activity = self.author.to_activity()
|
||||
self.assertEqual(activity['id'], self.author.remote_id)
|
||||
self.assertIsInstance(activity['aliases'], list)
|
||||
self.assertEqual(activity['aliases'], ['One', 'Two'])
|
||||
self.assertEqual(activity['name'], 'Author fullname')
|
||||
self.assertEqual(activity["id"], self.author.remote_id)
|
||||
self.assertIsInstance(activity["aliases"], list)
|
||||
self.assertEqual(activity["aliases"], ["One", "Two"])
|
||||
self.assertEqual(activity["name"], "Author fullname")
|
||||
|
@ -1,4 +1,4 @@
|
||||
''' tests the base functionality for activitypub dataclasses '''
|
||||
""" tests the base functionality for activitypub dataclasses """
|
||||
from io import BytesIO
|
||||
import json
|
||||
import pathlib
|
||||
@ -10,231 +10,229 @@ from PIL import Image
|
||||
import responses
|
||||
|
||||
from bookwyrm import activitypub
|
||||
from bookwyrm.activitypub.base_activity import ActivityObject, \
|
||||
resolve_remote_id, set_related_field
|
||||
from bookwyrm.activitypub.base_activity import (
|
||||
ActivityObject,
|
||||
resolve_remote_id,
|
||||
set_related_field,
|
||||
)
|
||||
from bookwyrm.activitypub import ActivitySerializerError
|
||||
from bookwyrm import models
|
||||
|
||||
|
||||
class BaseActivity(TestCase):
|
||||
''' the super class for model-linked activitypub dataclasses '''
|
||||
""" the super class for model-linked activitypub dataclasses """
|
||||
|
||||
def setUp(self):
|
||||
''' we're probably going to re-use this so why copy/paste '''
|
||||
""" we're probably going to re-use this so why copy/paste """
|
||||
self.user = models.User.objects.create_user(
|
||||
'mouse', 'mouse@mouse.mouse', 'mouseword',
|
||||
local=True, localname='mouse')
|
||||
self.user.remote_id = 'http://example.com/a/b'
|
||||
"mouse", "mouse@mouse.mouse", "mouseword", local=True, localname="mouse"
|
||||
)
|
||||
self.user.remote_id = "http://example.com/a/b"
|
||||
self.user.save(broadcast=False)
|
||||
|
||||
datafile = pathlib.Path(__file__).parent.joinpath(
|
||||
'../data/ap_user.json'
|
||||
)
|
||||
datafile = pathlib.Path(__file__).parent.joinpath("../data/ap_user.json")
|
||||
self.userdata = json.loads(datafile.read_bytes())
|
||||
# don't try to load the user icon
|
||||
del self.userdata['icon']
|
||||
del self.userdata["icon"]
|
||||
|
||||
image_file = pathlib.Path(__file__).parent.joinpath(
|
||||
'../../static/images/default_avi.jpg')
|
||||
"../../static/images/default_avi.jpg"
|
||||
)
|
||||
image = Image.open(image_file)
|
||||
output = BytesIO()
|
||||
image.save(output, format=image.format)
|
||||
self.image_data = output.getvalue()
|
||||
|
||||
def test_init(self):
|
||||
''' simple successfuly init '''
|
||||
instance = ActivityObject(id='a', type='b')
|
||||
self.assertTrue(hasattr(instance, 'id'))
|
||||
self.assertTrue(hasattr(instance, 'type'))
|
||||
""" simple successfuly init """
|
||||
instance = ActivityObject(id="a", type="b")
|
||||
self.assertTrue(hasattr(instance, "id"))
|
||||
self.assertTrue(hasattr(instance, "type"))
|
||||
|
||||
def test_init_missing(self):
|
||||
''' init with missing required params '''
|
||||
""" init with missing required params """
|
||||
with self.assertRaises(ActivitySerializerError):
|
||||
ActivityObject()
|
||||
|
||||
def test_init_extra_fields(self):
|
||||
''' init ignoring additional fields '''
|
||||
instance = ActivityObject(id='a', type='b', fish='c')
|
||||
self.assertTrue(hasattr(instance, 'id'))
|
||||
self.assertTrue(hasattr(instance, 'type'))
|
||||
""" init ignoring additional fields """
|
||||
instance = ActivityObject(id="a", type="b", fish="c")
|
||||
self.assertTrue(hasattr(instance, "id"))
|
||||
self.assertTrue(hasattr(instance, "type"))
|
||||
|
||||
def test_init_default_field(self):
|
||||
''' replace an existing required field with a default field '''
|
||||
""" replace an existing required field with a default field """
|
||||
|
||||
@dataclass(init=False)
|
||||
class TestClass(ActivityObject):
|
||||
''' test class with default field '''
|
||||
type: str = 'TestObject'
|
||||
""" test class with default field """
|
||||
|
||||
instance = TestClass(id='a')
|
||||
self.assertEqual(instance.id, 'a')
|
||||
self.assertEqual(instance.type, 'TestObject')
|
||||
type: str = "TestObject"
|
||||
|
||||
instance = TestClass(id="a")
|
||||
self.assertEqual(instance.id, "a")
|
||||
self.assertEqual(instance.type, "TestObject")
|
||||
|
||||
def test_serialize(self):
|
||||
''' simple function for converting dataclass to dict '''
|
||||
instance = ActivityObject(id='a', type='b')
|
||||
""" simple function for converting dataclass to dict """
|
||||
instance = ActivityObject(id="a", type="b")
|
||||
serialized = instance.serialize()
|
||||
self.assertIsInstance(serialized, dict)
|
||||
self.assertEqual(serialized['id'], 'a')
|
||||
self.assertEqual(serialized['type'], 'b')
|
||||
self.assertEqual(serialized["id"], "a")
|
||||
self.assertEqual(serialized["type"], "b")
|
||||
|
||||
@responses.activate
|
||||
def test_resolve_remote_id(self):
|
||||
''' look up or load remote data '''
|
||||
""" look up or load remote data """
|
||||
# existing item
|
||||
result = resolve_remote_id('http://example.com/a/b', model=models.User)
|
||||
result = resolve_remote_id("http://example.com/a/b", model=models.User)
|
||||
self.assertEqual(result, self.user)
|
||||
|
||||
# remote item
|
||||
responses.add(
|
||||
responses.GET,
|
||||
'https://example.com/user/mouse',
|
||||
"https://example.com/user/mouse",
|
||||
json=self.userdata,
|
||||
status=200)
|
||||
status=200,
|
||||
)
|
||||
|
||||
with patch('bookwyrm.models.user.set_remote_server.delay'):
|
||||
with patch("bookwyrm.models.user.set_remote_server.delay"):
|
||||
result = resolve_remote_id(
|
||||
'https://example.com/user/mouse', model=models.User)
|
||||
"https://example.com/user/mouse", model=models.User
|
||||
)
|
||||
self.assertIsInstance(result, models.User)
|
||||
self.assertEqual(result.remote_id, 'https://example.com/user/mouse')
|
||||
self.assertEqual(result.name, 'MOUSE?? MOUSE!!')
|
||||
self.assertEqual(result.remote_id, "https://example.com/user/mouse")
|
||||
self.assertEqual(result.name, "MOUSE?? MOUSE!!")
|
||||
|
||||
def test_to_model_invalid_model(self):
|
||||
''' catch mismatch between activity type and model type '''
|
||||
instance = ActivityObject(id='a', type='b')
|
||||
""" catch mismatch between activity type and model type """
|
||||
instance = ActivityObject(id="a", type="b")
|
||||
with self.assertRaises(ActivitySerializerError):
|
||||
instance.to_model(model=models.User)
|
||||
|
||||
|
||||
@responses.activate
|
||||
def test_to_model_image(self):
|
||||
''' update an image field '''
|
||||
""" update an image field """
|
||||
activity = activitypub.Person(
|
||||
id=self.user.remote_id,
|
||||
name='New Name',
|
||||
preferredUsername='mouse',
|
||||
inbox='http://www.com/',
|
||||
outbox='http://www.com/',
|
||||
followers='',
|
||||
summary='',
|
||||
publicKey={
|
||||
'id': 'hi',
|
||||
'owner': self.user.remote_id,
|
||||
'publicKeyPem': 'hi'},
|
||||
name="New Name",
|
||||
preferredUsername="mouse",
|
||||
inbox="http://www.com/",
|
||||
outbox="http://www.com/",
|
||||
followers="",
|
||||
summary="",
|
||||
publicKey={"id": "hi", "owner": self.user.remote_id, "publicKeyPem": "hi"},
|
||||
endpoints={},
|
||||
icon={
|
||||
'type': 'Image',
|
||||
'url': 'http://www.example.com/image.jpg'
|
||||
}
|
||||
icon={"type": "Image", "url": "http://www.example.com/image.jpg"},
|
||||
)
|
||||
|
||||
responses.add(
|
||||
responses.GET,
|
||||
'http://www.example.com/image.jpg',
|
||||
"http://www.example.com/image.jpg",
|
||||
body=self.image_data,
|
||||
status=200)
|
||||
status=200,
|
||||
)
|
||||
|
||||
self.assertIsNone(self.user.avatar.name)
|
||||
with self.assertRaises(ValueError):
|
||||
self.user.avatar.file #pylint: disable=pointless-statement
|
||||
self.user.avatar.file # pylint: disable=pointless-statement
|
||||
|
||||
# this would trigger a broadcast because it's a local user
|
||||
with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'):
|
||||
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
|
||||
activity.to_model(model=models.User, instance=self.user)
|
||||
self.assertIsNotNone(self.user.avatar.file)
|
||||
self.assertEqual(self.user.name, 'New Name')
|
||||
self.assertEqual(self.user.key_pair.public_key, 'hi')
|
||||
self.assertEqual(self.user.name, "New Name")
|
||||
self.assertEqual(self.user.key_pair.public_key, "hi")
|
||||
|
||||
def test_to_model_many_to_many(self):
|
||||
''' annoying that these all need special handling '''
|
||||
with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'):
|
||||
""" annoying that these all need special handling """
|
||||
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
|
||||
status = models.Status.objects.create(
|
||||
content='test status',
|
||||
content="test status",
|
||||
user=self.user,
|
||||
)
|
||||
book = models.Edition.objects.create(
|
||||
title='Test Edition', remote_id='http://book.com/book')
|
||||
title="Test Edition", remote_id="http://book.com/book"
|
||||
)
|
||||
update_data = activitypub.Note(
|
||||
id=status.remote_id,
|
||||
content=status.content,
|
||||
attributedTo=self.user.remote_id,
|
||||
published='hi',
|
||||
published="hi",
|
||||
to=[],
|
||||
cc=[],
|
||||
tag=[
|
||||
{"type": "Mention", "name": "gerald", "href": "http://example.com/a/b"},
|
||||
{
|
||||
'type': 'Mention',
|
||||
'name': 'gerald',
|
||||
'href': 'http://example.com/a/b'
|
||||
"type": "Edition",
|
||||
"name": "gerald j. books",
|
||||
"href": "http://book.com/book",
|
||||
},
|
||||
{
|
||||
'type': 'Edition',
|
||||
'name': 'gerald j. books',
|
||||
'href': 'http://book.com/book'
|
||||
},
|
||||
]
|
||||
],
|
||||
)
|
||||
update_data.to_model(model=models.Status, instance=status)
|
||||
self.assertEqual(status.mention_users.first(), self.user)
|
||||
self.assertEqual(status.mention_books.first(), book)
|
||||
|
||||
|
||||
@responses.activate
|
||||
def test_to_model_one_to_many(self):
|
||||
''' these are reversed relationships, where the secondary object
|
||||
keys the primary object but not vice versa '''
|
||||
with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'):
|
||||
"""these are reversed relationships, where the secondary object
|
||||
keys the primary object but not vice versa"""
|
||||
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
|
||||
status = models.Status.objects.create(
|
||||
content='test status',
|
||||
content="test status",
|
||||
user=self.user,
|
||||
)
|
||||
update_data = activitypub.Note(
|
||||
id=status.remote_id,
|
||||
content=status.content,
|
||||
attributedTo=self.user.remote_id,
|
||||
published='hi',
|
||||
published="hi",
|
||||
to=[],
|
||||
cc=[],
|
||||
attachment=[{
|
||||
'url': 'http://www.example.com/image.jpg',
|
||||
'name': 'alt text',
|
||||
'type': 'Image',
|
||||
}],
|
||||
attachment=[
|
||||
{
|
||||
"url": "http://www.example.com/image.jpg",
|
||||
"name": "alt text",
|
||||
"type": "Image",
|
||||
}
|
||||
],
|
||||
)
|
||||
|
||||
responses.add(
|
||||
responses.GET,
|
||||
'http://www.example.com/image.jpg',
|
||||
"http://www.example.com/image.jpg",
|
||||
body=self.image_data,
|
||||
status=200)
|
||||
status=200,
|
||||
)
|
||||
|
||||
# sets the celery task call to the function call
|
||||
with patch(
|
||||
'bookwyrm.activitypub.base_activity.set_related_field.delay'):
|
||||
with patch('bookwyrm.models.status.Status.ignore_activity') \
|
||||
as discarder:
|
||||
with patch("bookwyrm.activitypub.base_activity.set_related_field.delay"):
|
||||
with patch("bookwyrm.models.status.Status.ignore_activity") as discarder:
|
||||
discarder.return_value = False
|
||||
update_data.to_model(model=models.Status, instance=status)
|
||||
self.assertIsNone(status.attachments.first())
|
||||
|
||||
|
||||
@responses.activate
|
||||
def test_set_related_field(self):
|
||||
''' celery task to add back-references to created objects '''
|
||||
with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'):
|
||||
""" celery task to add back-references to created objects """
|
||||
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
|
||||
status = models.Status.objects.create(
|
||||
content='test status',
|
||||
content="test status",
|
||||
user=self.user,
|
||||
)
|
||||
data = {
|
||||
'url': 'http://www.example.com/image.jpg',
|
||||
'name': 'alt text',
|
||||
'type': 'Image',
|
||||
"url": "http://www.example.com/image.jpg",
|
||||
"name": "alt text",
|
||||
"type": "Image",
|
||||
}
|
||||
responses.add(
|
||||
responses.GET,
|
||||
'http://www.example.com/image.jpg',
|
||||
"http://www.example.com/image.jpg",
|
||||
body=self.image_data,
|
||||
status=200)
|
||||
set_related_field(
|
||||
'Image', 'Status', 'status', status.remote_id, data)
|
||||
status=200,
|
||||
)
|
||||
set_related_field("Image", "Status", "status", status.remote_id, data)
|
||||
|
||||
self.assertIsInstance(status.attachments.first(), models.Image)
|
||||
self.assertIsNotNone(status.attachments.first().image)
|
||||
|
@ -9,23 +9,19 @@ from bookwyrm import activitypub, models
|
||||
|
||||
class Person(TestCase):
|
||||
def setUp(self):
|
||||
datafile = pathlib.Path(__file__).parent.joinpath(
|
||||
'../data/ap_user.json'
|
||||
)
|
||||
datafile = pathlib.Path(__file__).parent.joinpath("../data/ap_user.json")
|
||||
self.user_data = json.loads(datafile.read_bytes())
|
||||
|
||||
|
||||
def test_load_user_data(self):
|
||||
activity = activitypub.Person(**self.user_data)
|
||||
self.assertEqual(activity.id, 'https://example.com/user/mouse')
|
||||
self.assertEqual(activity.preferredUsername, 'mouse')
|
||||
self.assertEqual(activity.type, 'Person')
|
||||
|
||||
self.assertEqual(activity.id, "https://example.com/user/mouse")
|
||||
self.assertEqual(activity.preferredUsername, "mouse")
|
||||
self.assertEqual(activity.type, "Person")
|
||||
|
||||
def test_user_to_model(self):
|
||||
activity = activitypub.Person(**self.user_data)
|
||||
with patch('bookwyrm.models.user.set_remote_server.delay'):
|
||||
with patch("bookwyrm.models.user.set_remote_server.delay"):
|
||||
user = activity.to_model(model=models.User)
|
||||
self.assertEqual(user.username, 'mouse@example.com')
|
||||
self.assertEqual(user.remote_id, 'https://example.com/user/mouse')
|
||||
self.assertEqual(user.username, "mouse@example.com")
|
||||
self.assertEqual(user.remote_id, "https://example.com/user/mouse")
|
||||
self.assertFalse(user.local)
|
||||
|
@ -1,4 +1,4 @@
|
||||
''' quotation activty object serializer class '''
|
||||
""" quotation activty object serializer class """
|
||||
import json
|
||||
import pathlib
|
||||
from unittest.mock import patch
|
||||
@ -8,43 +8,40 @@ from bookwyrm import activitypub, models
|
||||
|
||||
|
||||
class Quotation(TestCase):
|
||||
''' we have hecka ways to create statuses '''
|
||||
""" we have hecka ways to create statuses """
|
||||
|
||||
def setUp(self):
|
||||
''' model objects we'll need '''
|
||||
with patch('bookwyrm.models.user.set_remote_server.delay'):
|
||||
""" model objects we'll need """
|
||||
with patch("bookwyrm.models.user.set_remote_server.delay"):
|
||||
self.user = models.User.objects.create_user(
|
||||
'mouse', 'mouse@mouse.mouse', 'mouseword',
|
||||
"mouse",
|
||||
"mouse@mouse.mouse",
|
||||
"mouseword",
|
||||
local=False,
|
||||
inbox='https://example.com/user/mouse/inbox',
|
||||
outbox='https://example.com/user/mouse/outbox',
|
||||
remote_id='https://example.com/user/mouse',
|
||||
inbox="https://example.com/user/mouse/inbox",
|
||||
outbox="https://example.com/user/mouse/outbox",
|
||||
remote_id="https://example.com/user/mouse",
|
||||
)
|
||||
self.book = models.Edition.objects.create(
|
||||
title='Example Edition',
|
||||
remote_id='https://example.com/book/1',
|
||||
)
|
||||
datafile = pathlib.Path(__file__).parent.joinpath(
|
||||
'../data/ap_quotation.json'
|
||||
title="Example Edition",
|
||||
remote_id="https://example.com/book/1",
|
||||
)
|
||||
datafile = pathlib.Path(__file__).parent.joinpath("../data/ap_quotation.json")
|
||||
self.status_data = json.loads(datafile.read_bytes())
|
||||
|
||||
|
||||
def test_quotation_activity(self):
|
||||
''' create a Quoteation ap object from json '''
|
||||
""" create a Quoteation ap object from json """
|
||||
quotation = activitypub.Quotation(**self.status_data)
|
||||
|
||||
self.assertEqual(quotation.type, 'Quotation')
|
||||
self.assertEqual(
|
||||
quotation.id, 'https://example.com/user/mouse/quotation/13')
|
||||
self.assertEqual(quotation.content, 'commentary')
|
||||
self.assertEqual(quotation.quote, 'quote body')
|
||||
self.assertEqual(quotation.inReplyToBook, 'https://example.com/book/1')
|
||||
self.assertEqual(
|
||||
quotation.published, '2020-05-10T02:38:31.150343+00:00')
|
||||
|
||||
self.assertEqual(quotation.type, "Quotation")
|
||||
self.assertEqual(quotation.id, "https://example.com/user/mouse/quotation/13")
|
||||
self.assertEqual(quotation.content, "commentary")
|
||||
self.assertEqual(quotation.quote, "quote body")
|
||||
self.assertEqual(quotation.inReplyToBook, "https://example.com/book/1")
|
||||
self.assertEqual(quotation.published, "2020-05-10T02:38:31.150343+00:00")
|
||||
|
||||
def test_activity_to_model(self):
|
||||
''' create a model instance from an activity object '''
|
||||
""" create a model instance from an activity object """
|
||||
activity = activitypub.Quotation(**self.status_data)
|
||||
quotation = activity.to_model(model=models.Quotation)
|
||||
|
||||
|
Reference in New Issue
Block a user