diff --git a/bookwyrm/templatetags/rating_tags.py b/bookwyrm/templatetags/rating_tags.py index b986f6b8..670599e2 100644 --- a/bookwyrm/templatetags/rating_tags.py +++ b/bookwyrm/templatetags/rating_tags.py @@ -15,7 +15,7 @@ def get_rating(book, user): return cache.get_or_set( f"book-rating-{book.parent_work.id}-{user.id}", lambda u, b: models.Review.privacy_filter(u) - .filter(book__parent_work__editions=b) + .filter(book__parent_work__editions=b, rating__gt=0) .aggregate(Avg("rating"))["rating__avg"] or 0, user, diff --git a/bookwyrm/tests/templatetags/test_book_display_tags.py b/bookwyrm/tests/templatetags/test_book_display_tags.py index 36fdbcd0..54ae8806 100644 --- a/bookwyrm/tests/templatetags/test_book_display_tags.py +++ b/bookwyrm/tests/templatetags/test_book_display_tags.py @@ -4,12 +4,13 @@ from unittest.mock import patch from django.test import TestCase from bookwyrm import models -from bookwyrm.templatetags import bookwyrm_tags +from bookwyrm.templatetags import book_display_tags @patch("bookwyrm.activitystreams.add_status_task.delay") @patch("bookwyrm.activitystreams.remove_status_task.delay") -class BookWyrmTags(TestCase): +@patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async") +class BookDisplayTags(TestCase): """lotta different things here""" def setUp(self): @@ -24,14 +25,6 @@ class BookWyrmTags(TestCase): 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_book_description(self, *_): @@ -40,12 +33,30 @@ class BookWyrmTags(TestCase): self.book.parent_work = work self.book.save() - self.assertIsNone(bookwyrm_tags.get_book_description(self.book)) + self.assertIsNone(book_display_tags.get_book_description(self.book)) work.description = "hi" work.save() - self.assertEqual(bookwyrm_tags.get_book_description(self.book), "hi") + self.assertEqual(book_display_tags.get_book_description(self.book), "hi") self.book.description = "hello" self.book.save() - self.assertEqual(bookwyrm_tags.get_book_description(self.book), "hello") + self.assertEqual(book_display_tags.get_book_description(self.book), "hello") + + def test_get_book_file_links(self, *_): + """load approved links""" + link = models.FileLink.objects.create( + book=self.book, + url="https://web.site/hello", + ) + links = book_display_tags.get_book_file_links(self.book) + # the link is pending + self.assertFalse(links.exists()) + + domain = link.domain + domain.status = "approved" + domain.save() + + links = book_display_tags.get_book_file_links(self.book) + self.assertTrue(links.exists()) + self.assertEqual(links[0], link) diff --git a/bookwyrm/tests/templatetags/test_rating_tags.py b/bookwyrm/tests/templatetags/test_rating_tags.py index fad864ee..52fc44d5 100644 --- a/bookwyrm/tests/templatetags/test_rating_tags.py +++ b/bookwyrm/tests/templatetags/test_rating_tags.py @@ -4,7 +4,7 @@ from unittest.mock import patch from django.test import TestCase from bookwyrm import models -from bookwyrm.templatetags import bookwyrm_tags +from bookwyrm.templatetags import rating_tags @patch("bookwyrm.activitystreams.add_status_task.delay") @@ -17,7 +17,7 @@ class RatingTags(TestCase): with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch( "bookwyrm.activitystreams.populate_stream_task.delay" ), patch("bookwyrm.lists_stream.populate_lists_task.delay"): - self.user = models.User.objects.create_user( + self.local_user = models.User.objects.create_user( "mouse@example.com", "mouse@mouse.mouse", "mouseword", @@ -32,14 +32,56 @@ class RatingTags(TestCase): remote_id="http://example.com/rat", local=False, ) - self.book = models.Edition.objects.create(title="Test Book") + work = models.Work.objects.create(title="Work title") + self.book = models.Edition.objects.create( + title="Test Book", + parent_work=work, + ) + + @patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async") + def test_get_rating(self, *_): + """privacy filtered rating""" + # follows-only: not included + models.ReviewRating.objects.create( + user=self.remote_user, + rating=5, + book=self.book, + privacy="followers", + ) + self.assertEqual( + rating_tags.get_rating(self.book, self.local_user), 0 + ) + + # public: included + models.ReviewRating.objects.create( + user=self.remote_user, + rating=5, + book=self.book, + privacy="public", + ) + self.assertEqual( + rating_tags.get_rating(self.book, self.local_user), 5 + ) + + # rating unset: not included + models.Review.objects.create( + name="blah", + user=self.local_user, + rating=0, + book=self.book, + privacy="public", + ) + self.assertEqual( + rating_tags.get_rating(self.book, self.local_user), 5 + ) + 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) + models.Review.objects.create(user=self.local_user, book=self.book, rating=3) + self.assertEqual(rating_tags.get_user_rating(self.book, self.local_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) + self.assertEqual(rating_tags.get_user_rating(self.book, self.local_user), 0)