2007-04-01 05:26:26 +00:00
# -*- coding: utf-8 -*-
# Tests to prevent against recurrences of earlier bugs.
2007-09-19 23:40:47 +00:00
tests = r """
2007-04-01 05:26:26 +00:00
It should be possible to re - use attribute dictionaries ( #3810)
2008-07-19 01:22:26 +00:00
>> > from django . forms import *
2007-04-01 05:26:26 +00:00
>> > extra_attrs = { ' class ' : ' special ' }
>> > class TestForm ( Form ) :
. . . f1 = CharField ( max_length = 10 , widget = TextInput ( attrs = extra_attrs ) )
. . . f2 = CharField ( widget = TextInput ( attrs = extra_attrs ) )
>> > 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> '
2007-04-02 10:53:05 +00:00
2007-04-04 13:52:35 +00:00
#######################
# Tests for form i18n #
#######################
2007-04-02 10:53:05 +00:00
There were some problems with form translations in #3600
2007-04-04 13:52:35 +00:00
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 12:11:04 +00:00
>> > from django . utils . translation import ugettext_lazy , activate , deactivate
2007-04-02 10:53:05 +00:00
>> > class SomeForm ( Form ) :
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 12:11:04 +00:00
. . . username = CharField ( max_length = 10 , label = ugettext_lazy ( ' Username ' ) )
2007-04-02 10:53:05 +00:00
>> > f = SomeForm ( )
>> > print f . as_p ( )
< p > < label for = " id_username " > Username : < / label > < input id = " id_username " type = " text " name = " username " maxlength = " 10 " / > < / p >
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 12:11:04 +00:00
Translations are done at rendering time , so multi - lingual apps can define forms
early and still send back the right translation .
2007-04-02 10:53:05 +00:00
>> > activate ( ' de ' )
>> > print f . as_p ( )
< p > < label for = " id_username " > Benutzername : < / label > < input id = " id_username " type = " text " name = " username " maxlength = " 10 " / > < / p >
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 12:11:04 +00:00
>> > activate ( ' pl ' )
>> > f . as_p ( )
u ' <p><label for= " id_username " >Nazwa u \u017c ytkownika:</label> <input id= " id_username " type= " text " name= " username " maxlength= " 10 " /></p> '
2007-04-02 10:53:05 +00:00
>> > deactivate ( )
2007-04-04 13:52:35 +00:00
2008-08-31 11:39:06 +00:00
There was some problems with form translations in #5216
>> > class SomeForm ( Form ) :
. . . field_1 = CharField ( max_length = 10 , label = ugettext_lazy ( ' field_1 ' ) )
. . . field_2 = CharField ( max_length = 10 , label = ugettext_lazy ( ' field_2 ' ) , widget = TextInput ( attrs = { ' id ' : ' field_2_id ' } ) )
>> > f = SomeForm ( )
>> > print f [ ' field_1 ' ] . label_tag ( )
< label for = " id_field_1 " > field_1 < / label >
>> > print f [ ' field_2 ' ] . label_tag ( )
< label for = " field_2_id " > field_2 < / label >
2007-04-04 13:52:35 +00:00
Unicode decoding problems . . .
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 12:11:04 +00:00
>> > GENDERS = ( ( u ' \xc5 ' , u ' En tied \xe4 ' ) , ( u ' \xf8 ' , u ' Mies ' ) , ( u ' \xdf ' , u ' Nainen ' ) )
2007-04-04 13:52:35 +00:00
>> > class SomeForm ( Form ) :
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 12:11:04 +00:00
. . . somechoice = ChoiceField ( choices = GENDERS , widget = RadioSelect ( ) , label = u ' \xc5 \xf8 \xdf ' )
2007-04-04 13:52:35 +00:00
>> > f = SomeForm ( )
>> > f . as_p ( )
2008-06-18 16:33:04 +00:00
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> '
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 12:11:04 +00:00
Testing choice validation with UTF - 8 bytestrings as input ( these are the
Russian abbreviations " мес. " and " шт. " .
>> > UNITS = ( ( ' \xd0 \xbc \xd0 \xb5 \xd1 \x81 . ' , ' \xd0 \xbc \xd0 \xb5 \xd1 \x81 . ' ) , ( ' \xd1 \x88 \xd1 \x82 . ' , ' \xd1 \x88 \xd1 \x82 . ' ) )
>> > f = ChoiceField ( choices = UNITS )
>> > f . clean ( u ' \u0448 \u0442 . ' )
u ' \u0448 \u0442 . '
>> > f . clean ( ' \xd1 \x88 \xd1 \x82 . ' )
u ' \u0448 \u0442 . '
Translated error messages used to be buggy .
>> > activate ( ' ru ' )
>> > f = SomeForm ( { } )
>> > f . as_p ( )
2008-06-18 16:33:04 +00:00
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> '
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 12:11:04 +00:00
>> > deactivate ( )
2007-09-15 10:57:03 +00:00
Deep copying translated text shouldn ' t raise an error
>> > from django . utils . translation import gettext_lazy
>> > class CopyForm ( Form ) :
. . . degree = IntegerField ( widget = Select ( choices = ( ( 1 , gettext_lazy ( ' test ' ) ) , ) ) )
>> > f = CopyForm ( )
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 12:11:04 +00:00
#######################
# Miscellaneous Tests #
#######################
2007-05-14 14:02:36 +00:00
There once was a problem with Form fields called " data " . Let ' s make sure that
doesn ' t come back.
>> > class DataForm ( Form ) :
. . . data = CharField ( max_length = 10 )
>> > f = DataForm ( { ' data ' : ' xyzzy ' } )
>> > f . is_valid ( )
True
2007-05-14 16:24:51 +00:00
>> > f . cleaned_data
2007-05-14 14:02:36 +00:00
{ ' data ' : u ' xyzzy ' }
2008-09-18 07:16:08 +00:00
A form with * only * hidden fields that has errors is going to be very unusual .
But we can try to make sure it doesn ' t generate invalid XHTML. In this case,
the as_p ( ) method is the tricky one , since error lists cannot be nested
( validly ) inside p elements .
>> > class HiddenForm ( Form ) :
. . . data = IntegerField ( widget = HiddenInput )
>> > f = HiddenForm ( { } )
>> > 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> '
>> > 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> '
2009-11-20 14:59:44 +00:00
###################################################
# Tests for XSS vulnerabilities in error messages #
###################################################
# The forms layer doesn't escape input values directly because error messages
# might be presented in non-HTML contexts. Instead, the message is just marked
# for escaping by the template engine. So we'll need to construct a little
# silly template to trigger the escaping.
>> > from django . template import Template , Context
>> > t = Template ( ' {{ form.errors }} ' )
>> > class SomeForm ( Form ) :
. . . field = ChoiceField ( choices = [ ( ' one ' , ' One ' ) ] )
>> > f = SomeForm ( { ' field ' : ' <script> ' } )
>> > t . render ( Context ( { ' form ' : f } ) )
u ' <ul class= " errorlist " ><li>field<ul class= " errorlist " ><li>Select a valid choice. <script> is not one of the available choices.</li></ul></li></ul> '
>> > class SomeForm ( Form ) :
. . . field = MultipleChoiceField ( choices = [ ( ' one ' , ' One ' ) ] )
>> > f = SomeForm ( { ' field ' : [ ' <script> ' ] } )
>> > t . render ( Context ( { ' form ' : f } ) )
u ' <ul class= " errorlist " ><li>field<ul class= " errorlist " ><li>Select a valid choice. <script> is not one of the available choices.</li></ul></li></ul> '
>> > from regressiontests . forms . models import ChoiceModel
>> > class SomeForm ( Form ) :
. . . field = ModelMultipleChoiceField ( ChoiceModel . objects . all ( ) )
>> > f = SomeForm ( { ' field ' : [ ' <script> ' ] } )
>> > t . render ( Context ( { ' form ' : f } ) )
u ' <ul class= " errorlist " ><li>field<ul class= " errorlist " ><li>"<script>" is not a valid value for a primary key.</li></ul></li></ul> '
2007-04-01 05:26:26 +00:00
"""