From 2cd22483f0aca18ea58a44729440b7f655e17dd8 Mon Sep 17 00:00:00 2001 From: Christoph Volkert Date: Sat, 12 Oct 2024 16:20:49 +0200 Subject: [PATCH] InykoaMarkup: Dont strip whitespace in form fields Otherwise valid InyokaMarkup like ``` * a * b ``` will be rendered wrong, as the space before `* a` is stripped. The `validate_empty_text` validator of `StrippedCharField` assures that still no empty content can be submitted. Fix https://github.com/inyokaproject/inyoka/issues/1335 --- inyoka/forum/forms.py | 4 ++-- inyoka/ikhaya/forms.py | 2 +- tests/apps/forum/test_views.py | 39 +++++++++++++++++++++++++++++++++ tests/apps/ikhaya/test_forms.py | 37 +++++++++++++++++++++++++++++++ 4 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 tests/apps/ikhaya/test_forms.py diff --git a/inyoka/forum/forms.py b/inyoka/forum/forms.py index 8346eb384..6c0375a14 100644 --- a/inyoka/forum/forms.py +++ b/inyoka/forum/forms.py @@ -70,7 +70,7 @@ class EditPostForm(SurgeProtectionMixin, forms.Form): This form takes the additional keyword argument `is_first_post`. It's generally used together with `AddAttachmentForm`. """ - text = StrippedCharField(widget=forms.Textarea) + text = StrippedCharField(widget=forms.Textarea, strip=False) # the following fields only appear if the post is the first post of the # topic. #: the user can select, whether the post's topic should be sticky or not. @@ -119,7 +119,7 @@ class NewTopicForm(SurgeProtectionMixin, forms.Form): """ title = StrippedCharField(widget=forms.TextInput(attrs={'size': 60, 'spellcheck': 'true'}), max_length=100) - text = StrippedCharField(widget=forms.Textarea) + text = StrippedCharField(widget=forms.Textarea, strip=False) ubuntu_version = forms.ChoiceField(required=False) ubuntu_distro = forms.ChoiceField(required=False) sticky = forms.BooleanField(required=False) diff --git a/inyoka/ikhaya/forms.py b/inyoka/ikhaya/forms.py index c5071a720..0df16dbc5 100644 --- a/inyoka/ikhaya/forms.py +++ b/inyoka/ikhaya/forms.py @@ -48,7 +48,7 @@ class EditCommentForm(forms.Form): text = StrippedCharField(label=gettext_lazy('Text'), widget=forms.Textarea, help_text=gettext_lazy('To refer to another comment, you ' 'can write @commentnumber.
' - 'Clicking on “reply” will automatically insert this code.')) + 'Clicking on “reply” will automatically insert this code.'), strip=False) class EditArticleForm(forms.ModelForm): diff --git a/tests/apps/forum/test_views.py b/tests/apps/forum/test_views.py index a106fd527..15d610fdb 100644 --- a/tests/apps/forum/test_views.py +++ b/tests/apps/forum/test_views.py @@ -574,6 +574,25 @@ def test_newtopic(self): 'newpost text', ) + def test_newtopic__text_whitespace_not_stripped(self): + self.client.login(username='admin', password='admin') + + text = ' * a \n * b\n' + postdata = { + 'title': 'newpost_title', + 'ubuntu_distro': constants.get_distro_choices()[2][0], + 'text': text, + } + + # Test send + self.post_request(f'/forum/{self.forum.slug}/newtopic/', postdata, 1, 1, submit=True) + + # Check that the content is in the database + self.assertEqual( + Topic.objects.get(slug='newpost-title').last_post.text, + text, + ) + def test_newtopic_user(self): self.client.login(username='user', password='user') # Test preview @@ -926,6 +945,26 @@ def test_new_post(self): 'newpost text', ) + def test_new_post__text_whitespace_not_stripped(self): + topic = Topic.objects.create(title='topic', author=self.admin, forum=self.forum) + Post.objects.create(text='first post', author=self.admin, topic=topic) + + self.client.login(username='admin', password='admin') + text = ' * a \n * b\n' + postdata = { + 'text': text, + } + + # Test send + self.post_request(f'/topic/{topic.slug}/reply/', postdata, 1, 2, submit=True) + + # Test that the data is in the database + topic.refresh_from_db() + self.assertEqual( + topic.last_post.text, + text, + ) + def test_new_post_user(self): topic = Topic.objects.create(title='topic', author=self.admin, forum=self.forum) Post.objects.create(text='first post', author=self.admin, position=0, topic=topic) diff --git a/tests/apps/ikhaya/test_forms.py b/tests/apps/ikhaya/test_forms.py new file mode 100644 index 000000000..ef4a8e467 --- /dev/null +++ b/tests/apps/ikhaya/test_forms.py @@ -0,0 +1,37 @@ +""" + tests.apps.ikhaya.test_views + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + Test ikhaya forms. + + :copyright: (c) 2012-2024 by the Inyoka Team, see AUTHORS for more details. + :license: BSD, see LICENSE for more details. +""" + +from inyoka.ikhaya.forms import EditCommentForm +from inyoka.utils.test import TestCase + + +class TestEditCommentForm(TestCase): + form = EditCommentForm + + def test_text__whitespace_not_stripped(self): + text = ' * a \n * b\n' + data = {'text': text} + + form = self.form(data) + + self.assertTrue(form.is_valid()) + self.assertEqual(form.cleaned_data['text'], text) + + def test_text__empty_text_rejected(self): + text = ' \n \t' + data = {'text': text} + + form = self.form(data) + + self.assertFalse(form.is_valid()) + self.assertIn( + 'Text must not be empty', + form.errors['text'], + )