PageRenderTime 40ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/regressiontests/forms/tests/regressions.py

https://code.google.com/p/mango-py/
Python | 145 lines | 83 code | 36 blank | 26 comment | 0 complexity | a284dbd798004f20035b3dec6bf96ff8 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. # -*- coding: utf-8 -*-
  2. from django.forms import *
  3. from django.utils.unittest import TestCase
  4. from django.utils.translation import ugettext_lazy, activate, deactivate
  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.assertEqual(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.assertEqual(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. activate('de')
  23. self.assertEqual(f.as_p(), '<p><label for="id_username">Benutzername:</label> <input id="id_username" type="text" name="username" maxlength="10" /></p>')
  24. activate('pl')
  25. self.assertEqual(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. deactivate()
  27. def test_regression_5216(self):
  28. # There was some problems with form translations in #5216
  29. class SomeForm(Form):
  30. field_1 = CharField(max_length=10, label=ugettext_lazy('field_1'))
  31. field_2 = CharField(max_length=10, label=ugettext_lazy('field_2'), widget=TextInput(attrs={'id': 'field_2_id'}))
  32. f = SomeForm()
  33. self.assertEqual(f['field_1'].label_tag(), '<label for="id_field_1">field_1</label>')
  34. self.assertEqual(f['field_2'].label_tag(), '<label for="field_2_id">field_2</label>')
  35. # Unicode decoding problems...
  36. GENDERS = ((u'\xc5', u'En tied\xe4'), (u'\xf8', u'Mies'), (u'\xdf', u'Nainen'))
  37. class SomeForm(Form):
  38. somechoice = ChoiceField(choices=GENDERS, widget=RadioSelect(), label=u'\xc5\xf8\xdf')
  39. f = SomeForm()
  40. self.assertEqual(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>')
  41. # Testing choice validation with UTF-8 bytestrings as input (these are the
  42. # Russian abbreviations "???." and "??.".
  43. UNITS = (('\xd0\xbc\xd0\xb5\xd1\x81.', '\xd0\xbc\xd0\xb5\xd1\x81.'), ('\xd1\x88\xd1\x82.', '\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('\xd1\x88\xd1\x82.'), u'\u0448\u0442.')
  47. # Translated error messages used to be buggy.
  48. activate('ru')
  49. f = SomeForm({})
  50. self.assertEqual(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. deactivate()
  52. # Deep copying translated text shouldn't raise an error)
  53. from django.utils.translation import gettext_lazy
  54. class CopyForm(Form):
  55. degree = IntegerField(widget=Select(choices=((1, gettext_lazy('test')),)))
  56. f = CopyForm()
  57. def test_misc(self):
  58. # There once was a problem with Form fields called "data". Let's make sure that
  59. # doesn't come back.
  60. class DataForm(Form):
  61. data = CharField(max_length=10)
  62. f = DataForm({'data': 'xyzzy'})
  63. self.assertTrue(f.is_valid())
  64. self.assertEqual(f.cleaned_data, {'data': u'xyzzy'})
  65. # A form with *only* hidden fields that has errors is going to be very unusual.
  66. class HiddenForm(Form):
  67. data = IntegerField(widget=HiddenInput)
  68. f = HiddenForm({})
  69. self.assertEqual(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>')
  70. self.assertEqual(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>')
  71. def test_xss_error_messages(self):
  72. ###################################################
  73. # Tests for XSS vulnerabilities in error messages #
  74. ###################################################
  75. # The forms layer doesn't escape input values directly because error messages
  76. # might be presented in non-HTML contexts. Instead, the message is just marked
  77. # for escaping by the template engine. So we'll need to construct a little
  78. # silly template to trigger the escaping.
  79. from django.template import Template, Context
  80. t = Template('{{ form.errors }}')
  81. class SomeForm(Form):
  82. field = ChoiceField(choices=[('one', 'One')])
  83. f = SomeForm({'field': '<script>'})
  84. self.assertEqual(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>')
  85. class SomeForm(Form):
  86. field = MultipleChoiceField(choices=[('one', 'One')])
  87. f = SomeForm({'field': ['<script>']})
  88. self.assertEqual(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>')
  89. from regressiontests.forms.models import ChoiceModel
  90. class SomeForm(Form):
  91. field = ModelMultipleChoiceField(ChoiceModel.objects.all())
  92. f = SomeForm({'field': ['<script>']})
  93. self.assertEqual(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>')
  94. def test_regression_14234(self):
  95. """
  96. Re-cleaning an instance that was added via a ModelForm should not raise
  97. a pk uniqueness error.
  98. """
  99. class CheeseForm(ModelForm):
  100. class Meta:
  101. model = Cheese
  102. form = CheeseForm({
  103. 'name': 'Brie',
  104. })
  105. self.assertTrue(form.is_valid())
  106. obj = form.save()
  107. obj.name = 'Camembert'
  108. obj.full_clean()