moviewyrm/bookwyrm/tests/models/test_status_model.py

454 lines
19 KiB
Python
Raw Normal View History

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
import responses
2020-05-10 00:10:02 +00:00
from bookwyrm import activitypub, models, settings
2020-05-10 00:10:02 +00:00
# pylint: disable=too-many-public-methods
2021-03-08 16:49:10 +00:00
@patch("bookwyrm.models.Status.broadcast")
2021-03-23 17:41:18 +00:00
@patch("bookwyrm.activitystreams.ActivityStream.add_status")
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"""
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"
)
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-03-08 16:49:10 +00:00
self.book = models.Edition.objects.create(title="Test Edition")
2020-05-10 00:10:02 +00:00
2020-12-18 19:00:30 +00:00
image_file = pathlib.Path(__file__).parent.joinpath(
2021-03-08 16:49:10 +00:00
"../../static/images/default_avi.jpg"
)
2020-12-18 19:00:30 +00:00
image = Image.open(image_file)
output = BytesIO()
2021-03-08 16:49:10 +00:00
with patch("bookwyrm.models.Status.broadcast"):
2021-02-09 21:06:59 +00:00
image.save(output, format=image.format)
2021-03-08 16:49:10 +00:00
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"""
status = models.Status.objects.create(content="bleh", user=self.local_user)
2021-03-08 16:49:10 +00:00
expected_id = "https://%s/user/mouse/status/%d" % (settings.DOMAIN, status.id)
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"""
parent = models.Status.objects.create(content="hi", user=self.local_user)
2020-12-18 19:00:30 +00:00
child = models.Status.objects.create(
content="hello", reply_parent=parent, user=self.local_user
2021-03-08 16:49:10 +00:00
)
2020-12-18 19:00:30 +00:00
models.Review.objects.create(
content="hey", reply_parent=parent, user=self.local_user, book=self.book
2021-03-08 16:49:10 +00:00
)
2020-12-18 19:00:30 +00:00
models.Status.objects.create(
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 = models.Status.replies(parent)
self.assertEqual(replies.count(), 2)
self.assertEqual(replies.first(), child)
# should select subclasses
self.assertIsInstance(replies.last(), models.Review)
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"""
parent = models.Status.objects.create(content="hi", user=self.local_user)
2020-12-18 19:00:30 +00:00
child = models.Status.objects.create(
content="hello", reply_parent=parent, user=self.local_user
2021-03-08 16:49:10 +00:00
)
2020-12-18 19:00:30 +00:00
models.Review.objects.create(
content="hey", reply_parent=parent, user=self.local_user, book=self.book
2021-03-08 16:49:10 +00:00
)
2020-12-18 19:00:30 +00:00
models.Status.objects.create(
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-03-08 16:49:10 +00:00
self.assertEqual(replies["id"], "%s/replies" % parent.remote_id)
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"""
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-04-05 21:08:24 +00:00
with patch(
"bookwyrm.activitystreams.ActivityStream.remove_object_from_related_stores"
):
2021-03-23 17:41:18 +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"""
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(
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])
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(
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])
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"],
'mouse test content <a href="%s">"Test Edition"</a>' % self.book.remote_id,
)
self.assertEqual(len(activity["tag"]), 2)
self.assertEqual(activity["type"], "Note")
self.assertEqual(activity["sensitive"], False)
self.assertIsInstance(activity["attachment"], list)
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,
"https://%s%s" % (settings.DOMAIN, self.book.cover.url),
)
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(
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(
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"],
'test content<p>(comment on <a href="%s">"Test Edition"</a>)</p>'
% self.book.remote_id,
)
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,
"https://%s%s" % (settings.DOMAIN, self.book.cover.url),
)
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",
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",
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"],
'a sickening sense <p>-- <a href="%s">"Test Edition"</a></p>'
"test content" % self.book.remote_id,
)
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,
"https://%s%s" % (settings.DOMAIN, self.book.cover.url),
)
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"""
2020-12-18 19:34:21 +00:00
status = models.Review.objects.create(
2021-03-08 16:49:10 +00:00
name="Review name",
content="test content",
rating=3.0,
user=self.local_user,
2021-03-08 16:49:10 +00:00
book=self.book,
)
2020-12-18 19:34:21 +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"], "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"""
2020-12-18 19:34:21 +00:00
status = models.Review.objects.create(
name="Review's name",
2021-03-08 16:49:10 +00:00
content="test content",
rating=3.0,
user=self.local_user,
2021-03-08 16:49:10 +00:00
book=self.book,
)
2020-12-18 19:34:21 +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"], "Article")
2020-12-18 19:34:21 +00:00
self.assertEqual(
activity["name"],
'Review of "%s" (3 stars): Review\'s name' % self.book.title,
)
self.assertEqual(activity["content"], "test content")
self.assertEqual(activity["attachment"][0].type, "Document")
self.assertEqual(
activity["attachment"][0].url,
"https://%s%s" % (settings.DOMAIN, self.book.cover.url),
)
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"""
status = models.Review.objects.create(
name="Review name",
content="test content",
user=self.local_user,
book=self.book,
)
activity = status.to_activity(pure=True)
self.assertEqual(activity["id"], status.remote_id)
self.assertEqual(activity["type"], "Article")
self.assertEqual(
2021-03-24 18:01:23 +00:00
activity["name"], 'Review of "%s": Review name' % self.book.title
2021-03-08 16:49:10 +00:00
)
self.assertEqual(activity["content"], "test content")
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,
"https://%s%s" % (settings.DOMAIN, self.book.cover.url),
)
self.assertEqual(activity["attachment"][0].name, "Test Edition")
2020-12-18 19:34:21 +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"""
status = models.ReviewRating.objects.create(
rating=3.0,
user=self.local_user,
book=self.book,
)
activity = status.to_activity(pure=True)
self.assertEqual(activity["id"], status.remote_id)
self.assertEqual(activity["type"], "Note")
self.assertEqual(
activity["content"],
'Rated <em><a href="%s">%s</a></em>: 3 stars'
% (self.book.remote_id, self.book.title),
)
self.assertEqual(activity["attachment"][0].type, "Document")
self.assertEqual(
activity["attachment"][0].url,
"https://%s%s" % (settings.DOMAIN, self.book.cover.url),
)
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"""
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
status = models.Status.objects.create(
content="test content", user=self.local_user
)
fav = models.Favorite.objects.create(status=status, user=self.local_user)
2020-12-18 19:34:21 +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")
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"""
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()
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(
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(
user=self.local_user, notification_type="GLORB"
2021-03-08 16:49:10 +00:00
)
2021-02-20 19:24:41 +00:00
2021-03-23 17:41:18 +00:00
def test_create_broadcast(self, _, broadcast_mock):
2021-04-26 16:15:42 +00:00
"""should send out two verions of a status on create"""
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-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"""
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"""
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"""
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])
@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"""
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="",
)
responses.add(responses.GET, "http://fish.com/nothing", status=404)
self.assertTrue(models.Status.ignore_activity(activity))