From 224dc4100ac5487f23106ef279710ab461481934 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Fri, 28 Jan 2022 17:32:41 -0800 Subject: [PATCH 1/8] Activitstreams tests --- .../activitystreams/test_abstractstream.py | 57 ++++++++++++++++++- .../tests/activitystreams/test_booksstream.py | 26 +++++++++ 2 files changed, 81 insertions(+), 2 deletions(-) diff --git a/bookwyrm/tests/activitystreams/test_abstractstream.py b/bookwyrm/tests/activitystreams/test_abstractstream.py index 2c5cf610..8d278041 100644 --- a/bookwyrm/tests/activitystreams/test_abstractstream.py +++ b/bookwyrm/tests/activitystreams/test_abstractstream.py @@ -1,6 +1,9 @@ """ testing activitystreams """ +from datetime import datetime from unittest.mock import patch from django.test import TestCase +from django.utils import timezone + from bookwyrm import activitystreams, models @@ -51,13 +54,63 @@ class Activitystreams(TestCase): """the abstract base class for stream objects""" self.assertEqual( self.test_stream.stream_id(self.local_user), - "{}-test".format(self.local_user.id), + f"{self.local_user.id}-test", ) self.assertEqual( self.test_stream.unread_id(self.local_user), - "{}-test-unread".format(self.local_user.id), + f"{self.local_user.id}-test-unread", ) + def test_unread_by_status_type_id(self, *_): + """stream for status type""" + self.assertEqual( + self.test_stream.unread_by_status_type_id(self.local_user), + f"{self.local_user.id}-test-unread-by-type", + ) + + def test_get_rank(self, *_): + """sort order""" + date = datetime(2022, 1, 28, 0, 0, tzinfo=timezone.utc) + status = models.Status.objects.create( + user=self.remote_user, + content="hi", + privacy="direct", + published_date=date, + ) + self.assertEqual( + str(self.test_stream.get_rank(status)), + "1643328000.0", + ) + + def test_get_activity_stream(self, *_): + """load statuses""" + status = models.Status.objects.create( + user=self.remote_user, + content="hi", + privacy="direct", + ) + status2 = models.Comment.objects.create( + user=self.remote_user, + content="hi", + privacy="direct", + book=self.book, + ) + models.Comment.objects.create( + user=self.remote_user, + content="hi", + privacy="direct", + book=self.book, + ) + with patch("bookwyrm.activitystreams.r.set"), patch( + "bookwyrm.activitystreams.r.delete" + ), patch("bookwyrm.activitystreams.ActivityStream.get_store") as redis_mock: + redis_mock.return_value = [status.id, status2.id] + result = self.test_stream.get_activity_stream(self.local_user) + self.assertEqual(result.count(), 2) + self.assertEqual(result.first(), status) + self.assertEqual(result.last(), status2) + self.assertIsInstance(result.last(), models.Comment) + def test_abstractstream_get_audience(self, *_): """get a list of users that should see a status""" status = models.Status.objects.create( diff --git a/bookwyrm/tests/activitystreams/test_booksstream.py b/bookwyrm/tests/activitystreams/test_booksstream.py index c001d6dd..dedf488a 100644 --- a/bookwyrm/tests/activitystreams/test_booksstream.py +++ b/bookwyrm/tests/activitystreams/test_booksstream.py @@ -52,3 +52,29 @@ class Activitystreams(TestCase): # yes book, yes audience result = activitystreams.BooksStream().get_statuses_for_user(self.local_user) self.assertEqual(list(result), [status]) + + def test_book_statuses(self, *_): + """statuses about a book""" + alt_book = models.Edition.objects.create( + title="hi", parent_work=self.book.parent_work + ) + status = models.Status.objects.create( + user=self.local_user, content="hi", privacy="public" + ) + status = models.Comment.objects.create( + user=self.remote_user, content="hi", privacy="public", book=alt_book + ) + models.ShelfBook.objects.create( + user=self.local_user, + shelf=self.local_user.shelf_set.first(), + book=self.book, + ) + with patch( + "bookwyrm.activitystreams.BooksStream.bulk_add_objects_to_store" + ) as redis_mock: + activitystreams.BooksStream().add_book_statuses(self.local_user, self.book) + args = redis_mock.call_args[0] + queryset = args[0] + self.assertEqual(queryset.count(), 1) + self.assertTrue(status in queryset) + self.assertEqual(args[1], f"{self.local_user.id}-books") From 5a3ce5e328c46f45d730a1aed1606c1e7e355264 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Thu, 3 Feb 2022 11:48:56 -0800 Subject: [PATCH 2/8] Fixes rating in about page superlatives --- bookwyrm/templates/about/about.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bookwyrm/templates/about/about.html b/bookwyrm/templates/about/about.html index 4e533b11..6f16aa67 100644 --- a/bookwyrm/templates/about/about.html +++ b/bookwyrm/templates/about/about.html @@ -28,7 +28,7 @@
{% if superlatives.top_rated %} - {% with book=superlatives.top_rated.default_edition rating=top_rated.rating %} + {% with book=superlatives.top_rated.default_edition rating=superlatives.top_rated.rating %}
From 1f6ecc39ac32b5867c970549e72a1a8c8a3b025b Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Thu, 3 Feb 2022 13:15:06 -0800 Subject: [PATCH 3/8] Adds allowlist for html attrs --- bookwyrm/sanitize_html.py | 12 +++++++++++- bookwyrm/tests/test_sanitize_html.py | 13 ++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/bookwyrm/sanitize_html.py b/bookwyrm/sanitize_html.py index 8b0e3c4c..538264fc 100644 --- a/bookwyrm/sanitize_html.py +++ b/bookwyrm/sanitize_html.py @@ -22,6 +22,9 @@ class InputHtmlParser(HTMLParser): # pylint: disable=abstract-method "ol", "li", ] + self.allowed_attrs = [ + "href", "rel", "src", "alt" + ] self.tag_stack = [] self.output = [] # if the html appears invalid, we just won't allow any at all @@ -30,7 +33,14 @@ class InputHtmlParser(HTMLParser): # pylint: disable=abstract-method def handle_starttag(self, tag, attrs): """check if the tag is valid""" if self.allow_html and tag in self.allowed_tags: - self.output.append(("tag", self.get_starttag_text())) + allowed_attrs = " ".join( + f'{a}="{v}"' for a, v in attrs if a in self.allowed_attrs + ) + reconstructed = f'<{tag}' + if allowed_attrs: + reconstructed += " " + allowed_attrs + reconstructed += ">" + self.output.append(("tag", reconstructed)) self.tag_stack.append(tag) else: self.output.append(("data", "")) diff --git a/bookwyrm/tests/test_sanitize_html.py b/bookwyrm/tests/test_sanitize_html.py index 6c405348..572f8332 100644 --- a/bookwyrm/tests/test_sanitize_html.py +++ b/bookwyrm/tests/test_sanitize_html.py @@ -24,13 +24,24 @@ class Sanitizer(TestCase): self.assertEqual(input_text, output) def test_valid_html_attrs(self): - """and don't remove attributes""" + """and don't remove useful attributes""" input_text = 'yes html' parser = InputHtmlParser() parser.feed(input_text) output = parser.get_output() self.assertEqual(input_text, output) + def test_valid_html_invalid_attrs(self): + """do remove un-approved attributes""" + input_text = 'yes html' + parser = InputHtmlParser() + parser.feed(input_text) + output = parser.get_output() + self.assertEqual( + output, + 'yes html' + ) + def test_invalid_html(self): """remove all html when the html is malformed""" input_text = "yes html" From 2c7a6e85186255c123708f4936af6465fd9b193f Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Thu, 3 Feb 2022 13:17:16 -0800 Subject: [PATCH 4/8] Correct status order --- bookwyrm/tests/activitystreams/test_abstractstream.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bookwyrm/tests/activitystreams/test_abstractstream.py b/bookwyrm/tests/activitystreams/test_abstractstream.py index 8d278041..f81c6fd6 100644 --- a/bookwyrm/tests/activitystreams/test_abstractstream.py +++ b/bookwyrm/tests/activitystreams/test_abstractstream.py @@ -107,8 +107,8 @@ class Activitystreams(TestCase): redis_mock.return_value = [status.id, status2.id] result = self.test_stream.get_activity_stream(self.local_user) self.assertEqual(result.count(), 2) - self.assertEqual(result.first(), status) - self.assertEqual(result.last(), status2) + self.assertEqual(result.first(), status2) + self.assertEqual(result.last(), status) self.assertIsInstance(result.last(), models.Comment) def test_abstractstream_get_audience(self, *_): From cae7191a2b65f82c70054bcb73efeb7518f77dee Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Thu, 3 Feb 2022 13:19:56 -0800 Subject: [PATCH 5/8] Python formatting --- bookwyrm/sanitize_html.py | 6 ++---- bookwyrm/tests/test_sanitize_html.py | 5 +---- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/bookwyrm/sanitize_html.py b/bookwyrm/sanitize_html.py index 538264fc..4edd2818 100644 --- a/bookwyrm/sanitize_html.py +++ b/bookwyrm/sanitize_html.py @@ -22,9 +22,7 @@ class InputHtmlParser(HTMLParser): # pylint: disable=abstract-method "ol", "li", ] - self.allowed_attrs = [ - "href", "rel", "src", "alt" - ] + self.allowed_attrs = ["href", "rel", "src", "alt"] self.tag_stack = [] self.output = [] # if the html appears invalid, we just won't allow any at all @@ -36,7 +34,7 @@ class InputHtmlParser(HTMLParser): # pylint: disable=abstract-method allowed_attrs = " ".join( f'{a}="{v}"' for a, v in attrs if a in self.allowed_attrs ) - reconstructed = f'<{tag}' + reconstructed = f"<{tag}" if allowed_attrs: reconstructed += " " + allowed_attrs reconstructed += ">" diff --git a/bookwyrm/tests/test_sanitize_html.py b/bookwyrm/tests/test_sanitize_html.py index 572f8332..5814f220 100644 --- a/bookwyrm/tests/test_sanitize_html.py +++ b/bookwyrm/tests/test_sanitize_html.py @@ -37,10 +37,7 @@ class Sanitizer(TestCase): parser = InputHtmlParser() parser.feed(input_text) output = parser.get_output() - self.assertEqual( - output, - 'yes html' - ) + self.assertEqual(output, 'yes html') def test_invalid_html(self): """remove all html when the html is malformed""" From 85aad7c219fbedbe8307c1e7b386f911740ca729 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Thu, 3 Feb 2022 13:25:44 -0800 Subject: [PATCH 6/8] Another sorting order error --- bookwyrm/tests/activitystreams/test_abstractstream.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bookwyrm/tests/activitystreams/test_abstractstream.py b/bookwyrm/tests/activitystreams/test_abstractstream.py index f81c6fd6..af94233f 100644 --- a/bookwyrm/tests/activitystreams/test_abstractstream.py +++ b/bookwyrm/tests/activitystreams/test_abstractstream.py @@ -109,7 +109,7 @@ class Activitystreams(TestCase): self.assertEqual(result.count(), 2) self.assertEqual(result.first(), status2) self.assertEqual(result.last(), status) - self.assertIsInstance(result.last(), models.Comment) + self.assertIsInstance(result.first(), models.Comment) def test_abstractstream_get_audience(self, *_): """get a list of users that should see a status""" From 3b12af63b60752f655834059c95456a4416a8836 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Thu, 3 Feb 2022 13:49:33 -0800 Subject: [PATCH 7/8] Fixes links on import page --- bookwyrm/templates/import/import_status.html | 4 ++-- bookwyrm/views/imports/troubleshoot.py | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/bookwyrm/templates/import/import_status.html b/bookwyrm/templates/import/import_status.html index 374ea22c..3a063954 100644 --- a/bookwyrm/templates/import/import_status.html +++ b/bookwyrm/templates/import/import_status.html @@ -47,7 +47,7 @@ {% trans "In progress" %} - {% trans "Refresh" %} + {% trans "Refresh" %}
@@ -230,7 +230,7 @@ {% if not legacy %}
- {% include 'snippets/pagination.html' with page=items %} + {% include 'snippets/pagination.html' with page=items path=page_path %}
{% endif %} {% endspaceless %}{% endblock %} diff --git a/bookwyrm/views/imports/troubleshoot.py b/bookwyrm/views/imports/troubleshoot.py index f637b966..e9ac275f 100644 --- a/bookwyrm/views/imports/troubleshoot.py +++ b/bookwyrm/views/imports/troubleshoot.py @@ -5,6 +5,7 @@ from django.core.paginator import Paginator from django.shortcuts import get_object_or_404, redirect from django.template.response import TemplateResponse from django.utils.decorators import method_decorator +from django.urls import reverse from django.views import View from bookwyrm import models @@ -35,6 +36,7 @@ class ImportTroubleshoot(View): page.number, on_each_side=2, on_ends=1 ), "complete": True, + "page_path": reverse("import-troubleshoot", args=[job.id]), } return TemplateResponse(request, "import/troubleshoot.html", data) From 9013b1417ae552681120858de1eea9b212a129a7 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Thu, 3 Feb 2022 13:59:53 -0800 Subject: [PATCH 8/8] Show cancel buttons on modals in static mode --- bookwyrm/templates/book/file_links/add_link_modal.html | 4 +--- bookwyrm/templates/readthrough/readthrough_modal.html | 2 -- bookwyrm/templates/snippets/report_modal.html | 4 +--- 3 files changed, 2 insertions(+), 8 deletions(-) diff --git a/bookwyrm/templates/book/file_links/add_link_modal.html b/bookwyrm/templates/book/file_links/add_link_modal.html index 0002b82b..d5b3fcd0 100644 --- a/bookwyrm/templates/book/file_links/add_link_modal.html +++ b/bookwyrm/templates/book/file_links/add_link_modal.html @@ -56,9 +56,7 @@ {% block modal-footer %} -{% if not static %} - -{% endif %} + {% endblock %} {% block modal-form-close %}{% endblock %} diff --git a/bookwyrm/templates/readthrough/readthrough_modal.html b/bookwyrm/templates/readthrough/readthrough_modal.html index 07d27b66..73f445f6 100644 --- a/bookwyrm/templates/readthrough/readthrough_modal.html +++ b/bookwyrm/templates/readthrough/readthrough_modal.html @@ -70,9 +70,7 @@ {% block modal-footer %} -{% if not static %} -{% endif %} {% endblock %} {% block modal-form-close %} diff --git a/bookwyrm/templates/snippets/report_modal.html b/bookwyrm/templates/snippets/report_modal.html index 0cf786ec..f65cab59 100644 --- a/bookwyrm/templates/snippets/report_modal.html +++ b/bookwyrm/templates/snippets/report_modal.html @@ -50,9 +50,7 @@ {% block modal-footer %} -{% if not static %} - -{% endif %} + {% endblock %}