test_regressions.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. from django.forms import (
  4. CharField, ChoiceField, Form, HiddenInput, IntegerField, ModelForm,
  5. ModelMultipleChoiceField, MultipleChoiceField, RadioSelect, Select,
  6. TextInput,
  7. )
  8. from django.test import TestCase, ignore_warnings
  9. from django.utils import translation
  10. from django.utils.translation import gettext_lazy, ugettext_lazy
  11. from ..models import Cheese
  12. class FormsRegressionsTestCase(TestCase):
  13. def test_class(self):
  14. # Tests to prevent against recurrences of earlier bugs.
  15. extra_attrs = {'class': 'special'}
  16. class TestForm(Form):
  17. f1 = CharField(max_length=10, widget=TextInput(attrs=extra_attrs))
  18. f2 = CharField(widget=TextInput(attrs=extra_attrs))
  19. self.assertHTMLEqual(TestForm(auto_id=False).as_p(), '<p>F1: <input type="text" class="special" name="f1" maxlength="10" /></p>\n<p>F2: <input type="text" class="special" name="f2" /></p>')
  20. def test_regression_3600(self):
  21. # Tests for form i18n #
  22. # There were some problems with form translations in #3600
  23. class SomeForm(Form):
  24. username = CharField(max_length=10, label=ugettext_lazy('username'))
  25. f = SomeForm()
  26. self.assertHTMLEqual(f.as_p(), '<p><label for="id_username">username:</label> <input id="id_username" type="text" name="username" maxlength="10" /></p>')
  27. # Translations are done at rendering time, so multi-lingual apps can define forms)
  28. with translation.override('de'):
  29. self.assertHTMLEqual(f.as_p(), '<p><label for="id_username">Benutzername:</label> <input id="id_username" type="text" name="username" maxlength="10" /></p>')
  30. with translation.override('pl'):
  31. self.assertHTMLEqual(f.as_p(), '<p><label for="id_username">u\u017cytkownik:</label> <input id="id_username" type="text" name="username" maxlength="10" /></p>')
  32. def test_regression_5216(self):
  33. # There was some problems with form translations in #5216
  34. class SomeForm(Form):
  35. field_1 = CharField(max_length=10, label=ugettext_lazy('field_1'))
  36. field_2 = CharField(max_length=10, label=ugettext_lazy('field_2'), widget=TextInput(attrs={'id': 'field_2_id'}))
  37. f = SomeForm()
  38. self.assertHTMLEqual(f['field_1'].label_tag(), '<label for="id_field_1">field_1:</label>')
  39. self.assertHTMLEqual(f['field_2'].label_tag(), '<label for="field_2_id">field_2:</label>')
  40. # Unicode decoding problems...
  41. GENDERS = (('\xc5', 'En tied\xe4'), ('\xf8', 'Mies'), ('\xdf', 'Nainen'))
  42. class SomeForm(Form):
  43. somechoice = ChoiceField(choices=GENDERS, widget=RadioSelect(), label='\xc5\xf8\xdf')
  44. f = SomeForm()
  45. self.assertHTMLEqual(f.as_p(), '<p><label for="id_somechoice_0">\xc5\xf8\xdf:</label> <ul id="id_somechoice">\n<li><label for="id_somechoice_0"><input type="radio" id="id_somechoice_0" value="\xc5" name="somechoice" /> En tied\xe4</label></li>\n<li><label for="id_somechoice_1"><input type="radio" id="id_somechoice_1" value="\xf8" name="somechoice" /> Mies</label></li>\n<li><label for="id_somechoice_2"><input type="radio" id="id_somechoice_2" value="\xdf" name="somechoice" /> Nainen</label></li>\n</ul></p>')
  46. # Translated error messages used to be buggy.
  47. with translation.override('ru'):
  48. f = SomeForm({})
  49. self.assertHTMLEqual(f.as_p(), '<ul class="errorlist"><li>\u041e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u043f\u043e\u043b\u0435.</li></ul>\n<p><label for="id_somechoice_0">\xc5\xf8\xdf:</label> <ul id="id_somechoice">\n<li><label for="id_somechoice_0"><input type="radio" id="id_somechoice_0" value="\xc5" name="somechoice" /> En tied\xe4</label></li>\n<li><label for="id_somechoice_1"><input type="radio" id="id_somechoice_1" value="\xf8" name="somechoice" /> Mies</label></li>\n<li><label for="id_somechoice_2"><input type="radio" id="id_somechoice_2" value="\xdf" name="somechoice" /> Nainen</label></li>\n</ul></p>')
  50. # Deep copying translated text shouldn't raise an error)
  51. class CopyForm(Form):
  52. degree = IntegerField(widget=Select(choices=((1, gettext_lazy('test')),)))
  53. f = CopyForm()
  54. @ignore_warnings(category=UnicodeWarning)
  55. def test_regression_5216_b(self):
  56. # Testing choice validation with UTF-8 bytestrings as input (these are the
  57. # Russian abbreviations "мес." and "шт.".
  58. UNITS = ((b'\xd0\xbc\xd0\xb5\xd1\x81.', b'\xd0\xbc\xd0\xb5\xd1\x81.'),
  59. (b'\xd1\x88\xd1\x82.', b'\xd1\x88\xd1\x82.'))
  60. f = ChoiceField(choices=UNITS)
  61. self.assertEqual(f.clean('\u0448\u0442.'), '\u0448\u0442.')
  62. self.assertEqual(f.clean(b'\xd1\x88\xd1\x82.'), '\u0448\u0442.')
  63. def test_misc(self):
  64. # There once was a problem with Form fields called "data". Let's make sure that
  65. # doesn't come back.
  66. class DataForm(Form):
  67. data = CharField(max_length=10)
  68. f = DataForm({'data': 'xyzzy'})
  69. self.assertTrue(f.is_valid())
  70. self.assertEqual(f.cleaned_data, {'data': 'xyzzy'})
  71. # A form with *only* hidden fields that has errors is going to be very unusual.
  72. class HiddenForm(Form):
  73. data = IntegerField(widget=HiddenInput)
  74. f = HiddenForm({})
  75. self.assertHTMLEqual(f.as_p(), '<ul class="errorlist nonfield"><li>(Hidden field data) This field is required.</li></ul>\n<p> <input type="hidden" name="data" id="id_data" /></p>')
  76. self.assertHTMLEqual(f.as_table(), '<tr><td colspan="2"><ul class="errorlist nonfield"><li>(Hidden field data) This field is required.</li></ul><input type="hidden" name="data" id="id_data" /></td></tr>')
  77. def test_xss_error_messages(self):
  78. ###################################################
  79. # Tests for XSS vulnerabilities in error messages #
  80. ###################################################
  81. # The forms layer doesn't escape input values directly because error messages
  82. # might be presented in non-HTML contexts. Instead, the message is just marked
  83. # for escaping by the template engine. So we'll need to construct a little
  84. # silly template to trigger the escaping.
  85. from django.template import Template, Context
  86. t = Template('{{ form.errors }}')
  87. class SomeForm(Form):
  88. field = ChoiceField(choices=[('one', 'One')])
  89. f = SomeForm({'field': '<script>'})
  90. self.assertHTMLEqual(t.render(Context({'form': f})), '<ul class="errorlist"><li>field<ul class="errorlist"><li>Select a valid choice. &lt;script&gt; is not one of the available choices.</li></ul></li></ul>')
  91. class SomeForm(Form):
  92. field = MultipleChoiceField(choices=[('one', 'One')])
  93. f = SomeForm({'field': ['<script>']})
  94. self.assertHTMLEqual(t.render(Context({'form': f})), '<ul class="errorlist"><li>field<ul class="errorlist"><li>Select a valid choice. &lt;script&gt; is not one of the available choices.</li></ul></li></ul>')
  95. from forms_tests.models import ChoiceModel
  96. class SomeForm(Form):
  97. field = ModelMultipleChoiceField(ChoiceModel.objects.all())
  98. f = SomeForm({'field': ['<script>']})
  99. self.assertHTMLEqual(t.render(Context({'form': f})), '<ul class="errorlist"><li>field<ul class="errorlist"><li>&quot;&lt;script&gt;&quot; is not a valid value for a primary key.</li></ul></li></ul>')
  100. def test_regression_14234(self):
  101. """
  102. Re-cleaning an instance that was added via a ModelForm should not raise
  103. a pk uniqueness error.
  104. """
  105. class CheeseForm(ModelForm):
  106. class Meta:
  107. model = Cheese
  108. fields = '__all__'
  109. form = CheeseForm({
  110. 'name': 'Brie',
  111. })
  112. self.assertTrue(form.is_valid())
  113. obj = form.save()
  114. obj.name = 'Camembert'
  115. obj.full_clean()