Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Move last remaining tests out of models.py files; prep for test disco…

…very.
  • Loading branch information...
commit 8ce46375ae3927eec142a8689215ac5039123a03 1 parent f2d3c4b
Preston Timmons authored April 02, 2013 carljm committed April 02, 2013
129  tests/conditional_processing/models.py
... ...
@@ -1,129 +0,0 @@
1  
-# -*- coding:utf-8 -*-
2  
-from __future__ import unicode_literals
3  
-
4  
-from datetime import datetime
5  
-
6  
-from django.test import TestCase
7  
-
8  
-
9  
-FULL_RESPONSE = 'Test conditional get response'
10  
-LAST_MODIFIED = datetime(2007, 10, 21, 23, 21, 47)
11  
-LAST_MODIFIED_STR = 'Sun, 21 Oct 2007 23:21:47 GMT'
12  
-LAST_MODIFIED_NEWER_STR = 'Mon, 18 Oct 2010 16:56:23 GMT'
13  
-LAST_MODIFIED_INVALID_STR = 'Mon, 32 Oct 2010 16:56:23 GMT'
14  
-EXPIRED_LAST_MODIFIED_STR = 'Sat, 20 Oct 2007 23:21:47 GMT'
15  
-ETAG = 'b4246ffc4f62314ca13147c9d4f76974'
16  
-EXPIRED_ETAG = '7fae4cd4b0f81e7d2914700043aa8ed6'
17  
-
18  
-class ConditionalGet(TestCase):
19  
-    urls = 'conditional_processing.urls'
20  
-
21  
-    def assertFullResponse(self, response, check_last_modified=True, check_etag=True):
22  
-        self.assertEqual(response.status_code, 200)
23  
-        self.assertEqual(response.content, FULL_RESPONSE.encode())
24  
-        if check_last_modified:
25  
-            self.assertEqual(response['Last-Modified'], LAST_MODIFIED_STR)
26  
-        if check_etag:
27  
-            self.assertEqual(response['ETag'], '"%s"' % ETAG)
28  
-
29  
-    def assertNotModified(self, response):
30  
-        self.assertEqual(response.status_code, 304)
31  
-        self.assertEqual(response.content, b'')
32  
-
33  
-    def testWithoutConditions(self):
34  
-        response = self.client.get('/condition/')
35  
-        self.assertFullResponse(response)
36  
-
37  
-    def testIfModifiedSince(self):
38  
-        self.client.defaults['HTTP_IF_MODIFIED_SINCE'] = LAST_MODIFIED_STR
39  
-        response = self.client.get('/condition/')
40  
-        self.assertNotModified(response)
41  
-        self.client.defaults['HTTP_IF_MODIFIED_SINCE'] = LAST_MODIFIED_NEWER_STR
42  
-        response = self.client.get('/condition/')
43  
-        self.assertNotModified(response)
44  
-        self.client.defaults['HTTP_IF_MODIFIED_SINCE'] = LAST_MODIFIED_INVALID_STR
45  
-        response = self.client.get('/condition/')
46  
-        self.assertFullResponse(response)
47  
-        self.client.defaults['HTTP_IF_MODIFIED_SINCE'] = EXPIRED_LAST_MODIFIED_STR
48  
-        response = self.client.get('/condition/')
49  
-        self.assertFullResponse(response)
50  
-
51  
-    def testIfNoneMatch(self):
52  
-        self.client.defaults['HTTP_IF_NONE_MATCH'] = '"%s"' % ETAG
53  
-        response = self.client.get('/condition/')
54  
-        self.assertNotModified(response)
55  
-        self.client.defaults['HTTP_IF_NONE_MATCH'] = '"%s"' % EXPIRED_ETAG
56  
-        response = self.client.get('/condition/')
57  
-        self.assertFullResponse(response)
58  
-
59  
-        # Several etags in If-None-Match is a bit exotic but why not?
60  
-        self.client.defaults['HTTP_IF_NONE_MATCH'] = '"%s", "%s"' % (ETAG, EXPIRED_ETAG)
61  
-        response = self.client.get('/condition/')
62  
-        self.assertNotModified(response)
63  
-
64  
-    def testIfMatch(self):
65  
-        self.client.defaults['HTTP_IF_MATCH'] = '"%s"' % ETAG
66  
-        response = self.client.put('/condition/etag/')
67  
-        self.assertEqual(response.status_code, 200)
68  
-        self.client.defaults['HTTP_IF_MATCH'] = '"%s"' % EXPIRED_ETAG
69  
-        response = self.client.put('/condition/etag/')
70  
-        self.assertEqual(response.status_code, 412)
71  
-
72  
-    def testBothHeaders(self):
73  
-        self.client.defaults['HTTP_IF_MODIFIED_SINCE'] = LAST_MODIFIED_STR
74  
-        self.client.defaults['HTTP_IF_NONE_MATCH'] = '"%s"' % ETAG
75  
-        response = self.client.get('/condition/')
76  
-        self.assertNotModified(response)
77  
-
78  
-        self.client.defaults['HTTP_IF_MODIFIED_SINCE'] = EXPIRED_LAST_MODIFIED_STR
79  
-        self.client.defaults['HTTP_IF_NONE_MATCH'] = '"%s"' % ETAG
80  
-        response = self.client.get('/condition/')
81  
-        self.assertFullResponse(response)
82  
-
83  
-        self.client.defaults['HTTP_IF_MODIFIED_SINCE'] = LAST_MODIFIED_STR
84  
-        self.client.defaults['HTTP_IF_NONE_MATCH'] = '"%s"' % EXPIRED_ETAG
85  
-        response = self.client.get('/condition/')
86  
-        self.assertFullResponse(response)
87  
-
88  
-    def testSingleCondition1(self):
89  
-        self.client.defaults['HTTP_IF_MODIFIED_SINCE'] = LAST_MODIFIED_STR
90  
-        response = self.client.get('/condition/last_modified/')
91  
-        self.assertNotModified(response)
92  
-        response = self.client.get('/condition/etag/')
93  
-        self.assertFullResponse(response, check_last_modified=False)
94  
-
95  
-    def testSingleCondition2(self):
96  
-        self.client.defaults['HTTP_IF_NONE_MATCH'] = '"%s"' % ETAG
97  
-        response = self.client.get('/condition/etag/')
98  
-        self.assertNotModified(response)
99  
-        response = self.client.get('/condition/last_modified/')
100  
-        self.assertFullResponse(response, check_etag=False)
101  
-
102  
-    def testSingleCondition3(self):
103  
-        self.client.defaults['HTTP_IF_MODIFIED_SINCE'] = EXPIRED_LAST_MODIFIED_STR
104  
-        response = self.client.get('/condition/last_modified/')
105  
-        self.assertFullResponse(response, check_etag=False)
106  
-
107  
-    def testSingleCondition4(self):
108  
-        self.client.defaults['HTTP_IF_NONE_MATCH'] = '"%s"' % EXPIRED_ETAG
109  
-        response = self.client.get('/condition/etag/')
110  
-        self.assertFullResponse(response, check_last_modified=False)
111  
-
112  
-    def testSingleCondition5(self):
113  
-        self.client.defaults['HTTP_IF_MODIFIED_SINCE'] = LAST_MODIFIED_STR
114  
-        response = self.client.get('/condition/last_modified2/')
115  
-        self.assertNotModified(response)
116  
-        response = self.client.get('/condition/etag2/')
117  
-        self.assertFullResponse(response, check_last_modified=False)
118  
-
119  
-    def testSingleCondition6(self):
120  
-        self.client.defaults['HTTP_IF_NONE_MATCH'] = '"%s"' % ETAG
121  
-        response = self.client.get('/condition/etag2/')
122  
-        self.assertNotModified(response)
123  
-        response = self.client.get('/condition/last_modified2/')
124  
-        self.assertFullResponse(response, check_etag=False)
125  
-
126  
-    def testInvalidETag(self):
127  
-        self.client.defaults['HTTP_IF_NONE_MATCH'] = r'"\"'
128  
-        response = self.client.get('/condition/etag/')
129  
-        self.assertFullResponse(response, check_last_modified=False)
129  tests/conditional_processing/tests.py
... ...
@@ -0,0 +1,129 @@
  1
+# -*- coding:utf-8 -*-
  2
+from __future__ import unicode_literals
  3
+
  4
+from datetime import datetime
  5
+
  6
+from django.test import TestCase
  7
+
  8
+
  9
+FULL_RESPONSE = 'Test conditional get response'
  10
+LAST_MODIFIED = datetime(2007, 10, 21, 23, 21, 47)
  11
+LAST_MODIFIED_STR = 'Sun, 21 Oct 2007 23:21:47 GMT'
  12
+LAST_MODIFIED_NEWER_STR = 'Mon, 18 Oct 2010 16:56:23 GMT'
  13
+LAST_MODIFIED_INVALID_STR = 'Mon, 32 Oct 2010 16:56:23 GMT'
  14
+EXPIRED_LAST_MODIFIED_STR = 'Sat, 20 Oct 2007 23:21:47 GMT'
  15
+ETAG = 'b4246ffc4f62314ca13147c9d4f76974'
  16
+EXPIRED_ETAG = '7fae4cd4b0f81e7d2914700043aa8ed6'
  17
+
  18
+class ConditionalGet(TestCase):
  19
+    urls = 'conditional_processing.urls'
  20
+
  21
+    def assertFullResponse(self, response, check_last_modified=True, check_etag=True):
  22
+        self.assertEqual(response.status_code, 200)
  23
+        self.assertEqual(response.content, FULL_RESPONSE.encode())
  24
+        if check_last_modified:
  25
+            self.assertEqual(response['Last-Modified'], LAST_MODIFIED_STR)
  26
+        if check_etag:
  27
+            self.assertEqual(response['ETag'], '"%s"' % ETAG)
  28
+
  29
+    def assertNotModified(self, response):
  30
+        self.assertEqual(response.status_code, 304)
  31
+        self.assertEqual(response.content, b'')
  32
+
  33
+    def testWithoutConditions(self):
  34
+        response = self.client.get('/condition/')
  35
+        self.assertFullResponse(response)
  36
+
  37
+    def testIfModifiedSince(self):
  38
+        self.client.defaults['HTTP_IF_MODIFIED_SINCE'] = LAST_MODIFIED_STR
  39
+        response = self.client.get('/condition/')
  40
+        self.assertNotModified(response)
  41
+        self.client.defaults['HTTP_IF_MODIFIED_SINCE'] = LAST_MODIFIED_NEWER_STR
  42
+        response = self.client.get('/condition/')
  43
+        self.assertNotModified(response)
  44
+        self.client.defaults['HTTP_IF_MODIFIED_SINCE'] = LAST_MODIFIED_INVALID_STR
  45
+        response = self.client.get('/condition/')
  46
+        self.assertFullResponse(response)
  47
+        self.client.defaults['HTTP_IF_MODIFIED_SINCE'] = EXPIRED_LAST_MODIFIED_STR
  48
+        response = self.client.get('/condition/')
  49
+        self.assertFullResponse(response)
  50
+
  51
+    def testIfNoneMatch(self):
  52
+        self.client.defaults['HTTP_IF_NONE_MATCH'] = '"%s"' % ETAG
  53
+        response = self.client.get('/condition/')
  54
+        self.assertNotModified(response)
  55
+        self.client.defaults['HTTP_IF_NONE_MATCH'] = '"%s"' % EXPIRED_ETAG
  56
+        response = self.client.get('/condition/')
  57
+        self.assertFullResponse(response)
  58
+
  59
+        # Several etags in If-None-Match is a bit exotic but why not?
  60
+        self.client.defaults['HTTP_IF_NONE_MATCH'] = '"%s", "%s"' % (ETAG, EXPIRED_ETAG)
  61
+        response = self.client.get('/condition/')
  62
+        self.assertNotModified(response)
  63
+
  64
+    def testIfMatch(self):
  65
+        self.client.defaults['HTTP_IF_MATCH'] = '"%s"' % ETAG
  66
+        response = self.client.put('/condition/etag/')
  67
+        self.assertEqual(response.status_code, 200)
  68
+        self.client.defaults['HTTP_IF_MATCH'] = '"%s"' % EXPIRED_ETAG
  69
+        response = self.client.put('/condition/etag/')
  70
+        self.assertEqual(response.status_code, 412)
  71
+
  72
+    def testBothHeaders(self):
  73
+        self.client.defaults['HTTP_IF_MODIFIED_SINCE'] = LAST_MODIFIED_STR
  74
+        self.client.defaults['HTTP_IF_NONE_MATCH'] = '"%s"' % ETAG
  75
+        response = self.client.get('/condition/')
  76
+        self.assertNotModified(response)
  77
+
  78
+        self.client.defaults['HTTP_IF_MODIFIED_SINCE'] = EXPIRED_LAST_MODIFIED_STR
  79
+        self.client.defaults['HTTP_IF_NONE_MATCH'] = '"%s"' % ETAG
  80
+        response = self.client.get('/condition/')
  81
+        self.assertFullResponse(response)
  82
+
  83
+        self.client.defaults['HTTP_IF_MODIFIED_SINCE'] = LAST_MODIFIED_STR
  84
+        self.client.defaults['HTTP_IF_NONE_MATCH'] = '"%s"' % EXPIRED_ETAG
  85
+        response = self.client.get('/condition/')
  86
+        self.assertFullResponse(response)
  87
+
  88
+    def testSingleCondition1(self):
  89
+        self.client.defaults['HTTP_IF_MODIFIED_SINCE'] = LAST_MODIFIED_STR
  90
+        response = self.client.get('/condition/last_modified/')
  91
+        self.assertNotModified(response)
  92
+        response = self.client.get('/condition/etag/')
  93
+        self.assertFullResponse(response, check_last_modified=False)
  94
+
  95
+    def testSingleCondition2(self):
  96
+        self.client.defaults['HTTP_IF_NONE_MATCH'] = '"%s"' % ETAG
  97
+        response = self.client.get('/condition/etag/')
  98
+        self.assertNotModified(response)
  99
+        response = self.client.get('/condition/last_modified/')
  100
+        self.assertFullResponse(response, check_etag=False)
  101
+
  102
+    def testSingleCondition3(self):
  103
+        self.client.defaults['HTTP_IF_MODIFIED_SINCE'] = EXPIRED_LAST_MODIFIED_STR
  104
+        response = self.client.get('/condition/last_modified/')
  105
+        self.assertFullResponse(response, check_etag=False)
  106
+
  107
+    def testSingleCondition4(self):
  108
+        self.client.defaults['HTTP_IF_NONE_MATCH'] = '"%s"' % EXPIRED_ETAG
  109
+        response = self.client.get('/condition/etag/')
  110
+        self.assertFullResponse(response, check_last_modified=False)
  111
+
  112
+    def testSingleCondition5(self):
  113
+        self.client.defaults['HTTP_IF_MODIFIED_SINCE'] = LAST_MODIFIED_STR
  114
+        response = self.client.get('/condition/last_modified2/')
  115
+        self.assertNotModified(response)
  116
+        response = self.client.get('/condition/etag2/')
  117
+        self.assertFullResponse(response, check_last_modified=False)
  118
+
  119
+    def testSingleCondition6(self):
  120
+        self.client.defaults['HTTP_IF_NONE_MATCH'] = '"%s"' % ETAG
  121
+        response = self.client.get('/condition/etag2/')
  122
+        self.assertNotModified(response)
  123
+        response = self.client.get('/condition/last_modified2/')
  124
+        self.assertFullResponse(response, check_etag=False)
  125
+
  126
+    def testInvalidETag(self):
  127
+        self.client.defaults['HTTP_IF_NONE_MATCH'] = r'"\"'
  128
+        response = self.client.get('/condition/etag/')
  129
+        self.assertFullResponse(response, check_last_modified=False)
2  tests/conditional_processing/views.py
@@ -4,7 +4,7 @@
4 4
 from django.views.decorators.http import condition, etag, last_modified
5 5
 from django.http import HttpResponse
6 6
 
7  
-from .models import FULL_RESPONSE, LAST_MODIFIED, ETAG
  7
+from .tests import FULL_RESPONSE, LAST_MODIFIED, ETAG
8 8
 
9 9
 
10 10
 def index(request):
2  tests/forms_tests/tests/__init__.py
@@ -12,7 +12,7 @@
12 12
     SimpleDateFormatTests, LocalizedDateTimeTests,
13 13
     CustomDateTimeInputFormatsTests, SimpleDateTimeFormatTests)
14 14
 from .media import FormsMediaTestCase, StaticFormsMediaTestCase
15  
-from .models import (TestTicket12510, ModelFormCallableModelDefault,
  15
+from .tests import (TestTicket12510, ModelFormCallableModelDefault,
16 16
     FormsModelTestCase, RelatedModelFormTests)
17 17
 from .regressions import FormsRegressionsTestCase
18 18
 from .util import FormsUtilTestCase
218  tests/forms_tests/tests/models.py
... ...
@@ -1,218 +0,0 @@
1  
-# -*- coding: utf-8 -*-
2  
-from __future__ import absolute_import, unicode_literals
3  
-
4  
-import datetime
5  
-
6  
-from django.core.files.uploadedfile import SimpleUploadedFile
7  
-from django.db import models
8  
-from django.forms import Form, ModelForm, FileField, ModelChoiceField
9  
-from django.forms.models import ModelFormMetaclass
10  
-from django.test import TestCase
11  
-from django.utils import six
12  
-
13  
-from ..models import (ChoiceOptionModel, ChoiceFieldModel, FileModel, Group,
14  
-    BoundaryModel, Defaults, OptionalMultiChoiceModel)
15  
-
16  
-
17  
-class ChoiceFieldForm(ModelForm):
18  
-    class Meta:
19  
-        model = ChoiceFieldModel
20  
-
21  
-
22  
-class OptionalMultiChoiceModelForm(ModelForm):
23  
-    class Meta:
24  
-        model = OptionalMultiChoiceModel
25  
-
26  
-
27  
-class FileForm(Form):
28  
-    file1 = FileField()
29  
-
30  
-
31  
-class TestTicket12510(TestCase):
32  
-    ''' It is not necessary to generate choices for ModelChoiceField (regression test for #12510). '''
33  
-    def setUp(self):
34  
-        self.groups = [Group.objects.create(name=name) for name in 'abc']
35  
-
36  
-    def test_choices_not_fetched_when_not_rendering(self):
37  
-        # only one query is required to pull the model from DB
38  
-        with self.assertNumQueries(1):
39  
-            field = ModelChoiceField(Group.objects.order_by('-name'))
40  
-            self.assertEqual('a', field.clean(self.groups[0].pk).name)
41  
-
42  
-
43  
-class TestTicket14567(TestCase):
44  
-    """
45  
-    Check that the return values of ModelMultipleChoiceFields are QuerySets
46  
-    """
47  
-    def test_empty_queryset_return(self):
48  
-        "If a model's ManyToManyField has blank=True and is saved with no data, a queryset is returned."
49  
-        form = OptionalMultiChoiceModelForm({'multi_choice_optional': '', 'multi_choice': ['1']})
50  
-        self.assertTrue(form.is_valid())
51  
-        # Check that the empty value is a QuerySet
52  
-        self.assertTrue(isinstance(form.cleaned_data['multi_choice_optional'], models.query.QuerySet))
53  
-        # While we're at it, test whether a QuerySet is returned if there *is* a value.
54  
-        self.assertTrue(isinstance(form.cleaned_data['multi_choice'], models.query.QuerySet))
55  
-
56  
-
57  
-class ModelFormCallableModelDefault(TestCase):
58  
-    def test_no_empty_option(self):
59  
-        "If a model's ForeignKey has blank=False and a default, no empty option is created (Refs #10792)."
60  
-        option = ChoiceOptionModel.objects.create(name='default')
61  
-
62  
-        choices = list(ChoiceFieldForm().fields['choice'].choices)
63  
-        self.assertEqual(len(choices), 1)
64  
-        self.assertEqual(choices[0], (option.pk, six.text_type(option)))
65  
-
66  
-    def test_callable_initial_value(self):
67  
-        "The initial value for a callable default returning a queryset is the pk (refs #13769)"
68  
-        obj1 = ChoiceOptionModel.objects.create(id=1, name='default')
69  
-        obj2 = ChoiceOptionModel.objects.create(id=2, name='option 2')
70  
-        obj3 = ChoiceOptionModel.objects.create(id=3, name='option 3')
71  
-        self.assertHTMLEqual(ChoiceFieldForm().as_p(), """<p><label for="id_choice">Choice:</label> <select name="choice" id="id_choice">
72  
-<option value="1" selected="selected">ChoiceOption 1</option>
73  
-<option value="2">ChoiceOption 2</option>
74  
-<option value="3">ChoiceOption 3</option>
75  
-</select><input type="hidden" name="initial-choice" value="1" id="initial-id_choice" /></p>
76  
-<p><label for="id_choice_int">Choice int:</label> <select name="choice_int" id="id_choice_int">
77  
-<option value="1" selected="selected">ChoiceOption 1</option>
78  
-<option value="2">ChoiceOption 2</option>
79  
-<option value="3">ChoiceOption 3</option>
80  
-</select><input type="hidden" name="initial-choice_int" value="1" id="initial-id_choice_int" /></p>
81  
-<p><label for="id_multi_choice">Multi choice:</label> <select multiple="multiple" name="multi_choice" id="id_multi_choice">
82  
-<option value="1" selected="selected">ChoiceOption 1</option>
83  
-<option value="2">ChoiceOption 2</option>
84  
-<option value="3">ChoiceOption 3</option>
85  
-</select><input type="hidden" name="initial-multi_choice" value="1" id="initial-id_multi_choice_0" /> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></p>
86  
-<p><label for="id_multi_choice_int">Multi choice int:</label> <select multiple="multiple" name="multi_choice_int" id="id_multi_choice_int">
87  
-<option value="1" selected="selected">ChoiceOption 1</option>
88  
-<option value="2">ChoiceOption 2</option>
89  
-<option value="3">ChoiceOption 3</option>
90  
-</select><input type="hidden" name="initial-multi_choice_int" value="1" id="initial-id_multi_choice_int_0" /> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></p>""")
91  
-
92  
-    def test_initial_instance_value(self):
93  
-        "Initial instances for model fields may also be instances (refs #7287)"
94  
-        obj1 = ChoiceOptionModel.objects.create(id=1, name='default')
95  
-        obj2 = ChoiceOptionModel.objects.create(id=2, name='option 2')
96  
-        obj3 = ChoiceOptionModel.objects.create(id=3, name='option 3')
97  
-        self.assertHTMLEqual(ChoiceFieldForm(initial={
98  
-                'choice': obj2,
99  
-                'choice_int': obj2,
100  
-                'multi_choice': [obj2,obj3],
101  
-                'multi_choice_int': ChoiceOptionModel.objects.exclude(name="default"),
102  
-            }).as_p(), """<p><label for="id_choice">Choice:</label> <select name="choice" id="id_choice">
103  
-<option value="1">ChoiceOption 1</option>
104  
-<option value="2" selected="selected">ChoiceOption 2</option>
105  
-<option value="3">ChoiceOption 3</option>
106  
-</select><input type="hidden" name="initial-choice" value="2" id="initial-id_choice" /></p>
107  
-<p><label for="id_choice_int">Choice int:</label> <select name="choice_int" id="id_choice_int">
108  
-<option value="1">ChoiceOption 1</option>
109  
-<option value="2" selected="selected">ChoiceOption 2</option>
110  
-<option value="3">ChoiceOption 3</option>
111  
-</select><input type="hidden" name="initial-choice_int" value="2" id="initial-id_choice_int" /></p>
112  
-<p><label for="id_multi_choice">Multi choice:</label> <select multiple="multiple" name="multi_choice" id="id_multi_choice">
113  
-<option value="1">ChoiceOption 1</option>
114  
-<option value="2" selected="selected">ChoiceOption 2</option>
115  
-<option value="3" selected="selected">ChoiceOption 3</option>
116  
-</select><input type="hidden" name="initial-multi_choice" value="2" id="initial-id_multi_choice_0" />
117  
-<input type="hidden" name="initial-multi_choice" value="3" id="initial-id_multi_choice_1" /> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></p>
118  
-<p><label for="id_multi_choice_int">Multi choice int:</label> <select multiple="multiple" name="multi_choice_int" id="id_multi_choice_int">
119  
-<option value="1">ChoiceOption 1</option>
120  
-<option value="2" selected="selected">ChoiceOption 2</option>
121  
-<option value="3" selected="selected">ChoiceOption 3</option>
122  
-</select><input type="hidden" name="initial-multi_choice_int" value="2" id="initial-id_multi_choice_int_0" />
123  
-<input type="hidden" name="initial-multi_choice_int" value="3" id="initial-id_multi_choice_int_1" /> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></p>""")
124  
-
125  
-
126  
-class FormsModelTestCase(TestCase):
127  
-    def test_unicode_filename(self):
128  
-        # FileModel with unicode filename and data #########################
129  
-        f = FileForm(data={}, files={'file1': SimpleUploadedFile('我隻氣墊船裝滿晒鱔.txt', 'मेरी मँडराने वाली नाव सर्पमीनों से भरी ह'.encode('utf-8'))}, auto_id=False)
130  
-        self.assertTrue(f.is_valid())
131  
-        self.assertTrue('file1' in f.cleaned_data)
132  
-        m = FileModel.objects.create(file=f.cleaned_data['file1'])
133  
-        self.assertEqual(m.file.name, 'tests/\u6211\u96bb\u6c23\u588a\u8239\u88dd\u6eff\u6652\u9c54.txt')
134  
-        m.delete()
135  
-
136  
-    def test_boundary_conditions(self):
137  
-        # Boundary conditions on a PostitiveIntegerField #########################
138  
-        class BoundaryForm(ModelForm):
139  
-            class Meta:
140  
-                model = BoundaryModel
141  
-
142  
-        f = BoundaryForm({'positive_integer': 100})
143  
-        self.assertTrue(f.is_valid())
144  
-        f = BoundaryForm({'positive_integer': 0})
145  
-        self.assertTrue(f.is_valid())
146  
-        f = BoundaryForm({'positive_integer': -100})
147  
-        self.assertFalse(f.is_valid())
148  
-
149  
-    def test_formfield_initial(self):
150  
-        # Formfield initial values ########
151  
-        # If the model has default values for some fields, they are used as the formfield
152  
-        # initial values.
153  
-        class DefaultsForm(ModelForm):
154  
-            class Meta:
155  
-                model = Defaults
156  
-
157  
-        self.assertEqual(DefaultsForm().fields['name'].initial, 'class default value')
158  
-        self.assertEqual(DefaultsForm().fields['def_date'].initial, datetime.date(1980, 1, 1))
159  
-        self.assertEqual(DefaultsForm().fields['value'].initial, 42)
160  
-        r1 = DefaultsForm()['callable_default'].as_widget()
161  
-        r2 = DefaultsForm()['callable_default'].as_widget()
162  
-        self.assertNotEqual(r1, r2)
163  
-
164  
-        # In a ModelForm that is passed an instance, the initial values come from the
165  
-        # instance's values, not the model's defaults.
166  
-        foo_instance = Defaults(name='instance value', def_date=datetime.date(1969, 4, 4), value=12)
167  
-        instance_form = DefaultsForm(instance=foo_instance)
168  
-        self.assertEqual(instance_form.initial['name'], 'instance value')
169  
-        self.assertEqual(instance_form.initial['def_date'], datetime.date(1969, 4, 4))
170  
-        self.assertEqual(instance_form.initial['value'], 12)
171  
-
172  
-        from django.forms import CharField
173  
-
174  
-        class ExcludingForm(ModelForm):
175  
-            name = CharField(max_length=255)
176  
-
177  
-            class Meta:
178  
-                model = Defaults
179  
-                exclude = ['name', 'callable_default']
180  
-
181  
-        f = ExcludingForm({'name': 'Hello', 'value': 99, 'def_date': datetime.date(1999, 3, 2)})
182  
-        self.assertTrue(f.is_valid())
183  
-        self.assertEqual(f.cleaned_data['name'], 'Hello')
184  
-        obj = f.save()
185  
-        self.assertEqual(obj.name, 'class default value')
186  
-        self.assertEqual(obj.value, 99)
187  
-        self.assertEqual(obj.def_date, datetime.date(1999, 3, 2))
188  
-
189  
-class RelatedModelFormTests(TestCase):
190  
-    def test_invalid_loading_order(self):
191  
-        """
192  
-        Test for issue 10405
193  
-        """
194  
-        class A(models.Model):
195  
-            ref = models.ForeignKey("B")
196  
-
197  
-        class Meta:
198  
-            model=A
199  
-
200  
-        self.assertRaises(ValueError, ModelFormMetaclass, str('Form'), (ModelForm,), {'Meta': Meta})
201  
-
202  
-        class B(models.Model):
203  
-            pass
204  
-
205  
-    def test_valid_loading_order(self):
206  
-        """
207  
-        Test for issue 10405
208  
-        """
209  
-        class A(models.Model):
210  
-            ref = models.ForeignKey("B")
211  
-
212  
-        class B(models.Model):
213  
-            pass
214  
-
215  
-        class Meta:
216  
-            model=A
217  
-
218  
-        self.assertTrue(issubclass(ModelFormMetaclass(str('Form'), (ModelForm,), {'Meta': Meta}), ModelForm))
218  tests/forms_tests/tests/tests.py
... ...
@@ -0,0 +1,218 @@
  1
+# -*- coding: utf-8 -*-
  2
+from __future__ import absolute_import, unicode_literals
  3
+
  4
+import datetime
  5
+
  6
+from django.core.files.uploadedfile import SimpleUploadedFile
  7
+from django.db import models
  8
+from django.forms import Form, ModelForm, FileField, ModelChoiceField
  9
+from django.forms.models import ModelFormMetaclass
  10
+from django.test import TestCase
  11
+from django.utils import six
  12
+
  13
+from ..models import (ChoiceOptionModel, ChoiceFieldModel, FileModel, Group,
  14
+    BoundaryModel, Defaults, OptionalMultiChoiceModel)
  15
+
  16
+
  17
+class ChoiceFieldForm(ModelForm):
  18
+    class Meta:
  19
+        model = ChoiceFieldModel
  20
+
  21
+
  22
+class OptionalMultiChoiceModelForm(ModelForm):
  23
+    class Meta:
  24
+        model = OptionalMultiChoiceModel
  25
+
  26
+
  27
+class FileForm(Form):
  28
+    file1 = FileField()
  29
+
  30
+
  31
+class TestTicket12510(TestCase):
  32
+    ''' It is not necessary to generate choices for ModelChoiceField (regression test for #12510). '''
  33
+    def setUp(self):
  34
+        self.groups = [Group.objects.create(name=name) for name in 'abc']
  35
+
  36
+    def test_choices_not_fetched_when_not_rendering(self):
  37
+        # only one query is required to pull the model from DB
  38
+        with self.assertNumQueries(1):
  39
+            field = ModelChoiceField(Group.objects.order_by('-name'))
  40
+            self.assertEqual('a', field.clean(self.groups[0].pk).name)
  41
+
  42
+
  43
+class TestTicket14567(TestCase):
  44
+    """
  45
+    Check that the return values of ModelMultipleChoiceFields are QuerySets
  46
+    """
  47
+    def test_empty_queryset_return(self):
  48
+        "If a model's ManyToManyField has blank=True and is saved with no data, a queryset is returned."
  49
+        form = OptionalMultiChoiceModelForm({'multi_choice_optional': '', 'multi_choice': ['1']})
  50
+        self.assertTrue(form.is_valid())
  51
+        # Check that the empty value is a QuerySet
  52
+        self.assertTrue(isinstance(form.cleaned_data['multi_choice_optional'], models.query.QuerySet))
  53
+        # While we're at it, test whether a QuerySet is returned if there *is* a value.
  54
+        self.assertTrue(isinstance(form.cleaned_data['multi_choice'], models.query.QuerySet))
  55
+
  56
+
  57
+class ModelFormCallableModelDefault(TestCase):
  58
+    def test_no_empty_option(self):
  59
+        "If a model's ForeignKey has blank=False and a default, no empty option is created (Refs #10792)."
  60
+        option = ChoiceOptionModel.objects.create(name='default')
  61
+
  62
+        choices = list(ChoiceFieldForm().fields['choice'].choices)
  63
+        self.assertEqual(len(choices), 1)
  64
+        self.assertEqual(choices[0], (option.pk, six.text_type(option)))
  65
+
  66
+    def test_callable_initial_value(self):
  67
+        "The initial value for a callable default returning a queryset is the pk (refs #13769)"
  68
+        obj1 = ChoiceOptionModel.objects.create(id=1, name='default')
  69
+        obj2 = ChoiceOptionModel.objects.create(id=2, name='option 2')
  70
+        obj3 = ChoiceOptionModel.objects.create(id=3, name='option 3')
  71
+        self.assertHTMLEqual(ChoiceFieldForm().as_p(), """<p><label for="id_choice">Choice:</label> <select name="choice" id="id_choice">
  72
+<option value="1" selected="selected">ChoiceOption 1</option>
  73
+<option value="2">ChoiceOption 2</option>
  74
+<option value="3">ChoiceOption 3</option>
  75
+</select><input type="hidden" name="initial-choice" value="1" id="initial-id_choice" /></p>
  76
+<p><label for="id_choice_int">Choice int:</label> <select name="choice_int" id="id_choice_int">
  77
+<option value="1" selected="selected">ChoiceOption 1</option>
  78
+<option value="2">ChoiceOption 2</option>
  79
+<option value="3">ChoiceOption 3</option>
  80
+</select><input type="hidden" name="initial-choice_int" value="1" id="initial-id_choice_int" /></p>
  81
+<p><label for="id_multi_choice">Multi choice:</label> <select multiple="multiple" name="multi_choice" id="id_multi_choice">
  82
+<option value="1" selected="selected">ChoiceOption 1</option>
  83
+<option value="2">ChoiceOption 2</option>
  84
+<option value="3">ChoiceOption 3</option>
  85
+</select><input type="hidden" name="initial-multi_choice" value="1" id="initial-id_multi_choice_0" /> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></p>
  86
+<p><label for="id_multi_choice_int">Multi choice int:</label> <select multiple="multiple" name="multi_choice_int" id="id_multi_choice_int">
  87
+<option value="1" selected="selected">ChoiceOption 1</option>
  88
+<option value="2">ChoiceOption 2</option>
  89
+<option value="3">ChoiceOption 3</option>
  90
+</select><input type="hidden" name="initial-multi_choice_int" value="1" id="initial-id_multi_choice_int_0" /> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></p>""")
  91
+
  92
+    def test_initial_instance_value(self):
  93
+        "Initial instances for model fields may also be instances (refs #7287)"
  94
+        obj1 = ChoiceOptionModel.objects.create(id=1, name='default')
  95
+        obj2 = ChoiceOptionModel.objects.create(id=2, name='option 2')
  96
+        obj3 = ChoiceOptionModel.objects.create(id=3, name='option 3')
  97
+        self.assertHTMLEqual(ChoiceFieldForm(initial={
  98
+                'choice': obj2,
  99
+                'choice_int': obj2,
  100
+                'multi_choice': [obj2,obj3],
  101
+                'multi_choice_int': ChoiceOptionModel.objects.exclude(name="default"),
  102
+            }).as_p(), """<p><label for="id_choice">Choice:</label> <select name="choice" id="id_choice">
  103
+<option value="1">ChoiceOption 1</option>
  104
+<option value="2" selected="selected">ChoiceOption 2</option>
  105
+<option value="3">ChoiceOption 3</option>
  106
+</select><input type="hidden" name="initial-choice" value="2" id="initial-id_choice" /></p>
  107
+<p><label for="id_choice_int">Choice int:</label> <select name="choice_int" id="id_choice_int">
  108
+<option value="1">ChoiceOption 1</option>
  109
+<option value="2" selected="selected">ChoiceOption 2</option>
  110
+<option value="3">ChoiceOption 3</option>
  111
+</select><input type="hidden" name="initial-choice_int" value="2" id="initial-id_choice_int" /></p>
  112
+<p><label for="id_multi_choice">Multi choice:</label> <select multiple="multiple" name="multi_choice" id="id_multi_choice">
  113
+<option value="1">ChoiceOption 1</option>
  114
+<option value="2" selected="selected">ChoiceOption 2</option>
  115
+<option value="3" selected="selected">ChoiceOption 3</option>
  116
+</select><input type="hidden" name="initial-multi_choice" value="2" id="initial-id_multi_choice_0" />
  117
+<input type="hidden" name="initial-multi_choice" value="3" id="initial-id_multi_choice_1" /> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></p>
  118
+<p><label for="id_multi_choice_int">Multi choice int:</label> <select multiple="multiple" name="multi_choice_int" id="id_multi_choice_int">
  119
+<option value="1">ChoiceOption 1</option>
  120
+<option value="2" selected="selected">ChoiceOption 2</option>
  121
+<option value="3" selected="selected">ChoiceOption 3</option>
  122
+</select><input type="hidden" name="initial-multi_choice_int" value="2" id="initial-id_multi_choice_int_0" />
  123
+<input type="hidden" name="initial-multi_choice_int" value="3" id="initial-id_multi_choice_int_1" /> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></p>""")
  124
+
  125
+
  126
+class FormsModelTestCase(TestCase):
  127
+    def test_unicode_filename(self):
  128
+        # FileModel with unicode filename and data #########################
  129
+        f = FileForm(data={}, files={'file1': SimpleUploadedFile('我隻氣墊船裝滿晒鱔.txt', 'मेरी मँडराने वाली नाव सर्पमीनों से भरी ह'.encode('utf-8'))}, auto_id=False)
  130
+        self.assertTrue(f.is_valid())
  131
+        self.assertTrue('file1' in f.cleaned_data)
  132
+        m = FileModel.objects.create(file=f.cleaned_data['file1'])
  133
+        self.assertEqual(m.file.name, 'tests/\u6211\u96bb\u6c23\u588a\u8239\u88dd\u6eff\u6652\u9c54.txt')
  134
+        m.delete()
  135
+
  136
+    def test_boundary_conditions(self):
  137
+        # Boundary conditions on a PostitiveIntegerField #########################
  138
+        class BoundaryForm(ModelForm):
  139
+            class Meta:
  140
+                model = BoundaryModel
  141
+
  142
+        f = BoundaryForm({'positive_integer': 100})
  143
+        self.assertTrue(f.is_valid())
  144
+        f = BoundaryForm({'positive_integer': 0})
  145
+        self.assertTrue(f.is_valid())
  146
+        f = BoundaryForm({'positive_integer': -100})
  147
+        self.assertFalse(f.is_valid())
  148
+
  149
+    def test_formfield_initial(self):
  150
+        # Formfield initial values ########
  151
+        # If the model has default values for some fields, they are used as the formfield
  152
+        # initial values.
  153
+        class DefaultsForm(ModelForm):
  154
+            class Meta:
  155
+                model = Defaults
  156
+
  157
+        self.assertEqual(DefaultsForm().fields['name'].initial, 'class default value')
  158
+        self.assertEqual(DefaultsForm().fields['def_date'].initial, datetime.date(1980, 1, 1))
  159
+        self.assertEqual(DefaultsForm().fields['value'].initial, 42)
  160
+        r1 = DefaultsForm()['callable_default'].as_widget()
  161
+        r2 = DefaultsForm()['callable_default'].as_widget()
  162
+        self.assertNotEqual(r1, r2)
  163
+
  164
+        # In a ModelForm that is passed an instance, the initial values come from the
  165
+        # instance's values, not the model's defaults.
  166
+        foo_instance = Defaults(name='instance value', def_date=datetime.date(1969, 4, 4), value=12)
  167
+        instance_form = DefaultsForm(instance=foo_instance)
  168
+        self.assertEqual(instance_form.initial['name'], 'instance value')
  169
+        self.assertEqual(instance_form.initial['def_date'], datetime.date(1969, 4, 4))
  170
+        self.assertEqual(instance_form.initial['value'], 12)
  171
+
  172
+        from django.forms import CharField
  173
+
  174
+        class ExcludingForm(ModelForm):
  175
+            name = CharField(max_length=255)
  176
+
  177
+            class Meta:
  178
+                model = Defaults
  179
+                exclude = ['name', 'callable_default']
  180
+
  181
+        f = ExcludingForm({'name': 'Hello', 'value': 99, 'def_date': datetime.date(1999, 3, 2)})
  182
+        self.assertTrue(f.is_valid())
  183
+        self.assertEqual(f.cleaned_data['name'], 'Hello')
  184
+        obj = f.save()
  185
+        self.assertEqual(obj.name, 'class default value')
  186
+        self.assertEqual(obj.value, 99)
  187
+        self.assertEqual(obj.def_date, datetime.date(1999, 3, 2))
  188
+
  189
+class RelatedModelFormTests(TestCase):
  190
+    def test_invalid_loading_order(self):
  191
+        """
  192
+        Test for issue 10405
  193
+        """
  194
+        class A(models.Model):
  195
+            ref = models.ForeignKey("B")
  196
+
  197
+        class Meta:
  198
+            model=A
  199
+
  200
+        self.assertRaises(ValueError, ModelFormMetaclass, str('Form'), (ModelForm,), {'Meta': Meta})
  201
+
  202
+        class B(models.Model):
  203
+            pass
  204
+
  205
+    def test_valid_loading_order(self):
  206
+        """
  207
+        Test for issue 10405
  208
+        """
  209
+        class A(models.Model):
  210
+            ref = models.ForeignKey("B")
  211
+
  212
+        class B(models.Model):
  213
+            pass
  214
+
  215
+        class Meta:
  216
+            model=A
  217
+
  218
+        self.assertTrue(issubclass(ModelFormMetaclass(str('Form'), (ModelForm,), {'Meta': Meta}), ModelForm))
508  tests/test_client/models.py
... ...
@@ -1,508 +0,0 @@
1  
-# coding: utf-8
2  
-"""
3  
-39. Testing using the Test Client
4  
-
5  
-The test client is a class that can act like a simple
6  
-browser for testing purposes.
7  
-
8  
-It allows the user to compose GET and POST requests, and
9  
-obtain the response that the server gave to those requests.
10  
-The server Response objects are annotated with the details
11  
-of the contexts and templates that were rendered during the
12  
-process of serving the request.
13  
-
14  
-``Client`` objects are stateful - they will retain cookie (and
15  
-thus session) details for the lifetime of the ``Client`` instance.
16  
-
17  
-This is not intended as a replacement for Twill, Selenium, or
18  
-other browser automation frameworks - it is here to allow
19  
-testing against the contexts and templates produced by a view,
20  
-rather than the HTML rendered to the end-user.
21  
-
22  
-"""
23  
-from __future__ import absolute_import, unicode_literals
24  
-
25  
-from django.conf import settings
26  
-from django.core import mail
27  
-from django.test import Client, TestCase, RequestFactory
28  
-from django.test.utils import override_settings
29  
-
30  
-from .views import get_view
31  
-
32  
-@override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',))
33  
-class ClientTest(TestCase):
34  
-    fixtures = ['testdata.json']
35  
-
36  
-    def test_get_view(self):
37  
-        "GET a view"
38  
-        # The data is ignored, but let's check it doesn't crash the system
39  
-        # anyway.
40  
-        data = {'var': '\xf2'}
41  
-        response = self.client.get('/test_client/get_view/', data)
42  
-
43  
-        # Check some response details
44  
-        self.assertContains(response, 'This is a test')
45  
-        self.assertEqual(response.context['var'], '\xf2')
46  
-        self.assertEqual(response.templates[0].name, 'GET Template')
47  
-
48  
-    def test_get_post_view(self):
49  
-        "GET a view that normally expects POSTs"
50  
-        response = self.client.get('/test_client/post_view/', {})
51  
-
52  
-        # Check some response details
53  
-        self.assertEqual(response.status_code, 200)
54  
-        self.assertEqual(response.templates[0].name, 'Empty GET Template')
55  
-        self.assertTemplateUsed(response, 'Empty GET Template')
56  
-        self.assertTemplateNotUsed(response, 'Empty POST Template')
57  
-
58  
-    def test_empty_post(self):
59  
-        "POST an empty dictionary to a view"
60  
-        response = self.client.post('/test_client/post_view/', {})
61  
-
62  
-        # Check some response details
63  
-        self.assertEqual(response.status_code, 200)
64  
-        self.assertEqual(response.templates[0].name, 'Empty POST Template')
65  
-        self.assertTemplateNotUsed(response, 'Empty GET Template')
66  
-        self.assertTemplateUsed(response, 'Empty POST Template')
67  
-
68  
-    def test_post(self):
69  
-        "POST some data to a view"
70  
-        post_data = {
71  
-            'value': 37
72  
-        }
73  
-        response = self.client.post('/test_client/post_view/', post_data)
74  
-
75  
-        # Check some response details
76  
-        self.assertEqual(response.status_code, 200)
77  
-        self.assertEqual(response.context['data'], '37')
78  
-        self.assertEqual(response.templates[0].name, 'POST Template')
79  
-        self.assertContains(response, 'Data received')
80  
-
81  
-    def test_response_headers(self):
82  
-        "Check the value of HTTP headers returned in a response"
83  
-        response = self.client.get("/test_client/header_view/")
84  
-
85  
-        self.assertEqual(response['X-DJANGO-TEST'], 'Slartibartfast')
86  
-
87  
-    def test_raw_post(self):
88  
-        "POST raw data (with a content type) to a view"
89  
-        test_doc = """<?xml version="1.0" encoding="utf-8"?><library><book><title>Blink</title><author>Malcolm Gladwell</author></book></library>"""
90  
-        response = self.client.post("/test_client/raw_post_view/", test_doc,
91  
-                                    content_type="text/xml")
92  
-        self.assertEqual(response.status_code, 200)
93  
-        self.assertEqual(response.templates[0].name, "Book template")
94  
-        self.assertEqual(response.content, b"Blink - Malcolm Gladwell")
95  
-
96  
-    def test_redirect(self):
97  
-        "GET a URL that redirects elsewhere"
98  
-        response = self.client.get('/test_client/redirect_view/')
99  
-        # Check that the response was a 302 (redirect) and that
100  
-        # assertRedirect() understands to put an implicit http://testserver/ in
101  
-        # front of non-absolute URLs.
102  
-        self.assertRedirects(response, '/test_client/get_view/')
103  
-
104  
-        host = 'django.testserver'
105  
-        client_providing_host = Client(HTTP_HOST=host)
106  
-        response = client_providing_host.get('/test_client/redirect_view/')
107  
-        # Check that the response was a 302 (redirect) with absolute URI
108  
-        self.assertRedirects(response, '/test_client/get_view/', host=host)
109  
-
110  
-    def test_redirect_with_query(self):
111  
-        "GET a URL that redirects with given GET parameters"
112  
-        response = self.client.get('/test_client/redirect_view/', {'var': 'value'})
113  
-
114  
-        # Check if parameters are intact
115  
-        self.assertRedirects(response, 'http://testserver/test_client/get_view/?var=value')
116  
-
117  
-    def test_permanent_redirect(self):
118  
-        "GET a URL that redirects permanently elsewhere"
119  
-        response = self.client.get('/test_client/permanent_redirect_view/')
120  
-        # Check that the response was a 301 (permanent redirect)
121  
-        self.assertRedirects(response, 'http://testserver/test_client/get_view/', status_code=301)
122  
-
123  
-        client_providing_host = Client(HTTP_HOST='django.testserver')
124  
-        response = client_providing_host.get('/test_client/permanent_redirect_view/')
125  
-        # Check that the response was a 301 (permanent redirect) with absolute URI
126  
-        self.assertRedirects(response, 'http://django.testserver/test_client/get_view/', status_code=301)
127  
-
128  
-    def test_temporary_redirect(self):
129  
-        "GET a URL that does a non-permanent redirect"
130  
-        response = self.client.get('/test_client/temporary_redirect_view/')
131  
-        # Check that the response was a 302 (non-permanent redirect)
132  
-        self.assertRedirects(response, 'http://testserver/test_client/get_view/', status_code=302)
133  
-
134  
-    def test_redirect_to_strange_location(self):
135  
-        "GET a URL that redirects to a non-200 page"
136  
-        response = self.client.get('/test_client/double_redirect_view/')
137  
-
138  
-        # Check that the response was a 302, and that
139  
-        # the attempt to get the redirection location returned 301 when retrieved
140  
-        self.assertRedirects(response, 'http://testserver/test_client/permanent_redirect_view/', target_status_code=301)
141  
-
142  
-    def test_follow_redirect(self):
143  
-        "A URL that redirects can be followed to termination."
144  
-        response = self.client.get('/test_client/double_redirect_view/', follow=True)
145  
-        self.assertRedirects(response, 'http://testserver/test_client/get_view/', status_code=302, target_status_code=200)
146  
-        self.assertEqual(len(response.redirect_chain), 2)
147  
-
148  
-    def test_redirect_http(self):
149  
-        "GET a URL that redirects to an http URI"
150  
-        response = self.client.get('/test_client/http_redirect_view/',follow=True)
151  
-        self.assertFalse(response.test_was_secure_request)
152  
-
153  
-    def test_redirect_https(self):
154  
-        "GET a URL that redirects to an https URI"
155  
-        response = self.client.get('/test_client/https_redirect_view/',follow=True)
156  
-        self.assertTrue(response.test_was_secure_request)
157  
-
158  
-    def test_notfound_response(self):
159  
-        "GET a URL that responds as '404:Not Found'"
160  
-        response = self.client.get('/test_client/bad_view/')
161  
-
162  
-        # Check that the response was a 404, and that the content contains MAGIC
163  
-        self.assertContains(response, 'MAGIC', status_code=404)
164  
-
165  
-    def test_valid_form(self):
166  
-        "POST valid data to a form"
167  
-        post_data = {
168  
-            'text': 'Hello World',
169  
-            'email': 'foo@example.com',
170  
-            'value': 37,
171  
-            'single': 'b',
172  
-            'multi': ('b','c','e')
173  
-        }
174  
-        response = self.client.post('/test_client/form_view/', post_data)
175  
-        self.assertEqual(response.status_code, 200)
176  
-        self.assertTemplateUsed(response, "Valid POST Template")
177  
-
178  
-    def test_valid_form_with_hints(self):
179  
-        "GET a form, providing hints in the GET data"
180  
-        hints = {
181  
-            'text': 'Hello World',
182  
-            'multi': ('b','c','e')
183  
-        }
184  
-        response = self.client.get('/test_client/form_view/', data=hints)
185  
-        self.assertEqual(response.status_code, 200)
186  
-        self.assertTemplateUsed(response, "Form GET Template")
187  
-        # Check that the multi-value data has been rolled out ok
188  
-        self.assertContains(response, 'Select a valid choice.', 0)
189  
-
190  
-    def test_incomplete_data_form(self):
191  
-        "POST incomplete data to a form"
192  
-        post_data = {
193  
-            'text': 'Hello World',
194  
-            'value': 37
195  
-        }
196  
-        response = self.client.post('/test_client/form_view/', post_data)
197  
-        self.assertContains(response, 'This field is required.', 3)
198  
-        self.assertEqual(response.status_code, 200)
199  
-        self.assertTemplateUsed(response, "Invalid POST Template")
200  
-
201  
-        self.assertFormError(response, 'form', 'email', 'This field is required.')
202  
-        self.assertFormError(response, 'form', 'single', 'This field is required.')
203  
-        self.assertFormError(response, 'form', 'multi', 'This field is required.')
204  
-
205  
-    def test_form_error(self):
206  
-        "POST erroneous data to a form"
207  
-        post_data = {
208  
-            'text': 'Hello World',
209  
-            'email': 'not an email address',
210  
-            'value': 37,
211  
-            'single': 'b',
212  
-            'multi': ('b','c','e')
213  
-        }
214  
-        response = self.client.post('/test_client/form_view/', post_data)
215  
-        self.assertEqual(response.status_code, 200)
216  
-        self.assertTemplateUsed(response, "Invalid POST Template")
217  
-
218  
-        self.assertFormError(response, 'form', 'email', 'Enter a valid email address.')
219  
-
220  
-    def test_valid_form_with_template(self):
221  
-        "POST valid data to a form using multiple templates"
222  
-        post_data = {
223  
-            'text': 'Hello World',
224  
-            'email': 'foo@example.com',
225  
-            'value': 37,
226  
-            'single': 'b',
227  
-            'multi': ('b','c','e')
228  
-        }
229  
-        response = self.client.post('/test_client/form_view_with_template/', post_data)
230  
-        self.assertContains(response, 'POST data OK')
231  
-        self.assertTemplateUsed(response, "form_view.html")
232  
-        self.assertTemplateUsed(response, 'base.html')
233  
-        self.assertTemplateNotUsed(response, "Valid POST Template")
234  
-
235  
-    def test_incomplete_data_form_with_template(self):
236  
-        "POST incomplete data to a form using multiple templates"
237  
-        post_data = {
238  
-            'text': 'Hello World',
239  
-            'value': 37
240  
-        }
241  
-        response = self.client.post('/test_client/form_view_with_template/', post_data)
242  
-        self.assertContains(response, 'POST data has errors')
243  
-        self.assertTemplateUsed(response, 'form_view.html')
244  
-        self.assertTemplateUsed(response, 'base.html')
245  
-        self.assertTemplateNotUsed(response, "Invalid POST Template")
246  
-
247  
-        self.assertFormError(response, 'form', 'email', 'This field is required.')
248  
-        self.assertFormError(response, 'form', 'single', 'This field is required.')
249  
-        self.assertFormError(response, 'form', 'multi', 'This field is required.')
250  
-
251  
-    def test_form_error_with_template(self):
252  
-        "POST erroneous data to a form using multiple templates"
253  
-        post_data = {
254  
-            'text': 'Hello World',
255  
-            'email': 'not an email address',
256  
-            'value': 37,
257  
-            'single': 'b',
258  
-            'multi': ('b','c','e')
259  
-        }
260  
-        response = self.client.post('/test_client/form_view_with_template/', post_data)
261  
-        self.assertContains(response, 'POST data has errors')
262  
-        self.assertTemplateUsed(response, "form_view.html")
263  
-        self.assertTemplateUsed(response, 'base.html')
264  
-        self.assertTemplateNotUsed(response, "Invalid POST Template")
265  
-
266  
-        self.assertFormError(response, 'form', 'email', 'Enter a valid email address.')
267  
-
268  
-    def test_unknown_page(self):
269  
-        "GET an invalid URL"
270  
-        response = self.client.get('/test_client/unknown_view/')
271  
-
272  
-        # Check that the response was a 404
273  
-        self.assertEqual(response.status_code, 404)
274  
-
275  
-    def test_url_parameters(self):
276  
-        "Make sure that URL ;-parameters are not stripped."
277  
-        response = self.client.get('/test_client/unknown_view/;some-parameter')
278  
-
279  
-        # Check that the path in the response includes it (ignore that it's a 404)
280  
-        self.assertEqual(response.request['PATH_INFO'], '/test_client/unknown_view/;some-parameter')
281  
-
282  
-    def test_view_with_login(self):
283  
-        "Request a page that is protected with @login_required"
284  
-
285  
-        # Get the page without logging in. Should result in 302.
286  
-        response = self.client.get('/test_client/login_protected_view/')
287  
-        self.assertRedirects(response, 'http://testserver/accounts/login/?next=/test_client/login_protected_view/')
288  
-
289  
-        # Log in
290  
-        login = self.client.login(username='testclient', password='password')
291  
-        self.assertTrue(login, 'Could not log in')
292  
-
293  
-        # Request a page that requires a login
294  
-        response = self.client.get('/test_client/login_protected_view/')
295  
-        self.assertEqual(response.status_code, 200)
296  
-        self.assertEqual(response.context['user'].username, 'testclient')
297  
-
298  
-    def test_view_with_method_login(self):
299  
-        "Request a page that is protected with a @login_required method"
300  
-
301  
-        # Get the page without logging in. Should result in 302.
302  
-        response = self.client.get('/test_client/login_protected_method_view/')
303  
-        self.assertRedirects(response, 'http://testserver/accounts/login/?next=/test_client/login_protected_method_view/')
304  
-
305  
-        # Log in
306  
-        login = self.client.login(username='testclient', password='password')
307  
-        self.assertTrue(login, 'Could not log in')
308  
-
309  
-        # Request a page that requires a login
310  
-        response = self.client.get('/test_client/login_protected_method_view/')
311  
-        self.assertEqual(response.status_code, 200)
312  
-        self.assertEqual(response.context['user'].username, 'testclient')
313  
-
314  
-    def test_view_with_login_and_custom_redirect(self):
315  
-        "Request a page that is protected with @login_required(redirect_field_name='redirect_to')"
316  
-
317  
-        # Get the page without logging in. Should result in 302.
318  
-        response = self.client.get('/test_client/login_protected_view_custom_redirect/')
319  
-        self.assertRedirects(response, 'http://testserver/accounts/login/?redirect_to=/test_client/login_protected_view_custom_redirect/')
320  
-
321  
-        # Log in
322  
-        login = self.client.login(username='testclient', password='password')
323  
-        self.assertTrue(login, 'Could not log in')
324  
-
325  
-        # Request a page that requires a login
326  
-        response = self.client.get('/test_client/login_protected_view_custom_redirect/')
327  
-        self.assertEqual(response.status_code, 200)
328  
-        self.assertEqual(response.context['user'].username, 'testclient')
329  
-
330  
-    def test_view_with_bad_login(self):
331  
-        "Request a page that is protected with @login, but use bad credentials"
332  
-
333  
-        login = self.client.login(username='otheruser', password='nopassword')
334  
-        self.assertFalse(login)
335  
-
336  
-    def test_view_with_inactive_login(self):
337  
-        "Request a page that is protected with @login, but use an inactive login"
338  
-
339  
-        login = self.client.login(username='inactive', password='password')
340  
-        self.assertFalse(login)
341  
-
342  
-    def test_logout(self):
343  
-        "Request a logout after logging in"
344  
-        # Log in
345  
-        self.client.login(username='testclient', password='password')
346  
-
347  
-        # Request a page that requires a login
348  
-        response = self.client.get('/test_client/login_protected_view/')
349  
-        self.assertEqual(response.status_code, 200)
350  
-        self.assertEqual(response.context['user'].username, 'testclient')
351  
-
352  
-        # Log out
353  
-        self.client.logout()
354  
-
355  
-        # Request a page that requires a login
356  
-        response = self.client.get('/test_client/login_protected_view/')
357  
-        self.assertRedirects(response, 'http://testserver/accounts/login/?next=/test_client/login_protected_view/')
358  
-
359  
-    def test_view_with_permissions(self):
360  
-        "Request a page that is protected with @permission_required"
361  
-
362  
-        # Get the page without logging in. Should result in 302.
363  
-        response = self.client.get('/test_client/permission_protected_view/')
364  
-        self.assertRedirects(response, 'http://testserver/accounts/login/?next=/test_client/permission_protected_view/')
365  
-
366  
-        # Log in
367  
-        login = self.client.login(username='testclient', password='password')
368  
-        self.assertTrue(login, 'Could not log in')
369  
-
370  
-        # Log in with wrong permissions. Should result in 302.
371  
-        response = self.client.get('/test_client/permission_protected_view/')
372  
-        self.assertRedirects(response, 'http://testserver/accounts/login/?next=/test_client/permission_protected_view/')
373  
-
374  
-        # TODO: Log in with right permissions and request the page again
375  
-
376  
-    def test_view_with_permissions_exception(self):
377  
-        "Request a page that is protected with @permission_required but raises a exception"
378  
-
379  
-        # Get the page without logging in. Should result in 403.
380  
-        response = self.client.get('/test_client/permission_protected_view_exception/')
381  
-        self.assertEqual(response.status_code, 403)
382  
-
383  
-        # Log in
384  
-        login = self.client.login(username='testclient', password='password')
385  
-        self.assertTrue(login, 'Could not log in')
386  
-
387  
-        # Log in with wrong permissions. Should result in 403.
388  
-        response = self.client.get('/test_client/permission_protected_view_exception/')
389  
-        self.assertEqual(response.status_code, 403)
390  
-
391  
-    def test_view_with_method_permissions(self):
392  
-        "Request a page that is protected with a @permission_required method"
393  
-
394  
-        # Get the page without logging in. Should result in 302.
395  
-        response = self.client.get('/test_client/permission_protected_method_view/')
396  
-        self.assertRedirects(response, 'http://testserver/accounts/login/?next=/test_client/permission_protected_method_view/')
397  
-
398  
-        # Log in
399  
-        login = self.client.login(username='testclient', password='password')
400  
-        self.assertTrue(login, 'Could not log in')
401  
-