Adds deduplication fields
This commit is contained in:
@ -11,13 +11,16 @@ from bookwyrm.models.base_model import ActivitypubMixin
|
||||
from bookwyrm.settings import DOMAIN
|
||||
|
||||
class BaseModel(TestCase):
|
||||
''' functionality shared across models '''
|
||||
def test_remote_id(self):
|
||||
''' these should be generated '''
|
||||
instance = base_model.BookWyrmModel()
|
||||
instance.id = 1
|
||||
expected = instance.get_remote_id()
|
||||
self.assertEqual(expected, 'https://%s/bookwyrmmodel/1' % DOMAIN)
|
||||
|
||||
def test_remote_id_with_user(self):
|
||||
''' format of remote id when there's a user object '''
|
||||
user = models.User.objects.create_user(
|
||||
'mouse', 'mouse@mouse.com', 'mouseword', local=True)
|
||||
instance = base_model.BookWyrmModel()
|
||||
@ -46,6 +49,7 @@ class BaseModel(TestCase):
|
||||
self.assertIsNone(instance.remote_id)
|
||||
|
||||
def test_to_create_activity(self):
|
||||
''' wrapper for ActivityPub "create" action '''
|
||||
user = models.User.objects.create_user(
|
||||
'mouse', 'mouse@mouse.com', 'mouseword', local=True)
|
||||
|
||||
@ -75,6 +79,7 @@ class BaseModel(TestCase):
|
||||
)
|
||||
|
||||
def test_to_delete_activity(self):
|
||||
''' wrapper for Delete activity '''
|
||||
user = models.User.objects.create_user(
|
||||
'mouse', 'mouse@mouse.com', 'mouseword', local=True)
|
||||
|
||||
@ -98,6 +103,7 @@ class BaseModel(TestCase):
|
||||
['https://www.w3.org/ns/activitystreams#Public'])
|
||||
|
||||
def test_to_update_activity(self):
|
||||
''' ditto above but for Update '''
|
||||
user = models.User.objects.create_user(
|
||||
'mouse', 'mouse@mouse.com', 'mouseword', local=True)
|
||||
|
||||
@ -121,6 +127,7 @@ class BaseModel(TestCase):
|
||||
self.assertEqual(activity['object'], {})
|
||||
|
||||
def test_to_undo_activity(self):
|
||||
''' and again, for Undo '''
|
||||
user = models.User.objects.create_user(
|
||||
'mouse', 'mouse@mouse.com', 'mouseword', local=True)
|
||||
|
||||
@ -140,12 +147,14 @@ class BaseModel(TestCase):
|
||||
|
||||
|
||||
def test_to_activity(self):
|
||||
''' model to ActivityPub json '''
|
||||
@dataclass(init=False)
|
||||
class TestActivity(ActivityObject):
|
||||
''' real simple mock '''
|
||||
type: str = 'Test'
|
||||
|
||||
class TestModel(ActivitypubMixin, base_model.BookWyrmModel):
|
||||
pass
|
||||
''' real simple mock model because BookWyrmModel is abstract '''
|
||||
|
||||
instance = TestModel()
|
||||
instance.remote_id = 'https://www.example.com/test'
|
||||
@ -155,3 +164,32 @@ class BaseModel(TestCase):
|
||||
self.assertIsInstance(activity, dict)
|
||||
self.assertEqual(activity['id'], 'https://www.example.com/test')
|
||||
self.assertEqual(activity['type'], 'Test')
|
||||
|
||||
|
||||
def test_find_existing_by_remote_id(self):
|
||||
''' attempt to match a remote id to an object in the db '''
|
||||
# uses a different remote id scheme
|
||||
# this isn't really part of this test directly but it's helpful to state
|
||||
self.assertEqual(self.book.origin_id, 'http://book.com/book')
|
||||
self.assertNotEqual(self.book.remote_id, 'http://book.com/book')
|
||||
|
||||
# uses subclasses
|
||||
models.Comment.objects.create(
|
||||
user=self.user, content='test status', book=self.book, \
|
||||
remote_id='https://comment.net')
|
||||
|
||||
result = models.User.find_existing_by_remote_id('hi')
|
||||
self.assertIsNone(result)
|
||||
|
||||
result = models.User.find_existing_by_remote_id(
|
||||
'http://example.com/a/b')
|
||||
self.assertEqual(result, self.user)
|
||||
|
||||
# test using origin id
|
||||
result = models.Edition.find_existing_by_remote_id(
|
||||
'http://book.com/book')
|
||||
self.assertEqual(result, self.book)
|
||||
|
||||
# test subclass match
|
||||
result = models.Status.find_existing_by_remote_id(
|
||||
'https://comment.net')
|
||||
|
@ -48,6 +48,7 @@ class ActivitypubFields(TestCase):
|
||||
instance = fields.ActivitypubFieldMixin()
|
||||
self.assertEqual(instance.field_to_activity('fish'), 'fish')
|
||||
self.assertEqual(instance.field_from_activity('fish'), 'fish')
|
||||
self.assertFalse(instance.deduplication_field)
|
||||
|
||||
instance = fields.ActivitypubFieldMixin(
|
||||
activitypub_wrapper='endpoints', activitypub_field='outbox'
|
||||
@ -70,6 +71,7 @@ class ActivitypubFields(TestCase):
|
||||
''' just sets some defaults on charfield '''
|
||||
instance = fields.RemoteIdField()
|
||||
self.assertEqual(instance.max_length, 255)
|
||||
self.assertTrue(instance.deduplication_field)
|
||||
|
||||
with self.assertRaises(ValidationError):
|
||||
instance.run_validators('http://www.example.com/dlfjg 23/x')
|
||||
@ -97,7 +99,7 @@ class ActivitypubFields(TestCase):
|
||||
self.assertEqual(instance.field_to_activity(item), 'https://e.b/c')
|
||||
|
||||
@responses.activate
|
||||
def test_foreign_key_from_activity(self):
|
||||
def test_foreign_key_from_activity_str(self):
|
||||
''' this is the important stuff '''
|
||||
instance = fields.ForeignKey(User, on_delete=models.CASCADE)
|
||||
|
||||
@ -117,15 +119,47 @@ class ActivitypubFields(TestCase):
|
||||
with patch('bookwyrm.models.user.set_remote_server.delay'):
|
||||
value = instance.field_from_activity(
|
||||
'https://example.com/user/mouse')
|
||||
self.assertIsInstance(value, User)
|
||||
self.assertEqual(value.remote_id, 'https://example.com/user/mouse')
|
||||
self.assertEqual(value.name, 'MOUSE?? MOUSE!!')
|
||||
|
||||
# test recieving activity json
|
||||
value = instance.field_from_activity(userdata)
|
||||
|
||||
def test_foreign_key_from_activity_dict(self):
|
||||
''' test recieving activity json '''
|
||||
instance = fields.ForeignKey(User, on_delete=models.CASCADE)
|
||||
|
||||
datafile = pathlib.Path(__file__).parent.joinpath(
|
||||
'../data/ap_user.json'
|
||||
)
|
||||
userdata = json.loads(datafile.read_bytes())
|
||||
# don't try to load the user icon
|
||||
del userdata['icon']
|
||||
with patch('bookwyrm.models.user.set_remote_server.delay'):
|
||||
value = instance.field_from_activity(userdata)
|
||||
self.assertIsInstance(value, User)
|
||||
self.assertEqual(value.remote_id, 'https://example.com/user/mouse')
|
||||
self.assertEqual(value.name, 'MOUSE?? MOUSE!!')
|
||||
# et cetera but we're not testing serializing user json
|
||||
|
||||
# test receiving a remote id of an object in the db
|
||||
|
||||
def test_foreign_key_from_activity_dict_existing(self):
|
||||
''' test receiving a dict of an existing object in the db '''
|
||||
instance = fields.ForeignKey(User, on_delete=models.CASCADE)
|
||||
datafile = pathlib.Path(__file__).parent.joinpath(
|
||||
'../data/ap_user.json'
|
||||
)
|
||||
userdata = json.loads(datafile.read_bytes())
|
||||
user = User.objects.create_user(
|
||||
'mouse', 'mouse@mouse.mouse', 'mouseword', local=True)
|
||||
user.remote_id = 'https://example.com/user/mouse'
|
||||
user.save()
|
||||
value = instance.field_from_activity(userdata)
|
||||
self.assertEqual(value, user)
|
||||
|
||||
|
||||
def test_foreign_key_from_activity_str_existing(self):
|
||||
''' test receiving a remote id of an existing object in the db '''
|
||||
instance = fields.ForeignKey(User, on_delete=models.CASCADE)
|
||||
user = User.objects.create_user(
|
||||
'mouse', 'mouse@mouse.mouse', 'mouseword', local=True)
|
||||
value = instance.field_from_activity(user.remote_id)
|
||||
|
Reference in New Issue
Block a user