moviewyrm/bookwyrm/tests/models/test_list.py

56 lines
2.2 KiB
Python
Raw Normal View History

2021-03-08 16:49:10 +00:00
""" testing models """
2021-02-07 00:13:59 +00:00
from unittest.mock import patch
2021-01-31 05:03:50 +00:00
from django.test import TestCase
from bookwyrm import models, settings
2021-03-08 16:49:10 +00:00
@patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay")
2021-01-31 05:03:50 +00:00
class List(TestCase):
2021-03-08 16:49:10 +00:00
""" some activitypub oddness ahead """
2021-01-31 05:03:50 +00:00
def setUp(self):
2021-03-08 16:49:10 +00:00
""" look, a list """
2021-01-31 05:03:50 +00:00
self.user = models.User.objects.create_user(
2021-03-08 16:49:10 +00:00
"mouse", "mouse@mouse.mouse", "mouseword", local=True, localname="mouse"
)
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
self.list = models.List.objects.create(name="Test List", user=self.user)
2021-01-31 05:03:50 +00:00
2021-02-07 00:13:59 +00:00
def test_remote_id(self, _):
2021-03-08 16:49:10 +00:00
""" shelves use custom remote ids """
expected_id = "https://%s/list/%d" % (settings.DOMAIN, self.list.id)
2021-01-31 05:03:50 +00:00
self.assertEqual(self.list.get_remote_id(), expected_id)
2021-02-07 00:13:59 +00:00
def test_to_activity(self, _):
2021-03-08 16:49:10 +00:00
""" jsonify it """
2021-01-31 05:03:50 +00:00
activity_json = self.list.to_activity()
self.assertIsInstance(activity_json, dict)
2021-03-08 16:49:10 +00:00
self.assertEqual(activity_json["id"], self.list.remote_id)
self.assertEqual(activity_json["totalItems"], 0)
self.assertEqual(activity_json["type"], "BookList")
self.assertEqual(activity_json["name"], "Test List")
self.assertEqual(activity_json["owner"], self.user.remote_id)
2021-01-31 21:31:49 +00:00
2021-02-07 00:13:59 +00:00
def test_list_item(self, _):
2021-03-08 16:49:10 +00:00
""" a list entry """
work = models.Work.objects.create(title="hello")
book = models.Edition.objects.create(title="hi", parent_work=work)
2021-01-31 21:31:49 +00:00
item = models.ListItem.objects.create(
book_list=self.list,
book=book,
user=self.user,
2021-01-31 21:31:49 +00:00
)
self.assertTrue(item.approved)
2021-02-07 00:13:59 +00:00
add_activity = item.to_add_activity()
2021-03-08 16:49:10 +00:00
self.assertEqual(add_activity["actor"], self.user.remote_id)
self.assertEqual(add_activity["object"]["id"], book.remote_id)
self.assertEqual(add_activity["target"], self.list.remote_id)
2021-01-31 21:31:49 +00:00
2021-02-07 00:13:59 +00:00
remove_activity = item.to_remove_activity()
2021-03-08 16:49:10 +00:00
self.assertEqual(remove_activity["actor"], self.user.remote_id)
self.assertEqual(remove_activity["object"]["id"], book.remote_id)
self.assertEqual(remove_activity["target"], self.list.remote_id)