From b4c60c059197acc08b6a179d795c5be173f8da46 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 21 Dec 2020 14:25:10 -0800 Subject: [PATCH] Catches exception thrown when boosting unknown statuses --- bookwyrm/activitypub/base_activity.py | 7 +++++-- bookwyrm/tests/test_incoming.py | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/bookwyrm/activitypub/base_activity.py b/bookwyrm/activitypub/base_activity.py index c344c120..b9facf2f 100644 --- a/bookwyrm/activitypub/base_activity.py +++ b/bookwyrm/activitypub/base_activity.py @@ -3,7 +3,7 @@ from dataclasses import dataclass, fields, MISSING from json import JSONEncoder from django.apps import apps -from django.db import transaction +from django.db import IntegrityError, transaction from bookwyrm.connectors import ConnectorException, get_data from bookwyrm.tasks import app @@ -92,7 +92,10 @@ class ActivityObject: with transaction.atomic(): # we can't set many to many and reverse fields on an unsaved object - instance.save() + try: + instance.save() + except IntegrityError as e: + raise ActivitySerializerError(e) # add many to many fields, which have to be set post-save for field in instance.many_to_many_fields: diff --git a/bookwyrm/tests/test_incoming.py b/bookwyrm/tests/test_incoming.py index 7e58da86..0269c64c 100644 --- a/bookwyrm/tests/test_incoming.py +++ b/bookwyrm/tests/test_incoming.py @@ -8,6 +8,7 @@ from django.http import HttpResponseBadRequest, HttpResponseNotAllowed, \ HttpResponseNotFound from django.test import TestCase from django.test.client import RequestFactory +import responses from bookwyrm import models, incoming @@ -421,6 +422,25 @@ class Incoming(TestCase): self.assertEqual(notification.related_status, self.status) + @responses.activate + def test_handle_discarded_boost(self): + ''' test a boost of a mastodon status that will be discarded ''' + activity = { + 'type': 'Announce', + 'id': 'http://www.faraway.com/boost/12', + 'actor': self.remote_user.remote_id, + 'object': self.status.to_activity(), + } + responses.add( + responses.GET, + 'http://www.faraway.com/boost/12', + json={'id': 'http://www.faraway.com/boost/12'}, + status=200) + incoming.handle_boost(activity) + self.assertEqual(models.Boost.objects.count(), 0) + + + def test_handle_unboost(self): ''' undo a boost ''' activity = {