def test_add_book_self_curated(self): """put a book on a list automatically approved""" self.list.curation = "curated" self.list.save(broadcast=False) request = self.factory.post( "", { "book": self.book.id, "book_list": self.list.id, "user": self.local_user.id, }, ) request.user = self.local_user with patch( "bookwyrm.models.activitypub_mixin.broadcast_task.apply_async" ) as mock: views.add_book(request) self.assertEqual(mock.call_count, 1) activity = json.loads(mock.call_args[1]["args"][1]) self.assertEqual(activity["type"], "Add") self.assertEqual(activity["actor"], self.local_user.remote_id) self.assertEqual(activity["target"], self.list.remote_id) item = self.list.listitem_set.get() self.assertEqual(item.book, self.book) self.assertEqual(item.user, self.local_user) self.assertTrue(item.approved)
def test_add_two_books(self): """ Putting two books on the list. The first should have an order value of 1 and the second should have an order value of 2. """ request_one = self.factory.post( "", { "book": self.book.id, "book_list": self.list.id, "user": self.local_user.id, }, ) request_one.user = self.local_user request_two = self.factory.post( "", { "book": self.book_two.id, "book_list": self.list.id, "user": self.local_user.id, }, ) request_two.user = self.local_user with patch( "bookwyrm.models.activitypub_mixin.broadcast_task.apply_async" ): views.add_book(request_one) views.add_book(request_two) items = self.list.listitem_set.order_by("order").all() self.assertEqual(items[0].book, self.book) self.assertEqual(items[1].book, self.book_two) self.assertEqual(items[0].order, 1) self.assertEqual(items[1].order, 2)
def test_add_book_permission_denied(self): """you can't add to that list""" self.list.curation = "closed" self.list.save(broadcast=False) request = self.factory.post( "", { "book": self.book.id, "book_list": self.list.id, "user": self.rat.id, }, ) request.user = self.rat with self.assertRaises(PermissionDenied): views.add_book(request)
def test_adding_book_with_a_pending_book(self): """ When a list contains any pending books, the pending books should have be at the end of the list by order. If a book is added while a book is pending, its order should precede the pending books. """ request = self.factory.post( "", { "book": self.book_three.id, "book_list": self.list.id, "user": self.local_user.id, }, ) request.user = self.local_user with patch( "bookwyrm.models.activitypub_mixin.broadcast_task.apply_async" ): models.ListItem.objects.create( book_list=self.list, user=self.local_user, book=self.book, approved=True, order=1, ) models.ListItem.objects.create( book_list=self.list, user=self.rat, book=self.book_two, approved=False, order=2, ) views.add_book(request) items = self.list.listitem_set.order_by("order").all() self.assertEqual(items[0].book, self.book) self.assertEqual(items[0].order, 1) self.assertTrue(items[0].approved) self.assertEqual(items[1].book, self.book_three) self.assertEqual(items[1].order, 2) self.assertTrue(items[1].approved) self.assertEqual(items[2].book, self.book_two) self.assertEqual(items[2].order, 3) self.assertFalse(items[2].approved)
def test_add_three_books_and_move_last_to_first(self): """ Put three books on the list and move the last book to the first position. """ request_one = self.factory.post( "", { "book": self.book.id, "book_list": self.list.id, "user": self.local_user.id, }, ) request_one.user = self.local_user request_two = self.factory.post( "", { "book": self.book_two.id, "book_list": self.list.id, "user": self.local_user.id, }, ) request_two.user = self.local_user request_three = self.factory.post( "", { "book": self.book_three.id, "book_list": self.list.id, "user": self.local_user.id, }, ) request_three.user = self.local_user with patch( "bookwyrm.models.activitypub_mixin.broadcast_task.apply_async" ): views.add_book(request_one) views.add_book(request_two) views.add_book(request_three) items = self.list.listitem_set.order_by("order").all() self.assertEqual(items[0].book, self.book) self.assertEqual(items[1].book, self.book_two) self.assertEqual(items[2].book, self.book_three) self.assertEqual(items[0].order, 1) self.assertEqual(items[1].order, 2) self.assertEqual(items[2].order, 3) set_position_request = self.factory.post("", {"position": 1}) set_position_request.user = self.local_user with patch( "bookwyrm.models.activitypub_mixin.broadcast_task.apply_async" ): views.set_book_position(set_position_request, items[2].id) items = self.list.listitem_set.order_by("order").all() self.assertEqual(items[0].book, self.book_three) self.assertEqual(items[1].book, self.book) self.assertEqual(items[2].book, self.book_two) self.assertEqual(items[0].order, 1) self.assertEqual(items[1].order, 2) self.assertEqual(items[2].order, 3)
def test_add_three_books_and_remove_second(self): """ Put three books on a list and then remove the one in the middle. The ordering of the list should adjust to not have a gap. """ request_one = self.factory.post( "", { "book": self.book.id, "book_list": self.list.id, "user": self.local_user.id, }, ) request_one.user = self.local_user request_two = self.factory.post( "", { "book": self.book_two.id, "book_list": self.list.id, "user": self.local_user.id, }, ) request_two.user = self.local_user request_three = self.factory.post( "", { "book": self.book_three.id, "book_list": self.list.id, "user": self.local_user.id, }, ) request_three.user = self.local_user with patch( "bookwyrm.models.activitypub_mixin.broadcast_task.apply_async" ): views.add_book(request_one) views.add_book(request_two) views.add_book(request_three) items = self.list.listitem_set.order_by("order").all() self.assertEqual(items[0].book, self.book) self.assertEqual(items[1].book, self.book_two) self.assertEqual(items[2].book, self.book_three) self.assertEqual(items[0].order, 1) self.assertEqual(items[1].order, 2) self.assertEqual(items[2].order, 3) remove_request = self.factory.post("", {"item": items[1].id}) remove_request.user = self.local_user with patch( "bookwyrm.models.activitypub_mixin.broadcast_task.apply_async" ): views.remove_book(remove_request, self.list.id) items = self.list.listitem_set.order_by("order").all() self.assertEqual(items[0].book, self.book) self.assertEqual(items[1].book, self.book_three) self.assertEqual(items[0].order, 1) self.assertEqual(items[1].order, 2)