1 from django import newforms as forms |
1 from django import forms |
2 from django.contrib.formtools import preview |
2 from django.contrib.formtools import preview, wizard |
3 from django import http |
3 from django import http |
4 from django.conf import settings |
|
5 from django.test import TestCase |
4 from django.test import TestCase |
6 |
5 |
7 success_string = "Done was called!" |
6 success_string = "Done was called!" |
8 test_data = {'field1': u'foo', |
|
9 'field1_': u'asdf'} |
|
10 |
|
11 |
7 |
12 class TestFormPreview(preview.FormPreview): |
8 class TestFormPreview(preview.FormPreview): |
13 |
9 |
14 def done(self, request, cleaned_data): |
10 def done(self, request, cleaned_data): |
15 return http.HttpResponse(success_string) |
11 return http.HttpResponse(success_string) |
16 |
12 |
17 |
|
18 class TestForm(forms.Form): |
13 class TestForm(forms.Form): |
19 field1 = forms.CharField() |
14 field1 = forms.CharField() |
20 field1_ = forms.CharField() |
15 field1_ = forms.CharField() |
21 |
16 bool1 = forms.BooleanField(required=False) |
22 |
17 |
23 class PreviewTests(TestCase): |
18 class PreviewTests(TestCase): |
|
19 urls = 'django.contrib.formtools.test_urls' |
24 |
20 |
25 def setUp(self): |
21 def setUp(self): |
26 settings.ROOT_URLCONF = 'django.contrib.formtools.test_urls' |
|
27 # Create a FormPreview instance to share between tests |
22 # Create a FormPreview instance to share between tests |
28 self.preview = preview.FormPreview(TestForm) |
23 self.preview = preview.FormPreview(TestForm) |
29 input_template = '<input type="hidden" name="%s" value="%s" />' |
24 input_template = '<input type="hidden" name="%s" value="%s" />' |
30 self.input = input_template % (self.preview.unused_name('stage'), "%d") |
25 self.input = input_template % (self.preview.unused_name('stage'), "%d") |
|
26 self.test_data = {'field1':u'foo', 'field1_':u'asdf'} |
31 |
27 |
32 def test_unused_name(self): |
28 def test_unused_name(self): |
33 """ |
29 """ |
34 Verifies name mangling to get uniue field name. |
30 Verifies name mangling to get uniue field name. |
35 """ |
31 """ |
58 value is correctly managing the state of the form. |
54 value is correctly managing the state of the form. |
59 |
55 |
60 """ |
56 """ |
61 # Pass strings for form submittal and add stage variable to |
57 # Pass strings for form submittal and add stage variable to |
62 # show we previously saw first stage of the form. |
58 # show we previously saw first stage of the form. |
63 test_data.update({'stage': 1}) |
59 self.test_data.update({'stage': 1}) |
64 response = self.client.post('/test1/', test_data) |
60 response = self.client.post('/test1/', self.test_data) |
65 # Check to confirm stage is set to 2 in output form. |
61 # Check to confirm stage is set to 2 in output form. |
66 stage = self.input % 2 |
62 stage = self.input % 2 |
67 self.assertContains(response, stage, 1) |
63 self.assertContains(response, stage, 1) |
68 |
64 |
69 def test_form_submit(self): |
65 def test_form_submit(self): |
76 hash and verify sucess. |
72 hash and verify sucess. |
77 |
73 |
78 """ |
74 """ |
79 # Pass strings for form submittal and add stage variable to |
75 # Pass strings for form submittal and add stage variable to |
80 # show we previously saw first stage of the form. |
76 # show we previously saw first stage of the form. |
81 test_data.update({'stage': 2}) |
77 self.test_data.update({'stage':2}) |
82 response = self.client.post('/test1/', test_data) |
78 response = self.client.post('/test1/', self.test_data) |
83 self.failIfEqual(response.content, success_string) |
79 self.failIfEqual(response.content, success_string) |
84 hash = self.preview.security_hash(None, TestForm(test_data)) |
80 hash = self.preview.security_hash(None, TestForm(self.test_data)) |
85 test_data.update({'hash': hash}) |
81 self.test_data.update({'hash': hash}) |
86 response = self.client.post('/test1/', test_data) |
82 response = self.client.post('/test1/', self.test_data) |
87 self.assertEqual(response.content, success_string) |
83 self.assertEqual(response.content, success_string) |
88 |
84 |
|
85 def test_bool_submit(self): |
|
86 """ |
|
87 Test contrib.formtools.preview form submittal when form contains: |
|
88 BooleanField(required=False) |
|
89 |
|
90 Ticket: #6209 - When an unchecked BooleanField is previewed, the preview |
|
91 form's hash would be computed with no value for ``bool1``. However, when |
|
92 the preview form is rendered, the unchecked hidden BooleanField would be |
|
93 rendered with the string value 'False'. So when the preview form is |
|
94 resubmitted, the hash would be computed with the value 'False' for |
|
95 ``bool1``. We need to make sure the hashes are the same in both cases. |
|
96 |
|
97 """ |
|
98 self.test_data.update({'stage':2}) |
|
99 hash = self.preview.security_hash(None, TestForm(self.test_data)) |
|
100 self.test_data.update({'hash':hash, 'bool1':u'False'}) |
|
101 response = self.client.post('/test1/', self.test_data) |
|
102 self.assertEqual(response.content, success_string) |
|
103 |
|
104 # |
|
105 # FormWizard tests |
|
106 # |
|
107 |
|
108 class WizardPageOneForm(forms.Form): |
|
109 field = forms.CharField() |
|
110 |
|
111 class WizardPageTwoForm(forms.Form): |
|
112 field = forms.CharField() |
|
113 |
|
114 class WizardClass(wizard.FormWizard): |
|
115 def render_template(self, *args, **kw): |
|
116 return "" |
|
117 |
|
118 def done(self, request, cleaned_data): |
|
119 return http.HttpResponse(success_string) |
|
120 |
|
121 class DummyRequest(object): |
|
122 def __init__(self, POST=None): |
|
123 self.method = POST and "POST" or "GET" |
|
124 self.POST = POST |
|
125 |
|
126 class WizardTests(TestCase): |
|
127 def test_step_starts_at_zero(self): |
|
128 """ |
|
129 step should be zero for the first form |
|
130 """ |
|
131 wizard = WizardClass([WizardPageOneForm, WizardPageTwoForm]) |
|
132 request = DummyRequest() |
|
133 wizard(request) |
|
134 self.assertEquals(0, wizard.step) |
|
135 |
|
136 def test_step_increments(self): |
|
137 """ |
|
138 step should be incremented when we go to the next page |
|
139 """ |
|
140 wizard = WizardClass([WizardPageOneForm, WizardPageTwoForm]) |
|
141 request = DummyRequest(POST={"0-field":"test", "wizard_step":"0"}) |
|
142 response = wizard(request) |
|
143 self.assertEquals(1, wizard.step) |
|
144 |