Ticket #10643: security_hash.diff

File security_hash.diff, 2.1 KB (added by Ulrich Petri, 16 years ago)

Cleaned up testcase

  • django/contrib/formtools/utils.py

    diff --git a/django/contrib/formtools/utils.py b/django/contrib/formtools/utils.py
    index a357255..d557460 100644
    a b def security_hash(request, form, *args):  
    1515    order, pickles the result with the SECRET_KEY setting, then takes an md5
    1616    hash of that.
    1717    """
    18 
    19     data = [(bf.name, bf.field.clean(bf.data) or '') for bf in form]
     18    if form.empty_permitted and not form.has_changed():
     19        data = [(bf.name, bf.data or '') for bf in form]
     20    else:
     21        data = [(bf.name, bf.field.clean(bf.data) or '') for bf in form]
    2022    data.extend(args)
    2123    data.append(settings.SECRET_KEY)
    2224
  • new file tests/regressiontests/forms/formtools.py

    diff --git a/tests/regressiontests/forms/formtools.py b/tests/regressiontests/forms/formtools.py
    new file mode 100644
    index 0000000..7ac488e
    - +  
     1# coding: utf-8
     2from django.test import TestCase
     3
     4class FormToolsUtilsTests(TestCase):
     5    def test_security_hash_for_empty_permitted_form(self):
     6        """Check that formtools.utils.security_hash handles forms with
     7        empty_permitted=True"""
     8        from django import forms
     9
     10        class BlankForm(forms.Form):
     11            name = forms.CharField(max_length=10, required=False)
     12        class NotBlankForm(forms.Form):
     13            name = forms.CharField(max_length=10)
     14        blank_form = BlankForm({})
     15        not_blank_form = NotBlankForm({}, empty_permitted=True)
     16       
     17        from django.contrib.formtools.utils import security_hash
     18        self.assertEqual(security_hash(None, blank_form), security_hash(None, not_blank_form))
  • tests/regressiontests/forms/tests.py

    diff --git a/tests/regressiontests/forms/tests.py b/tests/regressiontests/forms/tests.py
    index 6d418fa..7b73d99 100644
    a b from widgets import tests as widgets_tests  
    3232from formsets import tests as formset_tests
    3333from media import media_tests
    3434
     35from formtools import *
     36
    3537__test__ = {
    3638    'extra_tests': extra_tests,
    3739    'fields_tests': fields_tests,
Back to Top