Skip to content

Commit

Permalink
Fix format errors
Browse files Browse the repository at this point in the history
  • Loading branch information
fsbraun committed Nov 26, 2024
1 parent 9ece90b commit 0c8cb5a
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 96 deletions.
3 changes: 2 additions & 1 deletion djangocms_text/editors.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Iterable, Optional
from typing import Optional
from collections.abc import Iterable

from django.conf import settings
from django.core.serializers.json import DjangoJSONEncoder
Expand Down
24 changes: 15 additions & 9 deletions tests/test_app/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,21 @@ class ToppingInlineAdmin(admin.TabularInline):

class PizzaAdmin(admin.ModelAdmin):
fieldsets = (
('', {
'fields': ('description',),
}),
('Advanced', {
# NOTE: Disabled because when PizzaAdmin uses a collapsed
# class then the order of javascript libs is incorrect.
# 'classes': ('collapse',),
'fields': ('allergens',),
}),
(
"",
{
"fields": ("description",),
},
),
(
"Advanced",
{
# NOTE: Disabled because when PizzaAdmin uses a collapsed
# class then the order of javascript libs is incorrect.
# 'classes': ('collapse',),
"fields": ("allergens",),
},
),
)
inlines = [ToppingInlineAdmin]

Expand Down
22 changes: 11 additions & 11 deletions tests/test_app/cms_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ class PreviewDisabledPlugin(CMSPluginBase):
text_editor_preview = False

def get_render_template(self, context, instance, placeholder):
template = '<span>Preview is disabled for this plugin</span>'
return engines['django'].from_string(template)
template = "<span>Preview is disabled for this plugin</span>"
return engines["django"].from_string(template)


@plugin_pool.register_plugin
class SekizaiPlugin(CMSPluginBase):
name = 'Sekizai'
render_template = 'test_app/plugin_with_sekizai.html'
name = "Sekizai"
render_template = "test_app/plugin_with_sekizai.html"


@plugin_pool.register_plugin
class ExtendedTextPlugin(TextPlugin):
name = 'Extended'
name = "Extended"


@plugin_pool.register_plugin
Expand All @@ -43,12 +43,12 @@ class DummySpacerPlugin(CMSPluginBase):

@plugin_pool.register_plugin
class DummyParentPlugin(CMSPluginBase):
render_template = 'test_app/dummy_parent_plugin.html'
render_template = "test_app/dummy_parent_plugin.html"
model = DummyLink
allow_children = True

_ckeditor_body_class = 'parent-plugin-css-class'
_ckeditor_body_class_label_trigger = 'parent link label'
_ckeditor_body_class = "parent-plugin-css-class"
_ckeditor_body_class_label_trigger = "parent link label"

@classmethod
def get_child_ckeditor_body_css_class(cls, plugin: CMSPlugin) -> str:
Expand All @@ -57,11 +57,11 @@ def get_child_ckeditor_body_css_class(cls, plugin: CMSPlugin) -> str:
if plugin_instance.label == cls._ckeditor_body_class_label_trigger:
return cls._ckeditor_body_class
else:
return ''
return ""


@plugin_pool.register_plugin
class DummyChildPlugin(CMSPluginBase):
render_template = 'test_app/dummy_child_plugin.html'
child_ckeditor_body_css_class = 'child-plugin-css-class'
render_template = "test_app/dummy_child_plugin.html"
child_ckeditor_body_css_class = "child-plugin-css-class"
allow_children = True
4 changes: 2 additions & 2 deletions tests/test_app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ class Meta:
abstract = False

def __str__(self):
return 'dummy link object'
return "dummy link object"


class DummySpacer(CMSPlugin):
class Meta:
abstract = False

def __str__(self):
return 'dummy spacer object'
return "dummy spacer object"


class Pizza(models.Model):
Expand Down
43 changes: 20 additions & 23 deletions tests/test_field.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

from django.template import Context, Template
from django.utils.safestring import SafeData

Expand All @@ -10,32 +9,30 @@


class HtmlFieldTestCase(BaseTestCase):

def test_html_form_field(self):
html_field = HTMLFormField()
self.assertTrue(isinstance(html_field.clean('some text'), SafeData))
self.assertTrue(isinstance(html_field.clean("some text"), SafeData))


class FieldTestCase(BaseTestCase):

text_normal = '<p>some non malicious text</p>'
text_with_iframe = ('<p>some non malicious text</p>'
'<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>'
'<script>alert("Hello! I am an alert box!");</script>')
text_with_script_escaped = ('<p>some non malicious text</p>&lt;script&gt;'
'alert("Hello! I am an alert box!");&lt;/script&gt;')
text_normal = "<p>some non malicious text</p>"
text_with_iframe = "<p>some non malicious text</p>" '<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>" '<script>alert("Hello! I am an alert box!");</script>'
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):
original = 'Hello <h2>There</h2>'
template = Template('{{ obj.text }}')
text = SimpleText.objects.create(text='Hello <h2>There</h2>')
original = "Hello <h2>There</h2>"
template = Template("{{ obj.text }}")
text = SimpleText.objects.create(text="Hello <h2>There</h2>")
# Fetching a new instance should now have the string marked
# as safe.
text = SimpleText.objects.get(pk=text.pk)
rendered = template.render(Context({'obj': text}))
rendered = template.render(Context({"obj": text}))
self.assertEqual(original, rendered)

def test_model_field_sanitized(self):
Expand All @@ -60,17 +57,17 @@ def test_model_field_sanitized(self):
self.assertEqual(obj.text, self.text_normal)

def test_form_field_sanitized(self):
form = SimpleTextForm(data={'text': self.text_normal})
form = SimpleTextForm(data={"text": self.text_normal})
self.assertTrue(form.is_valid())

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

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

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

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

self.assertEqual(form.cleaned_data['text'], self.text_normal)
self.assertEqual(form.cleaned_data["text"], self.text_normal)
17 changes: 8 additions & 9 deletions tests/test_migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,23 @@


class MigrationTestCase(TestCase):

@override_settings(MIGRATION_MODULES={})
def test_for_missing_migrations(self):
output = io.StringIO()
options = {
'interactive': False,
'dry_run': True,
'stdout': output,
'check_changes': True,
"interactive": False,
"dry_run": True,
"stdout": output,
"check_changes": True,
}

try:
call_command('makemigrations', 'djangocms_text', **options)
call_command("makemigrations", "djangocms_text", **options)
except SystemExit as e:
status_code = str(e)
else:
# the "no changes" exit code is 0
status_code = '0'
status_code = "0"

if status_code == '1':
self.fail(f'There are missing migrations:\n {output.getvalue()}')
if status_code == "1":
self.fail(f"There are missing migrations:\n {output.getvalue()}")
4 changes: 1 addition & 3 deletions tests/test_sanitizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@


class SanitizerTestCase(TestCase):

def setUp(self):
self.cms_parser = html.cms_parser

Expand All @@ -22,7 +21,6 @@ def test_sanitizer(self):
self.assertTrue('data-two="2"' in body)

def test_sanitizer_with_custom_token_parser(self):

cleaner = NH3Parser(additional_attributes={"span": {"donut"}})
body = '<span donut="yummy">some text</span>'
body = html.clean_html(body, cleaner=cleaner)
Expand All @@ -32,4 +30,4 @@ def test_sanitizer_with_custom_token_parser(self):
def test_sanitizer_without_token_parsers(self):
body = '<span data-one="1" data-two="2">some text</span>'
body = html.clean_html(body)
self.assertEqual('<span>some text</span>', body)
self.assertEqual("<span>some text</span>", body)
84 changes: 46 additions & 38 deletions tests/test_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@


class WidgetTestCase(TestFixture, BaseTestCase):

def setUp(self):
self.super_user = self._create_user('test', True, True)
self.super_user = self._create_user("test", True, True)
self.default_parser = html.cms_parser
super().setUp()

def tearDown(self):
settings.ALLOW_TOKEN_PARSERS = (
'djangocms_text.attribute_parsers.DataAttributeParser',
)
settings.ALLOW_TOKEN_PARSERS = ("djangocms_text.attribute_parsers.DataAttributeParser",)
html.cms_parser = self.default_parser

def test_sub_plugin_config(self):
page = self.create_page(title='home', template='page.html', language='en')
page = self.create_page(title="home", template="page.html", language="en")
plugin = add_plugin(
self.get_placeholders(page, 'en').get(slot='content'), 'TextPlugin', 'en', body='some text',
self.get_placeholders(page, "en").get(slot="content"),
"TextPlugin",
"en",
body="some text",
)
endpoint = self.get_change_plugin_uri(plugin)

Expand All @@ -37,47 +37,53 @@ def test_sub_plugin_config(self):
self.assertContains(response, '"title": "Image"')

def test_plugin_edit(self):
page = self.create_page(title='pagina', template='page.html', language='en')
placeholder = self.get_placeholders(page, 'en').get(slot='content')
add_plugin(placeholder, 'TextPlugin', 'en', body='Lorem ipsum')
self.publish(page, 'en')
response = self.client.get(page.get_absolute_url('en'))
self.assertContains(response, 'Lorem ipsum')
page = self.create_page(title="pagina", template="page.html", language="en")
placeholder = self.get_placeholders(page, "en").get(slot="content")
add_plugin(placeholder, "TextPlugin", "en", body="Lorem ipsum")
self.publish(page, "en")
response = self.client.get(page.get_absolute_url("en"))
self.assertContains(response, "Lorem ipsum")

def test_child_plugin(self):
page = self.create_page(title='pagina', template='page.html', language='en')
placeholder = self.get_placeholders(page, 'en').get(slot='content')
plugin = add_plugin(placeholder, 'TextPlugin', 'en', body='Lorem ipsum')
page = self.create_page(title="pagina", template="page.html", language="en")
placeholder = self.get_placeholders(page, "en").get(slot="content")
plugin = add_plugin(placeholder, "TextPlugin", "en", body="Lorem ipsum")
test_image = self.create_filer_image_object()
pic_plugin = add_plugin(
placeholder, 'PicturePlugin', 'en', target=plugin, picture=test_image,
placeholder,
"PicturePlugin",
"en",
target=plugin,
picture=test_image,
)
plugin.body = f'{plugin.body} {plugin_to_tag(pic_plugin)}'
plugin.body = f"{plugin.body} {plugin_to_tag(pic_plugin)}"
plugin.save()
self.publish(page, 'en')
response = self.client.get(page.get_absolute_url('en'))
self.assertContains(response, 'Lorem ipsum')
self.publish(page, "en")
response = self.client.get(page.get_absolute_url("en"))
self.assertContains(response, "Lorem ipsum")
self.assertContains(response, '<img src="/media/')

def test_contain_text(self):
page = self.create_page(title='home', template='page.html', language='en')
placeholder = self.get_placeholders(page, 'en').get(slot='content')
add_plugin(placeholder, 'TextPlugin', 'en', body='some text')
language = 'en'
self.publish(page, 'en')
page = self.create_page(title="home", template="page.html", language="en")
placeholder = self.get_placeholders(page, "en").get(slot="content")
add_plugin(placeholder, "TextPlugin", "en", body="some text")
language = "en"
self.publish(page, "en")
url = page.get_absolute_url(language)
response = self.client.get(url)
self.assertContains(response, 'some text')
self.assertContains(response, "some text")

def test_text_sanitizer(self):
page = self.create_page(title='home', template='page.html', language='en')
placeholder = self.get_placeholders(page, 'en').get(slot='content')
page = self.create_page(title="home", template="page.html", language="en")
placeholder = self.get_placeholders(page, "en").get(slot="content")
add_plugin(
placeholder, 'TextPlugin', 'en',
placeholder,
"TextPlugin",
"en",
body='<span data-one="1" data-two="2">some text</span>',
)
language = 'en'
self.publish(page, 'en')
language = "en"
self.publish(page, "en")
url = page.get_absolute_url(language)
response = self.client.get(url)
self.assertContains(response, 'data-one="1"')
Expand All @@ -86,14 +92,16 @@ def test_text_sanitizer(self):
@skipIf(True, "sanitizer deactivated")
def test_text_sanitizer_no_settings(self):
settings.ALLOW_TOKEN_PARSERS = []
page = self.create_page(title='home', template='page.html', language='en')
placeholder = self.get_placeholders(page, 'en').get(slot='content')
page = self.create_page(title="home", template="page.html", language="en")
placeholder = self.get_placeholders(page, "en").get(slot="content")
add_plugin(
placeholder, 'TextPlugin', 'en',
placeholder,
"TextPlugin",
"en",
body='<span data-one="1" data-two="2">some text</span>',
)
language = 'en'
self.publish(page, 'en')
language = "en"
self.publish(page, "en")
url = page.get_absolute_url(language)
response = self.client.get(url)
self.assertContains(response, '<span>some text</span>')
self.assertContains(response, "<span>some text</span>")

0 comments on commit 0c8cb5a

Please sign in to comment.