Code

Ticket #5441: 5441.2.diff

File 5441.2.diff, 4.3 KB (added by simeon, 7 years ago)
Line 
1Index: django/contrib/formtools/tests.py
2===================================================================
3--- django/contrib/formtools/tests.py   (revision 0)
4+++ django/contrib/formtools/tests.py   (revision 0)
5@@ -0,0 +1,93 @@
6+from django import newforms as forms
7+from django.contrib.formtools import preview
8+from django import http
9+from django.conf import settings
10+from django.test import TestCase
11+from django.test.client import Client
12+
13+
14+success_string = "Done was called!"
15+test_data = {'field1': u'foo',
16+             'field1_': u'asdf'}
17+
18+
19+class TestFormPreview(preview.FormPreview):
20+
21+    def done(self, request, cleaned_data):
22+        return http.HttpResponse(success_string)
23+
24+
25+class TestForm(forms.Form):
26+    field1 = forms.CharField()
27+    field1_ = forms.CharField()
28+
29+
30+class PreviewTests(TestCase):
31+
32+    def setUp(self):
33+        settings.ROOT_URLCONF = 'django.contrib.formtools.test_urls'
34+        # Create a FormPreview instance to share between tests
35+        self.preview = preview.FormPreview(TestForm)
36+        input_template = '<input type="hidden" name="%s" value="%s" />'
37+        self.input = input_template % (self.preview.unused_name('stage'), "%d")
38+
39+    def test_unused_name(self):
40+        """
41+        Verifies name mangling to get uniue field name.
42+        """
43+        self.assertEqual(self.preview.unused_name('field1'), 'field1__')
44+
45+    def test_form_get(self):
46+        """
47+        Test contrib.formtools.preview form retrieval.
48+
49+        Use the client library to see if we can sucessfully retrieve
50+        the form (mostly testing the setup ROOT_URLCONF
51+        process). Verify that an additional  hidden input field
52+        is created to manage the stage.
53+
54+        """
55+        response = self.client.get('/test1/')
56+        stage = self.input % 1
57+        self.assertContains(response, stage, 1)
58+
59+    def test_form_preview(self):
60+        """
61+        Test contrib.formtools.preview form preview rendering.
62+
63+        Use the client library to POST to the form to see if a preview
64+        is returned.  If we do get a form back check that the hidden
65+        value is correctly managing the state of the form.
66+
67+        """
68+        # Pass strings for form submittal and add stage variable to
69+        # show we previously saw first stage of the form.
70+        test_data.update({'stage': 1})
71+        response = self.client.post('/test1/', test_data)
72+        # Check to confirm stage is set to 2 in output form.
73+        stage = self.input % 2
74+        self.assertContains(response, stage, 1)
75+
76+    def test_form_submit(self):
77+        """
78+        Test contrib.formtools.preview form submittal.
79+
80+        Use the client library to POST to the form with stage set to 3
81+        to see if our forms done() method is called. Check first
82+        without the security hash, verify failure, retry with security
83+        hash and verify sucess.
84+
85+        """
86+        # Pass strings for form submittal and add stage variable to
87+        # show we previously saw first stage of the form.
88+        test_data.update({'stage': 2})
89+        response = self.client.post('/test1/', test_data)
90+        self.failIfEqual(response.content, success_string)
91+        hash = self.preview.security_hash(None, TestForm(test_data))
92+        test_data.update({'hash': hash})
93+        response = self.client.post('/test1/', test_data)
94+        self.assertEqual(response.content, success_string)
95+
96+
97+if __name__ == '__main__':
98+    unittest.main()
99Index: django/contrib/formtools/models.py
100===================================================================
101--- django/contrib/formtools/models.py  (revision 0)
102+++ django/contrib/formtools/models.py  (revision 0)
103@@ -0,0 +1 @@
104+""" models.py (even empty) currently required by the runtests.py to enable unit tests """
105Index: django/contrib/formtools/test_urls.py
106===================================================================
107--- django/contrib/formtools/test_urls.py       (revision 0)
108+++ django/contrib/formtools/test_urls.py       (revision 0)
109@@ -0,0 +1,12 @@
110+"""
111+
112+This is a urlconf to be loaded by tests.py. Add any urls needed
113+for tests only.
114+
115+"""
116+from django.conf.urls.defaults import *
117+from django.contrib.formtools.tests import *
118+
119+urlpatterns = patterns('',
120+                       (r'^test1/', TestFormPreview(TestForm)),
121+                      )