diff --git a/bookwyrm/tests/templatetags/__init__.py b/bookwyrm/tests/templatetags/__init__.py new file mode 100644 index 00000000..b6e690fd --- /dev/null +++ b/bookwyrm/tests/templatetags/__init__.py @@ -0,0 +1 @@ +from . import * diff --git a/bookwyrm/tests/templatetags/test_bookwyrm_tags.py b/bookwyrm/tests/templatetags/test_bookwyrm_tags.py new file mode 100644 index 00000000..04f6a5d4 --- /dev/null +++ b/bookwyrm/tests/templatetags/test_bookwyrm_tags.py @@ -0,0 +1,101 @@ +""" style fixes and lookups for templates """ +from unittest.mock import patch + +from django.test import TestCase + +from bookwyrm import models +from bookwyrm.templatetags import bookwyrm_tags + + +@patch("bookwyrm.activitystreams.add_status_task.delay") +@patch("bookwyrm.activitystreams.remove_status_task.delay") +class BookWyrmTags(TestCase): + """lotta different things here""" + + def setUp(self): + """create some filler objects""" + with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch( + "bookwyrm.activitystreams.populate_stream_task.delay" + ): + self.user = models.User.objects.create_user( + "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( + "rat", + "rat@rat.rat", + "ratword", + remote_id="http://example.com/rat", + local=False, + ) + self.book = models.Edition.objects.create(title="Test Book") + + def test_get_user_rating(self, *_): + """get a user's most recent rating of a book""" + with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"): + models.Review.objects.create(user=self.user, book=self.book, rating=3) + self.assertEqual(bookwyrm_tags.get_user_rating(self.book, self.user), 3) + + def test_get_user_rating_doesnt_exist(self, *_): + """there is no rating available""" + self.assertEqual(bookwyrm_tags.get_user_rating(self.book, self.user), 0) + + def test_get_book_description(self, *_): + """grab it from the edition or the parent""" + 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)) + + work.description = "hi" + work.save() + self.assertEqual(bookwyrm_tags.get_book_description(self.book), "hi") + + self.book.description = "hello" + self.book.save() + self.assertEqual(bookwyrm_tags.get_book_description(self.book), "hello") + + def test_get_next_shelf(self, *_): + """self progress helper""" + self.assertEqual(bookwyrm_tags.get_next_shelf("to-read"), "reading") + self.assertEqual(bookwyrm_tags.get_next_shelf("reading"), "read") + self.assertEqual(bookwyrm_tags.get_next_shelf("read"), "complete") + self.assertEqual(bookwyrm_tags.get_next_shelf("blooooga"), "to-read") + + @patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async") + def test_load_subclass(self, *_): + """get a status' real type""" + review = models.Review.objects.create(user=self.user, book=self.book, rating=3) + status = models.Status.objects.get(id=review.id) + self.assertIsInstance(status, models.Status) + self.assertIsInstance(bookwyrm_tags.load_subclass(status), models.Review) + + quote = models.Quotation.objects.create( + user=self.user, book=self.book, content="hi" + ) + status = models.Status.objects.get(id=quote.id) + self.assertIsInstance(status, models.Status) + self.assertIsInstance(bookwyrm_tags.load_subclass(status), models.Quotation) + + comment = models.Comment.objects.create( + user=self.user, book=self.book, content="hi" + ) + status = models.Status.objects.get(id=comment.id) + self.assertIsInstance(status, models.Status) + self.assertIsInstance(bookwyrm_tags.load_subclass(status), models.Comment) + + def test_related_status(self, *_): + """gets the subclass model for a notification status""" + with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"): + status = models.Status.objects.create(content="hi", user=self.user) + notification = models.Notification.objects.create( + user=self.user, notification_type="MENTION", related_status=status + ) + + result = bookwyrm_tags.related_status(notification) + self.assertIsInstance(result, models.Status) diff --git a/bookwyrm/tests/templatetags/test_interaction.py b/bookwyrm/tests/templatetags/test_interaction.py new file mode 100644 index 00000000..d32548e4 --- /dev/null +++ b/bookwyrm/tests/templatetags/test_interaction.py @@ -0,0 +1,53 @@ +""" style fixes and lookups for templates """ +from unittest.mock import patch + +from django.test import TestCase + +from bookwyrm import models +from bookwyrm.templatetags import interaction + + +@patch("bookwyrm.activitystreams.add_status_task.delay") +@patch("bookwyrm.activitystreams.remove_status_task.delay") +class InteractionTags(TestCase): + """lotta different things here""" + + def setUp(self): + """create some filler objects""" + with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch( + "bookwyrm.activitystreams.populate_stream_task.delay" + ): + self.user = models.User.objects.create_user( + "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( + "rat", + "rat@rat.rat", + "ratword", + remote_id="http://example.com/rat", + local=False, + ) + self.book = models.Edition.objects.create(title="Test Book") + + def test_get_user_liked(self, *_): + """did a user like a status""" + status = models.Review.objects.create(user=self.remote_user, book=self.book) + + self.assertFalse(interaction.get_user_liked(self.user, status)) + with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"): + models.Favorite.objects.create(user=self.user, status=status) + self.assertTrue(interaction.get_user_liked(self.user, status)) + + def test_get_user_boosted(self, *_): + """did a user boost a status""" + status = models.Review.objects.create(user=self.remote_user, book=self.book) + + self.assertFalse(interaction.get_user_boosted(self.user, status)) + with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"): + models.Boost.objects.create(user=self.user, boosted_status=status) + self.assertTrue(interaction.get_user_boosted(self.user, status)) diff --git a/bookwyrm/tests/templatetags/test_markdown.py b/bookwyrm/tests/templatetags/test_markdown.py new file mode 100644 index 00000000..ba283a4f --- /dev/null +++ b/bookwyrm/tests/templatetags/test_markdown.py @@ -0,0 +1,15 @@ +""" style fixes and lookups for templates """ +from django.test import TestCase +from bookwyrm.templatetags import markdown + + +class MarkdownTags(TestCase): + """lotta different things here""" + + def test_get_markdown(self): + """mardown format data""" + result = markdown.get_markdown("_hi_") + self.assertEqual(result, "

hi

") + + result = markdown.get_markdown("_hi_") + self.assertEqual(result, "

hi

") diff --git a/bookwyrm/tests/templatetags/test_status_display.py b/bookwyrm/tests/templatetags/test_status_display.py new file mode 100644 index 00000000..b3eaeab8 --- /dev/null +++ b/bookwyrm/tests/templatetags/test_status_display.py @@ -0,0 +1,90 @@ +""" style fixes and lookups for templates """ +from unittest.mock import patch + +from django.test import TestCase +from django.utils import timezone + +from bookwyrm import models +from bookwyrm.templatetags import status_display + + +@patch("bookwyrm.activitystreams.add_status_task.delay") +@patch("bookwyrm.activitystreams.remove_status_task.delay") +class StatusDisplayTags(TestCase): + """lotta different things here""" + + def setUp(self): + """create some filler objects""" + with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch( + "bookwyrm.activitystreams.populate_stream_task.delay" + ): + self.user = models.User.objects.create_user( + "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( + "rat", + "rat@rat.rat", + "ratword", + remote_id="http://example.com/rat", + local=False, + ) + self.book = models.Edition.objects.create(title="Test Book") + + @patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async") + def test_get_replies(self, *_): + """direct replies to a status""" + parent = models.Review.objects.create( + user=self.user, book=self.book, content="hi" + ) + first_child = models.Status.objects.create( + reply_parent=parent, user=self.user, content="hi" + ) + second_child = models.Status.objects.create( + reply_parent=parent, user=self.user, content="hi" + ) + third_child = models.Status.objects.create( + reply_parent=parent, + user=self.user, + deleted=True, + deleted_date=timezone.now(), + ) + + 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) + + def test_get_parent(self, *_): + """get the reply parent of a status""" + with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"): + parent = models.Review.objects.create( + user=self.user, book=self.book, content="hi" + ) + child = models.Status.objects.create( + reply_parent=parent, user=self.user, content="hi" + ) + + result = status_display.get_parent(child) + self.assertEqual(result, parent) + self.assertIsInstance(result, models.Review) + + def test_get_boosted(self, *_): + """load a boosted status""" + with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"): + status = models.Review.objects.create(user=self.remote_user, book=self.book) + boost = models.Boost.objects.create(user=self.user, boosted_status=status) + boosted = status_display.get_boosted(boost) + self.assertIsInstance(boosted, models.Review) + self.assertEqual(boosted, status) + + def test_get_mentions(self, *_): + """list of people mentioned""" + status = models.Status.objects.create(content="hi", user=self.remote_user) + result = status_display.get_mentions(status, self.user) + self.assertEqual(result, "@rat@example.com ") diff --git a/bookwyrm/tests/templatetags/test_utilities.py b/bookwyrm/tests/templatetags/test_utilities.py new file mode 100644 index 00000000..f40d24dc --- /dev/null +++ b/bookwyrm/tests/templatetags/test_utilities.py @@ -0,0 +1,52 @@ +""" style fixes and lookups for templates """ +import re +from unittest.mock import patch + +from django.test import TestCase + +from bookwyrm import models +from bookwyrm.templatetags import utilities + + +@patch("bookwyrm.activitystreams.add_status_task.delay") +@patch("bookwyrm.activitystreams.remove_status_task.delay") +class UtilitiesTags(TestCase): + """lotta different things here""" + + def setUp(self): + """create some filler objects""" + with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch( + "bookwyrm.activitystreams.populate_stream_task.delay" + ): + self.user = models.User.objects.create_user( + "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( + "rat", + "rat@rat.rat", + "ratword", + remote_id="http://example.com/rat", + local=False, + ) + self.book = models.Edition.objects.create(title="Test Book") + + def test_get_user_identifer_local(self, *_): + """fall back to the simplest uid available""" + self.assertNotEqual(self.user.username, self.user.localname) + self.assertEqual(utilities.get_user_identifier(self.user), "mouse") + + def test_get_user_identifer_remote(self, *_): + """for a remote user, should be their full username""" + self.assertEqual( + utilities.get_user_identifier(self.remote_user), "rat@example.com" + ) + + def test_get_uuid(self, *_): + """uuid functionality""" + uuid = utilities.get_uuid("hi") + self.assertTrue(re.match(r"hi[A-Za-z0-9\-]", uuid)) diff --git a/bookwyrm/tests/test_templatetags.py b/bookwyrm/tests/test_templatetags.py deleted file mode 100644 index ed4466f5..00000000 --- a/bookwyrm/tests/test_templatetags.py +++ /dev/null @@ -1,183 +0,0 @@ -""" 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 -from bookwyrm.templatetags import ( - bookwyrm_tags, - interaction, - markdown, - status_display, - utilities, -) - - -@patch("bookwyrm.activitystreams.add_status_task.delay") -@patch("bookwyrm.activitystreams.remove_status_task.delay") -class TemplateTags(TestCase): - """lotta different things here""" - - def setUp(self): - """create some filler objects""" - with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch( - "bookwyrm.activitystreams.populate_stream_task.delay" - ): - self.user = models.User.objects.create_user( - "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( - "rat", - "rat@rat.rat", - "ratword", - remote_id="http://example.com/rat", - local=False, - ) - self.book = models.Edition.objects.create(title="Test Book") - - def test_get_user_rating(self, *_): - """get a user's most recent rating of a book""" - with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"): - models.Review.objects.create(user=self.user, book=self.book, rating=3) - self.assertEqual(bookwyrm_tags.get_user_rating(self.book, self.user), 3) - - def test_get_user_rating_doesnt_exist(self, *_): - """there is no rating available""" - self.assertEqual(bookwyrm_tags.get_user_rating(self.book, self.user), 0) - - def test_get_user_identifer_local(self, *_): - """fall back to the simplest uid available""" - self.assertNotEqual(self.user.username, self.user.localname) - self.assertEqual(utilities.get_user_identifier(self.user), "mouse") - - def test_get_user_identifer_remote(self, *_): - """for a remote user, should be their full username""" - self.assertEqual( - utilities.get_user_identifier(self.remote_user), "rat@example.com" - ) - - @patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async") - def test_get_replies(self, *_): - """direct replies to a status""" - parent = models.Review.objects.create( - user=self.user, book=self.book, content="hi" - ) - first_child = models.Status.objects.create( - reply_parent=parent, user=self.user, content="hi" - ) - second_child = models.Status.objects.create( - reply_parent=parent, user=self.user, content="hi" - ) - third_child = models.Status.objects.create( - reply_parent=parent, - user=self.user, - deleted=True, - deleted_date=timezone.now(), - ) - - 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) - - def test_get_parent(self, *_): - """get the reply parent of a status""" - with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"): - parent = models.Review.objects.create( - user=self.user, book=self.book, content="hi" - ) - child = models.Status.objects.create( - reply_parent=parent, user=self.user, content="hi" - ) - - result = status_display.get_parent(child) - self.assertEqual(result, parent) - self.assertIsInstance(result, models.Review) - - def test_get_user_liked(self, *_): - """did a user like a status""" - status = models.Review.objects.create(user=self.remote_user, book=self.book) - - self.assertFalse(interaction.get_user_liked(self.user, status)) - with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"): - models.Favorite.objects.create(user=self.user, status=status) - self.assertTrue(interaction.get_user_liked(self.user, status)) - - def test_get_user_boosted(self, *_): - """did a user boost a status""" - status = models.Review.objects.create(user=self.remote_user, book=self.book) - - self.assertFalse(interaction.get_user_boosted(self.user, status)) - with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"): - models.Boost.objects.create(user=self.user, boosted_status=status) - self.assertTrue(interaction.get_user_boosted(self.user, status)) - - def test_get_boosted(self, *_): - """load a boosted status""" - with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"): - status = models.Review.objects.create(user=self.remote_user, book=self.book) - boost = models.Boost.objects.create(user=self.user, boosted_status=status) - boosted = status_display.get_boosted(boost) - self.assertIsInstance(boosted, models.Review) - self.assertEqual(boosted, status) - - def test_get_book_description(self, *_): - """grab it from the edition or the parent""" - 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)) - - work.description = "hi" - work.save() - self.assertEqual(bookwyrm_tags.get_book_description(self.book), "hi") - - self.book.description = "hello" - self.book.save() - self.assertEqual(bookwyrm_tags.get_book_description(self.book), "hello") - - def test_get_uuid(self, *_): - """uuid functionality""" - uuid = utilities.get_uuid("hi") - self.assertTrue(re.match(r"hi[A-Za-z0-9\-]", uuid)) - - def test_get_markdown(self, *_): - """mardown format data""" - result = markdown.get_markdown("_hi_") - self.assertEqual(result, "

hi

") - - result = markdown.get_markdown("_hi_") - self.assertEqual(result, "

hi

") - - def test_get_mentions(self, *_): - """list of people mentioned""" - status = models.Status.objects.create(content="hi", user=self.remote_user) - result = status_display.get_mentions(status, self.user) - self.assertEqual(result, "@rat@example.com ") - - def test_related_status(self, *_): - """gets the subclass model for a notification status""" - with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"): - status = models.Status.objects.create(content="hi", user=self.user) - notification = models.Notification.objects.create( - user=self.user, notification_type="MENTION", related_status=status - ) - - result = bookwyrm_tags.related_status(notification) - self.assertIsInstance(result, models.Status) - - def test_get_next_shelf(self, *_): - """self progress helper""" - self.assertEqual(bookwyrm_tags.get_next_shelf("to-read"), "reading") - self.assertEqual(bookwyrm_tags.get_next_shelf("reading"), "read") - self.assertEqual(bookwyrm_tags.get_next_shelf("read"), "complete") - self.assertEqual(bookwyrm_tags.get_next_shelf("blooooga"), "to-read")