Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Modified formtools to work with unittest2 discovery.

  • Loading branch information...
commit 756a70b48de73daccf2a4581996830900239a4b7 1 parent 8aedde0
Preston Timmons authored April 02, 2013 carljm committed April 02, 2013
188  django/contrib/formtools/tests/__init__.py
... ...
@@ -1,188 +1,2 @@
1  
-# -*- coding: utf-8 -*-
2  
-from __future__ import unicode_literals
3  
-
4  
-import datetime
5  
-import os
6  
-import pickle
7  
-import re
8  
-import warnings
9  
-
10  
-from django import http
11  
-from django.conf import settings
12  
-from django.contrib.formtools import preview, utils
13  
-from django.test import TestCase
14  
-from django.test.html import parse_html
15  
-from django.test.utils import override_settings
16  
-from django.utils._os import upath
17  
-from django.utils import unittest
18  
-
  1
+from django.contrib.formtools.tests.tests import *
19 2
 from django.contrib.formtools.tests.wizard import *
20  
-from django.contrib.formtools.tests.forms import *
21  
-
22  
-success_string = "Done was called!"
23  
-success_string_encoded = success_string.encode()
24  
-
25  
-class TestFormPreview(preview.FormPreview):
26  
-    def get_context(self, request, form):
27  
-        context = super(TestFormPreview, self).get_context(request, form)
28  
-        context.update({'custom_context': True})
29  
-        return context
30  
-
31  
-    def get_initial(self, request):
32  
-        return {'field1': 'Works!'}
33  
-
34  
-    def done(self, request, cleaned_data):
35  
-        return http.HttpResponse(success_string)
36  
-
37  
-@override_settings(
38  
-    TEMPLATE_DIRS=(
39  
-        os.path.join(os.path.dirname(upath(__file__)), 'templates'),
40  
-    ),
41  
-)
42  
-class PreviewTests(TestCase):
43  
-    urls = 'django.contrib.formtools.tests.urls'
44  
-
45  
-    def setUp(self):
46  
-        super(PreviewTests, self).setUp()
47  
-        # Create a FormPreview instance to share between tests
48  
-        self.preview = preview.FormPreview(TestForm)
49  
-        input_template = '<input type="hidden" name="%s" value="%s" />'
50  
-        self.input = input_template % (self.preview.unused_name('stage'), "%d")
51  
-        self.test_data = {'field1': 'foo', 'field1_': 'asdf'}
52  
-
53  
-    def test_unused_name(self):
54  
-        """
55  
-        Verifies name mangling to get uniue field name.
56  
-        """
57  
-        self.assertEqual(self.preview.unused_name('field1'), 'field1__')
58  
-
59  
-    def test_form_get(self):
60  
-        """
61  
-        Test contrib.formtools.preview form retrieval.
62  
-
63  
-        Use the client library to see if we can sucessfully retrieve
64  
-        the form (mostly testing the setup ROOT_URLCONF
65  
-        process). Verify that an additional  hidden input field
66  
-        is created to manage the stage.
67  
-
68  
-        """
69  
-        response = self.client.get('/preview/')
70  
-        stage = self.input % 1
71  
-        self.assertContains(response, stage, 1)
72  
-        self.assertEqual(response.context['custom_context'], True)
73  
-        self.assertEqual(response.context['form'].initial, {'field1': 'Works!'})
74  
-
75  
-    def test_form_preview(self):
76  
-        """
77  
-        Test contrib.formtools.preview form preview rendering.
78  
-
79  
-        Use the client library to POST to the form to see if a preview
80  
-        is returned.  If we do get a form back check that the hidden
81  
-        value is correctly managing the state of the form.
82  
-
83  
-        """
84  
-        # Pass strings for form submittal and add stage variable to
85  
-        # show we previously saw first stage of the form.
86  
-        self.test_data.update({'stage': 1, 'date1': datetime.date(2006, 10, 25)})
87  
-        response = self.client.post('/preview/', self.test_data)
88  
-        # Check to confirm stage is set to 2 in output form.
89  
-        stage = self.input % 2
90  
-        self.assertContains(response, stage, 1)
91  
-
92  
-    def test_form_submit(self):
93  
-        """
94  
-        Test contrib.formtools.preview form submittal.
95  
-
96  
-        Use the client library to POST to the form with stage set to 3
97  
-        to see if our forms done() method is called. Check first
98  
-        without the security hash, verify failure, retry with security
99  
-        hash and verify sucess.
100  
-
101  
-        """
102  
-        # Pass strings for form submittal and add stage variable to
103  
-        # show we previously saw first stage of the form.
104  
-        self.test_data.update({'stage': 2, 'date1': datetime.date(2006, 10, 25)})
105  
-        response = self.client.post('/preview/', self.test_data)
106  
-        self.assertNotEqual(response.content, success_string_encoded)
107  
-        hash = self.preview.security_hash(None, TestForm(self.test_data))
108  
-        self.test_data.update({'hash': hash})
109  
-        response = self.client.post('/preview/', self.test_data)
110  
-        self.assertEqual(response.content, success_string_encoded)
111  
-
112  
-    def test_bool_submit(self):
113  
-        """
114  
-        Test contrib.formtools.preview form submittal when form contains:
115  
-        BooleanField(required=False)
116  
-
117  
-        Ticket: #6209 - When an unchecked BooleanField is previewed, the preview
118  
-        form's hash would be computed with no value for ``bool1``. However, when
119  
-        the preview form is rendered, the unchecked hidden BooleanField would be
120  
-        rendered with the string value 'False'. So when the preview form is
121  
-        resubmitted, the hash would be computed with the value 'False' for
122  
-        ``bool1``. We need to make sure the hashes are the same in both cases.
123  
-
124  
-        """
125  
-        self.test_data.update({'stage':2})
126  
-        hash = self.preview.security_hash(None, TestForm(self.test_data))
127  
-        self.test_data.update({'hash': hash, 'bool1': 'False'})
128  
-        with warnings.catch_warnings(record=True):
129  
-            response = self.client.post('/preview/', self.test_data)
130  
-            self.assertEqual(response.content, success_string_encoded)
131  
-
132  
-    def test_form_submit_good_hash(self):
133  
-        """
134  
-        Test contrib.formtools.preview form submittal, using a correct
135  
-        hash
136  
-        """
137  
-        # Pass strings for form submittal and add stage variable to
138  
-        # show we previously saw first stage of the form.
139  
-        self.test_data.update({'stage':2})
140  
-        response = self.client.post('/preview/', self.test_data)
141  
-        self.assertNotEqual(response.content, success_string_encoded)
142  
-        hash = utils.form_hmac(TestForm(self.test_data))
143  
-        self.test_data.update({'hash': hash})
144  
-        response = self.client.post('/preview/', self.test_data)
145  
-        self.assertEqual(response.content, success_string_encoded)
146  
-
147  
-
148  
-    def test_form_submit_bad_hash(self):
149  
-        """
150  
-        Test contrib.formtools.preview form submittal does not proceed
151  
-        if the hash is incorrect.
152  
-        """
153  
-        # Pass strings for form submittal and add stage variable to
154  
-        # show we previously saw first stage of the form.
155  
-        self.test_data.update({'stage':2})
156  
-        response = self.client.post('/preview/', self.test_data)
157  
-        self.assertEqual(response.status_code, 200)
158  
-        self.assertNotEqual(response.content, success_string_encoded)
159  
-        hash = utils.form_hmac(TestForm(self.test_data)) + "bad"
160  
-        self.test_data.update({'hash': hash})
161  
-        response = self.client.post('/previewpreview/', self.test_data)
162  
-        self.assertNotEqual(response.content, success_string_encoded)
163  
-
164  
-
165  
-class FormHmacTests(unittest.TestCase):
166  
-
167  
-    def test_textfield_hash(self):
168  
-        """
169  
-        Regression test for #10034: the hash generation function should ignore
170  
-        leading/trailing whitespace so as to be friendly to broken browsers that
171  
-        submit it (usually in textareas).
172  
-        """
173  
-        f1 = HashTestForm({'name': 'joe', 'bio': 'Speaking español.'})
174  
-        f2 = HashTestForm({'name': '  joe', 'bio': 'Speaking español.  '})
175  
-        hash1 = utils.form_hmac(f1)
176  
-        hash2 = utils.form_hmac(f2)
177  
-        self.assertEqual(hash1, hash2)
178  
-
179  
-    def test_empty_permitted(self):
180  
-        """
181  
-        Regression test for #10643: the security hash should allow forms with
182  
-        empty_permitted = True, or forms where data has not changed.
183  
-        """
184  
-        f1 = HashTestBlankForm({})
185  
-        f2 = HashTestForm({}, empty_permitted=True)
186  
-        hash1 = utils.form_hmac(f1)
187  
-        hash2 = utils.form_hmac(f2)
188  
-        self.assertEqual(hash1, hash2)
187  django/contrib/formtools/tests/tests.py
... ...
@@ -0,0 +1,187 @@
  1
+# -*- coding: utf-8 -*-
  2
+from __future__ import unicode_literals
  3
+
  4
+import datetime
  5
+import os
  6
+import pickle
  7
+import re
  8
+import warnings
  9
+
  10
+from django import http
  11
+from django.conf import settings
  12
+from django.contrib.formtools import preview, utils
  13
+from django.test import TestCase
  14
+from django.test.html import parse_html
  15
+from django.test.utils import override_settings
  16
+from django.utils._os import upath
  17
+from django.utils import unittest
  18
+
  19
+from django.contrib.formtools.tests.forms import *
  20
+
  21
+success_string = "Done was called!"
  22
+success_string_encoded = success_string.encode()
  23
+
  24
+class TestFormPreview(preview.FormPreview):
  25
+    def get_context(self, request, form):
  26
+        context = super(TestFormPreview, self).get_context(request, form)
  27
+        context.update({'custom_context': True})
  28
+        return context
  29
+
  30
+    def get_initial(self, request):
  31
+        return {'field1': 'Works!'}
  32
+
  33
+    def done(self, request, cleaned_data):
  34
+        return http.HttpResponse(success_string)
  35
+
  36
+@override_settings(
  37
+    TEMPLATE_DIRS=(
  38
+        os.path.join(os.path.dirname(upath(__file__)), 'templates'),
  39
+    ),
  40
+)
  41
+class PreviewTests(TestCase):
  42
+    urls = 'django.contrib.formtools.tests.urls'
  43
+
  44
+    def setUp(self):
  45
+        super(PreviewTests, self).setUp()
  46
+        # Create a FormPreview instance to share between tests
  47
+        self.preview = preview.FormPreview(TestForm)
  48
+        input_template = '<input type="hidden" name="%s" value="%s" />'
  49
+        self.input = input_template % (self.preview.unused_name('stage'), "%d")
  50
+        self.test_data = {'field1': 'foo', 'field1_': 'asdf'}
  51
+
  52
+    def test_unused_name(self):
  53
+        """
  54
+        Verifies name mangling to get uniue field name.
  55
+        """
  56
+        self.assertEqual(self.preview.unused_name('field1'), 'field1__')
  57
+
  58
+    def test_form_get(self):
  59
+        """
  60
+        Test contrib.formtools.preview form retrieval.
  61
+
  62
+        Use the client library to see if we can sucessfully retrieve
  63
+        the form (mostly testing the setup ROOT_URLCONF
  64
+        process). Verify that an additional  hidden input field
  65
+        is created to manage the stage.
  66
+
  67
+        """
  68
+        response = self.client.get('/preview/')
  69
+        stage = self.input % 1
  70
+        self.assertContains(response, stage, 1)
  71
+        self.assertEqual(response.context['custom_context'], True)
  72
+        self.assertEqual(response.context['form'].initial, {'field1': 'Works!'})
  73
+
  74
+    def test_form_preview(self):
  75
+        """
  76
+        Test contrib.formtools.preview form preview rendering.
  77
+
  78
+        Use the client library to POST to the form to see if a preview
  79
+        is returned.  If we do get a form back check that the hidden
  80
+        value is correctly managing the state of the form.
  81
+
  82
+        """
  83
+        # Pass strings for form submittal and add stage variable to
  84
+        # show we previously saw first stage of the form.
  85
+        self.test_data.update({'stage': 1, 'date1': datetime.date(2006, 10, 25)})
  86
+        response = self.client.post('/preview/', self.test_data)
  87
+        # Check to confirm stage is set to 2 in output form.
  88
+        stage = self.input % 2
  89
+        self.assertContains(response, stage, 1)
  90
+
  91
+    def test_form_submit(self):
  92
+        """
  93
+        Test contrib.formtools.preview form submittal.
  94
+
  95
+        Use the client library to POST to the form with stage set to 3
  96
+        to see if our forms done() method is called. Check first
  97
+        without the security hash, verify failure, retry with security
  98
+        hash and verify sucess.
  99
+
  100
+        """
  101
+        # Pass strings for form submittal and add stage variable to
  102
+        # show we previously saw first stage of the form.
  103
+        self.test_data.update({'stage': 2, 'date1': datetime.date(2006, 10, 25)})
  104
+        response = self.client.post('/preview/', self.test_data)
  105
+        self.assertNotEqual(response.content, success_string_encoded)
  106
+        hash = self.preview.security_hash(None, TestForm(self.test_data))
  107
+        self.test_data.update({'hash': hash})
  108
+        response = self.client.post('/preview/', self.test_data)
  109
+        self.assertEqual(response.content, success_string_encoded)
  110
+
  111
+    def test_bool_submit(self):
  112
+        """
  113
+        Test contrib.formtools.preview form submittal when form contains:
  114
+        BooleanField(required=False)
  115
+
  116
+        Ticket: #6209 - When an unchecked BooleanField is previewed, the preview
  117
+        form's hash would be computed with no value for ``bool1``. However, when
  118
+        the preview form is rendered, the unchecked hidden BooleanField would be
  119
+        rendered with the string value 'False'. So when the preview form is
  120
+        resubmitted, the hash would be computed with the value 'False' for
  121
+        ``bool1``. We need to make sure the hashes are the same in both cases.
  122
+
  123
+        """
  124
+        self.test_data.update({'stage':2})
  125
+        hash = self.preview.security_hash(None, TestForm(self.test_data))
  126
+        self.test_data.update({'hash': hash, 'bool1': 'False'})
  127
+        with warnings.catch_warnings(record=True):
  128
+            response = self.client.post('/preview/', self.test_data)
  129
+            self.assertEqual(response.content, success_string_encoded)
  130
+
  131
+    def test_form_submit_good_hash(self):
  132
+        """
  133
+        Test contrib.formtools.preview form submittal, using a correct
  134
+        hash
  135
+        """
  136
+        # Pass strings for form submittal and add stage variable to
  137
+        # show we previously saw first stage of the form.
  138
+        self.test_data.update({'stage':2})
  139
+        response = self.client.post('/preview/', self.test_data)
  140
+        self.assertNotEqual(response.content, success_string_encoded)
  141
+        hash = utils.form_hmac(TestForm(self.test_data))
  142
+        self.test_data.update({'hash': hash})
  143
+        response = self.client.post('/preview/', self.test_data)
  144
+        self.assertEqual(response.content, success_string_encoded)
  145
+
  146
+
  147
+    def test_form_submit_bad_hash(self):
  148
+        """
  149
+        Test contrib.formtools.preview form submittal does not proceed
  150
+        if the hash is incorrect.
  151
+        """
  152
+        # Pass strings for form submittal and add stage variable to
  153
+        # show we previously saw first stage of the form.
  154
+        self.test_data.update({'stage':2})
  155
+        response = self.client.post('/preview/', self.test_data)
  156
+        self.assertEqual(response.status_code, 200)
  157
+        self.assertNotEqual(response.content, success_string_encoded)
  158
+        hash = utils.form_hmac(TestForm(self.test_data)) + "bad"
  159
+        self.test_data.update({'hash': hash})
  160
+        response = self.client.post('/previewpreview/', self.test_data)
  161
+        self.assertNotEqual(response.content, success_string_encoded)
  162
+
  163
+
  164
+class FormHmacTests(unittest.TestCase):
  165
+
  166
+    def test_textfield_hash(self):
  167
+        """
  168
+        Regression test for #10034: the hash generation function should ignore
  169
+        leading/trailing whitespace so as to be friendly to broken browsers that
  170
+        submit it (usually in textareas).
  171
+        """
  172
+        f1 = HashTestForm({'name': 'joe', 'bio': 'Speaking español.'})
  173
+        f2 = HashTestForm({'name': '  joe', 'bio': 'Speaking español.  '})
  174
+        hash1 = utils.form_hmac(f1)
  175
+        hash2 = utils.form_hmac(f2)
  176
+        self.assertEqual(hash1, hash2)
  177
+
  178
+    def test_empty_permitted(self):
  179
+        """
  180
+        Regression test for #10643: the security hash should allow forms with
  181
+        empty_permitted = True, or forms where data has not changed.
  182
+        """
  183
+        f1 = HashTestBlankForm({})
  184
+        f2 = HashTestForm({}, empty_permitted=True)
  185
+        hash1 = utils.form_hmac(f1)
  186
+        hash2 = utils.form_hmac(f2)
  187
+        self.assertEqual(hash1, hash2)
8  django/contrib/formtools/tests/wizard/__init__.py
... ...
@@ -1,6 +1,6 @@
1  
-from django.contrib.formtools.tests.wizard.cookiestorage import TestCookieStorage
2  
-from django.contrib.formtools.tests.wizard.forms import FormTests, SessionFormTests, CookieFormTests
3  
-from django.contrib.formtools.tests.wizard.loadstorage import TestLoadStorage
  1
+from django.contrib.formtools.tests.wizard.test_cookiestorage import TestCookieStorage
  2
+from django.contrib.formtools.tests.wizard.test_forms import FormTests, SessionFormTests, CookieFormTests
  3
+from django.contrib.formtools.tests.wizard.test_loadstorage import TestLoadStorage
4 4
 from django.contrib.formtools.tests.wizard.namedwizardtests.tests import (
5 5
     NamedSessionWizardTests,
6 6
     NamedCookieWizardTests,
@@ -9,7 +9,7 @@
9 9
     NamedSessionFormTests,
10 10
     NamedCookieFormTests,
11 11
 )
12  
-from django.contrib.formtools.tests.wizard.sessionstorage import TestSessionStorage
  12
+from django.contrib.formtools.tests.wizard.test_sessionstorage import TestSessionStorage
13 13
 from django.contrib.formtools.tests.wizard.wizardtests.tests import (
14 14
     SessionWizardTests,
15 15
     CookieWizardTests,
2  django/contrib/formtools/tests/wizard/namedwizardtests/tests.py
@@ -9,7 +9,7 @@
9 9
 
10 10
 from django.contrib.formtools.wizard.views import (NamedUrlSessionWizardView,
11 11
                                                    NamedUrlCookieWizardView)
12  
-from django.contrib.formtools.tests.wizard.forms import get_request, Step1, Step2
  12
+from django.contrib.formtools.tests.wizard.test_forms import get_request, Step1, Step2
13 13
 
14 14
 
15 15
 class NamedWizardTests(object):
0  ...o/contrib/formtools/tests/wizard/cookiestorage.py → ...trib/formtools/tests/wizard/test_cookiestorage.py
File renamed without changes
0  django/contrib/formtools/tests/wizard/forms.py → django/contrib/formtools/tests/wizard/test_forms.py
File renamed without changes
0  django/contrib/formtools/tests/wizard/loadstorage.py → ...ontrib/formtools/tests/wizard/test_loadstorage.py
File renamed without changes
0  .../contrib/formtools/tests/wizard/sessionstorage.py → ...rib/formtools/tests/wizard/test_sessionstorage.py
File renamed without changes

0 notes on commit 756a70b

Please sign in to comment.
Something went wrong with that request. Please try again.