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