regressions.py 7.5 KB

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