moviewyrm/bookwyrm/tests/test_sanitize_html.py

63 lines
2.1 KiB
Python
Raw Normal View History

2021-03-08 16:49:10 +00:00
""" make sure only valid html gets to the app """
2020-05-10 02:48:30 +00:00
from django.test import TestCase
from bookwyrm.sanitize_html import InputHtmlParser
2020-05-10 02:48:30 +00:00
2021-03-08 16:49:10 +00:00
2020-05-10 02:48:30 +00:00
class Sanitizer(TestCase):
2021-04-26 16:15:42 +00:00
"""sanitizer tests"""
2021-03-08 16:49:10 +00:00
2020-05-10 02:48:30 +00:00
def test_no_html(self):
2021-04-26 16:15:42 +00:00
"""just text"""
2021-03-08 16:49:10 +00:00
input_text = "no html "
2020-05-10 02:48:30 +00:00
parser = InputHtmlParser()
parser.feed(input_text)
output = parser.get_output()
self.assertEqual(input_text, output)
def test_valid_html(self):
2021-04-26 16:15:42 +00:00
"""leave the html untouched"""
2021-03-08 16:49:10 +00:00
input_text = "<b>yes </b> <i>html</i>"
2020-05-10 02:48:30 +00:00
parser = InputHtmlParser()
parser.feed(input_text)
output = parser.get_output()
self.assertEqual(input_text, output)
def test_valid_html_attrs(self):
2022-02-03 21:15:06 +00:00
"""and don't remove useful attributes"""
2020-05-10 02:48:30 +00:00
input_text = '<a href="fish.com">yes </a> <i>html</i>'
parser = InputHtmlParser()
parser.feed(input_text)
output = parser.get_output()
self.assertEqual(input_text, output)
2022-02-03 21:15:06 +00:00
def test_valid_html_invalid_attrs(self):
"""do remove un-approved attributes"""
input_text = '<a href="fish.com" fish="hello">yes </a> <i>html</i>'
parser = InputHtmlParser()
parser.feed(input_text)
output = parser.get_output()
2022-02-03 21:19:56 +00:00
self.assertEqual(output, '<a href="fish.com">yes </a> <i>html</i>')
2022-02-03 21:15:06 +00:00
2020-05-10 02:48:30 +00:00
def test_invalid_html(self):
2021-04-26 16:15:42 +00:00
"""remove all html when the html is malformed"""
2021-03-08 16:49:10 +00:00
input_text = "<b>yes <i>html</i>"
2020-05-10 02:48:30 +00:00
parser = InputHtmlParser()
parser.feed(input_text)
output = parser.get_output()
2021-03-08 16:49:10 +00:00
self.assertEqual("yes html", output)
2020-05-10 02:48:30 +00:00
2021-03-08 16:49:10 +00:00
input_text = "yes <i></b>html </i>"
2020-05-10 02:48:30 +00:00
parser = InputHtmlParser()
parser.feed(input_text)
output = parser.get_output()
2021-03-08 16:49:10 +00:00
self.assertEqual("yes html ", output)
2020-05-10 02:48:30 +00:00
def test_disallowed_html(self):
2021-04-26 16:15:42 +00:00
"""remove disallowed html but keep allowed html"""
2021-03-08 16:49:10 +00:00
input_text = "<div> yes <i>html</i></div>"
2020-05-10 02:48:30 +00:00
parser = InputHtmlParser()
parser.feed(input_text)
output = parser.get_output()
2021-03-08 16:49:10 +00:00
self.assertEqual(" yes <i>html</i>", output)