bookwyrm-mastodon/bookwyrm/tests/models/test_shelf_model.py

109 lines
4.8 KiB
Python
Raw Normal View History

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