app/django/contrib/formtools/tests.py
changeset 54 03e267d67478
child 323 ff1a9aa48cfd
equal deleted inserted replaced
53:57b4279d8c4e 54:03e267d67478
       
     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 
       
     7 success_string = "Done was called!"
       
     8 test_data = {'field1': u'foo',
       
     9              'field1_': u'asdf'}
       
    10 
       
    11 
       
    12 class TestFormPreview(preview.FormPreview):
       
    13 
       
    14     def done(self, request, cleaned_data):
       
    15         return http.HttpResponse(success_string)
       
    16 
       
    17 
       
    18 class TestForm(forms.Form):
       
    19     field1 = forms.CharField()
       
    20     field1_ = forms.CharField()
       
    21 
       
    22 
       
    23 class PreviewTests(TestCase):
       
    24 
       
    25     def setUp(self):
       
    26         settings.ROOT_URLCONF = 'django.contrib.formtools.test_urls'
       
    27         # Create a FormPreview instance to share between tests
       
    28         self.preview = preview.FormPreview(TestForm)
       
    29         input_template = '<input type="hidden" name="%s" value="%s" />'
       
    30         self.input = input_template % (self.preview.unused_name('stage'), "%d")
       
    31 
       
    32     def test_unused_name(self):
       
    33         """
       
    34         Verifies name mangling to get uniue field name.
       
    35         """
       
    36         self.assertEqual(self.preview.unused_name('field1'), 'field1__')
       
    37 
       
    38     def test_form_get(self):
       
    39         """
       
    40         Test contrib.formtools.preview form retrieval.
       
    41 
       
    42         Use the client library to see if we can sucessfully retrieve
       
    43         the form (mostly testing the setup ROOT_URLCONF
       
    44         process). Verify that an additional  hidden input field
       
    45         is created to manage the stage.
       
    46 
       
    47         """
       
    48         response = self.client.get('/test1/')
       
    49         stage = self.input % 1
       
    50         self.assertContains(response, stage, 1)
       
    51 
       
    52     def test_form_preview(self):
       
    53         """
       
    54         Test contrib.formtools.preview form preview rendering.
       
    55 
       
    56         Use the client library to POST to the form to see if a preview
       
    57         is returned.  If we do get a form back check that the hidden
       
    58         value is correctly managing the state of the form.
       
    59 
       
    60         """
       
    61         # Pass strings for form submittal and add stage variable to
       
    62         # show we previously saw first stage of the form.
       
    63         test_data.update({'stage': 1})
       
    64         response = self.client.post('/test1/', test_data)
       
    65         # Check to confirm stage is set to 2 in output form.
       
    66         stage = self.input % 2
       
    67         self.assertContains(response, stage, 1)
       
    68 
       
    69     def test_form_submit(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