-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtest_html.py
195 lines (166 loc) · 6.77 KB
/
test_html.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
from unittest.mock import patch, MagicMock
from cms.api import create_page, add_plugin
from cms.test_utils.testcases import CMSTestCase
from django.test import TestCase
from lxml.etree import Element
from djangocms_text import html, settings
from djangocms_text.html import dynamic_href, dynamic_src, render_dynamic_attributes
from tests.fixtures import DJANGO_CMS4, TestFixture
class HtmlSanitizerAdditionalProtocolsTests:
def test_default_tag_escaping(self):
settings.TEXT_ADDITIONAL_TAGS = []
text = html.clean_html(
'<iframe src="rtmp://testurl.com/"></iframe>',
full=False,
)
self.assertEqual(
'<iframe src="rtmp://testurl.com/"></iframe>',
text,
)
def test_custom_tag_enabled(self):
settings.TEXT_ADDITIONAL_TAGS = ["iframe"]
text = html.clean_html(
'<iframe src="rtmp://testurl.com/"></iframe>',
full=False,
)
self.assertEqual(
'<iframe src="rtmp://testurl.com/"></iframe>',
text,
)
def test_default_attribute_escaping(self):
settings.TEXT_ADDITIONAL_ATTRIBUTES = []
text = html.clean_html(
'<span test-attr="2">foo</span>',
full=False,
)
self.assertEqual(
"<span>foo</span>",
text,
)
def test_custom_attribute_enabled(self):
settings.TEXT_ADDITIONAL_ATTRIBUTES = ["test-attr"]
text = html.clean_html(
'<span test-attr="2">foo</span>',
full=False,
)
self.assertEqual(
'<span test-attr="2">foo</span>',
text,
)
def test_default_protocol_escaping(self):
settings.TEXT_ADDITIONAL_PROTOCOLS = []
text = html.clean_html(
'<source src="rtmp://testurl.com/">',
full=False,
)
self.assertEqual("<source>", text)
def test_custom_protocol_enabled(self):
settings.TEXT_ADDITIONAL_PROTOCOLS = ["rtmp"]
text = html.clean_html(
'<source src="rtmp://testurl.com/">',
full=False,
)
self.assertEqual('<source src="rtmp://testurl.com/">', text)
def test_clean_html_with_sanitize_enabled(self):
old_text_html_sanitize = settings.TEXT_HTML_SANITIZE
settings.TEXT_HTML_SANITIZE = True
original = '<span test-attr="2">foo</span>'
cleaned = html.clean_html(
original,
full=False,
)
try:
self.assertHTMLEqual("<span>foo</span>", cleaned)
finally:
settings.TEXT_HTML_SANITIZE = old_text_html_sanitize
def test_clean_html_with_sanitize_disabled(self):
old_text_html_sanitize = settings.TEXT_HTML_SANITIZE
settings.TEXT_HTML_SANITIZE = False
original = '<span test-attr="2">foo</span>'
cleaned = html.clean_html(
original,
full=False,
)
try:
self.assertHTMLEqual(original, cleaned)
finally:
settings.TEXT_HTML_SANITIZE = old_text_html_sanitize
class HTMLDynamicAttriutesTest(TestFixture, CMSTestCase):
def test_dynamic_link(self):
page = self.create_page("page", "page.html", language="en")
self.publish(page, "en")
self.assertEqual(
page.get_absolute_url(),
"/en/page/",
)
dynamic_html = f'<a data-cms-href="cms.page:{page.pk}">Link</a>'
result = render_dynamic_attributes(dynamic_html)
self.assertEqual(
result,
f'<a href="{page.get_absolute_url()}">Link</a>',
)
def test_invalid_dynamic_link(self):
page = self.create_page("page", "page.html", language="en")
self.publish(page, "en")
self.assertEqual(
page.get_absolute_url(),
"/en/page/",
)
dynamic_html = '<a data-cms-href="cms.page:0">Link</a>'
result = render_dynamic_attributes(dynamic_html)
self.assertEqual(
result,
'<span data-cms-error="ref-not-found">Link</span>',
)
class DynamicAttributesTestCase(TestCase):
@patch("djangocms_text.html.apps.get_model")
def test_dynamic_href_sets_correct_attribute(self, mock_get_model):
mock_obj = MagicMock()
mock_obj.get_absolute_url.return_value = "/test-url"
mock_get_model.return_value.objects.filter.return_value = [mock_obj]
elem = Element("a", {"data-cms-href": "app.Model:1"})
dynamic_href(elem, mock_obj, "href")
self.assertEqual(elem.attrib["href"], "/test-url")
@patch("djangocms_text.html.apps.get_model")
def test_dynamic_src_sets_correct_attribute(self, mock_get_model):
mock_obj = MagicMock()
mock_obj.get_absolute_url.return_value = "/test-url"
mock_get_model.return_value.objects.filter.return_value = [mock_obj]
elem = Element("img", {"data-cms-src": "app.Model:1"})
dynamic_src(elem, mock_obj, "src")
self.assertEqual(elem.attrib["src"], "/test-url")
def test_render_dynamic_attributes_changes_html(self):
page = create_page("page", "page.html", language="en")
html = f'<a data-cms-href="cms.page:{page.pk}">Link</a>'
updated_html = render_dynamic_attributes(html)
self.assertIn('<a href="/en/page/">Link</a>', updated_html)
def test_render_dynamic_attributes_fails(self):
html = '<a data-cms-href="app.model:1">Link</a>'
updated_html = render_dynamic_attributes(html)
self.assertIn('<span data-cms-error="ref-not-found">Link</span>', updated_html)
def test_render_dynamic_attributes_handles_no_dynamic_attributes(self):
html = "<p>No dynamic attributes</p>"
updated_html = render_dynamic_attributes(html)
self.assertEqual(html, updated_html)
def save_image(filename, image, parent_plugin, width, height):
pass
class DjangoCMSPictureIntegrationTestCase(CMSTestCase):
def setUp(self):
super().setUp()
self.home = self.create_homepage("home", "page.html", "en")
if DJANGO_CMS4:
self.placeholder = (
self.home.pagecontent_set(manager="admin_manager").first().get_placeholders().get(slot="content")
)
else:
self.placeholder = self.home.get_placeholders().get(slot="content")
def test_extract_images(self):
with patch("tests.test_html.save_image") as mock_save_image:
add_plugin(
self.placeholder,
"TextPlugin",
"en",
body='<img src="https://imageworld.org/"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42m'
'P8z/C/HgAGgwJ/lK3Q6wAAAABJRU5ErkJggg==">',
)
mock_save_image.assert_called_once()