Skip to content

Commit

Permalink
feat: Use super class to strip value (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
marksweb authored Dec 21, 2023
1 parent 508a2d8 commit 25d6d24
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ jobs:
run: |
python -Im coverage combine
python -Im coverage html --skip-covered --skip-empty
python -Im coverage report
python -Im coverage report --fail-under=100
echo "## Coverage summary" >> $GITHUB_STEP_SUMMARY
python -Im coverage report --format=markdown >> $GITHUB_STEP_SUMMARY
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Changelog
unreleased
----------

- Use the super class to strip the value on the form field.

0.1.0 - 2023-12-19
------------------

Expand Down
6 changes: 3 additions & 3 deletions src/django_nh3/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ def to_python(self, value: Any) -> Any:
Mark the return value as template safe if it contains HTML.
"""
value = super().to_python(value)
if value in self.empty_values:
# Ensures that None is handled properly as an input
return self.empty_value
if nh3.is_html(value):
return mark_safe(nh3.clean(value, **self.nh3_options))
else:
return value
return mark_safe(nh3.clean(value, **self.nh3_options))
12 changes: 6 additions & 6 deletions tests/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@ class Migration(migrations.Migration):
),
),
("content", django_nh3.models.Nh3Field()),
(
"choice",
django_nh3.models.Nh3Field(
choices=[("f", "first choice"), ("s", "second choice")]
),
),
("blank_field", django_nh3.models.Nh3Field(blank=True)),
(
"null_field",
Expand All @@ -48,6 +42,12 @@ class Migration(migrations.Migration):
verbose_name="ID",
),
),
(
"choice",
django_nh3.models.Nh3Field(
choices=[("f", "first choice"), ("s", "second choice")]
),
),
("content", django_nh3.models.Nh3Field(blank=True, null=True)),
],
),
Expand Down
10 changes: 10 additions & 0 deletions tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
from django.utils.safestring import SafeString
from django_nh3.forms import Nh3Field

# import urls to get coverage
from . import urls # noqa: F401


class TestNh3Field(TestCase):
def test_empty(self):
Expand All @@ -18,3 +21,10 @@ def test_return_type(self):
field = Nh3Field()
self.assertIsInstance(field.to_python("some text"), str)
self.assertIsInstance(field.to_python("<h1>some text</h1>"), SafeString)

def test_values(self):
"""Test bleached values are SafeString objects"""
field = Nh3Field()
self.assertEqual(field.to_python("some text"), "some text")
self.assertEqual(field.to_python(" some text "), "some text")
self.assertEqual(field.to_python("<h1>some text</h1>"), "some text")
71 changes: 65 additions & 6 deletions tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,49 @@
from django.db import models
from django.forms import ModelForm
from django.test import TestCase
from django.utils.safestring import SafeString
from django_nh3.models import Nh3Field


class Nh3Content(models.Model):
"""Bleach test model"""
"""NH3 test model"""

CHOICES = (("f", "first choice"), ("s", "second choice"))
content = Nh3Field(
strip_comments=True,
)
choice = Nh3Field(choices=CHOICES)
blank_field = Nh3Field(blank=True)
null_field = Nh3Field(blank=True, null=True)


class Nh3ContentModelForm(ModelForm):
"""NH3 test model form"""

class Meta:
model = Nh3Content
fields = ["content"]


class Nh3NullableContent(models.Model):
"""Bleach test model"""
"""NH3 test model"""

CHOICES = (("f", "first choice"), ("s", "second choice"))
choice = Nh3Field(choices=CHOICES, blank=True)
content = Nh3Field(blank=True, null=True)


class Nh3NullableContentModelForm(ModelForm):
"""NH3 test model form"""

class Meta:
model = Nh3NullableContent
fields = ["choice"]


class TestNh3ModelField(TestCase):
"""Test model field"""

def test_cleaning(self):
"""Test values are bleached"""
"""Test values are sanitized"""
test_data = {
"html_data": "<h1>Heading</h1>",
"no_html": "Heading",
Expand Down Expand Up @@ -68,7 +85,7 @@ class TestNh3NullableModelField(TestCase):
"""Test model field"""

def test_cleaning(self):
"""Test values are bleached"""
"""Test values are sanitized"""
test_data = {
"none": None,
"empty": "",
Expand All @@ -85,3 +102,45 @@ def test_cleaning(self):
for key, value in test_data.items():
obj = Nh3NullableContent.objects.create(content=value)
self.assertEqual(obj.content, expected_values[key])


class TestNh3ModelFormField(TestCase):
"""Test model form field"""

def test_cleaning(self):
"""Test values are sanitized"""
test_data = {
"html_data": "<h1>Heading</h1>",
"no_html": "Heading",
"spacing": " Heading ",
}
expected_values = {
"html_data": "Heading",
"no_html": "Heading",
"spacing": "Heading",
}

for key, value in test_data.items():
form = Nh3ContentModelForm(data={"content": value})
self.assertTrue(form.is_valid())
obj = form.save()
self.assertEqual(obj.content, expected_values[key])

def test_stripped_comments(self):
"""Content field strips comments so ensure they aren't allowed"""

self.assertFalse(
Nh3ContentModelForm(
data={"content": "<!-- this is a comment -->"}
).is_valid()
)

def test_field_choices(self):
"""Content field strips comments so ensure they aren't allowed"""
test_data = dict(Nh3NullableContent.CHOICES)

for key, value in test_data.items():
form = Nh3NullableContentModelForm(data={"choice": key})
self.assertTrue(form.is_valid())
obj = form.save()
self.assertEqual(obj.get_choice_display(), value)

0 comments on commit 25d6d24

Please sign in to comment.