moviewyrm/bookwyrm/tests/test_templatetags.py

176 lines
6.9 KiB
Python
Raw Normal View History

2021-03-08 16:49:10 +00:00
""" style fixes and lookups for templates """
import re
from unittest.mock import patch
from django.test import TestCase
from django.utils import timezone
from bookwyrm import models
2021-05-11 22:52:41 +00:00
from bookwyrm.templatetags import (
bookwyrm_tags,
interaction,
markdown,
status_display,
utilities,
)
2021-03-23 16:19:11 +00:00
@patch("bookwyrm.activitystreams.ActivityStream.add_status")
class TemplateTags(TestCase):
2021-04-26 16:15:42 +00:00
"""lotta different things here"""
2021-03-08 16:49:10 +00:00
def setUp(self):
2021-04-26 16:15:42 +00:00
"""create some filler objects"""
self.user = models.User.objects.create_user(
2021-03-08 16:49:10 +00:00
"mouse@example.com",
"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(
2021-03-08 16:49:10 +00:00
"rat",
"rat@rat.rat",
"ratword",
remote_id="http://example.com/rat",
local=False,
)
self.book = models.Edition.objects.create(title="Test Book")
2021-03-23 16:19:11 +00:00
def test_get_user_rating(self, _):
2021-04-26 16:15:42 +00:00
"""get a user's most recent rating of a book"""
2021-03-08 16:49:10 +00:00
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
models.Review.objects.create(user=self.user, book=self.book, rating=3)
self.assertEqual(bookwyrm_tags.get_user_rating(self.book, self.user), 3)
2021-03-23 16:19:11 +00:00
def test_get_user_rating_doesnt_exist(self, _):
2021-04-26 16:15:42 +00:00
"""there is no rating available"""
2021-03-08 16:49:10 +00:00
self.assertEqual(bookwyrm_tags.get_user_rating(self.book, self.user), 0)
2021-03-23 16:19:11 +00:00
def test_get_user_identifer_local(self, _):
2021-04-26 16:15:42 +00:00
"""fall back to the simplest uid available"""
self.assertNotEqual(self.user.username, self.user.localname)
2021-05-11 22:52:41 +00:00
self.assertEqual(utilities.get_user_identifier(self.user), "mouse")
2021-03-23 16:19:11 +00:00
def test_get_user_identifer_remote(self, _):
2021-04-26 16:15:42 +00:00
"""for a remote user, should be their full username"""
self.assertEqual(
2021-05-11 22:52:41 +00:00
utilities.get_user_identifier(self.remote_user), "rat@example.com"
2021-03-08 16:49:10 +00:00
)
2021-03-23 16:19:11 +00:00
def test_get_replies(self, _):
2021-04-26 16:15:42 +00:00
"""direct replies to a status"""
2021-03-08 16:49:10 +00:00
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
2021-02-08 03:17:28 +00:00
parent = models.Review.objects.create(
2021-03-08 16:49:10 +00:00
user=self.user, book=self.book, content="hi"
)
2021-02-08 03:17:28 +00:00
first_child = models.Status.objects.create(
2021-03-08 16:49:10 +00:00
reply_parent=parent, user=self.user, content="hi"
)
2021-02-08 03:17:28 +00:00
second_child = models.Status.objects.create(
2021-03-08 16:49:10 +00:00
reply_parent=parent, user=self.user, content="hi"
)
2021-04-05 21:08:24 +00:00
with patch(
"bookwyrm.activitystreams.ActivityStream.remove_object_from_related_stores"
):
2021-03-23 16:19:11 +00:00
third_child = models.Status.objects.create(
reply_parent=parent,
user=self.user,
deleted=True,
deleted_date=timezone.now(),
)
2021-05-11 22:52:41 +00:00
replies = status_display.get_replies(parent)
self.assertEqual(len(replies), 2)
self.assertTrue(first_child in replies)
self.assertTrue(second_child in replies)
self.assertFalse(third_child in replies)
2021-03-23 16:19:11 +00:00
def test_get_parent(self, _):
2021-04-26 16:15:42 +00:00
"""get the reply parent of a status"""
2021-03-08 16:49:10 +00:00
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
2021-02-08 03:17:28 +00:00
parent = models.Review.objects.create(
2021-03-08 16:49:10 +00:00
user=self.user, book=self.book, content="hi"
)
2021-02-08 03:17:28 +00:00
child = models.Status.objects.create(
2021-03-08 16:49:10 +00:00
reply_parent=parent, user=self.user, content="hi"
)
2021-05-11 22:52:41 +00:00
result = status_display.get_parent(child)
self.assertEqual(result, parent)
self.assertIsInstance(result, models.Review)
2021-03-23 16:19:11 +00:00
def test_get_user_liked(self, _):
2021-04-26 16:15:42 +00:00
"""did a user like a status"""
2021-03-08 16:49:10 +00:00
status = models.Review.objects.create(user=self.remote_user, book=self.book)
2021-05-11 22:52:41 +00:00
self.assertFalse(interaction.get_user_liked(self.user, status))
2021-03-08 16:49:10 +00:00
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
models.Favorite.objects.create(user=self.user, status=status)
2021-05-11 22:52:41 +00:00
self.assertTrue(interaction.get_user_liked(self.user, status))
2021-03-23 16:19:11 +00:00
def test_get_user_boosted(self, _):
2021-04-26 16:15:42 +00:00
"""did a user boost a status"""
2021-03-08 16:49:10 +00:00
status = models.Review.objects.create(user=self.remote_user, book=self.book)
2021-05-11 22:52:41 +00:00
self.assertFalse(interaction.get_user_boosted(self.user, status))
2021-03-08 16:49:10 +00:00
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
models.Boost.objects.create(user=self.user, boosted_status=status)
2021-05-11 22:52:41 +00:00
self.assertTrue(interaction.get_user_boosted(self.user, status))
2021-03-23 16:19:11 +00:00
def test_get_boosted(self, _):
2021-04-26 16:15:42 +00:00
"""load a boosted status"""
2021-03-08 16:49:10 +00:00
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
status = models.Review.objects.create(user=self.remote_user, book=self.book)
boost = models.Boost.objects.create(user=self.user, boosted_status=status)
2021-05-11 22:52:41 +00:00
boosted = status_display.get_boosted(boost)
self.assertIsInstance(boosted, models.Review)
self.assertEqual(boosted, status)
2021-03-23 16:19:11 +00:00
def test_get_book_description(self, _):
2021-04-26 16:15:42 +00:00
"""grab it from the edition or the parent"""
2021-03-08 16:49:10 +00:00
work = models.Work.objects.create(title="Test Work")
self.book.parent_work = work
self.book.save()
self.assertIsNone(bookwyrm_tags.get_book_description(self.book))
2021-03-08 16:49:10 +00:00
work.description = "hi"
work.save()
2021-03-08 16:49:10 +00:00
self.assertEqual(bookwyrm_tags.get_book_description(self.book), "hi")
2021-03-08 16:49:10 +00:00
self.book.description = "hello"
self.book.save()
2021-03-08 16:49:10 +00:00
self.assertEqual(bookwyrm_tags.get_book_description(self.book), "hello")
2021-03-23 16:19:11 +00:00
def test_get_uuid(self, _):
2021-04-26 16:15:42 +00:00
"""uuid functionality"""
2021-05-11 22:52:41 +00:00
uuid = utilities.get_uuid("hi")
2021-03-08 16:49:10 +00:00
self.assertTrue(re.match(r"hi[A-Za-z0-9\-]", uuid))
2020-12-13 04:02:14 +00:00
2021-03-23 16:19:11 +00:00
def test_get_markdown(self, _):
2021-04-26 16:15:42 +00:00
"""mardown format data"""
2021-05-11 22:52:41 +00:00
result = markdown.get_markdown("_hi_")
2021-03-08 16:49:10 +00:00
self.assertEqual(result, "<p><em>hi</em></p>")
2021-01-14 03:28:44 +00:00
2021-05-11 22:52:41 +00:00
result = markdown.get_markdown("<marquee>_hi_</marquee>")
2021-03-08 16:49:10 +00:00
self.assertEqual(result, "<p><em>hi</em></p>")
2021-01-14 03:28:44 +00:00
2021-03-23 16:19:11 +00:00
def test_get_mentions(self, _):
2021-04-26 16:15:42 +00:00
"""list of people mentioned"""
2021-03-08 16:49:10 +00:00
status = models.Status.objects.create(content="hi", user=self.remote_user)
2021-05-11 22:52:41 +00:00
result = status_display.get_mentions(status, self.user)
2021-03-08 16:49:10 +00:00
self.assertEqual(result, "@rat@example.com ")
2021-01-14 03:28:44 +00:00
2021-03-23 16:19:11 +00:00
def test_related_status(self, _):
2021-04-26 16:15:42 +00:00
"""gets the subclass model for a notification status"""
2021-03-08 16:49:10 +00:00
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
status = models.Status.objects.create(content="hi", user=self.user)
2021-01-14 03:28:44 +00:00
notification = models.Notification.objects.create(
2021-03-08 16:49:10 +00:00
user=self.user, notification_type="MENTION", related_status=status
)
2021-01-14 03:28:44 +00:00
result = bookwyrm_tags.related_status(notification)
self.assertIsInstance(result, models.Status)