Fixes error on deletion requests for unknown users

This commit is contained in:
Mouse Reeve 2021-04-17 12:39:16 -07:00
parent 53e11051d4
commit 5df2ac676b
2 changed files with 52 additions and 9 deletions

View file

@ -16,11 +16,10 @@ class Verb(ActivityObject):
def action(self):
""" usually we just want to update and save """
obj = self.object
# it may return None if the object is invalid in an expected way
# self.object may return None if the object is invalid in an expected way
# ie, Question type
if obj:
obj.to_model()
if self.object:
self.object.to_model()
@dataclass(init=False)
@ -43,7 +42,16 @@ class Delete(Verb):
def action(self):
""" find and delete the activity object """
obj = self.object.to_model(save=False, allow_create=False)
if not self.object:
return
if isinstance(self.object, str):
# Deleted users are passed as strings. Not wild about this fix
model = apps.get_model("bookwyrm.User")
obj = model.find_existing_by_remote_id(self.object)
else:
obj = self.object.to_model(save=False, allow_create=False)
if obj:
obj.delete()
# if we can't find it, we don't need to delete it because we don't have it
@ -58,8 +66,7 @@ class Update(Verb):
def action(self):
""" update a model instance from the dataclass """
obj = self.object
if obj:
if self.object:
self.object.to_model(allow_create=False)

View file

@ -49,7 +49,7 @@ class InboxActivities(TestCase):
}
models.SiteSettings.objects.create()
def test_handle_delete_status(self):
def test_delete_status(self):
""" remove a status """
self.assertFalse(self.status.deleted)
activity = {
@ -70,7 +70,7 @@ class InboxActivities(TestCase):
self.assertTrue(status.deleted)
self.assertIsInstance(status.deleted_date, datetime)
def test_handle_delete_status_notifications(self):
def test_delete_status_notifications(self):
""" remove a status with related notifications """
models.Notification.objects.create(
related_status=self.status,
@ -104,3 +104,39 @@ class InboxActivities(TestCase):
# notifications should be truly deleted
self.assertEqual(models.Notification.objects.count(), 1)
self.assertEqual(models.Notification.objects.get(), notif)
def test_delete_user(self):
""" delete a user """
self.assertTrue(
models.User.objects.get(username="rat@example.com").is_active
)
activity = {
'@context': 'https://www.w3.org/ns/activitystreams',
'id': 'https://example.com/users/test-user#delete',
'type': 'Delete',
'actor': 'https://example.com/users/test-user',
'to': ['https://www.w3.org/ns/activitystreams#Public'],
'object': 'https://example.com/users/test-user',
}
views.inbox.activity_task(activity)
self.assertFalse(
models.User.objects.get(username="rat@example.com").is_active
)
def test_delete_user_unknown(self):
""" don't worry about it if we don't know the user """
self.assertEqual(models.User.objects.filter(is_active=True).count(), 2)
activity = {
'@context': 'https://www.w3.org/ns/activitystreams',
'id': 'https://example.com/users/test-user#delete',
'type': 'Delete',
'actor': 'https://example.com/users/test-user',
'to': ['https://www.w3.org/ns/activitystreams#Public'],
'object': 'https://example.com/users/test-user',
}
# nothing happens.
views.inbox.activity_task(activity)
self.assertEqual(models.User.objects.filter(is_active=True).count(), 2)