From a7375c4c139cff2866fe7e8df68dfa39cd7c2a11 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Thu, 8 Apr 2021 15:23:56 -0700 Subject: [PATCH] Adds remove list item test and updates add/create tests --- bookwyrm/tests/views/inbox/test_inbox_add.py | 9 +-- .../tests/views/inbox/test_inbox_create.py | 2 +- .../tests/views/inbox/test_inbox_remove.py | 62 +++++++++++++++---- 3 files changed, 57 insertions(+), 16 deletions(-) diff --git a/bookwyrm/tests/views/inbox/test_inbox_add.py b/bookwyrm/tests/views/inbox/test_inbox_add.py index 638d56d7..1f396caf 100644 --- a/bookwyrm/tests/views/inbox/test_inbox_add.py +++ b/bookwyrm/tests/views/inbox/test_inbox_add.py @@ -97,10 +97,9 @@ class InboxActivities(TestCase): "type": "Add", "actor": "https://example.com/users/rat", "object": { - "type": "Edition", - "title": "Test Title", - "work": work.remote_id, - "id": "https://bookwyrm.social/book/37292", + "type": "ListItem", + "book": self.edition.remote_id, + "id": "https://bookwyrm.social/listbook/6189", }, "target": "https://bookwyrm.social/user/mouse/list/to-read", "@context": "https://www.w3.org/ns/activitystreams", @@ -108,8 +107,10 @@ class InboxActivities(TestCase): views.inbox.activity_task(activity) booklist = models.List.objects.get() + listitem = models.ListItem.objects.get() self.assertEqual(booklist.name, "Test List") self.assertEqual(booklist.books.first(), book) + self.assertEqual(listitem.remote_id, "https://bookwyrm.social/listbook/6189") @responses.activate def test_handle_tag_book(self): diff --git a/bookwyrm/tests/views/inbox/test_inbox_create.py b/bookwyrm/tests/views/inbox/test_inbox_create.py index 437f6ffc..f8ed6a84 100644 --- a/bookwyrm/tests/views/inbox/test_inbox_create.py +++ b/bookwyrm/tests/views/inbox/test_inbox_create.py @@ -9,7 +9,7 @@ from bookwyrm import models, views # pylint: disable=too-many-public-methods -class InboxActivities(TestCase): +class InboxCreate(TestCase): """ readthrough tests """ def setUp(self): diff --git a/bookwyrm/tests/views/inbox/test_inbox_remove.py b/bookwyrm/tests/views/inbox/test_inbox_remove.py index b3e42bbd..778f5077 100644 --- a/bookwyrm/tests/views/inbox/test_inbox_remove.py +++ b/bookwyrm/tests/views/inbox/test_inbox_remove.py @@ -7,11 +7,20 @@ from bookwyrm import models, views # pylint: disable=too-many-public-methods -class InboxActivities(TestCase): +class InboxRemove(TestCase): """ inbox tests """ def setUp(self): """ basic user and book data """ + self.local_user = models.User.objects.create_user( + "mouse@example.com", + "mouse@mouse.com", + "mouseword", + local=True, + localname="mouse", + ) + self.local_user.remote_id = "https://example.com/user/mouse" + self.local_user.save(broadcast=False) with patch("bookwyrm.models.user.set_remote_server.delay"): self.remote_user = models.User.objects.create_user( "rat", @@ -22,26 +31,26 @@ class InboxActivities(TestCase): inbox="https://example.com/users/rat/inbox", outbox="https://example.com/users/rat/outbox", ) + self.work = models.Work.objects.create(title="work title") + self.book = models.Edition.objects.create( + title="Test", + remote_id="https://bookwyrm.social/book/37292", + parent_work=self.work, + ) models.SiteSettings.objects.create() def test_handle_unshelve_book(self): """ remove a book from a shelf """ - work = models.Work.objects.create(title="work title") - book = models.Edition.objects.create( - title="Test", - remote_id="https://bookwyrm.social/book/37292", - parent_work=work, - ) shelf = models.Shelf.objects.create(user=self.remote_user, name="Test Shelf") shelf.remote_id = "https://bookwyrm.social/user/mouse/shelf/to-read" shelf.save() shelfbook = models.ShelfBook.objects.create( - user=self.remote_user, shelf=shelf, book=book + user=self.remote_user, shelf=shelf, book=self.book ) - self.assertEqual(shelf.books.first(), book) + self.assertEqual(shelf.books.first(), self.book) self.assertEqual(shelf.books.count(), 1) activity = { @@ -51,11 +60,42 @@ class InboxActivities(TestCase): "object": { "type": "Edition", "title": "Test Title", - "work": work.remote_id, - "id": "https://bookwyrm.social/book/37292", + "work": self.work.remote_id, + "id": "https//bookwyrm.social/book/37292", }, "target": "https://bookwyrm.social/user/mouse/shelf/to-read", "@context": "https://www.w3.org/ns/activitystreams", } views.inbox.activity_task(activity) self.assertFalse(shelf.books.exists()) + + def test_handle_remove_book_from_list(self): + """ listing a book """ + with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"): + booklist = models.List.objects.create( + name="test list", + user=self.local_user, + ) + listitem = models.ListItem.objects.create( + user=self.local_user, + book=self.book, + book_list=booklist, + ) + self.assertEqual(booklist.books.count(), 1) + + activity = { + "id": listitem.remote_id, + "type": "Remove", + "actor": "https://example.com/users/rat", + "object": { + "type": "Edition", + "title": "Test Title", + "work": self.work.remote_id, + "id": "https://bookwyrm.social/book/37292", + }, + "target": booklist.remote_id, + "@context": "https://www.w3.org/ns/activitystreams", + } + views.inbox.activity_task(activity) + + self.assertEqual(booklist.books.count(), 0)