| | 1 | from django import newforms as forms |
|---|
| | 2 | from django.contrib.formtools import preview |
|---|
| | 3 | from django import http |
|---|
| | 4 | from django.conf import settings |
|---|
| | 5 | from django.test import TestCase |
|---|
| | 6 | from django.test.client import Client |
|---|
| | 7 | |
|---|
| | 8 | |
|---|
| | 9 | success_string = "Done was called!" |
|---|
| | 10 | |
|---|
| | 11 | class TestFormPreview(preview.FormPreview): |
|---|
| | 12 | def done(self, request, cleaned_data): |
|---|
| | 13 | return http.HttpResponse(success_string) |
|---|
| | 14 | |
|---|
| | 15 | class TestForm(forms.Form): |
|---|
| | 16 | field1 = forms.CharField() |
|---|
| | 17 | field1_ = forms.CharField() |
|---|
| | 18 | |
|---|
| | 19 | test_data = {'field1': u'foo', |
|---|
| | 20 | 'field1_': u'asdf'} |
|---|
| | 21 | |
|---|
| | 22 | class 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 | self.input = '<input type="hidden" name="%s" value="%s" />' % (self.preview.unused_name('stage'), "%d") |
|---|
| | 27 | |
|---|
| | 28 | def testUnusedName(self): |
|---|
| | 29 | """ Verify that unused_name() correctly appends underscores to |
|---|
| | 30 | a name until the name is unique. |
|---|
| | 31 | |
|---|
| | 32 | """ |
|---|
| | 33 | self.assertEqual(self.preview.unused_name('field1'), 'field1__') |
|---|
| | 34 | |
|---|
| | 35 | def testFormGet(self): |
|---|
| | 36 | """ |
|---|
| | 37 | Test contrib.formtools.preview form retrieval. |
|---|
| | 38 | |
|---|
| | 39 | Use the client library to see if we can sucessfully retrieve |
|---|
| | 40 | the form (mostly testing the setup ROOT_URLCONF |
|---|
| | 41 | process). Verify that an additional hidden input field |
|---|
| | 42 | is created to manage the stage. |
|---|
| | 43 | |
|---|
| | 44 | """ |
|---|
| | 45 | response = self.client.get('/test1/') |
|---|
| | 46 | stage = self.input % 1 |
|---|
| | 47 | self.assertContains(response, stage, 1) |
|---|
| | 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 in output form. |
|---|
| | 63 | stage = self.input % 2 |
|---|
| | 64 | self.assertContains(response, stage, 1) |
|---|
| | 65 | |
|---|
| | 66 | |
|---|
| | 67 | def testFormSubmit(self): |
|---|
| | 68 | """ |
|---|
| | 69 | Test contrib.formtools.preview form submittal. |
|---|
| | 70 | |
|---|
| | 71 | Use the client library to POST to the form with stage set to 3 |
|---|
| | 72 | to see if our forms done() method is called. Check first |
|---|
| | 73 | without the security hash, verify failure, retry with security |
|---|
| | 74 | hash and verify sucess. |
|---|
| | 75 | |
|---|
| | 76 | """ |
|---|
| | 77 | # Pass strings for form submittal and add stage variable to |
|---|
| | 78 | # show we previously saw first stage of the form. |
|---|
| | 79 | test_data.update({'stage':2}) |
|---|
| | 80 | response = self.client.post('/test1/', test_data) |
|---|
| | 81 | self.failIfEqual(response.content, success_string) |
|---|
| | 82 | hash = self.preview.security_hash(None, TestForm(test_data)); |
|---|
| | 83 | test_data.update({'hash':hash}) |
|---|
| | 84 | response = self.client.post('/test1/', test_data) |
|---|
| | 85 | self.assertEqual(response.content, success_string); |
|---|
| | 86 | |
|---|
| | 87 | |
|---|
| | 88 | if __name__ == '__main__': |
|---|
| | 89 | unittest.main() |