moviewyrm/bookwyrm/tests/test_sanitize_html.py

53 lines
1.8 KiB
Python
Raw Normal View History

2020-12-17 00:47:05 +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
class Sanitizer(TestCase):
2020-12-17 00:47:05 +00:00
''' sanitizer tests '''
2020-05-10 02:48:30 +00:00
def test_no_html(self):
2020-12-17 00:47:05 +00:00
''' just text '''
2020-05-10 02:48:30 +00:00
input_text = 'no html '
parser = InputHtmlParser()
parser.feed(input_text)
output = parser.get_output()
self.assertEqual(input_text, output)
def test_valid_html(self):
2020-12-17 00:47:05 +00:00
''' leave the html untouched '''
2020-05-10 02:48:30 +00:00
input_text = '<b>yes </b> <i>html</i>'
parser = InputHtmlParser()
parser.feed(input_text)
output = parser.get_output()
self.assertEqual(input_text, output)
def test_valid_html_attrs(self):
2020-12-17 00:47:05 +00:00
''' and don't remove 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)
def test_invalid_html(self):
2020-12-17 00:47:05 +00:00
''' remove all html when the html is malformed '''
2020-05-10 02:48:30 +00:00
input_text = '<b>yes <i>html</i>'
parser = InputHtmlParser()
parser.feed(input_text)
output = parser.get_output()
self.assertEqual('yes html', output)
input_text = 'yes <i></b>html </i>'
parser = InputHtmlParser()
parser.feed(input_text)
output = parser.get_output()
self.assertEqual('yes html ', output)
def test_disallowed_html(self):
2020-12-17 00:47:05 +00:00
''' remove disallowed html but keep allowed html '''
2020-05-10 02:48:30 +00:00
input_text = '<div> yes <i>html</i></div>'
parser = InputHtmlParser()
parser.feed(input_text)
output = parser.get_output()
self.assertEqual(' yes <i>html</i>', output)