Fixes base activity tests

This commit is contained in:
Mouse Reeve 2021-02-16 20:24:37 -08:00
parent a9ca3a4290
commit 77781d57c3
2 changed files with 6 additions and 40 deletions

View file

@ -107,7 +107,10 @@ class ActivityObject:
instance = instance or model() instance = instance or model()
for field in instance.simple_fields: for field in instance.simple_fields:
field.set_field_from_activity(instance, self) try:
field.set_field_from_activity(instance, self)
except AttributeError as e:
raise ActivitySerializerError(e)
# image fields have to be set after other fields because they can save # image fields have to be set after other fields because they can save
# too early and jank up users # too early and jank up users

View file

@ -102,44 +102,6 @@ class BaseActivity(TestCase):
with self.assertRaises(ActivitySerializerError): with self.assertRaises(ActivitySerializerError):
instance.to_model(model=models.User) instance.to_model(model=models.User)
def test_to_model_simple_fields(self):
''' test setting simple fields '''
self.assertIsNone(self.user.name)
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(model=models.User, instance=self.user)
self.assertEqual(self.user.name, 'New Name')
def test_to_model_foreign_key(self):
''' test setting one to one/foreign key '''
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(model=models.User, instance=self.user)
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):
@ -176,8 +138,9 @@ class BaseActivity(TestCase):
# this would trigger a broadcast because it's a local user # 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) activity.to_model(model=models.User, instance=self.user)
self.assertIsNotNone(self.user.avatar.name)
self.assertIsNotNone(self.user.avatar.file) self.assertIsNotNone(self.user.avatar.file)
self.assertEqual(self.user.name, 'New Name')
self.assertEqual(self.user.key_pair.public_key, 'hi')
def test_to_model_many_to_many(self): def test_to_model_many_to_many(self):
''' annoying that these all need special handling ''' ''' annoying that these all need special handling '''