From 20cca69f064e8b4f96936c12033122441f17ba97 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Fri, 15 Oct 2021 08:51:59 -0700 Subject: [PATCH] Test update status via inbox --- bookwyrm/activitypub/verbs.py | 5 +- .../tests/views/inbox/test_inbox_update.py | 58 ++++++++++++------- 2 files changed, 39 insertions(+), 24 deletions(-) diff --git a/bookwyrm/activitypub/verbs.py b/bookwyrm/activitypub/verbs.py index 50a479b7..b32b0413 100644 --- a/bookwyrm/activitypub/verbs.py +++ b/bookwyrm/activitypub/verbs.py @@ -69,8 +69,9 @@ class Update(Verb): def action(self): """update a model instance from the dataclass""" - if self.object: - self.object.to_model(allow_create=False) + if not self.object: + return + self.object.to_model(allow_create=False) @dataclass(init=False) diff --git a/bookwyrm/tests/views/inbox/test_inbox_update.py b/bookwyrm/tests/views/inbox/test_inbox_update.py index ec10f2f8..2345495d 100644 --- a/bookwyrm/tests/views/inbox/test_inbox_update.py +++ b/bookwyrm/tests/views/inbox/test_inbox_update.py @@ -37,9 +37,9 @@ class InboxUpdate(TestCase): outbox="https://example.com/users/rat/outbox", ) - self.create_json = { + self.update_json = { "id": "hi", - "type": "Create", + "type": "Update", "actor": "hi", "to": ["https://www.w3.org/ns/activitystreams#public"], "cc": ["https://example.com/user/mouse/followers"], @@ -54,26 +54,20 @@ class InboxUpdate(TestCase): book_list = models.List.objects.create( name="hi", remote_id="https://example.com/list/22", user=self.local_user ) - activity = { - "type": "Update", - "to": [], - "cc": [], - "actor": "hi", - "id": "sdkjf", - "object": { - "id": "https://example.com/list/22", - "type": "BookList", - "totalItems": 1, - "first": "https://example.com/list/22?page=1", - "last": "https://example.com/list/22?page=1", - "name": "Test List", - "owner": "https://example.com/user/mouse", - "to": ["https://www.w3.org/ns/activitystreams#Public"], - "cc": ["https://example.com/user/mouse/followers"], - "summary": "summary text", - "curation": "curated", - "@context": "https://www.w3.org/ns/activitystreams", - }, + activity = self.update_json + activity["object"] = { + "id": "https://example.com/list/22", + "type": "BookList", + "totalItems": 1, + "first": "https://example.com/list/22?page=1", + "last": "https://example.com/list/22?page=1", + "name": "Test List", + "owner": "https://example.com/user/mouse", + "to": ["https://www.w3.org/ns/activitystreams#Public"], + "cc": ["https://example.com/user/mouse/followers"], + "summary": "summary text", + "curation": "curated", + "@context": "https://www.w3.org/ns/activitystreams", } views.inbox.activity_task(activity) book_list.refresh_from_db() @@ -176,3 +170,23 @@ class InboxUpdate(TestCase): ) book = models.Work.objects.get(id=book.id) self.assertEqual(book.title, "Piranesi") + + @patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay") + @patch("bookwyrm.activitystreams.add_status_task.delay") + def test_update_status(self, *_): + """edit a status""" + status = models.Status.objects.create(user=self.remote_user, content="hi") + + datafile = pathlib.Path(__file__).parent.joinpath("../../data/ap_note.json") + status_data = json.loads(datafile.read_bytes()) + status_data["id"] = status.remote_id + + activity = self.update_json + activity["object"] = status_data + + with patch("bookwyrm.activitypub.base_activity.set_related_field.delay"): + views.inbox.activity_task(activity) + + status.refresh_from_db() + self.assertEqual(status.content, "test content in note") + self.assertTrue(status.edited)