summaryrefslogtreecommitdiff
path: root/tests/model_forms
diff options
context:
space:
mode:
authorAlexander Lötvall <alexander.lotvall@kognity.com>2024-06-08 12:14:46 +0200
committerSarah Boyce <42296566+sarahboyce@users.noreply.github.com>2024-06-17 12:22:32 +0200
commitb8983dcf57b46a4391891cecdfc856c9c1c8d363 (patch)
treec2123561710f57f209df5f921529adb5212362e2 /tests/model_forms
parenta0f6835f723167768d4f44df2c840eb91dd42c7c (diff)
[5.1.x] Fixed #35483 -- Added NUL (0x00) character validation to ModelChoiceFields.
Applied the ProhibitNullCharactersValidator to ModelChoiceField and ModelMultipleChoiceField. Co-authored-by: Viktor Paripás <viktor.paripas@gmail.com> Co-authored-by: Vasyl Dizhak <vasyl@dizhak.com> Co-authored-by: Arthur Vasconcelos <vasconcelos.arthur@gmail.com> Backport of 38ad710aba885ad26944ff5708ce1a02a446d2d3 from main.
Diffstat (limited to 'tests/model_forms')
-rw-r--r--tests/model_forms/test_modelchoicefield.py8
-rw-r--r--tests/model_forms/tests.py9
2 files changed, 16 insertions, 1 deletions
diff --git a/tests/model_forms/test_modelchoicefield.py b/tests/model_forms/test_modelchoicefield.py
index 19e9db69a0..83d801768a 100644
--- a/tests/model_forms/test_modelchoicefield.py
+++ b/tests/model_forms/test_modelchoicefield.py
@@ -7,7 +7,7 @@ from django.forms.widgets import CheckboxSelectMultiple
from django.template import Context, Template
from django.test import TestCase
-from .models import Article, Author, Book, Category, Writer
+from .models import Article, Author, Book, Category, ExplicitPK, Writer
class ModelChoiceFieldTests(TestCase):
@@ -79,6 +79,12 @@ class ModelChoiceFieldTests(TestCase):
self.assertEqual(f.clean(self.c1.slug), self.c1)
self.assertEqual(f.clean(self.c1), self.c1)
+ def test_model_choice_null_characters(self):
+ f = forms.ModelChoiceField(queryset=ExplicitPK.objects.all())
+ msg = "Null characters are not allowed."
+ with self.assertRaisesMessage(ValidationError, msg):
+ f.clean("\x00something")
+
def test_choices(self):
f = forms.ModelChoiceField(
Category.objects.filter(pk=self.c1.id), required=False
diff --git a/tests/model_forms/tests.py b/tests/model_forms/tests.py
index 3f927cb053..c6e12e1aab 100644
--- a/tests/model_forms/tests.py
+++ b/tests/model_forms/tests.py
@@ -2227,6 +2227,15 @@ class ModelMultipleChoiceFieldTests(TestCase):
f = forms.ModelMultipleChoiceField(queryset=Writer.objects.all())
self.assertNumQueries(1, f.clean, [p.pk for p in persons[1:11:2]])
+ def test_model_multiple_choice_null_characters(self):
+ f = forms.ModelMultipleChoiceField(queryset=ExplicitPK.objects.all())
+ msg = "Null characters are not allowed."
+ with self.assertRaisesMessage(ValidationError, msg):
+ f.clean(["\x00something"])
+
+ with self.assertRaisesMessage(ValidationError, msg):
+ f.clean(["valid", "\x00something"])
+
def test_model_multiple_choice_run_validators(self):
"""
ModelMultipleChoiceField run given validators (#14144).