Skip to content

Commit

Permalink
Improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
fsbraun committed Sep 29, 2024
1 parent 5a83718 commit a1e1645
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 13 deletions.
4 changes: 2 additions & 2 deletions djangocms_text_ckeditor/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from djangocms_text.fields import HTMLFormField as TextHTMLFormField


class HTMLField(TextHTMLField):
class HTMLField(TextHTMLField): # pragma: no cover
def __init__(self, *args: Any, **kwargs: Any) -> None:
warnings.warn(
"djangocms_text_ckeditor.fields.HTMLField is deprecated. "
Expand All @@ -16,7 +16,7 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
super().__init__(*args, **kwargs)


class HTMLFormField(TextHTMLFormField):
class HTMLFormField(TextHTMLFormField): # pragma: no cover
def __init__(self, *args: Any, **kwargs: Any) -> None:
warnings.warn(
"djangocms_text_ckeditor.fields.HTMLFormField is deprecated. "
Expand Down
2 changes: 1 addition & 1 deletion djangocms_text_ckeditor/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from djangocms_text.widgets import TextEditorWidget as NewTextEditorWidget


class TextEditorWidget(NewTextEditorWidget):
class TextEditorWidget(NewTextEditorWidget): # pragma: no cover
def __init__(self, *args: Any, **kwargs: Any) -> None:
warnings.warn(
"djangocms_text_ckeditor.widgets.TextEditorWidget is deprecated. "
Expand Down
14 changes: 6 additions & 8 deletions tests/test_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ class FieldTestCase(BaseTestCase):
'<iframe src="http://www.w3schools.com"></iframe>')
text_with_iframe_escaped = ('<p>some non malicious text</p>&lt;iframe '
'src="http://www.w3schools.com"&gt;&lt;/iframe&gt;')
text_with_script = ('<p>some non malicious text</p> '
text_with_script = ('<p>some non malicious text</p>'
'<script>alert("Hello! I am an alert box!");</script>')
text_with_script_escaped = ('<p>some non malicious text</p> &lt;script&gt;'
text_with_script_escaped = ('<p>some non malicious text</p>&lt;script&gt;'
'alert("Hello! I am an alert box!");&lt;/script&gt;')

def test_model_field_text_is_safe(self):
Expand All @@ -39,7 +39,6 @@ def test_model_field_text_is_safe(self):
rendered = template.render(Context({'obj': text}))
self.assertEqual(original, rendered)

@skipIf(True, "sanitizer deactivated")
def test_model_field_sanitized(self):
obj = SimpleText(text=self.text_normal)
obj.full_clean()
Expand All @@ -53,15 +52,14 @@ def test_model_field_sanitized(self):
obj.full_clean()
obj.save()

self.assertEqual(obj.text, self.text_with_iframe_escaped)
self.assertEqual(obj.text, self.text_normal)

obj = SimpleText(text=self.text_with_script)
obj.full_clean()
obj.save()

self.assertEqual(obj.text, self.text_with_script_escaped)
self.assertEqual(obj.text, self.text_normal)

@skipIf(True, "sanitizer deactivated")
def test_form_field_sanitized(self):
form = SimpleTextForm(data={'text': self.text_normal})
self.assertTrue(form.is_valid())
Expand All @@ -71,9 +69,9 @@ def test_form_field_sanitized(self):
form = SimpleTextForm(data={'text': self.text_with_iframe})
self.assertTrue(form.is_valid())

self.assertEqual(form.cleaned_data['text'], self.text_with_iframe_escaped)
self.assertEqual(form.cleaned_data['text'], self.text_normal)

form = SimpleTextForm(data={'text': self.text_with_script})
self.assertTrue(form.is_valid())

self.assertEqual(form.cleaned_data['text'], self.text_with_script_escaped)
self.assertEqual(form.cleaned_data['text'], self.text_normal)
37 changes: 36 additions & 1 deletion tests/test_html.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

from cms.api import create_page
from cms.test_utils.testcases import CMSTestCase

from djangocms_text import html, settings
from djangocms_text.html import render_dynamic_attributes
from tests.fixtures import TestFixture


class HtmlSanitizerAdditionalProtocolsTests:
Expand Down Expand Up @@ -92,3 +95,35 @@ def test_clean_html_with_sanitize_disabled(self):
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 = f'<a data-cms-href="cms.page:{page.pk + 1}">Link</a>'

result = render_dynamic_attributes(dynamic_html)
self.assertEqual(
result,
'<span data-cms-error="ref-not-found">Link</span>',
)
1 change: 0 additions & 1 deletion tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,6 @@ def test_plugin_tags_to_id_list(self):
for markup, expected in pairs:
self.assertEqual(plugin_tags_to_id_list(markup), expected)

@skipIf(True, "sanitizer deactivated")
def test_text_plugin_xss(self):
page = self.create_page('test page', template='page.html', language='en')
placeholder = self.get_placeholders(page, 'en').get(slot='content')
Expand Down

0 comments on commit a1e1645

Please sign in to comment.