Code

Ticket #5441: 5441.txt

File 5441.txt, 4.1 KB (added by simeon, 7 years ago)

Diff spanning 3 files above

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