Adds unit tests that checks if rel field is set on links

This commit is contained in:
Mouse Reeve 2022-08-05 11:00:43 -07:00
parent 4dd7f88e0a
commit 1ba69acc49
2 changed files with 32 additions and 1 deletions

View file

@ -1,4 +1,5 @@
""" html validation on rendered templates """
from html.parser import HTMLParser
from tidylib import tidy_document
@ -23,3 +24,33 @@ def validate_html(html):
)
if errors:
raise Exception(errors)
validator = HtmlValidator()
# will raise exceptions
validator.feed(str(html.content))
raise Exception()
class HtmlValidator(HTMLParser): # pylint: disable=abstract-method
"""Checks for custom html validation requirements"""
def __init__(self):
HTMLParser.__init__(self)
def handle_starttag(self, tag, attrs):
"""check if the tag is valid"""
# filter out everything besides links that open in new tabs
if tag != "a" or ("target", "_blank") not in attrs:
return
for attr, value in attrs:
if (
attr == "rel"
and "nofollow" in value
and "noopener" in value
and "noreferrer" in value
):
return
raise Exception(
'Links to a new tab must have rel="nofollow noopener noreferrer"'
)

View file

@ -13,7 +13,7 @@ from bookwyrm.tests.validate_html import validate_html
class LandingViews(TestCase):
"""pages you land on without really trying"""
def setUp(self):
def setUp(self): # pylint: disable=invalid-name
"""we need basic test data and mocks"""
self.factory = RequestFactory()
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(