~ubuntu-branches/ubuntu/natty/python-django/natty-security

« back to all changes in this revision

Viewing changes to tests/regressiontests/forms/localflavor/utils.py

  • Committer: Bazaar Package Importer
  • Author(s): Jamie Strandboge
  • Date: 2011-02-17 13:34:07 UTC
  • mfrom: (1.1.13 upstream) (4.4.12 sid)
  • Revision ID: james.westby@ubuntu.com-20110217133407-rwr88elhhq6j7ba0
Tags: 1.2.5-1ubuntu1
* Merge from Debian for security fixes (LP: #719031). Remaining changes:
  - debian/control: don't Build-Depends on locales-all, which doesn't exist
    in natty
* Drop the following patches, now included upstream:
  - debian/patches/07_security_admin_infoleak.diff
  - debian/patches/08_security_pasword_reset_dos.diff

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from unittest import TestCase
 
2
 
 
3
from django.core.exceptions import ValidationError
 
4
from django.core.validators import EMPTY_VALUES
 
5
 
 
6
 
 
7
class LocalFlavorTestCase(TestCase):
 
8
    def assertFieldOutput(self, fieldclass, valid, invalid, field_args=[],
 
9
            field_kwargs={}, empty_value=u''):
 
10
        """
 
11
        Asserts that a field behaves correctly with various inputs.
 
12
 
 
13
        Args:
 
14
            fieldclass: the class of the field to be tested.
 
15
            valid: a dictionary mapping valid inputs to their expected
 
16
                    cleaned values.
 
17
            invalid: a dictionary mapping invalid inputs to one or more
 
18
                    raised error messages.
 
19
            field_args: the args passed to instantiate the field
 
20
            field_kwargs: the kwargs passed to instantiate the field
 
21
            empty_value: the expected clean output for inputs in EMPTY_VALUES
 
22
 
 
23
        """
 
24
        required = fieldclass(*field_args, **field_kwargs)
 
25
        optional = fieldclass(*field_args, **dict(field_kwargs, required=False))
 
26
        # test valid inputs
 
27
        for input, output in valid.items():
 
28
            self.assertEqual(required.clean(input), output)
 
29
            self.assertEqual(optional.clean(input), output)
 
30
        # test invalid inputs
 
31
        for input, errors in invalid.items():
 
32
            try:
 
33
                required.clean(input)
 
34
            except ValidationError, e:
 
35
                self.assertEqual(errors, e.messages)
 
36
            else:
 
37
                self.fail()
 
38
            try:
 
39
                optional.clean(input)
 
40
            except ValidationError, e:
 
41
                self.assertEqual(errors, e.messages)
 
42
            else:
 
43
                self.fail()
 
44
        # test required inputs
 
45
        error_required = [u'This field is required.']
 
46
        for val in EMPTY_VALUES:
 
47
            try:
 
48
                required.clean(val)
 
49
            except ValidationError, e:
 
50
                self.assertEqual(error_required, e.messages)
 
51
            else:
 
52
                self.fail()
 
53
            self.assertEqual(optional.clean(val), empty_value)