Fixes checking privacy when serializing status

This commit is contained in:
Mouse Reeve 2020-12-13 14:35:56 -08:00
parent b6907f39e9
commit 44cbf7c07f
2 changed files with 84 additions and 37 deletions

View File

@ -180,16 +180,16 @@ class PrivacyField(ActivitypubFieldMixin, models.CharField):
# this is a link to the followers list # this is a link to the followers list
followers = instance.user.__class__._meta.get_field('followers')\ followers = instance.user.__class__._meta.get_field('followers')\
.field_to_activity(instance.user.followers) .field_to_activity(instance.user.followers)
if self.privacy == 'public': if instance.privacy == 'public':
activity['to'] = [self.public] activity['to'] = [self.public]
activity['cc'] = [followers] + mentions activity['cc'] = [followers] + mentions
elif self.privacy == 'unlisted': elif instance.privacy == 'unlisted':
activity['to'] = [followers] activity['to'] = [followers]
activity['cc'] = [self.public] + mentions activity['cc'] = [self.public] + mentions
elif self.privacy == 'followers': elif instance.privacy == 'followers':
activity['to'] = [followers] activity['to'] = [followers]
activity['cc'] = mentions activity['cc'] = mentions
if self.privacy == 'direct': if instance.privacy == 'direct':
activity['to'] = mentions activity['to'] = mentions
activity['cc'] = [] activity['cc'] = []

View File

@ -95,34 +95,67 @@ class BaseActivity(TestCase):
self.assertEqual(result.remote_id, 'https://example.com/user/mouse') self.assertEqual(result.remote_id, 'https://example.com/user/mouse')
self.assertEqual(result.name, 'MOUSE?? MOUSE!!') self.assertEqual(result.name, 'MOUSE?? MOUSE!!')
def test_to_model(self): def test_to_model_invalid_model(self):
''' the big boy of this module. it feels janky to test this with actual ''' catch mismatch between activity type and model type '''
models rather than a test model, but I don't know how to make a test
model so here we are. '''
instance = ActivityObject(id='a', type='b') instance = ActivityObject(id='a', type='b')
with self.assertRaises(ActivitySerializerError): with self.assertRaises(ActivitySerializerError):
instance.to_model(models.User) instance.to_model(models.User)
# test setting simple fields def test_to_model_simple_fields(self):
''' test setting simple fields '''
self.assertEqual(self.user.name, '') self.assertEqual(self.user.name, '')
update_data = activitypub.Person(**self.user.to_activity())
update_data.name = 'New Name' activity = activitypub.Person(
update_data.to_model(models.User, self.user) id=self.user.remote_id,
name='New Name',
preferredUsername='mouse',
inbox='http://www.com/',
outbox='http://www.com/',
followers='',
summary='',
publicKey=None,
endpoints={},
)
activity.to_model(models.User, self.user)
self.assertEqual(self.user.name, 'New Name') self.assertEqual(self.user.name, 'New Name')
def test_to_model_foreign_key(self): def test_to_model_foreign_key(self):
''' test setting one to one/foreign key ''' ''' test setting one to one/foreign key '''
update_data = activitypub.Person(**self.user.to_activity()) activity = activitypub.Person(
update_data.publicKey['publicKeyPem'] = 'hi im secure' id=self.user.remote_id,
update_data.to_model(models.User, self.user) name='New Name',
preferredUsername='mouse',
inbox='http://www.com/',
outbox='http://www.com/',
followers='',
summary='',
publicKey=self.user.key_pair.to_activity(),
endpoints={},
)
activity.publicKey['publicKeyPem'] = 'hi im secure'
activity.to_model(models.User, self.user)
self.assertEqual(self.user.key_pair.public_key, 'hi im secure') self.assertEqual(self.user.key_pair.public_key, 'hi im secure')
@responses.activate @responses.activate
def test_to_model_image(self): def test_to_model_image(self):
''' update an image field ''' ''' update an image field '''
update_data = activitypub.Person(**self.user.to_activity()) activity = activitypub.Person(
update_data.icon = {'url': 'http://www.example.com/image.jpg'} id=self.user.remote_id,
name='New Name',
preferredUsername='mouse',
inbox='http://www.com/',
outbox='http://www.com/',
followers='',
summary='',
publicKey=None,
endpoints={},
icon={'url': 'http://www.example.com/image.jpg'}
)
responses.add( responses.add(
responses.GET, responses.GET,
'http://www.example.com/image.jpg', 'http://www.example.com/image.jpg',
@ -133,7 +166,7 @@ class BaseActivity(TestCase):
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
self.user.avatar.file #pylint: disable=pointless-statement self.user.avatar.file #pylint: disable=pointless-statement
update_data.to_model(models.User, self.user) activity.to_model(models.User, self.user)
self.assertIsNotNone(self.user.avatar.name) self.assertIsNotNone(self.user.avatar.name)
self.assertIsNotNone(self.user.avatar.file) self.assertIsNotNone(self.user.avatar.file)
@ -145,19 +178,26 @@ class BaseActivity(TestCase):
) )
book = models.Edition.objects.create( 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(**status.to_activity()) update_data = activitypub.Note(
update_data.tag = [ id=status.remote_id,
{ content=status.content,
'type': 'Mention', attributedTo=self.user.remote_id,
'name': 'gerald', published='hi',
'href': 'http://example.com/a/b' to=[],
}, cc=[],
{ tag=[
'type': 'Edition', {
'name': 'gerald j. books', 'type': 'Mention',
'href': 'http://book.com/book' 'name': 'gerald',
}, 'href': 'http://example.com/a/b'
] },
{
'type': 'Edition',
'name': 'gerald j. books',
'href': 'http://book.com/book'
},
]
)
update_data.to_model(models.Status, instance=status) update_data.to_model(models.Status, instance=status)
self.assertEqual(status.mention_users.first(), self.user) self.assertEqual(status.mention_users.first(), self.user)
self.assertEqual(status.mention_books.first(), book) self.assertEqual(status.mention_books.first(), book)
@ -171,12 +211,19 @@ class BaseActivity(TestCase):
content='test status', content='test status',
user=self.user, user=self.user,
) )
update_data = activitypub.Note(**status.to_activity()) update_data = activitypub.Note(
update_data.attachment = [{ id=status.remote_id,
'url': 'http://www.example.com/image.jpg', content=status.content,
'name': 'alt text', attributedTo=self.user.remote_id,
'type': 'Image', published='hi',
}] to=[],
cc=[],
attachment=[{
'url': 'http://www.example.com/image.jpg',
'name': 'alt text',
'type': 'Image',
}],
)
responses.add( responses.add(
responses.GET, responses.GET,