bookwyrm/bookwyrm/tests/models/test_shelf_model.py

112 lines
4.8 KiB
Python
Raw Normal View History

2021-03-08 16:49:10 +00:00
""" testing models """
2021-04-09 03:58:15 +00:00
import json
from unittest.mock import patch
2020-12-14 18:25:43 +00:00
from django.test import TestCase
from bookwyrm import models, settings
2021-03-08 16:49:10 +00:00
# pylint: disable=unused-argument
2021-08-03 17:25:53 +00:00
@patch("bookwyrm.suggested_users.rerank_suggestions_task.delay")
2021-09-06 21:50:33 +00:00
@patch("bookwyrm.activitystreams.populate_stream_task.delay")
2021-12-09 20:42:07 +00:00
@patch("bookwyrm.lists_stream.populate_lists_task.delay")
2021-09-06 23:59:58 +00:00
@patch("bookwyrm.activitystreams.add_book_statuses_task.delay")
2021-09-07 01:39:14 +00:00
@patch("bookwyrm.activitystreams.remove_book_statuses_task.delay")
2020-12-14 18:25:43 +00:00
class Shelf(TestCase):
2021-04-26 16:15:42 +00:00
"""some activitypub oddness ahead"""
2021-03-08 16:49:10 +00:00
@classmethod
def setUpTestData(cls):
2021-04-26 16:15:42 +00:00
"""look, a shelf"""
with (
patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"),
patch("bookwyrm.activitystreams.populate_stream_task.delay"),
patch("bookwyrm.lists_stream.populate_lists_task.delay"),
):
cls.local_user = models.User.objects.create_user(
2021-08-03 17:25:53 +00:00
"mouse", "mouse@mouse.mouse", "mouseword", local=True, localname="mouse"
)
2021-08-02 23:05:40 +00:00
work = models.Work.objects.create(title="Test Work")
cls.book = models.Edition.objects.create(title="test book", parent_work=work)
2020-12-14 18:25:43 +00:00
2021-09-06 22:09:04 +00:00
def test_remote_id(self, *_):
2021-04-26 16:15:42 +00:00
"""shelves use custom remote ids"""
2021-11-12 17:17:00 +00:00
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"):
2021-04-09 03:58:15 +00:00
shelf = models.Shelf.objects.create(
name="Test Shelf", identifier="test-shelf", user=self.local_user
)
expected_id = f"{settings.BASE_URL}/user/mouse/books/test-shelf"
2021-02-07 05:00:08 +00:00
self.assertEqual(shelf.get_remote_id(), expected_id)
2020-12-14 18:25:43 +00:00
2021-09-06 22:09:04 +00:00
def test_to_activity(self, *_):
2021-04-26 16:15:42 +00:00
"""jsonify it"""
2021-11-12 17:17:00 +00:00
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"):
2021-04-09 03:58:15 +00:00
shelf = models.Shelf.objects.create(
name="Test Shelf", identifier="test-shelf", user=self.local_user
)
2021-02-07 05:00:08 +00:00
activity_json = shelf.to_activity()
2020-12-14 18:25:43 +00:00
self.assertIsInstance(activity_json, dict)
2021-03-08 16:49:10 +00:00
self.assertEqual(activity_json["id"], shelf.remote_id)
self.assertEqual(activity_json["totalItems"], 0)
self.assertEqual(activity_json["type"], "Shelf")
self.assertEqual(activity_json["name"], "Test Shelf")
self.assertEqual(activity_json["owner"], self.local_user.remote_id)
2021-02-07 05:00:08 +00:00
2021-09-06 22:09:04 +00:00
def test_create_update_shelf(self, *_):
2021-04-26 16:15:42 +00:00
"""create and broadcast shelf creation"""
2021-02-07 05:00:08 +00:00
2021-11-12 17:17:00 +00:00
with patch(
"bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"
) as mock:
2021-04-09 03:58:15 +00:00
shelf = models.Shelf.objects.create(
name="Test Shelf", identifier="test-shelf", user=self.local_user
)
2021-11-12 17:17:00 +00:00
activity = json.loads(mock.call_args[1]["args"][1])
2021-04-09 03:58:15 +00:00
self.assertEqual(activity["type"], "Create")
self.assertEqual(activity["actor"], self.local_user.remote_id)
self.assertEqual(activity["object"]["name"], "Test Shelf")
2021-02-07 05:00:08 +00:00
2021-03-08 16:49:10 +00:00
shelf.name = "arthur russel"
2021-11-12 17:17:00 +00:00
with patch(
"bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"
) as mock:
2021-04-09 03:58:15 +00:00
shelf.save()
2021-11-12 17:17:00 +00:00
activity = json.loads(mock.call_args[1]["args"][1])
2021-04-09 03:58:15 +00:00
self.assertEqual(activity["type"], "Update")
self.assertEqual(activity["actor"], self.local_user.remote_id)
self.assertEqual(activity["object"]["name"], "arthur russel")
2021-03-08 16:49:10 +00:00
self.assertEqual(shelf.name, "arthur russel")
2021-02-07 05:00:08 +00:00
2021-09-06 22:09:04 +00:00
def test_shelve(self, *_):
2021-04-26 16:15:42 +00:00
"""create and broadcast shelf creation"""
2021-11-12 17:17:00 +00:00
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"):
2021-04-09 03:58:15 +00:00
shelf = models.Shelf.objects.create(
name="Test Shelf", identifier="test-shelf", user=self.local_user
)
2021-11-12 17:17:00 +00:00
with patch(
"bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"
) as mock:
2021-04-09 03:58:15 +00:00
shelf_book = models.ShelfBook.objects.create(
shelf=shelf, user=self.local_user, book=self.book
)
self.assertEqual(mock.call_count, 1)
2021-11-12 17:17:00 +00:00
activity = json.loads(mock.call_args[1]["args"][1])
2021-04-09 03:58:15 +00:00
self.assertEqual(activity["type"], "Add")
self.assertEqual(activity["actor"], self.local_user.remote_id)
self.assertEqual(activity["object"]["id"], shelf_book.remote_id)
self.assertEqual(activity["target"], shelf.remote_id)
2021-02-07 05:00:08 +00:00
self.assertEqual(shelf.books.first(), self.book)
2021-11-12 17:17:00 +00:00
with patch(
"bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"
) as mock:
2021-04-09 03:58:15 +00:00
shelf_book.delete()
self.assertEqual(mock.call_count, 1)
2021-11-12 17:17:00 +00:00
activity = json.loads(mock.call_args[1]["args"][1])
2021-04-09 03:58:15 +00:00
self.assertEqual(activity["type"], "Remove")
self.assertEqual(activity["actor"], self.local_user.remote_id)
self.assertEqual(activity["object"]["id"], shelf_book.remote_id)
self.assertEqual(activity["target"], shelf.remote_id)
2021-02-07 05:00:08 +00:00
self.assertFalse(shelf.books.exists())