2021-03-08 16:49:10 +00:00
|
|
|
""" testing models """
|
2021-02-09 20:48:46 +00:00
|
|
|
from unittest.mock import patch
|
2020-12-18 19:00:30 +00:00
|
|
|
from io import BytesIO
|
|
|
|
import pathlib
|
|
|
|
|
|
|
|
from PIL import Image
|
|
|
|
from django.core.files.base import ContentFile
|
2020-12-18 19:34:21 +00:00
|
|
|
from django.db import IntegrityError
|
2020-05-10 00:10:02 +00:00
|
|
|
from django.test import TestCase
|
2020-12-18 19:00:30 +00:00
|
|
|
from django.utils import timezone
|
2021-03-13 16:13:20 +00:00
|
|
|
import responses
|
2020-05-10 00:10:02 +00:00
|
|
|
|
2021-03-13 16:13:20 +00:00
|
|
|
from bookwyrm import activitypub, models, settings
|
2020-05-10 00:10:02 +00:00
|
|
|
|
|
|
|
|
2021-03-13 15:38:19 +00:00
|
|
|
# pylint: disable=too-many-public-methods
|
2021-10-01 21:41:30 +00:00
|
|
|
# pylint: disable=line-too-long
|
2021-03-08 16:49:10 +00:00
|
|
|
@patch("bookwyrm.models.Status.broadcast")
|
2021-09-06 20:53:49 +00:00
|
|
|
@patch("bookwyrm.activitystreams.add_status_task.delay")
|
2021-09-06 23:59:58 +00:00
|
|
|
@patch("bookwyrm.activitystreams.remove_status_task.delay")
|
2020-05-10 00:10:02 +00:00
|
|
|
class Status(TestCase):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""lotta types of statuses"""
|
2021-03-08 16:49:10 +00:00
|
|
|
|
2020-05-10 00:10:02 +00:00
|
|
|
def setUp(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""useful things for creating a status"""
|
2021-09-06 21:48:45 +00:00
|
|
|
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
|
|
|
|
"bookwyrm.activitystreams.populate_stream_task.delay"
|
|
|
|
):
|
2021-08-03 17:25:53 +00:00
|
|
|
self.local_user = models.User.objects.create_user(
|
|
|
|
"mouse", "mouse@mouse.mouse", "mouseword", local=True, localname="mouse"
|
|
|
|
)
|
2021-08-02 23:05:40 +00:00
|
|
|
with patch("bookwyrm.models.user.set_remote_server.delay"):
|
|
|
|
self.remote_user = models.User.objects.create_user(
|
|
|
|
"rat",
|
|
|
|
"rat@rat.com",
|
|
|
|
"ratword",
|
|
|
|
local=False,
|
|
|
|
remote_id="https://example.com/users/rat",
|
|
|
|
inbox="https://example.com/users/rat/inbox",
|
|
|
|
outbox="https://example.com/users/rat/outbox",
|
2021-05-26 21:57:29 +00:00
|
|
|
)
|
2021-08-02 23:05:40 +00:00
|
|
|
self.book = models.Edition.objects.create(title="Test Edition")
|
|
|
|
|
|
|
|
image_file = pathlib.Path(__file__).parent.joinpath(
|
|
|
|
"../../static/images/default_avi.jpg"
|
|
|
|
)
|
|
|
|
image = Image.open(image_file)
|
|
|
|
output = BytesIO()
|
|
|
|
with patch("bookwyrm.models.Status.broadcast"):
|
|
|
|
image.save(output, format=image.format)
|
|
|
|
self.book.cover.save("test.jpg", ContentFile(output.getvalue()))
|
2020-05-10 00:10:02 +00:00
|
|
|
|
2021-03-23 17:41:18 +00:00
|
|
|
def test_status_generated_fields(self, *_):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""setting remote id"""
|
2021-03-13 15:38:19 +00:00
|
|
|
status = models.Status.objects.create(content="bleh", user=self.local_user)
|
2021-10-01 21:24:11 +00:00
|
|
|
expected_id = f"https://{settings.DOMAIN}/user/mouse/status/{status.id}"
|
2020-05-13 01:56:28 +00:00
|
|
|
self.assertEqual(status.remote_id, expected_id)
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertEqual(status.privacy, "public")
|
2020-12-18 19:00:30 +00:00
|
|
|
|
2021-03-23 17:41:18 +00:00
|
|
|
def test_replies(self, *_):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""get a list of replies"""
|
2021-10-01 21:41:30 +00:00
|
|
|
parent = models.Status(content="hi", user=self.local_user)
|
|
|
|
parent.save(broadcast=False)
|
|
|
|
child = models.Status(
|
2021-03-13 15:38:19 +00:00
|
|
|
content="hello", reply_parent=parent, user=self.local_user
|
2021-03-08 16:49:10 +00:00
|
|
|
)
|
2021-10-01 21:41:30 +00:00
|
|
|
child.save(broadcast=False)
|
|
|
|
sibling = models.Review(
|
2021-08-02 23:05:40 +00:00
|
|
|
content="hey", reply_parent=parent, user=self.local_user, book=self.book
|
|
|
|
)
|
2021-10-01 21:41:30 +00:00
|
|
|
sibling.save(broadcast=False)
|
|
|
|
grandchild = models.Status(
|
2021-03-13 15:38:19 +00:00
|
|
|
content="hi hello", reply_parent=child, user=self.local_user
|
2021-03-08 16:49:10 +00:00
|
|
|
)
|
2021-10-01 21:41:30 +00:00
|
|
|
grandchild.save(broadcast=False)
|
2020-12-18 19:00:30 +00:00
|
|
|
|
|
|
|
replies = models.Status.replies(parent)
|
|
|
|
self.assertEqual(replies.count(), 2)
|
|
|
|
self.assertEqual(replies.first(), child)
|
|
|
|
# should select subclasses
|
|
|
|
self.assertIsInstance(replies.last(), models.Review)
|
|
|
|
|
2021-10-01 21:41:30 +00:00
|
|
|
self.assertEqual(parent.thread_id, parent.id)
|
|
|
|
self.assertEqual(child.thread_id, parent.id)
|
|
|
|
self.assertEqual(sibling.thread_id, parent.id)
|
|
|
|
self.assertEqual(grandchild.thread_id, parent.id)
|
|
|
|
|
2021-03-23 17:41:18 +00:00
|
|
|
def test_status_type(self, *_):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""class name"""
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertEqual(models.Status().status_type, "Note")
|
|
|
|
self.assertEqual(models.Review().status_type, "Review")
|
|
|
|
self.assertEqual(models.Quotation().status_type, "Quotation")
|
|
|
|
self.assertEqual(models.Comment().status_type, "Comment")
|
|
|
|
self.assertEqual(models.Boost().status_type, "Announce")
|
2020-12-18 19:00:30 +00:00
|
|
|
|
2021-03-23 17:41:18 +00:00
|
|
|
def test_boostable(self, *_):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""can a status be boosted, based on privacy"""
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertTrue(models.Status(privacy="public").boostable)
|
|
|
|
self.assertTrue(models.Status(privacy="unlisted").boostable)
|
|
|
|
self.assertFalse(models.Status(privacy="followers").boostable)
|
|
|
|
self.assertFalse(models.Status(privacy="direct").boostable)
|
2020-12-18 19:00:30 +00:00
|
|
|
|
2021-03-23 17:41:18 +00:00
|
|
|
def test_to_replies(self, *_):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""activitypub replies collection"""
|
2021-03-13 15:38:19 +00:00
|
|
|
parent = models.Status.objects.create(content="hi", user=self.local_user)
|
2020-12-18 19:00:30 +00:00
|
|
|
child = models.Status.objects.create(
|
2021-03-13 15:38:19 +00:00
|
|
|
content="hello", reply_parent=parent, user=self.local_user
|
2021-03-08 16:49:10 +00:00
|
|
|
)
|
2021-08-02 23:05:40 +00:00
|
|
|
models.Review.objects.create(
|
|
|
|
content="hey", reply_parent=parent, user=self.local_user, book=self.book
|
|
|
|
)
|
2020-12-18 19:00:30 +00:00
|
|
|
models.Status.objects.create(
|
2021-03-13 15:38:19 +00:00
|
|
|
content="hi hello", reply_parent=child, user=self.local_user
|
2021-03-08 16:49:10 +00:00
|
|
|
)
|
2020-12-18 19:00:30 +00:00
|
|
|
|
|
|
|
replies = parent.to_replies()
|
2021-10-01 21:24:11 +00:00
|
|
|
self.assertEqual(replies["id"], f"{parent.remote_id}/replies")
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertEqual(replies["totalItems"], 2)
|
2020-12-18 19:00:30 +00:00
|
|
|
|
2021-03-23 17:41:18 +00:00
|
|
|
def test_status_to_activity(self, *_):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""subclass of the base model version with a "pure" serializer"""
|
2021-03-13 15:38:19 +00:00
|
|
|
status = models.Status.objects.create(
|
|
|
|
content="test content", user=self.local_user
|
|
|
|
)
|
2020-12-18 19:00:30 +00:00
|
|
|
activity = status.to_activity()
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertEqual(activity["id"], status.remote_id)
|
|
|
|
self.assertEqual(activity["type"], "Note")
|
|
|
|
self.assertEqual(activity["content"], "test content")
|
|
|
|
self.assertEqual(activity["sensitive"], False)
|
2020-12-18 19:00:30 +00:00
|
|
|
|
2021-03-23 17:41:18 +00:00
|
|
|
def test_status_to_activity_tombstone(self, *_):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""subclass of the base model version with a "pure" serializer"""
|
2021-09-06 23:59:58 +00:00
|
|
|
status = models.Status.objects.create(
|
|
|
|
content="test content",
|
|
|
|
user=self.local_user,
|
|
|
|
deleted=True,
|
|
|
|
deleted_date=timezone.now(),
|
|
|
|
)
|
2020-12-18 19:00:30 +00:00
|
|
|
activity = status.to_activity()
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertEqual(activity["id"], status.remote_id)
|
|
|
|
self.assertEqual(activity["type"], "Tombstone")
|
|
|
|
self.assertFalse(hasattr(activity, "content"))
|
2020-12-18 19:00:30 +00:00
|
|
|
|
2021-03-23 17:41:18 +00:00
|
|
|
def test_status_to_pure_activity(self, *_):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""subclass of the base model version with a "pure" serializer"""
|
2021-03-13 15:38:19 +00:00
|
|
|
status = models.Status.objects.create(
|
|
|
|
content="test content", user=self.local_user
|
|
|
|
)
|
2020-12-18 19:00:30 +00:00
|
|
|
activity = status.to_activity(pure=True)
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertEqual(activity["id"], status.remote_id)
|
|
|
|
self.assertEqual(activity["type"], "Note")
|
|
|
|
self.assertEqual(activity["content"], "test content")
|
|
|
|
self.assertEqual(activity["sensitive"], False)
|
|
|
|
self.assertEqual(activity["attachment"], [])
|
2020-12-18 19:00:30 +00:00
|
|
|
|
2021-03-23 17:41:18 +00:00
|
|
|
def test_generated_note_to_activity(self, *_):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""subclass of the base model version with a "pure" serializer"""
|
2020-12-18 19:00:30 +00:00
|
|
|
status = models.GeneratedNote.objects.create(
|
2021-03-13 15:38:19 +00:00
|
|
|
content="test content", user=self.local_user
|
2021-03-08 16:49:10 +00:00
|
|
|
)
|
2020-12-18 19:00:30 +00:00
|
|
|
status.mention_books.set([self.book])
|
2021-03-13 15:38:19 +00:00
|
|
|
status.mention_users.set([self.local_user])
|
2020-12-18 19:00:30 +00:00
|
|
|
activity = status.to_activity()
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertEqual(activity["id"], status.remote_id)
|
|
|
|
self.assertEqual(activity["type"], "GeneratedNote")
|
|
|
|
self.assertEqual(activity["content"], "test content")
|
|
|
|
self.assertEqual(activity["sensitive"], False)
|
|
|
|
self.assertEqual(len(activity["tag"]), 2)
|
2020-12-18 19:00:30 +00:00
|
|
|
|
2021-03-23 17:41:18 +00:00
|
|
|
def test_generated_note_to_pure_activity(self, *_):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""subclass of the base model version with a "pure" serializer"""
|
2020-12-18 19:00:30 +00:00
|
|
|
status = models.GeneratedNote.objects.create(
|
2021-03-13 15:38:19 +00:00
|
|
|
content="test content", user=self.local_user
|
2021-03-08 16:49:10 +00:00
|
|
|
)
|
2020-12-18 19:00:30 +00:00
|
|
|
status.mention_books.set([self.book])
|
2021-03-13 15:38:19 +00:00
|
|
|
status.mention_users.set([self.local_user])
|
2020-12-18 19:00:30 +00:00
|
|
|
activity = status.to_activity(pure=True)
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertEqual(activity["id"], status.remote_id)
|
2020-12-18 19:00:30 +00:00
|
|
|
self.assertEqual(
|
2021-03-08 16:49:10 +00:00
|
|
|
activity["content"],
|
2021-10-01 21:24:11 +00:00
|
|
|
f'mouse test content <a href="{self.book.remote_id}">"Test Edition"</a>',
|
2021-03-08 16:49:10 +00:00
|
|
|
)
|
|
|
|
self.assertEqual(len(activity["tag"]), 2)
|
|
|
|
self.assertEqual(activity["type"], "Note")
|
|
|
|
self.assertEqual(activity["sensitive"], False)
|
|
|
|
self.assertIsInstance(activity["attachment"], list)
|
2021-03-15 22:44:26 +00:00
|
|
|
self.assertEqual(activity["attachment"][0].type, "Document")
|
2020-12-18 19:00:30 +00:00
|
|
|
self.assertEqual(
|
2021-03-08 16:49:10 +00:00
|
|
|
activity["attachment"][0].url,
|
2021-10-01 21:24:11 +00:00
|
|
|
f"https://{settings.DOMAIN}{self.book.cover.url}",
|
2021-03-08 16:49:10 +00:00
|
|
|
)
|
|
|
|
self.assertEqual(activity["attachment"][0].name, "Test Edition")
|
2020-12-18 19:00:30 +00:00
|
|
|
|
2021-03-23 17:41:18 +00:00
|
|
|
def test_comment_to_activity(self, *_):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""subclass of the base model version with a "pure" serializer"""
|
2020-12-18 19:00:30 +00:00
|
|
|
status = models.Comment.objects.create(
|
2021-03-13 15:38:19 +00:00
|
|
|
content="test content", user=self.local_user, book=self.book
|
2021-03-08 16:49:10 +00:00
|
|
|
)
|
2020-12-18 19:00:30 +00:00
|
|
|
activity = status.to_activity()
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertEqual(activity["id"], status.remote_id)
|
|
|
|
self.assertEqual(activity["type"], "Comment")
|
|
|
|
self.assertEqual(activity["content"], "test content")
|
|
|
|
self.assertEqual(activity["inReplyToBook"], self.book.remote_id)
|
2020-12-18 19:00:30 +00:00
|
|
|
|
2021-03-23 17:41:18 +00:00
|
|
|
def test_comment_to_pure_activity(self, *_):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""subclass of the base model version with a "pure" serializer"""
|
2020-12-18 19:00:30 +00:00
|
|
|
status = models.Comment.objects.create(
|
2021-03-13 15:38:19 +00:00
|
|
|
content="test content", user=self.local_user, book=self.book
|
2021-03-08 16:49:10 +00:00
|
|
|
)
|
2020-12-18 19:00:30 +00:00
|
|
|
activity = status.to_activity(pure=True)
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertEqual(activity["id"], status.remote_id)
|
|
|
|
self.assertEqual(activity["type"], "Note")
|
2020-12-18 19:00:30 +00:00
|
|
|
self.assertEqual(
|
2021-03-08 16:49:10 +00:00
|
|
|
activity["content"],
|
2021-10-01 21:24:11 +00:00
|
|
|
f'test content<p>(comment on <a href="{self.book.remote_id}">"Test Edition"</a>)</p>',
|
2021-03-08 16:49:10 +00:00
|
|
|
)
|
2021-03-15 22:44:26 +00:00
|
|
|
self.assertEqual(activity["attachment"][0].type, "Document")
|
2020-12-18 19:00:30 +00:00
|
|
|
self.assertEqual(
|
2021-03-08 16:49:10 +00:00
|
|
|
activity["attachment"][0].url,
|
2021-10-01 21:24:11 +00:00
|
|
|
f"https://{settings.DOMAIN}{self.book.cover.url}",
|
2021-03-08 16:49:10 +00:00
|
|
|
)
|
|
|
|
self.assertEqual(activity["attachment"][0].name, "Test Edition")
|
2020-12-18 19:00:30 +00:00
|
|
|
|
2021-03-23 17:41:18 +00:00
|
|
|
def test_quotation_to_activity(self, *_):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""subclass of the base model version with a "pure" serializer"""
|
2020-12-18 19:00:30 +00:00
|
|
|
status = models.Quotation.objects.create(
|
2021-03-08 16:49:10 +00:00
|
|
|
quote="a sickening sense",
|
|
|
|
content="test content",
|
2021-03-13 15:38:19 +00:00
|
|
|
user=self.local_user,
|
2021-03-08 16:49:10 +00:00
|
|
|
book=self.book,
|
|
|
|
)
|
2020-12-18 19:00:30 +00:00
|
|
|
activity = status.to_activity()
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertEqual(activity["id"], status.remote_id)
|
|
|
|
self.assertEqual(activity["type"], "Quotation")
|
|
|
|
self.assertEqual(activity["quote"], "a sickening sense")
|
|
|
|
self.assertEqual(activity["content"], "test content")
|
|
|
|
self.assertEqual(activity["inReplyToBook"], self.book.remote_id)
|
2020-05-10 00:10:02 +00:00
|
|
|
|
2021-03-23 17:41:18 +00:00
|
|
|
def test_quotation_to_pure_activity(self, *_):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""subclass of the base model version with a "pure" serializer"""
|
2020-12-18 19:00:30 +00:00
|
|
|
status = models.Quotation.objects.create(
|
2021-03-08 16:49:10 +00:00
|
|
|
quote="a sickening sense",
|
|
|
|
content="test content",
|
2021-03-13 15:38:19 +00:00
|
|
|
user=self.local_user,
|
2021-03-08 16:49:10 +00:00
|
|
|
book=self.book,
|
|
|
|
)
|
2020-12-18 19:00:30 +00:00
|
|
|
activity = status.to_activity(pure=True)
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertEqual(activity["id"], status.remote_id)
|
|
|
|
self.assertEqual(activity["type"], "Note")
|
2020-12-18 19:00:30 +00:00
|
|
|
self.assertEqual(
|
2021-03-08 16:49:10 +00:00
|
|
|
activity["content"],
|
2021-10-01 21:24:11 +00:00
|
|
|
f'a sickening sense <p>-- <a href="{self.book.remote_id}">"Test Edition"</a></p>test content',
|
2021-03-08 16:49:10 +00:00
|
|
|
)
|
2021-03-15 22:44:26 +00:00
|
|
|
self.assertEqual(activity["attachment"][0].type, "Document")
|
2020-12-18 19:00:30 +00:00
|
|
|
self.assertEqual(
|
2021-03-08 16:49:10 +00:00
|
|
|
activity["attachment"][0].url,
|
2021-10-01 21:24:11 +00:00
|
|
|
f"https://{settings.DOMAIN}{self.book.cover.url}",
|
2021-03-08 16:49:10 +00:00
|
|
|
)
|
|
|
|
self.assertEqual(activity["attachment"][0].name, "Test Edition")
|
2020-12-18 19:34:21 +00:00
|
|
|
|
2021-03-23 17:41:18 +00:00
|
|
|
def test_review_to_activity(self, *_):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""subclass of the base model version with a "pure" serializer"""
|
2021-08-02 23:05:40 +00:00
|
|
|
status = models.Review.objects.create(
|
|
|
|
name="Review name",
|
|
|
|
content="test content",
|
|
|
|
rating=3.0,
|
|
|
|
user=self.local_user,
|
|
|
|
book=self.book,
|
|
|
|
)
|
|
|
|
activity = status.to_activity()
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertEqual(activity["id"], status.remote_id)
|
|
|
|
self.assertEqual(activity["type"], "Review")
|
|
|
|
self.assertEqual(activity["rating"], 3)
|
|
|
|
self.assertEqual(activity["name"], "Review name")
|
|
|
|
self.assertEqual(activity["content"], "test content")
|
|
|
|
self.assertEqual(activity["inReplyToBook"], self.book.remote_id)
|
2020-12-18 19:34:21 +00:00
|
|
|
|
2021-03-23 17:41:18 +00:00
|
|
|
def test_review_to_pure_activity(self, *_):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""subclass of the base model version with a "pure" serializer"""
|
2021-08-02 23:05:40 +00:00
|
|
|
status = models.Review.objects.create(
|
|
|
|
name="Review's name",
|
|
|
|
content="test content",
|
|
|
|
rating=3.0,
|
|
|
|
user=self.local_user,
|
|
|
|
book=self.book,
|
|
|
|
)
|
|
|
|
activity = status.to_activity(pure=True)
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertEqual(activity["id"], status.remote_id)
|
|
|
|
self.assertEqual(activity["type"], "Article")
|
2020-12-18 19:34:21 +00:00
|
|
|
self.assertEqual(
|
2021-03-24 17:07:39 +00:00
|
|
|
activity["name"],
|
2021-10-01 21:24:11 +00:00
|
|
|
f'Review of "{self.book.title}" (3 stars): Review\'s name',
|
2021-03-24 17:07:39 +00:00
|
|
|
)
|
|
|
|
self.assertEqual(activity["content"], "test content")
|
|
|
|
self.assertEqual(activity["attachment"][0].type, "Document")
|
|
|
|
self.assertEqual(
|
|
|
|
activity["attachment"][0].url,
|
2021-10-01 21:24:11 +00:00
|
|
|
f"https://{settings.DOMAIN}{self.book.cover.url}",
|
2021-03-24 17:07:39 +00:00
|
|
|
)
|
|
|
|
self.assertEqual(activity["attachment"][0].name, "Test Edition")
|
|
|
|
|
|
|
|
def test_review_to_pure_activity_no_rating(self, *_):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""subclass of the base model version with a "pure" serializer"""
|
2021-08-02 23:05:40 +00:00
|
|
|
status = models.Review.objects.create(
|
|
|
|
name="Review name",
|
|
|
|
content="test content",
|
|
|
|
user=self.local_user,
|
|
|
|
book=self.book,
|
|
|
|
)
|
|
|
|
activity = status.to_activity(pure=True)
|
2021-03-24 17:07:39 +00:00
|
|
|
self.assertEqual(activity["id"], status.remote_id)
|
|
|
|
self.assertEqual(activity["type"], "Article")
|
|
|
|
self.assertEqual(
|
2021-10-01 21:24:11 +00:00
|
|
|
activity["name"], f'Review of "{self.book.title}": Review name'
|
2021-03-08 16:49:10 +00:00
|
|
|
)
|
|
|
|
self.assertEqual(activity["content"], "test content")
|
2021-03-15 22:44:26 +00:00
|
|
|
self.assertEqual(activity["attachment"][0].type, "Document")
|
2020-12-18 19:34:21 +00:00
|
|
|
self.assertEqual(
|
2021-03-08 16:49:10 +00:00
|
|
|
activity["attachment"][0].url,
|
2021-10-01 21:24:11 +00:00
|
|
|
f"https://{settings.DOMAIN}{self.book.cover.url}",
|
2021-03-08 16:49:10 +00:00
|
|
|
)
|
|
|
|
self.assertEqual(activity["attachment"][0].name, "Test Edition")
|
2020-12-18 19:34:21 +00:00
|
|
|
|
2021-03-24 17:17:45 +00:00
|
|
|
def test_reviewrating_to_pure_activity(self, *_):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""subclass of the base model version with a "pure" serializer"""
|
2021-08-02 23:05:40 +00:00
|
|
|
status = models.ReviewRating.objects.create(
|
|
|
|
rating=3.0,
|
|
|
|
user=self.local_user,
|
|
|
|
book=self.book,
|
|
|
|
)
|
|
|
|
activity = status.to_activity(pure=True)
|
2021-03-24 17:17:45 +00:00
|
|
|
self.assertEqual(activity["id"], status.remote_id)
|
|
|
|
self.assertEqual(activity["type"], "Note")
|
|
|
|
self.assertEqual(
|
|
|
|
activity["content"],
|
2021-10-01 21:24:11 +00:00
|
|
|
f'rated <em><a href="{self.book.remote_id}">{self.book.title}</a></em>: 3 stars',
|
2021-03-24 17:17:45 +00:00
|
|
|
)
|
|
|
|
self.assertEqual(activity["attachment"][0].type, "Document")
|
|
|
|
self.assertEqual(
|
|
|
|
activity["attachment"][0].url,
|
2021-10-01 21:24:11 +00:00
|
|
|
f"https://{settings.DOMAIN}{self.book.cover.url}",
|
2021-03-24 17:17:45 +00:00
|
|
|
)
|
|
|
|
self.assertEqual(activity["attachment"][0].name, "Test Edition")
|
|
|
|
|
2021-03-23 17:41:18 +00:00
|
|
|
def test_favorite(self, *_):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""fav a status"""
|
2021-02-08 17:38:28 +00:00
|
|
|
real_broadcast = models.Favorite.broadcast
|
2021-03-08 16:49:10 +00:00
|
|
|
|
2021-02-07 05:26:39 +00:00
|
|
|
def fav_broadcast_mock(_, activity, user):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""ok"""
|
2021-03-13 15:38:19 +00:00
|
|
|
self.assertEqual(user.remote_id, self.local_user.remote_id)
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertEqual(activity["type"], "Like")
|
|
|
|
|
2021-02-07 05:26:39 +00:00
|
|
|
models.Favorite.broadcast = fav_broadcast_mock
|
|
|
|
|
2021-03-13 15:38:19 +00:00
|
|
|
status = models.Status.objects.create(
|
|
|
|
content="test content", user=self.local_user
|
|
|
|
)
|
2021-08-02 23:05:40 +00:00
|
|
|
fav = models.Favorite.objects.create(status=status, user=self.local_user)
|
2020-12-18 19:34:21 +00:00
|
|
|
|
2021-08-02 23:05:40 +00:00
|
|
|
# can't fav a status twice
|
|
|
|
with self.assertRaises(IntegrityError):
|
|
|
|
models.Favorite.objects.create(status=status, user=self.local_user)
|
2020-12-18 19:34:21 +00:00
|
|
|
|
|
|
|
activity = fav.to_activity()
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertEqual(activity["type"], "Like")
|
2021-03-13 15:38:19 +00:00
|
|
|
self.assertEqual(activity["actor"], self.local_user.remote_id)
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertEqual(activity["object"], status.remote_id)
|
2021-02-08 17:38:28 +00:00
|
|
|
models.Favorite.broadcast = real_broadcast
|
2020-12-18 19:34:21 +00:00
|
|
|
|
2021-03-23 17:41:18 +00:00
|
|
|
def test_boost(self, *_):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""boosting, this one's a bit fussy"""
|
2021-03-13 15:38:19 +00:00
|
|
|
status = models.Status.objects.create(
|
|
|
|
content="test content", user=self.local_user
|
|
|
|
)
|
|
|
|
boost = models.Boost.objects.create(boosted_status=status, user=self.local_user)
|
2020-12-18 19:34:21 +00:00
|
|
|
activity = boost.to_activity()
|
2021-03-13 15:38:19 +00:00
|
|
|
self.assertEqual(activity["actor"], self.local_user.remote_id)
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertEqual(activity["object"], status.remote_id)
|
|
|
|
self.assertEqual(activity["type"], "Announce")
|
2020-12-18 19:34:21 +00:00
|
|
|
self.assertEqual(activity, boost.to_activity(pure=True))
|
|
|
|
|
2021-03-23 17:41:18 +00:00
|
|
|
def test_notification(self, *_):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""a simple model"""
|
2020-12-18 19:34:21 +00:00
|
|
|
notification = models.Notification.objects.create(
|
2021-03-13 15:38:19 +00:00
|
|
|
user=self.local_user, notification_type="FAVORITE"
|
2021-03-08 16:49:10 +00:00
|
|
|
)
|
2020-12-18 19:34:21 +00:00
|
|
|
self.assertFalse(notification.read)
|
|
|
|
|
|
|
|
with self.assertRaises(IntegrityError):
|
|
|
|
models.Notification.objects.create(
|
2021-03-13 15:38:19 +00:00
|
|
|
user=self.local_user, notification_type="GLORB"
|
2021-03-08 16:49:10 +00:00
|
|
|
)
|
2021-02-20 19:24:41 +00:00
|
|
|
|
2021-06-17 19:58:37 +00:00
|
|
|
# pylint: disable=unused-argument
|
|
|
|
def test_create_broadcast(self, one, two, broadcast_mock, *_):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""should send out two verions of a status on create"""
|
2021-03-13 15:38:19 +00:00
|
|
|
models.Comment.objects.create(
|
|
|
|
content="hi", user=self.local_user, book=self.book
|
|
|
|
)
|
2021-02-20 19:24:41 +00:00
|
|
|
self.assertEqual(broadcast_mock.call_count, 2)
|
|
|
|
pure_call = broadcast_mock.call_args_list[0]
|
|
|
|
bw_call = broadcast_mock.call_args_list[1]
|
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertEqual(pure_call[1]["software"], "other")
|
2021-02-20 19:24:41 +00:00
|
|
|
args = pure_call[0][0]
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertEqual(args["type"], "Create")
|
|
|
|
self.assertEqual(args["object"]["type"], "Note")
|
|
|
|
self.assertTrue("content" in args["object"])
|
2021-02-20 19:24:41 +00:00
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertEqual(bw_call[1]["software"], "bookwyrm")
|
2021-02-20 19:24:41 +00:00
|
|
|
args = bw_call[0][0]
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertEqual(args["type"], "Create")
|
|
|
|
self.assertEqual(args["object"]["type"], "Comment")
|
2021-03-13 15:38:19 +00:00
|
|
|
|
2021-03-23 17:41:18 +00:00
|
|
|
def test_recipients_with_mentions(self, *_):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""get recipients to broadcast a status"""
|
2021-03-13 15:38:19 +00:00
|
|
|
status = models.GeneratedNote.objects.create(
|
|
|
|
content="test content", user=self.local_user
|
|
|
|
)
|
|
|
|
status.mention_users.add(self.remote_user)
|
|
|
|
|
|
|
|
self.assertEqual(status.recipients, [self.remote_user])
|
|
|
|
|
2021-03-23 17:41:18 +00:00
|
|
|
def test_recipients_with_reply_parent(self, *_):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""get recipients to broadcast a status"""
|
2021-03-13 15:38:19 +00:00
|
|
|
parent_status = models.GeneratedNote.objects.create(
|
|
|
|
content="test content", user=self.remote_user
|
|
|
|
)
|
|
|
|
status = models.GeneratedNote.objects.create(
|
|
|
|
content="test content", user=self.local_user, reply_parent=parent_status
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(status.recipients, [self.remote_user])
|
|
|
|
|
2021-03-23 17:41:18 +00:00
|
|
|
def test_recipients_with_reply_parent_and_mentions(self, *_):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""get recipients to broadcast a status"""
|
2021-03-13 15:38:19 +00:00
|
|
|
parent_status = models.GeneratedNote.objects.create(
|
|
|
|
content="test content", user=self.remote_user
|
|
|
|
)
|
|
|
|
status = models.GeneratedNote.objects.create(
|
|
|
|
content="test content", user=self.local_user, reply_parent=parent_status
|
|
|
|
)
|
|
|
|
status.mention_users.set([self.remote_user])
|
|
|
|
|
|
|
|
self.assertEqual(status.recipients, [self.remote_user])
|
2021-03-13 16:13:20 +00:00
|
|
|
|
|
|
|
@responses.activate
|
2021-03-23 17:41:18 +00:00
|
|
|
def test_ignore_activity_boost(self, *_):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""don't bother with most remote statuses"""
|
2021-03-13 16:13:20 +00:00
|
|
|
activity = activitypub.Announce(
|
|
|
|
id="http://www.faraway.com/boost/12",
|
|
|
|
actor=self.remote_user.remote_id,
|
|
|
|
object="http://fish.com/nothing",
|
2021-03-24 21:19:12 +00:00
|
|
|
published="2021-03-24T18:59:41.841208+00:00",
|
|
|
|
cc="",
|
|
|
|
to="",
|
2021-03-13 16:13:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
responses.add(responses.GET, "http://fish.com/nothing", status=404)
|
|
|
|
|
|
|
|
self.assertTrue(models.Status.ignore_activity(activity))
|