diff options
| author | Justin Bronn <jbronn@gmail.com> | 2008-03-23 17:18:58 +0000 |
|---|---|---|
| committer | Justin Bronn <jbronn@gmail.com> | 2008-03-23 17:18:58 +0000 |
| commit | 2efc34dc5e65da792700af6594c537104cbee9dc (patch) | |
| tree | d78d906a2d7ea692b5c2f100f792cf1c1991e669 /tests/regressiontests | |
| parent | b0a895f9e891bb1fc653724691e6d85612de2708 (diff) | |
gis: Merged revisions 7280-7353 via svnmerge from trunk.
git-svn-id: http://code.djangoproject.com/svn/django/branches/gis@7354 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests/regressiontests')
| -rw-r--r-- | tests/regressiontests/forms/extra.py | 83 | ||||
| -rw-r--r-- | tests/regressiontests/forms/fields.py | 27 | ||||
| -rw-r--r-- | tests/regressiontests/forms/forms.py | 159 | ||||
| -rw-r--r-- | tests/regressiontests/model_fields/models.py | 24 | ||||
| -rw-r--r-- | tests/regressiontests/syndication/__init__.py | 0 | ||||
| -rw-r--r-- | tests/regressiontests/syndication/models.py | 0 | ||||
| -rw-r--r-- | tests/regressiontests/syndication/tests.py | 14 | ||||
| -rw-r--r-- | tests/regressiontests/syndication/urls.py | 18 | ||||
| -rw-r--r-- | tests/regressiontests/test_client_regress/models.py | 30 | ||||
| -rw-r--r-- | tests/regressiontests/test_client_regress/urls.py | 1 | ||||
| -rw-r--r-- | tests/regressiontests/test_client_regress/views.py | 16 |
11 files changed, 312 insertions, 60 deletions
diff --git a/tests/regressiontests/forms/extra.py b/tests/regressiontests/forms/extra.py index 9dff4071f1..a8b369715d 100644 --- a/tests/regressiontests/forms/extra.py +++ b/tests/regressiontests/forms/extra.py @@ -22,7 +22,7 @@ classes that demonstrate some of the library's abilities. >>> from django.newforms.extras import SelectDateWidget >>> w = SelectDateWidget(years=('2007','2008','2009','2010','2011','2012','2013','2014','2015','2016')) >>> print w.render('mydate', '') -<select name="mydate_month"> +<select name="mydate_month" id="id_mydate_month"> <option value="1">January</option> <option value="2">February</option> <option value="3">March</option> @@ -36,7 +36,7 @@ classes that demonstrate some of the library's abilities. <option value="11">November</option> <option value="12">December</option> </select> -<select name="mydate_day"> +<select name="mydate_day" id="id_mydate_day"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> @@ -69,7 +69,7 @@ classes that demonstrate some of the library's abilities. <option value="30">30</option> <option value="31">31</option> </select> -<select name="mydate_year"> +<select name="mydate_year" id="id_mydate_year"> <option value="2007">2007</option> <option value="2008">2008</option> <option value="2009">2009</option> @@ -84,7 +84,7 @@ classes that demonstrate some of the library's abilities. >>> w.render('mydate', None) == w.render('mydate', '') True >>> print w.render('mydate', '2010-04-15') -<select name="mydate_month"> +<select name="mydate_month" id="id_mydate_month"> <option value="1">January</option> <option value="2">February</option> <option value="3">March</option> @@ -98,7 +98,7 @@ True <option value="11">November</option> <option value="12">December</option> </select> -<select name="mydate_day"> +<select name="mydate_day" id="id_mydate_day"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> @@ -131,7 +131,74 @@ True <option value="30">30</option> <option value="31">31</option> </select> -<select name="mydate_year"> +<select name="mydate_year" id="id_mydate_year"> +<option value="2007">2007</option> +<option value="2008">2008</option> +<option value="2009">2009</option> +<option value="2010" selected="selected">2010</option> +<option value="2011">2011</option> +<option value="2012">2012</option> +<option value="2013">2013</option> +<option value="2014">2014</option> +<option value="2015">2015</option> +<option value="2016">2016</option> +</select> + +Accepts a datetime or a string: + +>>> w.render('mydate', datetime.date(2010, 4, 15)) == w.render('mydate', '2010-04-15') +True + +Invalid dates still render the failed date: +>>> print w.render('mydate', '2010-02-31') +<select name="mydate_month" id="id_mydate_month"> +<option value="1">January</option> +<option value="2" selected="selected">February</option> +<option value="3">March</option> +<option value="4">April</option> +<option value="5">May</option> +<option value="6">June</option> +<option value="7">July</option> +<option value="8">August</option> +<option value="9">September</option> +<option value="10">October</option> +<option value="11">November</option> +<option value="12">December</option> +</select> +<select name="mydate_day" id="id_mydate_day"> +<option value="1">1</option> +<option value="2">2</option> +<option value="3">3</option> +<option value="4">4</option> +<option value="5">5</option> +<option value="6">6</option> +<option value="7">7</option> +<option value="8">8</option> +<option value="9">9</option> +<option value="10">10</option> +<option value="11">11</option> +<option value="12">12</option> +<option value="13">13</option> +<option value="14">14</option> +<option value="15">15</option> +<option value="16">16</option> +<option value="17">17</option> +<option value="18">18</option> +<option value="19">19</option> +<option value="20">20</option> +<option value="21">21</option> +<option value="22">22</option> +<option value="23">23</option> +<option value="24">24</option> +<option value="25">25</option> +<option value="26">26</option> +<option value="27">27</option> +<option value="28">28</option> +<option value="29">29</option> +<option value="30">30</option> +<option value="31" selected="selected">31</option> +</select> +<select name="mydate_year" id="id_mydate_year"> <option value="2007">2007</option> <option value="2008">2008</option> <option value="2009">2009</option> @@ -252,8 +319,8 @@ ValidationError: [u'This field is required.'] </select> <input type="text" name="field1_2_0" value="2007-04-25" id="id_field1_2_0" /><input type="text" name="field1_2_1" value="06:24:00" id="id_field1_2_1" /></td></tr> ->>> f.cleaned_data -{'field1': u'some text,JP,2007-04-25 06:24:00'} +>>> f.cleaned_data['field1'] +u'some text,JP,2007-04-25 06:24:00' # IPAddressField ################################################################## diff --git a/tests/regressiontests/forms/fields.py b/tests/regressiontests/forms/fields.py index 9216210e09..9421d8c005 100644 --- a/tests/regressiontests/forms/fields.py +++ b/tests/regressiontests/forms/fields.py @@ -1133,6 +1133,33 @@ u'' >>> f.clean(None) u'' +# FilePathField ############################################################### + +>>> import os +>>> from django import newforms as forms +>>> path = forms.__file__ +>>> path = os.path.dirname(path) + '/' +>>> path +'.../django/newforms/' +>>> f = forms.FilePathField(path=path) +>>> f.choices.sort() +>>> f.choices +[('.../django/newforms/__init__.py', '__init__.py'), ('.../django/newforms/__init__.pyc', '__init__.pyc'), ('.../django/newforms/fields.py', 'fields.py'), ('.../django/newforms/fields.pyc', 'fields.pyc'), ('.../django/newforms/forms.py', 'forms.py'), ('.../django/newforms/forms.pyc', 'forms.pyc'), ('.../django/newforms/models.py', 'models.py'), ('.../django/newforms/models.pyc', 'models.pyc'), ('.../django/newforms/util.py', 'util.py'), ('.../django/newforms/util.pyc', 'util.pyc'), ('.../django/newforms/widgets.py', 'widgets.py'), ('.../django/newforms/widgets.pyc', 'widgets.pyc')] +>>> f.clean('fields.py') +Traceback (most recent call last): +... +ValidationError: [u'Select a valid choice. That choice is not one of the available choices.'] +>>> f.clean(path + 'fields.py') +u'.../django/newforms/fields.py' +>>> f = forms.FilePathField(path=path, match='^.*?\.py$') +>>> f.choices.sort() +>>> f.choices +[('.../django/newforms/__init__.py', '__init__.py'), ('.../django/newforms/fields.py', 'fields.py'), ('.../django/newforms/forms.py', 'forms.py'), ('.../django/newforms/models.py', 'models.py'), ('.../django/newforms/util.py', 'util.py'), ('.../django/newforms/widgets.py', 'widgets.py')] +>>> f = forms.FilePathField(path=path, recursive=True, match='^.*?\.py$') +>>> f.choices.sort() +>>> f.choices +[('.../django/newforms/__init__.py', '__init__.py'), ('.../django/newforms/extras/__init__.py', 'extras/__init__.py'), ('.../django/newforms/extras/widgets.py', 'extras/widgets.py'), ('.../django/newforms/fields.py', 'fields.py'), ('.../django/newforms/forms.py', 'forms.py'), ('.../django/newforms/models.py', 'models.py'), ('.../django/newforms/util.py', 'util.py'), ('.../django/newforms/widgets.py', 'widgets.py')] + # SplitDateTimeField ########################################################## >>> f = SplitDateTimeField() diff --git a/tests/regressiontests/forms/forms.py b/tests/regressiontests/forms/forms.py index 7c0cf8abf3..d7fa1780f5 100644 --- a/tests/regressiontests/forms/forms.py +++ b/tests/regressiontests/forms/forms.py @@ -36,8 +36,8 @@ True u'' >>> p.errors.as_text() u'' ->>> p.cleaned_data -{'first_name': u'John', 'last_name': u'Lennon', 'birthday': datetime.date(1940, 10, 9)} +>>> p.cleaned_data["first_name"], p.cleaned_data["last_name"], p.cleaned_data["birthday"] +(u'John', u'Lennon', datetime.date(1940, 10, 9)) >>> print p['first_name'] <input type="text" name="first_name" value="John" id="id_first_name" /> >>> print p['last_name'] @@ -68,8 +68,12 @@ Empty dictionaries are valid, too. >>> p = Person({}) >>> p.is_bound True ->>> p.errors -{'first_name': [u'This field is required.'], 'last_name': [u'This field is required.'], 'birthday': [u'This field is required.']} +>>> p.errors['first_name'] +[u'This field is required.'] +>>> p.errors['last_name'] +[u'This field is required.'] +>>> p.errors['birthday'] +[u'This field is required.'] >>> p.is_valid() False >>> p.cleaned_data @@ -137,8 +141,10 @@ u'<li><label for="id_first_name">First name:</label> <input type="text" name="fi u'<p><label for="id_first_name">First name:</label> <input type="text" name="first_name" value="John" id="id_first_name" /></p>\n<p><label for="id_last_name">Last name:</label> <input type="text" name="last_name" value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" id="id_last_name" /></p>\n<p><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" value="1940-10-9" id="id_birthday" /></p>' >>> p = Person({'last_name': u'Lennon'}) ->>> p.errors -{'first_name': [u'This field is required.'], 'birthday': [u'This field is required.']} +>>> p.errors['first_name'] +[u'This field is required.'] +>>> p.errors['birthday'] +[u'This field is required.'] >>> p.is_valid() False >>> p.errors.as_ul() @@ -175,8 +181,13 @@ but cleaned_data contains only the form's fields. >>> p = Person(data) >>> p.is_valid() True ->>> p.cleaned_data -{'first_name': u'John', 'last_name': u'Lennon', 'birthday': datetime.date(1940, 10, 9)} +>>> p.cleaned_data['first_name'] +u'John' +>>> p.cleaned_data['last_name'] +u'Lennon' +>>> p.cleaned_data['birthday'] +datetime.date(1940, 10, 9) + cleaned_data will include a key and value for *all* fields defined in the Form, even if the Form's data didn't include a value for fields that are not @@ -191,8 +202,12 @@ empty string. >>> f = OptionalPersonForm(data) >>> f.is_valid() True ->>> f.cleaned_data -{'nick_name': u'', 'first_name': u'John', 'last_name': u'Lennon'} +>>> f.cleaned_data['nick_name'] +u'' +>>> f.cleaned_data['first_name'] +u'John' +>>> f.cleaned_data['last_name'] +u'Lennon' For DateFields, it's set to None. >>> class OptionalPersonForm(Form): @@ -203,8 +218,12 @@ For DateFields, it's set to None. >>> f = OptionalPersonForm(data) >>> f.is_valid() True ->>> f.cleaned_data -{'birth_date': None, 'first_name': u'John', 'last_name': u'Lennon'} +>>> print f.cleaned_data['birth_date'] +None +>>> f.cleaned_data['first_name'] +u'John' +>>> f.cleaned_data['last_name'] +u'Lennon' "auto_id" tells the Form to add an "id" attribute to each form element. If it's a string that contains '%s', Django will use that as a format string @@ -549,18 +568,22 @@ The MultipleHiddenInput widget renders multiple values as hidden fields. When using CheckboxSelectMultiple, the framework expects a list of input and returns a list of input. >>> f = SongForm({'name': 'Yesterday'}, auto_id=False) ->>> f.errors -{'composers': [u'This field is required.']} +>>> f.errors['composers'] +[u'This field is required.'] >>> f = SongForm({'name': 'Yesterday', 'composers': ['J']}, auto_id=False) >>> f.errors {} ->>> f.cleaned_data -{'composers': [u'J'], 'name': u'Yesterday'} +>>> f.cleaned_data['composers'] +[u'J'] +>>> f.cleaned_data['name'] +u'Yesterday' >>> f = SongForm({'name': 'Yesterday', 'composers': ['J', 'P']}, auto_id=False) >>> f.errors {} ->>> f.cleaned_data -{'composers': [u'J', u'P'], 'name': u'Yesterday'} +>>> f.cleaned_data['composers'] +[u'J', u'P'] +>>> f.cleaned_data['name'] +u'Yesterday' Validation errors are HTML-escaped when output as HTML. >>> class EscapingForm(Form): @@ -598,16 +621,24 @@ including the current field (e.g., the field XXX if you're in clean_XXX()). >>> f.errors {} >>> f = UserRegistration({}, auto_id=False) ->>> f.errors -{'username': [u'This field is required.'], 'password1': [u'This field is required.'], 'password2': [u'This field is required.']} +>>> f.errors['username'] +[u'This field is required.'] +>>> f.errors['password1'] +[u'This field is required.'] +>>> f.errors['password2'] +[u'This field is required.'] >>> f = UserRegistration({'username': 'adrian', 'password1': 'foo', 'password2': 'bar'}, auto_id=False) ->>> f.errors -{'password2': [u'Please make sure your passwords match.']} +>>> f.errors['password2'] +[u'Please make sure your passwords match.'] >>> f = UserRegistration({'username': 'adrian', 'password1': 'foo', 'password2': 'foo'}, auto_id=False) >>> f.errors {} ->>> f.cleaned_data -{'username': u'adrian', 'password1': u'foo', 'password2': u'foo'} +>>> f.cleaned_data['username'] +u'adrian' +>>> f.cleaned_data['password1'] +u'foo' +>>> f.cleaned_data['password2'] +u'foo' Another way of doing multiple-field validation is by implementing the Form's clean() method. If you do this, any ValidationError raised by that @@ -632,11 +663,15 @@ Form.clean() is required to return a dictionary of all clean data. <tr><th>Username:</th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="username" maxlength="10" /></td></tr> <tr><th>Password1:</th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="password" name="password1" /></td></tr> <tr><th>Password2:</th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="password" name="password2" /></td></tr> ->>> f.errors -{'username': [u'This field is required.'], 'password1': [u'This field is required.'], 'password2': [u'This field is required.']} +>>> f.errors['username'] +[u'This field is required.'] +>>> f.errors['password1'] +[u'This field is required.'] +>>> f.errors['password2'] +[u'This field is required.'] >>> f = UserRegistration({'username': 'adrian', 'password1': 'foo', 'password2': 'bar'}, auto_id=False) ->>> f.errors -{'__all__': [u'Please make sure your passwords match.']} +>>> f.errors['__all__'] +[u'Please make sure your passwords match.'] >>> print f.as_table() <tr><td colspan="2"><ul class="errorlist"><li>Please make sure your passwords match.</li></ul></td></tr> <tr><th>Username:</th><td><input type="text" name="username" value="adrian" maxlength="10" /></td></tr> @@ -650,8 +685,12 @@ Form.clean() is required to return a dictionary of all clean data. >>> f = UserRegistration({'username': 'adrian', 'password1': 'foo', 'password2': 'foo'}, auto_id=False) >>> f.errors {} ->>> f.cleaned_data -{'username': u'adrian', 'password1': u'foo', 'password2': u'foo'} +>>> f.cleaned_data['username'] +u'adrian' +>>> f.cleaned_data['password1'] +u'foo' +>>> f.cleaned_data['password2'] +u'foo' # Dynamic construction ######################################################## @@ -1024,8 +1063,8 @@ An 'initial' value is *not* used as a fallback if data is not provided. In this example, we don't provide a value for 'username', and the form raises a validation error rather than using the initial value for 'username'. >>> p = UserRegistration({'password': 'secret'}) ->>> p.errors -{'username': [u'This field is required.']} +>>> p.errors['username'] +[u'This field is required.'] >>> p.is_valid() False @@ -1069,8 +1108,8 @@ A dynamic 'initial' value is *not* used as a fallback if data is not provided. In this example, we don't provide a value for 'username', and the form raises a validation error rather than using the initial value for 'username'. >>> p = UserRegistration({'password': 'secret'}, initial={'username': 'django'}) ->>> p.errors -{'username': [u'This field is required.']} +>>> p.errors['username'] +[u'This field is required.'] >>> p.is_valid() False @@ -1123,8 +1162,8 @@ A callable 'initial' value is *not* used as a fallback if data is not provided. In this example, we don't provide a value for 'username', and the form raises a validation error rather than using the initial value for 'username'. >>> p = UserRegistration({'password': 'secret'}, initial={'username': initial_django}) ->>> p.errors -{'username': [u'This field is required.']} +>>> p.errors['username'] +[u'This field is required.'] >>> p.is_valid() False @@ -1258,8 +1297,12 @@ actual field name. {} >>> p.is_valid() True ->>> p.cleaned_data -{'first_name': u'John', 'last_name': u'Lennon', 'birthday': datetime.date(1940, 10, 9)} +>>> p.cleaned_data['first_name'] +u'John' +>>> p.cleaned_data['last_name'] +u'Lennon' +>>> p.cleaned_data['birthday'] +datetime.date(1940, 10, 9) Let's try submitting some bad data to make sure form.errors and field.errors work as expected. @@ -1269,8 +1312,12 @@ work as expected. ... 'person1-birthday': u'' ... } >>> p = Person(data, prefix='person1') ->>> p.errors -{'first_name': [u'This field is required.'], 'last_name': [u'This field is required.'], 'birthday': [u'This field is required.']} +>>> p.errors['first_name'] +[u'This field is required.'] +>>> p.errors['last_name'] +[u'This field is required.'] +>>> p.errors['birthday'] +[u'This field is required.'] >>> p['first_name'].errors [u'This field is required.'] >>> p['person1-first_name'].errors @@ -1286,8 +1333,12 @@ the form doesn't "see" the fields. ... 'birthday': u'1940-10-9' ... } >>> p = Person(data, prefix='person1') ->>> p.errors -{'first_name': [u'This field is required.'], 'last_name': [u'This field is required.'], 'birthday': [u'This field is required.']} +>>> p.errors['first_name'] +[u'This field is required.'] +>>> p.errors['last_name'] +[u'This field is required.'] +>>> p.errors['birthday'] +[u'This field is required.'] With prefixes, a single data dictionary can hold data for multiple instances of the same form. @@ -1302,13 +1353,21 @@ of the same form. >>> p1 = Person(data, prefix='person1') >>> p1.is_valid() True ->>> p1.cleaned_data -{'first_name': u'John', 'last_name': u'Lennon', 'birthday': datetime.date(1940, 10, 9)} +>>> p1.cleaned_data['first_name'] +u'John' +>>> p1.cleaned_data['last_name'] +u'Lennon' +>>> p1.cleaned_data['birthday'] +datetime.date(1940, 10, 9) >>> p2 = Person(data, prefix='person2') >>> p2.is_valid() True ->>> p2.cleaned_data -{'first_name': u'Jim', 'last_name': u'Morrison', 'birthday': datetime.date(1943, 12, 8)} +>>> p2.cleaned_data['first_name'] +u'Jim' +>>> p2.cleaned_data['last_name'] +u'Morrison' +>>> p2.cleaned_data['birthday'] +datetime.date(1943, 12, 8) By default, forms append a hyphen between the prefix and the field name, but a form can alter that behavior by implementing the add_prefix() method. This @@ -1333,8 +1392,12 @@ self.prefix. >>> p = Person(data, prefix='foo') >>> p.is_valid() True ->>> p.cleaned_data -{'first_name': u'John', 'last_name': u'Lennon', 'birthday': datetime.date(1940, 10, 9)} +>>> p.cleaned_data['first_name'] +u'John' +>>> p.cleaned_data['last_name'] +u'Lennon' +>>> p.cleaned_data['birthday'] +datetime.date(1940, 10, 9) # Forms with NullBooleanFields ################################################ diff --git a/tests/regressiontests/model_fields/models.py b/tests/regressiontests/model_fields/models.py index e69de29bb2..7e07227961 100644 --- a/tests/regressiontests/model_fields/models.py +++ b/tests/regressiontests/model_fields/models.py @@ -0,0 +1,24 @@ + +from django.db import models + +class Foo(models.Model): + a = models.CharField(max_length=10) + +def get_foo(): + return Foo.objects.get(id=1) + +class Bar(models.Model): + b = models.CharField(max_length=10) + a = models.ForeignKey(Foo, default=get_foo) + +__test__ = {'API_TESTS':""" +# Create a couple of Places. +>>> f = Foo.objects.create(a='abc') +>>> f.id +1 +>>> b = Bar(b = "bcd") +>>> b.a +<Foo: Foo object> +>>> b.save() + +"""} diff --git a/tests/regressiontests/syndication/__init__.py b/tests/regressiontests/syndication/__init__.py new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/tests/regressiontests/syndication/__init__.py diff --git a/tests/regressiontests/syndication/models.py b/tests/regressiontests/syndication/models.py new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/tests/regressiontests/syndication/models.py diff --git a/tests/regressiontests/syndication/tests.py b/tests/regressiontests/syndication/tests.py new file mode 100644 index 0000000000..6a9dd643d7 --- /dev/null +++ b/tests/regressiontests/syndication/tests.py @@ -0,0 +1,14 @@ +# -*- coding: utf-8 -*- + +from django.test import TestCase +from django.test.client import Client + +class SyndicationFeedTest(TestCase): + def test_complex_base_url(self): + """ + Tests that that the base url for a complex feed doesn't raise a 500 + exception. + """ + c = Client() + response = c.get('/syndication/feeds/complex/') + self.assertEquals(response.status_code, 404) diff --git a/tests/regressiontests/syndication/urls.py b/tests/regressiontests/syndication/urls.py new file mode 100644 index 0000000000..24644ffd53 --- /dev/null +++ b/tests/regressiontests/syndication/urls.py @@ -0,0 +1,18 @@ +from django.conf.urls.defaults import patterns +from django.core.exceptions import ObjectDoesNotExist +from django.contrib.syndication import feeds + + +class ComplexFeed(feeds.Feed): + def get_object(self, bits): + if len(bits) != 1: + raise ObjectDoesNotExist + return None + + +urlpatterns = patterns('', + (r'^feeds/(?P<url>.*)/$', 'django.contrib.syndication.views.feed', { + 'feed_dict': dict( + complex = ComplexFeed, + )}), +) diff --git a/tests/regressiontests/test_client_regress/models.py b/tests/regressiontests/test_client_regress/models.py index b5d9ae63b9..305ccc9aa3 100644 --- a/tests/regressiontests/test_client_regress/models.py +++ b/tests/regressiontests/test_client_regress/models.py @@ -3,7 +3,7 @@ Regression tests for the Test Client, especially the customized assertions. """ from django.test import Client, TestCase -from django.core import mail +from django.core.urlresolvers import reverse import os class AssertContainsTests(TestCase): @@ -261,3 +261,31 @@ class LoginTests(TestCase): # Check that assertRedirects uses the original client, not the # default client. self.assertRedirects(response, "http://testserver/test_client_regress/get_view/") + + +class URLEscapingTests(TestCase): + def test_simple_argument_get(self): + "Get a view that has a simple string argument" + response = self.client.get(reverse('arg_view', args=['Slartibartfast'])) + self.assertEqual(response.status_code, 200) + self.assertEqual(response.content, 'Howdy, Slartibartfast') + + def test_argument_with_space_get(self): + "Get a view that has a string argument that requires escaping" + response = self.client.get(reverse('arg_view', args=['Arthur Dent'])) + self.assertEqual(response.status_code, 200) + self.assertEqual(response.content, 'Hi, Arthur') + + def test_simple_argument_post(self): + "Post for a view that has a simple string argument" + response = self.client.post(reverse('arg_view', args=['Slartibartfast'])) + self.assertEqual(response.status_code, 200) + self.assertEqual(response.content, 'Howdy, Slartibartfast') + + def test_argument_with_space_post(self): + "Post for a view that has a string argument that requires escaping" + response = self.client.post(reverse('arg_view', args=['Arthur Dent'])) + self.assertEqual(response.status_code, 200) + self.assertEqual(response.content, 'Hi, Arthur') + + diff --git a/tests/regressiontests/test_client_regress/urls.py b/tests/regressiontests/test_client_regress/urls.py index e771707f61..d3304caef0 100644 --- a/tests/regressiontests/test_client_regress/urls.py +++ b/tests/regressiontests/test_client_regress/urls.py @@ -5,5 +5,6 @@ urlpatterns = patterns('', (r'^no_template_view/$', views.no_template_view), (r'^file_upload/$', views.file_upload_view), (r'^get_view/$', views.get_view), + url(r'^arg_view/(?P<name>.+)/$', views.view_with_argument, name='arg_view'), (r'^login_protected_redirect_view/$', views.login_protected_redirect_view) ) diff --git a/tests/regressiontests/test_client_regress/views.py b/tests/regressiontests/test_client_regress/views.py index 75efd212e1..f44757dc10 100644 --- a/tests/regressiontests/test_client_regress/views.py +++ b/tests/regressiontests/test_client_regress/views.py @@ -1,7 +1,5 @@ from django.contrib.auth.decorators import login_required -from django.core.mail import EmailMessage, SMTPConnection from django.http import HttpResponse, HttpResponseRedirect, HttpResponseServerError -from django.shortcuts import render_to_response def no_template_view(request): "A simple view that expects a GET request, and returns a rendered template" @@ -20,10 +18,22 @@ def file_upload_view(request): return HttpResponseServerError() def get_view(request): - "A simple login protected view" + "A simple login protected view" return HttpResponse("Hello world") get_view = login_required(get_view) +def view_with_argument(request, name): + """A view that takes a string argument + + The purpose of this view is to check that if a space is provided in + the argument, the test framework unescapes the %20 before passing + the value to the view. + """ + if name == 'Arthur Dent': + return HttpResponse('Hi, Arthur') + else: + return HttpResponse('Howdy, %s' % name) + def login_protected_redirect_view(request): "A view that redirects all requests to the GET view" return HttpResponseRedirect('/test_client_regress/get_view/') |
