mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-11-27 12:01:14 +00:00
Adds unit tests that checks if rel field is set on links
This commit is contained in:
parent
4dd7f88e0a
commit
1ba69acc49
2 changed files with 32 additions and 1 deletions
|
@ -1,4 +1,5 @@
|
||||||
""" html validation on rendered templates """
|
""" html validation on rendered templates """
|
||||||
|
from html.parser import HTMLParser
|
||||||
from tidylib import tidy_document
|
from tidylib import tidy_document
|
||||||
|
|
||||||
|
|
||||||
|
@ -23,3 +24,33 @@ def validate_html(html):
|
||||||
)
|
)
|
||||||
if errors:
|
if errors:
|
||||||
raise Exception(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"'
|
||||||
|
)
|
||||||
|
|
|
@ -13,7 +13,7 @@ from bookwyrm.tests.validate_html import validate_html
|
||||||
class LandingViews(TestCase):
|
class LandingViews(TestCase):
|
||||||
"""pages you land on without really trying"""
|
"""pages you land on without really trying"""
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self): # pylint: disable=invalid-name
|
||||||
"""we need basic test data and mocks"""
|
"""we need basic test data and mocks"""
|
||||||
self.factory = RequestFactory()
|
self.factory = RequestFactory()
|
||||||
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
|
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
|
||||||
|
|
Loading…
Reference in a new issue