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-04-26 16:15:42 +00:00
|
|
|
"""some activitypub oddness ahead"""
|
2021-03-08 16:49:10 +00:00
|
|
|
|
2021-01-31 05:03:50 +00:00
|
|
|
def setUp(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""look, a list"""
|
2021-04-08 21:53:45 +00:00
|
|
|
self.local_user = models.User.objects.create_user(
|
2021-03-08 16:49:10 +00:00
|
|
|
"mouse", "mouse@mouse.mouse", "mouseword", local=True, localname="mouse"
|
|
|
|
)
|
2021-04-08 21:53:45 +00:00
|
|
|
work = models.Work.objects.create(title="hello")
|
|
|
|
self.book = models.Edition.objects.create(title="hi", parent_work=work)
|
2021-01-31 05:03:50 +00:00
|
|
|
|
2021-02-07 00:13:59 +00:00
|
|
|
def test_remote_id(self, _):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""shelves use custom remote ids"""
|
2021-04-08 21:53:45 +00:00
|
|
|
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
|
|
|
|
book_list = models.List.objects.create(
|
|
|
|
name="Test List", user=self.local_user
|
|
|
|
)
|
|
|
|
expected_id = "https://%s/list/%d" % (settings.DOMAIN, book_list.id)
|
|
|
|
self.assertEqual(book_list.get_remote_id(), expected_id)
|
2021-01-31 05:03:50 +00:00
|
|
|
|
2021-02-07 00:13:59 +00:00
|
|
|
def test_to_activity(self, _):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""jsonify it"""
|
2021-04-08 21:53:45 +00:00
|
|
|
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
|
|
|
|
book_list = models.List.objects.create(
|
|
|
|
name="Test List", user=self.local_user
|
|
|
|
)
|
|
|
|
activity_json = book_list.to_activity()
|
2021-01-31 05:03:50 +00:00
|
|
|
self.assertIsInstance(activity_json, dict)
|
2021-04-08 21:53:45 +00:00
|
|
|
self.assertEqual(activity_json["id"], book_list.remote_id)
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertEqual(activity_json["totalItems"], 0)
|
|
|
|
self.assertEqual(activity_json["type"], "BookList")
|
|
|
|
self.assertEqual(activity_json["name"], "Test List")
|
2021-04-08 21:53:45 +00:00
|
|
|
self.assertEqual(activity_json["owner"], self.local_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-04-26 16:15:42 +00:00
|
|
|
"""a list entry"""
|
2021-04-08 21:53:45 +00:00
|
|
|
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
|
|
|
|
book_list = models.List.objects.create(
|
|
|
|
name="Test List", user=self.local_user, privacy="unlisted"
|
|
|
|
)
|
|
|
|
|
2021-01-31 21:31:49 +00:00
|
|
|
item = models.ListItem.objects.create(
|
2021-04-08 21:53:45 +00:00
|
|
|
book_list=book_list,
|
|
|
|
book=self.book,
|
|
|
|
user=self.local_user,
|
2021-04-08 16:05:21 +00:00
|
|
|
order=1,
|
2021-01-31 21:31:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
self.assertTrue(item.approved)
|
2021-04-08 21:53:45 +00:00
|
|
|
self.assertEqual(item.privacy, "unlisted")
|
2021-04-15 23:09:59 +00:00
|
|
|
self.assertEqual(item.recipients, [])
|
2021-04-08 21:53:45 +00:00
|
|
|
|
|
|
|
def test_list_item_pending(self, _):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""a list entry"""
|
2021-04-08 21:53:45 +00:00
|
|
|
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
|
|
|
|
book_list = models.List.objects.create(
|
|
|
|
name="Test List", user=self.local_user
|
|
|
|
)
|
2021-01-31 21:31:49 +00:00
|
|
|
|
2021-04-08 21:53:45 +00:00
|
|
|
item = models.ListItem.objects.create(
|
2021-04-08 16:05:21 +00:00
|
|
|
book_list=book_list,
|
|
|
|
book=self.book,
|
|
|
|
user=self.local_user,
|
|
|
|
approved=False,
|
|
|
|
order=1,
|
2021-04-08 21:53:45 +00:00
|
|
|
)
|
2021-01-31 21:31:49 +00:00
|
|
|
|
2021-04-08 21:53:45 +00:00
|
|
|
self.assertFalse(item.approved)
|
|
|
|
self.assertEqual(item.book_list.privacy, "public")
|
|
|
|
self.assertEqual(item.privacy, "direct")
|
2021-04-15 23:09:59 +00:00
|
|
|
self.assertEqual(item.recipients, [])
|