Ticket #5441: 5441.2.diff

File 5441.2.diff, 4.3 KB (added by simeon, 17 years ago)
  • django/contrib/formtools/tests.py

     
     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!"
     10test_data = {'field1': u'foo',
     11             'field1_': u'asdf'}
     12
     13
     14class TestFormPreview(preview.FormPreview):
     15
     16    def done(self, request, cleaned_data):
     17        return http.HttpResponse(success_string)
     18
     19
     20class TestForm(forms.Form):
     21    field1 = forms.CharField()
     22    field1_ = forms.CharField()
     23
     24
     25class PreviewTests(TestCase):
     26
     27    def setUp(self):
     28        settings.ROOT_URLCONF = 'django.contrib.formtools.test_urls'
     29        # Create a FormPreview instance to share between tests
     30        self.preview = preview.FormPreview(TestForm)
     31        input_template = '<input type="hidden" name="%s" value="%s" />'
     32        self.input = input_template % (self.preview.unused_name('stage'), "%d")
     33
     34    def test_unused_name(self):
     35        """
     36        Verifies name mangling to get uniue field name.
     37        """
     38        self.assertEqual(self.preview.unused_name('field1'), 'field1__')
     39
     40    def test_form_get(self):
     41        """
     42        Test contrib.formtools.preview form retrieval.
     43
     44        Use the client library to see if we can sucessfully retrieve
     45        the form (mostly testing the setup ROOT_URLCONF
     46        process). Verify that an additional  hidden input field
     47        is created to manage the stage.
     48
     49        """
     50        response = self.client.get('/test1/')
     51        stage = self.input % 1
     52        self.assertContains(response, stage, 1)
     53
     54    def test_form_preview(self):
     55        """
     56        Test contrib.formtools.preview form preview rendering.
     57
     58        Use the client library to POST to the form to see if a preview
     59        is returned.  If we do get a form back check that the hidden
     60        value is correctly managing the state of the form.
     61
     62        """
     63        # Pass strings for form submittal and add stage variable to
     64        # show we previously saw first stage of the form.
     65        test_data.update({'stage': 1})
     66        response = self.client.post('/test1/', test_data)
     67        # Check to confirm stage is set to 2 in output form.
     68        stage = self.input % 2
     69        self.assertContains(response, stage, 1)
     70
     71    def test_form_submit(self):
     72        """
     73        Test contrib.formtools.preview form submittal.
     74
     75        Use the client library to POST to the form with stage set to 3
     76        to see if our forms done() method is called. Check first
     77        without the security hash, verify failure, retry with security
     78        hash and verify sucess.
     79
     80        """
     81        # Pass strings for form submittal and add stage variable to
     82        # show we previously saw first stage of the form.
     83        test_data.update({'stage': 2})
     84        response = self.client.post('/test1/', test_data)
     85        self.failIfEqual(response.content, success_string)
     86        hash = self.preview.security_hash(None, TestForm(test_data))
     87        test_data.update({'hash': hash})
     88        response = self.client.post('/test1/', test_data)
     89        self.assertEqual(response.content, success_string)
     90
     91
     92if __name__ == '__main__':
     93    unittest.main()
  • django/contrib/formtools/models.py

     
     1""" models.py (even empty) currently required by the runtests.py to enable unit tests """
  • django/contrib/formtools/test_urls.py

     
     1"""
     2
     3This is a urlconf to be loaded by tests.py. Add any urls needed
     4for tests only.
     5
     6"""
     7from django.conf.urls.defaults import *
     8from django.contrib.formtools.tests import *
     9
     10urlpatterns = patterns('',
     11                       (r'^test1/', TestFormPreview(TestForm)),
     12                      )
Back to Top