Ticket #5441: tests.py

File tests.py, 3.4 KB (added by simeon, 17 years ago)

Contains unit tests for contrib.formtools.preview

Line 
1from django import newforms as forms
2from django.contrib.formtools import preview
3from django import http
4from django.conf import settings
5from django.test import TestCase
6from django.test.client import Client
7
8
9success_string = "Done was called!"
10
11class TestFormPreview(preview.FormPreview):
12 def done(self, request, cleaned_data):
13 return http.HttpResponse(success_string)
14
15class TestForm(forms.Form):
16 field1 = forms.CharField()
17 field1_ = forms.CharField()
18
19test_data = {'field1': u'foo',
20 'field1_': u'asdf'}
21
22class tests(TestCase):
23 def setUp(self):
24 settings.ROOT_URLCONF = 'django.contrib.formtools.test_urls'
25 self.preview = preview.FormPreview(TestForm) # Create a FormPreview instance to share between tests
26
27 def testUnusedName(self):
28 """ Verify that unused_name() correctly appends underscores to
29 a name until the name is unique.
30
31 """
32 self.assertEqual(self.preview.unused_name('field1'), 'field1__')
33
34 def testFormGet(self):
35 """
36 Test contrib.formtools.preview form retrieval.
37
38 Use the client library to see if we can sucessfully retrieve
39 the form (mostly testing the setup ROOT_URLCONF process). If
40 we do get a form, verify that on the first view a hidden input
41 field is created to manage the stage.
42
43 """
44 response = self.client.get('/test1/')
45 self.assertEqual(response.status_code,200) #did we get a page back?
46 pos = response.content.find('<input type="hidden" name="%s" value="1" />' % self.preview.unused_name('stage'))
47 self.failIfEqual(-1,pos)
48
49 def testFormPreview(self):
50 """
51 Test contrib.formtools.preview form preview rendering.
52
53 Use the client library to POST to the form to see if a preview
54 is returned. If we do get a form back check that the hidden
55 value is correctly managing the state of the form.
56
57 """
58 # Pass strings for form submittal and add stage variable to
59 # show we previously saw first stage of the form.
60 test_data.update({'stage':1})
61 response = self.client.post('/test1/', test_data)
62 # Check to confirm stage is set to 2. Could also check that
63 # all the fields are now hidden fields as part of form preview.
64
65 pos = response.content.find('<input type="hidden" name="%s" value="2" />' %
66 self.preview.unused_name('stage'))
67 self.failIfEqual(-1,pos)
68
69 def testFormSubmit(self):
70 """
71 Test contrib.formtools.preview form submittal.
72
73 Use the client library to POST to the form with stage set to 3
74 to see if our forms done() method is called. Check first
75 without the security hash, verify failure, retry with security
76 hash and verify sucess.
77
78 """
79 # Pass strings for form submittal and add stage variable to
80 # show we previously saw first stage of the form.
81 test_data.update({'stage':2})
82 response = self.client.post('/test1/', test_data)
83 self.failIfEqual(response.content, success_string)
84 hash = self.preview.security_hash(None, TestForm(test_data));
85 test_data.update({'hash':hash})
86 response = self.client.post('/test1/', test_data)
87 self.assertEqual(response.content, success_string);
88
89
90if __name__ == '__main__':
91 unittest.main()
Back to Top