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:19:26 +0200
commit38ad710aba885ad26944ff5708ce1a02a446d2d3 (patch)
tree839e0f5f2a8a5bf68dca3f335965f82292e3fd6d /tests/model_forms
parentfa7848146738a9fe1d415ee4808664e54739eeb7 (diff)
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>
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).