Skip to content

Commit

Permalink
InykoaMarkup: Dont strip whitespace in form fields
Browse files Browse the repository at this point in the history
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 #1335
  • Loading branch information
chris34 committed Oct 12, 2024
1 parent f2a125b commit 2cd2248
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 3 deletions.
4 changes: 2 additions & 2 deletions inyoka/forum/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion inyoka/ikhaya/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <code>@commentnumber</code>.<br />'
'Clicking on “reply” will automatically insert this code.'))
'Clicking on “reply” will automatically insert this code.'), strip=False)


class EditArticleForm(forms.ModelForm):
Expand Down
39 changes: 39 additions & 0 deletions tests/apps/forum/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
37 changes: 37 additions & 0 deletions tests/apps/ikhaya/test_forms.py
Original file line number Diff line number Diff line change
@@ -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'],
)

0 comments on commit 2cd2248

Please sign in to comment.