1 from __future__
import with_statement
4 from django
import forms
5 from django
.test
import TestCase
6 from django
.test
.client
import RequestFactory
7 from django
.conf
import settings
8 from django
.contrib
.auth
.models
import User
9 from django
.contrib
.formtools
.wizard
.views
import CookieWizardView
10 from django
.contrib
.formtools
.tests
.wizard
.forms
import UserForm
, UserFormSet
13 class WizardTests(object):
14 urls
= 'django.contrib.formtools.tests.wizard.wizardtests.urls'
17 self
.testuser
, created
= User
.objects
.get_or_create(username
='testuser1')
18 self
.wizard_step_data
[0]['form1-user'] = self
.testuser
.pk
20 def test_initial_call(self
):
21 response
= self
.client
.get(self
.wizard_url
)
22 wizard
= response
.context
['wizard']
23 self
.assertEqual(response
.status_code
, 200)
24 self
.assertEqual(wizard
['steps'].current
, 'form1')
25 self
.assertEqual(wizard
['steps'].step0
, 0)
26 self
.assertEqual(wizard
['steps'].step1
, 1)
27 self
.assertEqual(wizard
['steps'].last
, 'form4')
28 self
.assertEqual(wizard
['steps'].prev
, None)
29 self
.assertEqual(wizard
['steps'].next
, 'form2')
30 self
.assertEqual(wizard
['steps'].count
, 4)
32 def test_form_post_error(self
):
33 response
= self
.client
.post(self
.wizard_url
, self
.wizard_step_1_data
)
34 self
.assertEqual(response
.status_code
, 200)
35 self
.assertEqual(response
.context
['wizard']['steps'].current
, 'form1')
36 self
.assertEqual(response
.context
['wizard']['form'].errors
,
37 {'name': [u
'This field is required.'],
38 'user': [u
'This field is required.']})
40 def test_form_post_success(self
):
41 response
= self
.client
.post(self
.wizard_url
, self
.wizard_step_data
[0])
42 wizard
= response
.context
['wizard']
43 self
.assertEqual(response
.status_code
, 200)
44 self
.assertEqual(wizard
['steps'].current
, 'form2')
45 self
.assertEqual(wizard
['steps'].step0
, 1)
46 self
.assertEqual(wizard
['steps'].prev
, 'form1')
47 self
.assertEqual(wizard
['steps'].next
, 'form3')
49 def test_form_stepback(self
):
50 response
= self
.client
.get(self
.wizard_url
)
51 self
.assertEqual(response
.status_code
, 200)
52 self
.assertEqual(response
.context
['wizard']['steps'].current
, 'form1')
54 response
= self
.client
.post(self
.wizard_url
, self
.wizard_step_data
[0])
55 self
.assertEqual(response
.status_code
, 200)
56 self
.assertEqual(response
.context
['wizard']['steps'].current
, 'form2')
58 response
= self
.client
.post(self
.wizard_url
, {
59 'wizard_goto_step': response
.context
['wizard']['steps'].prev
})
60 self
.assertEqual(response
.status_code
, 200)
61 self
.assertEqual(response
.context
['wizard']['steps'].current
, 'form1')
63 def test_template_context(self
):
64 response
= self
.client
.get(self
.wizard_url
)
65 self
.assertEqual(response
.status_code
, 200)
66 self
.assertEqual(response
.context
['wizard']['steps'].current
, 'form1')
67 self
.assertEqual(response
.context
.get('another_var', None), None)
69 response
= self
.client
.post(self
.wizard_url
, self
.wizard_step_data
[0])
70 self
.assertEqual(response
.status_code
, 200)
71 self
.assertEqual(response
.context
['wizard']['steps'].current
, 'form2')
72 self
.assertEqual(response
.context
.get('another_var', None), True)
74 def test_form_finish(self
):
75 response
= self
.client
.get(self
.wizard_url
)
76 self
.assertEqual(response
.status_code
, 200)
77 self
.assertEqual(response
.context
['wizard']['steps'].current
, 'form1')
79 response
= self
.client
.post(self
.wizard_url
, self
.wizard_step_data
[0])
80 self
.assertEqual(response
.status_code
, 200)
81 self
.assertEqual(response
.context
['wizard']['steps'].current
, 'form2')
83 post_data
= self
.wizard_step_data
[1]
84 post_data
['form2-file1'] = open(__file__
)
85 response
= self
.client
.post(self
.wizard_url
, post_data
)
86 self
.assertEqual(response
.status_code
, 200)
87 self
.assertEqual(response
.context
['wizard']['steps'].current
, 'form3')
89 response
= self
.client
.post(self
.wizard_url
, self
.wizard_step_data
[2])
90 self
.assertEqual(response
.status_code
, 200)
91 self
.assertEqual(response
.context
['wizard']['steps'].current
, 'form4')
93 response
= self
.client
.post(self
.wizard_url
, self
.wizard_step_data
[3])
94 self
.assertEqual(response
.status_code
, 200)
96 all_data
= response
.context
['form_list']
97 self
.assertEqual(all_data
[1]['file1'].read(), open(__file__
).read())
98 del all_data
[1]['file1']
99 self
.assertEqual(all_data
, [
100 {'name': u
'Pony', 'thirsty': True, 'user': self
.testuser
},
101 {'address1': u
'123 Main St', 'address2': u
'Djangoland'},
102 {'random_crap': u
'blah blah'},
103 [{'random_crap': u
'blah blah'},
104 {'random_crap': u
'blah blah'}]])
106 def test_cleaned_data(self
):
107 response
= self
.client
.get(self
.wizard_url
)
108 self
.assertEqual(response
.status_code
, 200)
110 response
= self
.client
.post(self
.wizard_url
, self
.wizard_step_data
[0])
111 self
.assertEqual(response
.status_code
, 200)
113 post_data
= self
.wizard_step_data
[1]
114 post_data
['form2-file1'] = open(__file__
)
115 response
= self
.client
.post(self
.wizard_url
, post_data
)
116 self
.assertEqual(response
.status_code
, 200)
118 response
= self
.client
.post(self
.wizard_url
, self
.wizard_step_data
[2])
119 self
.assertEqual(response
.status_code
, 200)
121 response
= self
.client
.post(self
.wizard_url
, self
.wizard_step_data
[3])
122 self
.assertEqual(response
.status_code
, 200)
124 all_data
= response
.context
['all_cleaned_data']
125 self
.assertEqual(all_data
['file1'].read(), open(__file__
).read())
126 del all_data
['file1']
127 self
.assertEqual(all_data
, {
128 'name': u
'Pony', 'thirsty': True, 'user': self
.testuser
,
129 'address1': u
'123 Main St', 'address2': u
'Djangoland',
130 'random_crap': u
'blah blah', 'formset-form4': [
131 {'random_crap': u
'blah blah'},
132 {'random_crap': u
'blah blah'}]})
134 def test_manipulated_data(self
):
135 response
= self
.client
.get(self
.wizard_url
)
136 self
.assertEqual(response
.status_code
, 200)
138 response
= self
.client
.post(self
.wizard_url
, self
.wizard_step_data
[0])
139 self
.assertEqual(response
.status_code
, 200)
141 post_data
= self
.wizard_step_data
[1]
142 post_data
['form2-file1'] = open(__file__
)
143 response
= self
.client
.post(self
.wizard_url
, post_data
)
144 self
.assertEqual(response
.status_code
, 200)
146 response
= self
.client
.post(self
.wizard_url
, self
.wizard_step_data
[2])
147 self
.assertEqual(response
.status_code
, 200)
148 self
.client
.cookies
.pop('sessionid', None)
149 self
.client
.cookies
.pop('wizard_cookie_contact_wizard', None)
151 response
= self
.client
.post(self
.wizard_url
, self
.wizard_step_data
[3])
152 self
.assertEqual(response
.status_code
, 200)
153 self
.assertEqual(response
.context
['wizard']['steps'].current
, 'form1')
155 def test_form_refresh(self
):
156 response
= self
.client
.get(self
.wizard_url
)
157 self
.assertEqual(response
.status_code
, 200)
158 self
.assertEqual(response
.context
['wizard']['steps'].current
, 'form1')
160 response
= self
.client
.post(self
.wizard_url
, self
.wizard_step_data
[0])
161 self
.assertEqual(response
.status_code
, 200)
162 self
.assertEqual(response
.context
['wizard']['steps'].current
, 'form2')
164 response
= self
.client
.post(self
.wizard_url
, self
.wizard_step_data
[0])
165 self
.assertEqual(response
.status_code
, 200)
166 self
.assertEqual(response
.context
['wizard']['steps'].current
, 'form2')
168 post_data
= self
.wizard_step_data
[1]
169 post_data
['form2-file1'] = open(__file__
)
170 response
= self
.client
.post(self
.wizard_url
, post_data
)
171 self
.assertEqual(response
.status_code
, 200)
172 self
.assertEqual(response
.context
['wizard']['steps'].current
, 'form3')
174 response
= self
.client
.post(self
.wizard_url
, self
.wizard_step_data
[2])
175 self
.assertEqual(response
.status_code
, 200)
176 self
.assertEqual(response
.context
['wizard']['steps'].current
, 'form4')
178 response
= self
.client
.post(self
.wizard_url
, self
.wizard_step_data
[0])
179 self
.assertEqual(response
.status_code
, 200)
180 self
.assertEqual(response
.context
['wizard']['steps'].current
, 'form2')
182 response
= self
.client
.post(self
.wizard_url
, self
.wizard_step_data
[3])
183 self
.assertEqual(response
.status_code
, 200)
186 class SessionWizardTests(WizardTests
, TestCase
):
187 wizard_url
= '/wiz_session/'
188 wizard_step_1_data
= {
189 'session_contact_wizard-current_step': 'form1',
193 'form1-name': 'Pony',
194 'form1-thirsty': '2',
195 'session_contact_wizard-current_step': 'form1',
198 'form2-address1': '123 Main St',
199 'form2-address2': 'Djangoland',
200 'session_contact_wizard-current_step': 'form2',
203 'form3-random_crap': 'blah blah',
204 'session_contact_wizard-current_step': 'form3',
207 'form4-INITIAL_FORMS': '0',
208 'form4-TOTAL_FORMS': '2',
209 'form4-MAX_NUM_FORMS': '0',
210 'form4-0-random_crap': 'blah blah',
211 'form4-1-random_crap': 'blah blah',
212 'session_contact_wizard-current_step': 'form4',
216 class CookieWizardTests(WizardTests
, TestCase
):
217 wizard_url
= '/wiz_cookie/'
218 wizard_step_1_data
= {
219 'cookie_contact_wizard-current_step': 'form1',
223 'form1-name': 'Pony',
224 'form1-thirsty': '2',
225 'cookie_contact_wizard-current_step': 'form1',
228 'form2-address1': '123 Main St',
229 'form2-address2': 'Djangoland',
230 'cookie_contact_wizard-current_step': 'form2',
233 'form3-random_crap': 'blah blah',
234 'cookie_contact_wizard-current_step': 'form3',
237 'form4-INITIAL_FORMS': '0',
238 'form4-TOTAL_FORMS': '2',
239 'form4-MAX_NUM_FORMS': '0',
240 'form4-0-random_crap': 'blah blah',
241 'form4-1-random_crap': 'blah blah',
242 'cookie_contact_wizard-current_step': 'form4',
246 class WizardTestKwargs(TestCase
):
247 wizard_url
= '/wiz_other_template/'
248 wizard_step_1_data
= {
249 'cookie_contact_wizard-current_step': 'form1',
253 'form1-name': 'Pony',
254 'form1-thirsty': '2',
255 'cookie_contact_wizard-current_step': 'form1',
258 'form2-address1': '123 Main St',
259 'form2-address2': 'Djangoland',
260 'cookie_contact_wizard-current_step': 'form2',
263 'form3-random_crap': 'blah blah',
264 'cookie_contact_wizard-current_step': 'form3',
267 'form4-INITIAL_FORMS': '0',
268 'form4-TOTAL_FORMS': '2',
269 'form4-MAX_NUM_FORMS': '0',
270 'form4-0-random_crap': 'blah blah',
271 'form4-1-random_crap': 'blah blah',
272 'cookie_contact_wizard-current_step': 'form4',
275 urls
= 'django.contrib.formtools.tests.wizard.wizardtests.urls'
278 self
.testuser
, created
= User
.objects
.get_or_create(username
='testuser1')
279 self
.wizard_step_data
[0]['form1-user'] = self
.testuser
.pk
281 def test_template(self
):
282 templates
= os
.path
.join(os
.path
.dirname(__file__
), 'templates')
284 TEMPLATE_DIRS
=list(settings
.TEMPLATE_DIRS
) + [templates
]):
285 response
= self
.client
.get(self
.wizard_url
)
286 self
.assertTemplateUsed(response
, 'other_wizard_form.html')
289 class WizardTestGenericViewInterface(TestCase
):
290 def test_get_context_data_inheritance(self
):
291 class TestWizard(CookieWizardView
):
293 A subclass that implements ``get_context_data`` using the standard
294 protocol for generic views (accept only **kwargs).
298 def get_context_data(self
, **kwargs
):
299 context
= super(TestWizard
, self
).get_context_data(**kwargs
)
300 context
['test_key'] = 'test_value'
303 factory
= RequestFactory()
304 view
= TestWizard
.as_view([forms
.Form
])
306 response
= view(factory
.get('/'))
307 self
.assertEquals(response
.context_data
['test_key'], 'test_value')
309 def test_get_context_data_with_mixin(self
):
310 class AnotherMixin(object):
311 def get_context_data(self
, **kwargs
):
312 context
= super(AnotherMixin
, self
).get_context_data(**kwargs
)
313 context
['another_key'] = 'another_value'
316 class TestWizard(AnotherMixin
, CookieWizardView
):
318 A subclass that implements ``get_context_data`` using the standard
319 protocol for generic views (accept only **kwargs).
323 def get_context_data(self
, **kwargs
):
324 context
= super(TestWizard
, self
).get_context_data(**kwargs
)
325 context
['test_key'] = 'test_value'
328 factory
= RequestFactory()
330 view
= TestWizard
.as_view([forms
.Form
])
332 response
= view(factory
.get('/'))
333 self
.assertEquals(response
.context_data
['test_key'], 'test_value')
334 self
.assertEquals(response
.context_data
['another_key'], 'another_value')
337 class WizardFormKwargsOverrideTests(TestCase
):
339 super(WizardFormKwargsOverrideTests
, self
).setUp()
340 self
.rf
= RequestFactory()
342 # Create two users so we can filter by is_staff when handing our
343 # wizard a queryset keyword argument.
344 self
.normal_user
= User
.objects
.create(username
='test1', email
='normal@example.com')
345 self
.staff_user
= User
.objects
.create(username
='test2', email
='staff@example.com', is_staff
=True)
347 def test_instance_is_maintained(self
):
348 self
.assertEqual(2, User
.objects
.count())
349 queryset
= User
.objects
.get(pk
=self
.staff_user
.pk
)
351 class InstanceOverrideWizard(CookieWizardView
):
352 def get_form_kwargs(self
, step
):
353 return {'instance': queryset
}
355 view
= InstanceOverrideWizard
.as_view([UserForm
])
356 response
= view(self
.rf
.get('/'))
358 form
= response
.context_data
['wizard']['form']
360 self
.assertNotEqual(form
.instance
.pk
, None)
361 self
.assertEqual(form
.instance
.pk
, self
.staff_user
.pk
)
362 self
.assertEqual('staff@example.com', form
.initial
.get('email', None))
364 def test_queryset_is_maintained(self
):
365 queryset
= User
.objects
.filter(pk
=self
.staff_user
.pk
)
367 class QuerySetOverrideWizard(CookieWizardView
):
368 def get_form_kwargs(self
, step
):
369 return {'queryset': queryset
}
371 view
= QuerySetOverrideWizard
.as_view([UserFormSet
])
372 response
= view(self
.rf
.get('/'))
374 formset
= response
.context_data
['wizard']['form']
376 self
.assertNotEqual(formset
.queryset
, None)
377 self
.assertEqual(formset
.initial_form_count(), 1)
378 self
.assertEqual(['staff@example.com'],
379 list(formset
.queryset
.values_list('email', flat
=True)))