From 44cbf7c07fd9449dca81b56e798b518fe8be034c Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sun, 13 Dec 2020 14:35:56 -0800 Subject: [PATCH] Fixes checking privacy when serializing status --- bookwyrm/models/fields.py | 8 +- .../tests/activitypub/test_base_activity.py | 113 +++++++++++++----- 2 files changed, 84 insertions(+), 37 deletions(-) diff --git a/bookwyrm/models/fields.py b/bookwyrm/models/fields.py index 05453397..960be161 100644 --- a/bookwyrm/models/fields.py +++ b/bookwyrm/models/fields.py @@ -180,16 +180,16 @@ class PrivacyField(ActivitypubFieldMixin, models.CharField): # this is a link to the followers list followers = instance.user.__class__._meta.get_field('followers')\ .field_to_activity(instance.user.followers) - if self.privacy == 'public': + if instance.privacy == 'public': activity['to'] = [self.public] activity['cc'] = [followers] + mentions - elif self.privacy == 'unlisted': + elif instance.privacy == 'unlisted': activity['to'] = [followers] activity['cc'] = [self.public] + mentions - elif self.privacy == 'followers': + elif instance.privacy == 'followers': activity['to'] = [followers] activity['cc'] = mentions - if self.privacy == 'direct': + if instance.privacy == 'direct': activity['to'] = mentions activity['cc'] = [] diff --git a/bookwyrm/tests/activitypub/test_base_activity.py b/bookwyrm/tests/activitypub/test_base_activity.py index 88997c44..87420aa7 100644 --- a/bookwyrm/tests/activitypub/test_base_activity.py +++ b/bookwyrm/tests/activitypub/test_base_activity.py @@ -95,34 +95,67 @@ class BaseActivity(TestCase): self.assertEqual(result.remote_id, 'https://example.com/user/mouse') self.assertEqual(result.name, 'MOUSE?? MOUSE!!') - def test_to_model(self): - ''' the big boy of this module. it feels janky to test this with actual - models rather than a test model, but I don't know how to make a test - model so here we are. ''' + def test_to_model_invalid_model(self): + ''' catch mismatch between activity type and model type ''' instance = ActivityObject(id='a', type='b') with self.assertRaises(ActivitySerializerError): 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, '') - update_data = activitypub.Person(**self.user.to_activity()) - update_data.name = 'New Name' - update_data.to_model(models.User, self.user) + + activity = activitypub.Person( + 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') def test_to_model_foreign_key(self): ''' test setting one to one/foreign key ''' - update_data = activitypub.Person(**self.user.to_activity()) - update_data.publicKey['publicKeyPem'] = 'hi im secure' - update_data.to_model(models.User, self.user) + activity = activitypub.Person( + id=self.user.remote_id, + 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') @responses.activate def test_to_model_image(self): ''' update an image field ''' - update_data = activitypub.Person(**self.user.to_activity()) - update_data.icon = {'url': 'http://www.example.com/image.jpg'} + activity = activitypub.Person( + 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.GET, 'http://www.example.com/image.jpg', @@ -133,7 +166,7 @@ class BaseActivity(TestCase): with self.assertRaises(ValueError): 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.file) @@ -145,19 +178,26 @@ class BaseActivity(TestCase): ) book = models.Edition.objects.create( title='Test Edition', remote_id='http://book.com/book') - update_data = activitypub.Note(**status.to_activity()) - update_data.tag = [ - { - 'type': 'Mention', - 'name': 'gerald', - 'href': 'http://example.com/a/b' - }, - { - 'type': 'Edition', - 'name': 'gerald j. books', - 'href': 'http://book.com/book' - }, - ] + update_data = activitypub.Note( + id=status.remote_id, + content=status.content, + attributedTo=self.user.remote_id, + published='hi', + to=[], + cc=[], + tag=[ + { + 'type': 'Mention', + '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) self.assertEqual(status.mention_users.first(), self.user) self.assertEqual(status.mention_books.first(), book) @@ -171,12 +211,19 @@ class BaseActivity(TestCase): content='test status', user=self.user, ) - update_data = activitypub.Note(**status.to_activity()) - update_data.attachment = [{ - 'url': 'http://www.example.com/image.jpg', - 'name': 'alt text', - 'type': 'Image', - }] + update_data = activitypub.Note( + id=status.remote_id, + content=status.content, + attributedTo=self.user.remote_id, + published='hi', + to=[], + cc=[], + attachment=[{ + 'url': 'http://www.example.com/image.jpg', + 'name': 'alt text', + 'type': 'Image', + }], + ) responses.add( responses.GET,