Helper function for html validation

This commit is contained in:
Mouse Reeve 2021-09-30 22:22:20 -07:00
parent 9c78a9d95c
commit bdb1d1998a
17 changed files with 76 additions and 338 deletions

View file

@ -0,0 +1,20 @@
""" html validation on rendered templates """
from tidylib import tidy_document
def validate_html(html):
""" run tidy on html """
_, errors = tidy_document(
html.content,
options={
"drop-empty-elements": False,
"warn-proprietary-attributes": False,
},
)
# idk how else to filter out these unescape amp errs
errors = "\n".join(
e
for e in errors.split("\n")
if "&book" not in e and "id and name attribute" not in e
)
if errors:
raise Exception(errors)

View file

@ -1,12 +1,11 @@
""" test for app action functionality """
from unittest.mock import patch
from tidylib import tidy_document
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 DashboardViews(TestCase):
"""every response to a get request, html or json"""
@ -35,8 +34,5 @@ class DashboardViews(TestCase):
request.user.is_superuser = True
result = view(request)
self.assertIsInstance(result, TemplateResponse)
html = result.render()
_, errors = tidy_document(html.content)
if errors:
raise Exception(errors)
validate_html(result.render())
self.assertEqual(result.status_code, 200)

View file

@ -1,12 +1,12 @@
""" test for app action functionality """
from unittest.mock import patch
from tidylib import tidy_document
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 EmailBlocklistViews(TestCase):
@ -38,10 +38,7 @@ class EmailBlocklistViews(TestCase):
result = view(request)
self.assertIsInstance(result, TemplateResponse)
html = result.render()
_, errors = tidy_document(html.content, options={"drop-empty-elements": False})
if errors:
raise Exception(errors)
validate_html(result.render())
self.assertEqual(result.status_code, 200)
def test_blocklist_page_post(self):
@ -54,10 +51,7 @@ class EmailBlocklistViews(TestCase):
result = view(request)
self.assertIsInstance(result, TemplateResponse)
html = result.render()
_, errors = tidy_document(html.content, options={"drop-empty-elements": False})
if errors:
raise Exception(errors)
validate_html(result.render())
self.assertEqual(result.status_code, 200)
self.assertTrue(

View file

@ -1,7 +1,6 @@
""" test for app action functionality """
import json
from unittest.mock import patch
from tidylib import tidy_document
from django.core.files.uploadedfile import SimpleUploadedFile
from django.template.response import TemplateResponse
@ -9,6 +8,7 @@ from django.test import TestCase
from django.test.client import RequestFactory
from bookwyrm import forms, models, views
from bookwyrm.tests.validate_html import validate_html
class FederationViews(TestCase):
@ -48,16 +48,7 @@ class FederationViews(TestCase):
request.user.is_superuser = True
result = view(request)
self.assertIsInstance(result, TemplateResponse)
html = result.render()
_, errors = tidy_document(
html.content,
options={
"drop-empty-elements": False,
"warn-proprietary-attributes": False,
},
)
if errors:
raise Exception(errors)
validate_html(result.render())
self.assertEqual(result.status_code, 200)
def test_instance_page(self):
@ -70,10 +61,7 @@ class FederationViews(TestCase):
result = view(request, server.id)
self.assertIsInstance(result, TemplateResponse)
html = result.render()
_, errors = tidy_document(html.content, options={"drop-empty-elements": False})
if errors:
raise Exception(errors)
validate_html(result.render())
self.assertEqual(result.status_code, 200)
def test_server_page_block(self):
@ -162,10 +150,7 @@ class FederationViews(TestCase):
result = view(request)
self.assertIsInstance(result, TemplateResponse)
html = result.render()
_, errors = tidy_document(html.content)
if errors:
raise Exception(errors)
validate_html(result.render())
self.assertEqual(result.status_code, 200)
def test_add_view_post_create(self):

View file

@ -1,11 +1,11 @@
""" test for app action functionality """
from unittest.mock import patch
from tidylib import tidy_document
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 IPBlocklistViews(TestCase):
@ -37,8 +37,5 @@ class IPBlocklistViews(TestCase):
result = view(request)
self.assertIsInstance(result, TemplateResponse)
html = result.render()
_, errors = tidy_document(html.content, options={"drop-empty-elements": False})
if errors:
raise Exception(errors)
validate_html(result.render())
self.assertEqual(result.status_code, 200)

View file

@ -1,13 +1,13 @@
""" test for app action functionality """
import json
from unittest.mock import patch
from tidylib import tidy_document
from django.template.response import TemplateResponse
from django.test import TestCase
from django.test.client import RequestFactory
from bookwyrm import forms, models, views
from bookwyrm.tests.validate_html import validate_html
class ReportViews(TestCase):
@ -44,16 +44,7 @@ class ReportViews(TestCase):
result = view(request)
self.assertIsInstance(result, TemplateResponse)
html = result.render()
_, errors = tidy_document(
html.content,
options={
"drop-empty-elements": False,
"warn-proprietary-attributes": False,
},
)
if errors:
raise Exception(errors)
validate_html(result.render())
self.assertEqual(result.status_code, 200)
def test_reports_page_with_data(self):
@ -66,16 +57,7 @@ class ReportViews(TestCase):
result = view(request)
self.assertIsInstance(result, TemplateResponse)
html = result.render()
_, errors = tidy_document(
html.content,
options={
"drop-empty-elements": False,
"warn-proprietary-attributes": False,
},
)
if errors:
raise Exception(errors)
validate_html(result.render())
self.assertEqual(result.status_code, 200)
def test_report_page(self):
@ -89,10 +71,7 @@ class ReportViews(TestCase):
result = view(request, report.id)
self.assertIsInstance(result, TemplateResponse)
html = result.render()
_, errors = tidy_document(html.content, options={"drop-empty-elements": False})
if errors:
raise Exception(errors)
validate_html(result.render())
self.assertEqual(result.status_code, 200)
def test_report_comment(self):

View file

@ -1,6 +1,5 @@
""" test for app action functionality """
from unittest.mock import patch
from tidylib import tidy_document
from django.contrib.auth.models import Group
from django.template.response import TemplateResponse
@ -8,6 +7,7 @@ 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 UserAdminViews(TestCase):
@ -36,10 +36,7 @@ class UserAdminViews(TestCase):
request.user.is_superuser = True
result = view(request)
self.assertIsInstance(result, TemplateResponse)
html = result.render()
_, errors = tidy_document(html.content, options={"drop-empty-elements": False})
if errors:
raise Exception(errors)
validate_html(result.render())
self.assertEqual(result.status_code, 200)
def test_user_admin_page(self):
@ -52,10 +49,7 @@ class UserAdminViews(TestCase):
result = view(request, self.local_user.id)
self.assertIsInstance(result, TemplateResponse)
html = result.render()
_, errors = tidy_document(html.content, options={"drop-empty-elements": False})
if errors:
raise Exception(errors)
validate_html(result.render())
self.assertEqual(result.status_code, 200)
@patch("bookwyrm.suggested_users.rerank_suggestions_task.delay")
@ -77,10 +71,7 @@ class UserAdminViews(TestCase):
result = view(request, self.local_user.id)
self.assertIsInstance(result, TemplateResponse)
html = result.render()
_, errors = tidy_document(html.content, options={"drop-empty-elements": False})
if errors:
raise Exception(errors)
validate_html(result.render())
self.assertEqual(
list(self.local_user.groups.values_list("name", flat=True)), ["editor"]

View file

@ -2,7 +2,6 @@
from io import BytesIO
import pathlib
from unittest.mock import patch
from tidylib import tidy_document
from PIL import Image
import responses
@ -18,6 +17,7 @@ from django.utils import timezone
from bookwyrm import forms, models, views
from bookwyrm.activitypub import ActivitypubResponse
from bookwyrm.tests.validate_html import validate_html
class BookViews(TestCase):
@ -68,22 +68,8 @@ class BookViews(TestCase):
is_api.return_value = False
result = view(request, self.book.id)
self.assertIsInstance(result, TemplateResponse)
html = result.render()
_, errors = tidy_document(
html.content,
options={
"drop-empty-elements": False,
"warn-proprietary-attributes": False,
},
)
# idk how else to filter out these unescape amp errs
errors = "\n".join(
e
for e in errors.split("\n")
if "&book" not in e and "id and name attribute" not in e
)
if errors:
raise Exception(errors)
validate_html(result.render())
self.assertEqual(result.status_code, 200)
with patch("bookwyrm.views.books.books.is_api_request") as is_api:
@ -123,21 +109,8 @@ class BookViews(TestCase):
is_api.return_value = False
result = view(request, self.book.id, user_statuses="review")
self.assertIsInstance(result, TemplateResponse)
html = result.render()
_, errors = tidy_document(
html.content,
options={
"drop-empty-elements": False,
"warn-proprietary-attributes": False,
},
)
errors = "\n".join(
e
for e in errors.split("\n")
if "&book" not in e and "id and name attribute" not in e
)
if errors:
raise Exception(errors)
validate_html(result.render())
self.assertEqual(result.status_code, 200)
self.assertEqual(result.context_data["statuses"].object_list[0], review)
@ -145,21 +118,7 @@ class BookViews(TestCase):
is_api.return_value = False
result = view(request, self.book.id, user_statuses="comment")
self.assertIsInstance(result, TemplateResponse)
html = result.render()
_, errors = tidy_document(
html.content,
options={
"drop-empty-elements": False,
"warn-proprietary-attributes": False,
},
)
errors = "\n".join(
e
for e in errors.split("\n")
if "&book" not in e and "id and name attribute" not in e
)
if errors:
raise Exception(errors)
validate_html(result.render())
self.assertEqual(result.status_code, 200)
self.assertEqual(result.context_data["statuses"].object_list[0], comment)
@ -167,21 +126,7 @@ class BookViews(TestCase):
is_api.return_value = False
result = view(request, self.book.id, user_statuses="quotation")
self.assertIsInstance(result, TemplateResponse)
html = result.render()
_, errors = tidy_document(
html.content,
options={
"drop-empty-elements": False,
"warn-proprietary-attributes": False,
},
)
errors = "\n".join(
e
for e in errors.split("\n")
if "&book" not in e and "id and name attribute" not in e
)
if errors:
raise Exception(errors)
validate_html(result.render())
self.assertEqual(result.status_code, 200)
self.assertEqual(result.context_data["statuses"].object_list[0], quote)
@ -203,21 +148,7 @@ class BookViews(TestCase):
with patch("bookwyrm.views.books.books.is_api_request") as is_api:
is_api.return_value = False
result = view(request, self.work.id)
html = result.render()
_, errors = tidy_document(
html.content,
options={
"drop-empty-elements": False,
"warn-proprietary-attributes": False,
},
)
errors = "\n".join(
e
for e in errors.split("\n")
if "&book" not in e and "id and name attribute" not in e
)
if errors:
raise Exception(errors)
validate_html(result.render())
self.assertEqual(result.status_code, 200)
self.assertEqual(result.context_data["book"], self.book)
@ -229,21 +160,7 @@ class BookViews(TestCase):
request.user.is_superuser = True
result = view(request, self.book.id)
self.assertIsInstance(result, TemplateResponse)
html = result.render()
_, errors = tidy_document(
html.content,
options={
"drop-empty-elements": False,
"warn-proprietary-attributes": False,
},
)
errors = "\n".join(
e
for e in errors.split("\n")
if "&book" not in e and "id and name attribute" not in e
)
if errors:
raise Exception(errors)
validate_html(result.render())
self.assertEqual(result.status_code, 200)
def test_edit_book(self):
@ -274,16 +191,7 @@ class BookViews(TestCase):
request.user = self.local_user
result = view(request, self.book.id)
html = result.render()
_, errors = tidy_document(
html.content,
options={
"drop-empty-elements": False,
"warn-proprietary-attributes": False,
},
)
if errors:
raise Exception(errors)
validate_html(result.render())
# the changes haven't been saved yet
self.book.refresh_from_db()

View file

@ -1,6 +1,5 @@
""" test for app action functionality """
from unittest.mock import patch
from tidylib import tidy_document
from django.template.response import TemplateResponse
from django.test import TestCase
@ -8,6 +7,7 @@ from django.test.client import RequestFactory
from bookwyrm import models, views
from bookwyrm.activitypub import ActivitypubResponse
from bookwyrm.tests.validate_html import validate_html
class BookViews(TestCase):
@ -45,16 +45,7 @@ class BookViews(TestCase):
is_api.return_value = False
result = view(request, self.work.id)
self.assertIsInstance(result, TemplateResponse)
html = result.render()
_, errors = tidy_document(
html.content,
options={
"drop-empty-elements": False,
"warn-proprietary-attributes": False,
},
)
if errors:
raise Exception(errors)
validate_html(result.render())
self.assertEqual(result.status_code, 200)
self.assertTrue("paperback" in result.context_data["formats"])
@ -71,16 +62,7 @@ class BookViews(TestCase):
is_api.return_value = False
result = view(request, self.work.id)
self.assertIsInstance(result, TemplateResponse)
html = result.render()
_, errors = tidy_document(
html.content,
options={
"drop-empty-elements": False,
"warn-proprietary-attributes": False,
},
)
if errors:
raise Exception(errors)
validate_html(result.render())
self.assertEqual(result.status_code, 200)
self.assertEqual(len(result.context_data["editions"].object_list), 2)
self.assertEqual(len(result.context_data["formats"]), 2)
@ -91,16 +73,7 @@ class BookViews(TestCase):
with patch("bookwyrm.views.editions.is_api_request") as is_api:
is_api.return_value = False
result = view(request, self.work.id)
html = result.render()
_, errors = tidy_document(
html.content,
options={
"drop-empty-elements": False,
"warn-proprietary-attributes": False,
},
)
if errors:
raise Exception(errors)
validate_html(result.render())
self.assertEqual(result.status_code, 200)
self.assertEqual(len(result.context_data["editions"].object_list), 1)
@ -108,16 +81,7 @@ class BookViews(TestCase):
with patch("bookwyrm.views.editions.is_api_request") as is_api:
is_api.return_value = False
result = view(request, self.work.id)
html = result.render()
_, errors = tidy_document(
html.content,
options={
"drop-empty-elements": False,
"warn-proprietary-attributes": False,
},
)
if errors:
raise Exception(errors)
validate_html(result.render())
self.assertEqual(result.status_code, 200)
self.assertEqual(len(result.context_data["editions"].object_list), 1)
@ -125,16 +89,7 @@ class BookViews(TestCase):
with patch("bookwyrm.views.editions.is_api_request") as is_api:
is_api.return_value = False
result = view(request, self.work.id)
html = result.render()
_, errors = tidy_document(
html.content,
options={
"drop-empty-elements": False,
"warn-proprietary-attributes": False,
},
)
if errors:
raise Exception(errors)
validate_html(result.render())
self.assertEqual(result.status_code, 200)
self.assertEqual(len(result.context_data["editions"].object_list), 1)

View file

@ -1,12 +1,12 @@
""" test for app action functionality """
from unittest.mock import patch
from tidylib import tidy_document
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
@patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay")
@ -46,10 +46,7 @@ class BlockViews(TestCase):
request.user = self.local_user
result = view(request)
self.assertIsInstance(result, TemplateResponse)
html = result.render()
_, errors = tidy_document(html.content)
if errors:
raise Exception(errors)
validate_html(result.render())
self.assertEqual(result.status_code, 200)
def test_block_post(self, _):

View file

@ -1,12 +1,12 @@
""" test for app action functionality """
from unittest.mock import patch
from tidylib import tidy_document
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 ChangePasswordViews(TestCase):
@ -35,10 +35,7 @@ class ChangePasswordViews(TestCase):
result = view(request)
self.assertIsInstance(result, TemplateResponse)
html = result.render()
_, errors = tidy_document(html.content)
if errors:
raise Exception(errors)
validate_html(result.render())
self.assertEqual(result.status_code, 200)
def test_password_change(self):

View file

@ -1,7 +1,6 @@
""" test for app action functionality """
import json
from unittest.mock import patch
from tidylib import tidy_document
from django.contrib.sessions.middleware import SessionMiddleware
from django.template.response import TemplateResponse
@ -9,6 +8,7 @@ from django.test import TestCase
from django.test.client import RequestFactory
from bookwyrm import forms, models, views
from bookwyrm.tests.validate_html import validate_html
@patch("bookwyrm.suggested_users.remove_user_task.delay")
@ -53,10 +53,7 @@ class DeleteUserViews(TestCase):
request.user = self.local_user
result = view(request)
self.assertIsInstance(result, TemplateResponse)
html = result.render()
_, errors = tidy_document(html.content)
if errors:
raise Exception(errors)
validate_html(result.render())
self.assertEqual(result.status_code, 200)
@patch("bookwyrm.suggested_users.rerank_suggestions_task")

View file

@ -2,7 +2,6 @@
import pathlib
from unittest.mock import patch
from PIL import Image
from tidylib import tidy_document
from django.contrib.auth.models import AnonymousUser
from django.core.files.base import ContentFile
@ -12,6 +11,7 @@ from django.test import TestCase
from django.test.client import RequestFactory
from bookwyrm import forms, models, views
from bookwyrm.tests.validate_html import validate_html
@patch("bookwyrm.suggested_users.remove_user_task.delay")
@ -58,10 +58,7 @@ class EditUserViews(TestCase):
request.user = self.local_user
result = view(request)
self.assertIsInstance(result, TemplateResponse)
html = result.render()
_, errors = tidy_document(html.content)
if errors:
raise Exception(errors)
validate_html(result.render())
self.assertEqual(result.status_code, 200)
def test_edit_user(self, _):

View file

@ -1,6 +1,5 @@
""" test for app action functionality """
from unittest.mock import patch
from tidylib import tidy_document
from django.contrib.auth.models import AnonymousUser
from django.template.response import TemplateResponse
@ -8,6 +7,7 @@ from django.test import TestCase
from django.test.client import RequestFactory
from bookwyrm import models, views
from bookwyrm.tests.validate_html import validate_html
# pylint: disable=unused-argument
class DirectoryViews(TestCase):
@ -52,16 +52,7 @@ class DirectoryViews(TestCase):
result = view(request)
self.assertIsInstance(result, TemplateResponse)
html = result.render()
_, errors = tidy_document(
html.content,
options={
"drop-empty-elements": False,
"warn-proprietary-attributes": False,
},
)
if errors:
raise Exception(errors)
validate_html(result.render())
self.assertEqual(result.status_code, 200)
def test_directory_page_empty(self):
@ -72,10 +63,7 @@ class DirectoryViews(TestCase):
result = view(request)
self.assertIsInstance(result, TemplateResponse)
html = result.render()
_, errors = tidy_document(html.content, options={"drop-empty-elements": False})
if errors:
raise Exception(errors)
validate_html(result.render())
self.assertEqual(result.status_code, 200)
def test_directory_page_logged_out(self):

View file

@ -1,6 +1,5 @@
""" test for app action functionality """
from unittest.mock import patch
from tidylib import tidy_document
from django.contrib.auth.models import AnonymousUser
from django.http import Http404
@ -10,6 +9,7 @@ from django.test.client import RequestFactory
from django.utils import timezone
from bookwyrm import models, views
from bookwyrm.tests.validate_html import validate_html
class GoalViews(TestCase):
@ -62,16 +62,7 @@ class GoalViews(TestCase):
request.user = self.local_user
result = view(request, self.local_user.localname, self.year)
html = result.render()
_, errors = tidy_document(
html.content,
options={
"drop-empty-elements": False,
"warn-proprietary-attributes": False,
},
)
if errors:
raise Exception(errors)
validate_html(result.render())
self.assertIsInstance(result, TemplateResponse)
def test_goal_page_anonymous(self):
@ -102,16 +93,7 @@ class GoalViews(TestCase):
request.user = self.rat
result = view(request, self.local_user.localname, timezone.now().year)
html = result.render()
_, errors = tidy_document(
html.content,
options={
"drop-empty-elements": False,
"warn-proprietary-attributes": False,
},
)
if errors:
raise Exception(errors)
validate_html(result.render())
self.assertIsInstance(result, TemplateResponse)
def test_goal_page_private(self):

View file

@ -1,7 +1,6 @@
""" test for app action functionality """
import json
from unittest.mock import patch
from tidylib import tidy_document
from django.core.exceptions import PermissionDenied
from django.template.response import TemplateResponse
@ -10,6 +9,7 @@ from django.test.client import RequestFactory
from bookwyrm import forms, models, views
from bookwyrm.activitypub import ActivitypubResponse
from bookwyrm.tests.validate_html import validate_html
@patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay")
@ -56,16 +56,7 @@ class ShelfViews(TestCase):
is_api.return_value = False
result = view(request, self.local_user.username, shelf.identifier)
self.assertIsInstance(result, TemplateResponse)
html = result.render()
_, errors = tidy_document(
html.content,
options={
"drop-empty-elements": False,
"warn-proprietary-attributes": False,
},
)
if errors:
raise Exception(errors)
validate_html(result.render())
self.assertEqual(result.status_code, 200)
with patch("bookwyrm.views.shelf.is_api_request") as is_api:

View file

@ -1,6 +1,5 @@
""" test for app action functionality """
from unittest.mock import patch
from tidylib import tidy_document
from django.contrib.auth.models import AnonymousUser
from django.http.response import Http404
@ -10,6 +9,7 @@ from django.test.client import RequestFactory
from bookwyrm import models, views
from bookwyrm.activitypub import ActivitypubResponse
from bookwyrm.tests.validate_html import validate_html
class UserViews(TestCase):
@ -56,16 +56,7 @@ class UserViews(TestCase):
is_api.return_value = False
result = view(request, "mouse")
self.assertIsInstance(result, TemplateResponse)
html = result.render()
_, errors = tidy_document(
html.content,
options={
"drop-empty-elements": False,
"warn-proprietary-attributes": False,
},
)
if errors:
raise Exception(errors)
validate_html(result.render())
self.assertEqual(result.status_code, 200)
request.user = self.anonymous_user
@ -73,16 +64,7 @@ class UserViews(TestCase):
is_api.return_value = False
result = view(request, "mouse")
self.assertIsInstance(result, TemplateResponse)
html = result.render()
_, errors = tidy_document(
html.content,
options={
"drop-empty-elements": False,
"warn-proprietary-attributes": False,
},
)
if errors:
raise Exception(errors)
validate_html(result.render())
self.assertEqual(result.status_code, 200)
with patch("bookwyrm.views.user.is_api_request") as is_api:
@ -111,16 +93,7 @@ class UserViews(TestCase):
is_api.return_value = False
result = view(request, "mouse")
self.assertIsInstance(result, TemplateResponse)
html = result.render()
_, errors = tidy_document(
html.content,
options={
"drop-empty-elements": False,
"warn-proprietary-attributes": False,
},
)
if errors:
raise Exception(errors)
validate_html(result.render())
self.assertEqual(result.status_code, 200)
with patch("bookwyrm.views.user.is_api_request") as is_api:
@ -151,16 +124,7 @@ class UserViews(TestCase):
is_api.return_value = False
result = view(request, "mouse")
self.assertIsInstance(result, TemplateResponse)
html = result.render()
_, errors = tidy_document(
html.content,
options={
"drop-empty-elements": False,
"warn-proprietary-attributes": False,
},
)
if errors:
raise Exception(errors)
validate_html(result.render())
self.assertEqual(result.status_code, 200)
with patch("bookwyrm.views.user.is_api_request") as is_api: