diff --git a/bookwyrm/templates/annual_summary/layout.html b/bookwyrm/templates/annual_summary/layout.html index 51a7dfe1b..f994e9787 100644 --- a/bookwyrm/templates/annual_summary/layout.html +++ b/bookwyrm/templates/annual_summary/layout.html @@ -59,6 +59,7 @@ + {% if book_pages_lowest and book_pages_highest %}
{% include 'snippets/book_cover.html' with book=book_pages_lowest cover_class='is-w-auto-tablet is-h-l-mobile' %} @@ -103,6 +104,7 @@

+ {% endif %}
diff --git a/bookwyrm/tests/views/test_annual_summary.py b/bookwyrm/tests/views/test_annual_summary.py new file mode 100644 index 000000000..69b13c9d1 --- /dev/null +++ b/bookwyrm/tests/views/test_annual_summary.py @@ -0,0 +1,118 @@ +"""testing the annual summary page""" +from datetime import datetime +from unittest.mock import patch + +from django.contrib.auth.models import AnonymousUser +from django.http import Http404 +from django.template.response import TemplateResponse +from django.test import TestCase +from django.test.client import RequestFactory + +from bookwyrm import models, views +from bookwyrm.tests.validate_html import validate_html + + +class AnnualSummary(TestCase): + """views""" + + def setUp(self): + """we need basic test data and mocks""" + self.factory = RequestFactory() + with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch( + "bookwyrm.activitystreams.populate_stream_task.delay" + ): + self.local_user = models.User.objects.create_user( + "mouse@local.com", + "mouse@mouse.com", + "mouseword", + local=True, + localname="mouse", + remote_id="https://example.com/users/mouse", + ) + self.work = models.Work.objects.create(title="Test Work") + self.book = models.Edition.objects.create( + title="Example Edition", + remote_id="https://example.com/book/1", + parent_work=self.work, + pages=300, + ) + self.review = models.Review.objects.create( + name="Review name", + content="test content", + rating=3.0, + user=self.local_user, + book=self.book, + ) + self.anonymous_user = AnonymousUser + self.anonymous_user.is_authenticated = False + + self.year = 2020 + models.SiteSettings.objects.create() + + def test_annual_summary_not_authenticated(self, *_): + """there are so many views, this just makes sure it DOESN’T LOAD""" + view = views.AnnualSummary.as_view() + request = self.factory.get("") + request.user = self.anonymous_user + + with patch( + "bookwyrm.views.annual_summary.is_year_available" + ) as is_year_available: + is_year_available.return_value = True + with self.assertRaises(Http404): + view(request, self.year) + + def test_annual_summary_wrong_year(self, *_): + """there are so many views, this just makes sure it DOESN’T LOAD""" + view = views.AnnualSummary.as_view() + request = self.factory.get("") + request.user = self.anonymous_user + + with patch( + "bookwyrm.views.annual_summary.is_year_available" + ) as is_year_available: + is_year_available.return_value = False + with self.assertRaises(Http404): + view(request, self.year) + + def test_annual_summary_empty_page(self, *_): + """there are so many views, this just makes sure it LOADS""" + view = views.AnnualSummary.as_view() + request = self.factory.get("") + request.user = self.local_user + + with patch( + "bookwyrm.views.annual_summary.is_year_available" + ) as is_year_available: + is_year_available.return_value = True + result = view(request, self.year) + self.assertIsInstance(result, TemplateResponse) + validate_html(result.render()) + self.assertEqual(result.status_code, 200) + + def test_annual_summary_page(self, *_): + """there are so many views, this just makes sure it LOADS""" + + shelf = self.local_user.shelf_set.get(identifier="read") + + with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"): + models.ShelfBook.objects.create( + book=self.book, + user=self.local_user, + shelf=shelf, + shelved_date=datetime(2020, 1, 1), + ) + + view = views.AnnualSummary.as_view() + request = self.factory.get("") + request.user = self.local_user + + with patch( + "bookwyrm.views.annual_summary.is_year_available" + ) as is_year_available: + is_year_available.return_value = True + result = view(request, self.year) + + self.assertIsInstance(result, TemplateResponse) + validate_html(result.render()) + self.assertEqual(result.status_code, 200) diff --git a/bookwyrm/views/annual_summary.py b/bookwyrm/views/annual_summary.py index 19e41fc8c..26374ea93 100644 --- a/bookwyrm/views/annual_summary.py +++ b/bookwyrm/views/annual_summary.py @@ -61,6 +61,9 @@ class AnnualSummary(View): user = request.user + if not user.is_authenticated: + raise Http404(f"Login or register {year} to access this page") + read_book_ids_in_year = get_read_book_ids_in_year(user, year) if len(read_book_ids_in_year) == 0: @@ -95,7 +98,9 @@ class AnnualSummary(View): "books_total": len(read_books_in_year), "books": read_books_in_year, "pages_total": page_stats["pages__sum"], - "pages_average": round(page_stats["pages__avg"]), + "pages_average": round( + page_stats["pages__avg"] if page_stats["pages__avg"] else 0 + ), "book_pages_lowest": book_list_by_pages.first(), "book_pages_highest": book_list_by_pages.last(), "no_page_number": no_page_list,