2010-01-05 04:56:19 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
2012-06-07 18:08:47 +02:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2013-07-01 14:22:27 +02:00
|
|
|
from datetime import datetime, timedelta
|
2010-01-05 04:56:19 +01:00
|
|
|
import re
|
|
|
|
import types
|
2013-07-01 14:22:27 +02:00
|
|
|
from unittest import TestCase
|
2010-10-11 14:55:17 +02:00
|
|
|
|
2010-01-05 04:56:19 +01:00
|
|
|
from django.core.exceptions import ValidationError
|
2013-10-22 14:28:34 +02:00
|
|
|
from django.core.validators import (
|
|
|
|
BaseValidator, EmailValidator, MaxLengthValidator, MaxValueValidator,
|
|
|
|
MinLengthValidator, MinValueValidator, RegexValidator, URLValidator,
|
|
|
|
validate_comma_separated_integer_list, validate_email, validate_integer,
|
|
|
|
validate_ipv46_address, validate_ipv4_address, validate_ipv6_address,
|
|
|
|
validate_slug,
|
|
|
|
)
|
2012-06-07 18:08:47 +02:00
|
|
|
from django.test.utils import str_prefix
|
2010-10-11 14:55:17 +02:00
|
|
|
|
2010-01-05 04:56:19 +01:00
|
|
|
|
|
|
|
NOW = datetime.now()
|
|
|
|
|
|
|
|
TEST_DATA = (
|
|
|
|
# (validator, value, expected),
|
|
|
|
(validate_integer, '42', None),
|
|
|
|
(validate_integer, '-42', None),
|
|
|
|
(validate_integer, -42, None),
|
|
|
|
(validate_integer, -42.5, None),
|
|
|
|
|
|
|
|
(validate_integer, None, ValidationError),
|
|
|
|
(validate_integer, 'a', ValidationError),
|
|
|
|
|
|
|
|
(validate_email, 'email@here.com', None),
|
|
|
|
(validate_email, 'weirder-email@here.and.there.com', None),
|
2011-06-10 12:22:42 +02:00
|
|
|
(validate_email, 'email@[127.0.0.1]', None),
|
2012-10-09 14:56:19 +02:00
|
|
|
(validate_email, 'example@valid-----hyphens.com', None),
|
|
|
|
(validate_email, 'example@valid-with-hyphens.com', None),
|
2012-10-09 15:01:38 +02:00
|
|
|
(validate_email, 'test@domain.with.idn.tld.उदाहरण.परीक्षा', None),
|
2013-01-11 20:42:33 +01:00
|
|
|
(validate_email, 'email@localhost', None),
|
|
|
|
(EmailValidator(whitelist=['localdomain']), 'email@localdomain', None),
|
2013-01-26 12:19:31 +01:00
|
|
|
(validate_email, '"test@test"@example.com', None),
|
2010-01-05 04:56:19 +01:00
|
|
|
|
|
|
|
(validate_email, None, ValidationError),
|
|
|
|
(validate_email, '', ValidationError),
|
|
|
|
(validate_email, 'abc', ValidationError),
|
2012-10-09 14:56:19 +02:00
|
|
|
(validate_email, 'abc@', ValidationError),
|
|
|
|
(validate_email, 'abc@bar', ValidationError),
|
2010-01-05 04:56:19 +01:00
|
|
|
(validate_email, 'a @x.cz', ValidationError),
|
2012-10-09 14:56:19 +02:00
|
|
|
(validate_email, 'abc@.com', ValidationError),
|
2010-01-05 04:56:19 +01:00
|
|
|
(validate_email, 'something@@somewhere.com', ValidationError),
|
2011-06-10 12:22:42 +02:00
|
|
|
(validate_email, 'email@127.0.0.1', ValidationError),
|
2012-10-09 14:56:19 +02:00
|
|
|
(validate_email, 'example@invalid-.com', ValidationError),
|
|
|
|
(validate_email, 'example@-invalid.com', ValidationError),
|
|
|
|
(validate_email, 'example@inv-.alid-.com', ValidationError),
|
|
|
|
(validate_email, 'example@inv-.-alid.com', ValidationError),
|
2013-07-20 18:00:23 +02:00
|
|
|
(validate_email, 'test@example.com\n\n<script src="x.js">', ValidationError),
|
2012-01-07 19:43:46 +01:00
|
|
|
# Quoted-string format (CR not allowed)
|
|
|
|
(validate_email, '"\\\011"@here.com', None),
|
|
|
|
(validate_email, '"\\\012"@here.com', ValidationError),
|
2013-10-23 18:13:17 +02:00
|
|
|
(validate_email, 'trailingdot@shouldfail.com.', ValidationError),
|
2010-01-05 04:56:19 +01:00
|
|
|
|
|
|
|
(validate_slug, 'slug-ok', None),
|
|
|
|
(validate_slug, 'longer-slug-still-ok', None),
|
|
|
|
(validate_slug, '--------', None),
|
|
|
|
(validate_slug, 'nohyphensoranything', None),
|
|
|
|
|
|
|
|
(validate_slug, '', ValidationError),
|
|
|
|
(validate_slug, ' text ', ValidationError),
|
|
|
|
(validate_slug, ' ', ValidationError),
|
|
|
|
(validate_slug, 'some@mail.com', ValidationError),
|
|
|
|
(validate_slug, '你好', ValidationError),
|
|
|
|
(validate_slug, '\n', ValidationError),
|
|
|
|
|
|
|
|
(validate_ipv4_address, '1.1.1.1', None),
|
|
|
|
(validate_ipv4_address, '255.0.0.0', None),
|
|
|
|
(validate_ipv4_address, '0.0.0.0', None),
|
|
|
|
|
|
|
|
(validate_ipv4_address, '256.1.1.1', ValidationError),
|
|
|
|
(validate_ipv4_address, '25.1.1.', ValidationError),
|
|
|
|
(validate_ipv4_address, '25,1,1,1', ValidationError),
|
|
|
|
(validate_ipv4_address, '25.1 .1.1', ValidationError),
|
|
|
|
|
2011-06-11 15:48:24 +02:00
|
|
|
# validate_ipv6_address uses django.utils.ipv6, which
|
|
|
|
# is tested in much greater detail in it's own testcase
|
|
|
|
(validate_ipv6_address, 'fe80::1', None),
|
|
|
|
(validate_ipv6_address, '::1', None),
|
|
|
|
(validate_ipv6_address, '1:2:3:4:5:6:7:8', None),
|
|
|
|
|
|
|
|
(validate_ipv6_address, '1:2', ValidationError),
|
|
|
|
(validate_ipv6_address, '::zzz', ValidationError),
|
|
|
|
(validate_ipv6_address, '12345::', ValidationError),
|
|
|
|
|
|
|
|
(validate_ipv46_address, '1.1.1.1', None),
|
|
|
|
(validate_ipv46_address, '255.0.0.0', None),
|
|
|
|
(validate_ipv46_address, '0.0.0.0', None),
|
|
|
|
(validate_ipv46_address, 'fe80::1', None),
|
|
|
|
(validate_ipv46_address, '::1', None),
|
|
|
|
(validate_ipv46_address, '1:2:3:4:5:6:7:8', None),
|
|
|
|
|
|
|
|
(validate_ipv46_address, '256.1.1.1', ValidationError),
|
|
|
|
(validate_ipv46_address, '25.1.1.', ValidationError),
|
|
|
|
(validate_ipv46_address, '25,1,1,1', ValidationError),
|
|
|
|
(validate_ipv46_address, '25.1 .1.1', ValidationError),
|
|
|
|
(validate_ipv46_address, '1:2', ValidationError),
|
|
|
|
(validate_ipv46_address, '::zzz', ValidationError),
|
|
|
|
(validate_ipv46_address, '12345::', ValidationError),
|
|
|
|
|
2010-01-05 04:56:19 +01:00
|
|
|
(validate_comma_separated_integer_list, '1', None),
|
|
|
|
(validate_comma_separated_integer_list, '1,2,3', None),
|
|
|
|
(validate_comma_separated_integer_list, '1,2,3,', None),
|
|
|
|
|
|
|
|
(validate_comma_separated_integer_list, '', ValidationError),
|
|
|
|
(validate_comma_separated_integer_list, 'a,b,c', ValidationError),
|
|
|
|
(validate_comma_separated_integer_list, '1, 2, 3', ValidationError),
|
|
|
|
|
|
|
|
(MaxValueValidator(10), 10, None),
|
|
|
|
(MaxValueValidator(10), -10, None),
|
|
|
|
(MaxValueValidator(10), 0, None),
|
|
|
|
(MaxValueValidator(NOW), NOW, None),
|
|
|
|
(MaxValueValidator(NOW), NOW - timedelta(days=1), None),
|
|
|
|
|
|
|
|
(MaxValueValidator(0), 1, ValidationError),
|
|
|
|
(MaxValueValidator(NOW), NOW + timedelta(days=1), ValidationError),
|
|
|
|
|
|
|
|
(MinValueValidator(-10), -10, None),
|
|
|
|
(MinValueValidator(-10), 10, None),
|
|
|
|
(MinValueValidator(-10), 0, None),
|
|
|
|
(MinValueValidator(NOW), NOW, None),
|
|
|
|
(MinValueValidator(NOW), NOW + timedelta(days=1), None),
|
|
|
|
|
|
|
|
(MinValueValidator(0), -1, ValidationError),
|
|
|
|
(MinValueValidator(NOW), NOW - timedelta(days=1), ValidationError),
|
|
|
|
|
|
|
|
(MaxLengthValidator(10), '', None),
|
2013-11-02 22:34:05 +01:00
|
|
|
(MaxLengthValidator(10), 10 * 'x', None),
|
2010-01-05 04:56:19 +01:00
|
|
|
|
2013-11-02 22:34:05 +01:00
|
|
|
(MaxLengthValidator(10), 15 * 'x', ValidationError),
|
2010-01-05 04:56:19 +01:00
|
|
|
|
2013-11-02 22:34:05 +01:00
|
|
|
(MinLengthValidator(10), 15 * 'x', None),
|
|
|
|
(MinLengthValidator(10), 10 * 'x', None),
|
2010-01-05 04:56:19 +01:00
|
|
|
|
|
|
|
(MinLengthValidator(10), '', ValidationError),
|
|
|
|
|
|
|
|
(URLValidator(), 'http://www.djangoproject.com/', None),
|
|
|
|
(URLValidator(), 'http://localhost/', None),
|
|
|
|
(URLValidator(), 'http://example.com/', None),
|
|
|
|
(URLValidator(), 'http://www.example.com/', None),
|
|
|
|
(URLValidator(), 'http://www.example.com:8000/test', None),
|
|
|
|
(URLValidator(), 'http://valid-with-hyphens.com/', None),
|
2010-11-02 17:19:17 +01:00
|
|
|
(URLValidator(), 'http://subdomain.example.com/', None),
|
2010-01-05 04:56:19 +01:00
|
|
|
(URLValidator(), 'http://200.8.9.10/', None),
|
|
|
|
(URLValidator(), 'http://200.8.9.10:8000/test', None),
|
|
|
|
(URLValidator(), 'http://valid-----hyphens.com/', None),
|
|
|
|
(URLValidator(), 'http://example.com?something=value', None),
|
|
|
|
(URLValidator(), 'http://example.com/index.php?something=value&another=value2', None),
|
2013-10-11 23:58:07 +02:00
|
|
|
(URLValidator(), 'https://example.com/', None),
|
|
|
|
(URLValidator(), 'ftp://example.com/', None),
|
|
|
|
(URLValidator(), 'ftps://example.com/', None),
|
2010-01-05 04:56:19 +01:00
|
|
|
|
|
|
|
(URLValidator(), 'foo', ValidationError),
|
|
|
|
(URLValidator(), 'http://', ValidationError),
|
|
|
|
(URLValidator(), 'http://example', ValidationError),
|
|
|
|
(URLValidator(), 'http://example.', ValidationError),
|
|
|
|
(URLValidator(), 'http://.com', ValidationError),
|
|
|
|
(URLValidator(), 'http://invalid-.com', ValidationError),
|
|
|
|
(URLValidator(), 'http://-invalid.com', ValidationError),
|
|
|
|
(URLValidator(), 'http://inv-.alid-.com', ValidationError),
|
|
|
|
(URLValidator(), 'http://inv-.-alid.com', ValidationError),
|
|
|
|
|
|
|
|
(BaseValidator(True), True, None),
|
|
|
|
(BaseValidator(True), False, ValidationError),
|
|
|
|
|
2011-06-10 01:51:03 +02:00
|
|
|
(RegexValidator(), '', None),
|
|
|
|
(RegexValidator(), 'x1x2', None),
|
|
|
|
(RegexValidator('[0-9]+'), 'xxxxxx', ValidationError),
|
|
|
|
(RegexValidator('[0-9]+'), '1234', None),
|
|
|
|
(RegexValidator(re.compile('[0-9]+')), '1234', None),
|
2010-01-05 04:56:19 +01:00
|
|
|
(RegexValidator('.*'), '', None),
|
|
|
|
(RegexValidator(re.compile('.*')), '', None),
|
|
|
|
(RegexValidator('.*'), 'xxxxx', None),
|
|
|
|
|
|
|
|
(RegexValidator('x'), 'y', ValidationError),
|
|
|
|
(RegexValidator(re.compile('x')), 'y', ValidationError),
|
|
|
|
)
|
|
|
|
|
2013-11-02 22:34:05 +01:00
|
|
|
|
2010-01-05 04:56:19 +01:00
|
|
|
def create_simple_test_method(validator, expected, value, num):
|
2010-01-21 04:02:13 +01:00
|
|
|
if expected is not None and issubclass(expected, Exception):
|
2010-01-05 04:56:19 +01:00
|
|
|
test_mask = 'test_%s_raises_error_%d'
|
2013-10-22 12:21:07 +02:00
|
|
|
|
2010-01-05 04:56:19 +01:00
|
|
|
def test_func(self):
|
2012-10-09 13:34:03 +02:00
|
|
|
# assertRaises not used, so as to be able to produce an error message
|
|
|
|
# containing the tested value
|
|
|
|
try:
|
|
|
|
validator(value)
|
|
|
|
except expected:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
self.fail("%s not raised when validating '%s'" % (
|
|
|
|
expected.__name__, value))
|
2010-01-05 04:56:19 +01:00
|
|
|
else:
|
|
|
|
test_mask = 'test_%s_%d'
|
2013-10-22 12:21:07 +02:00
|
|
|
|
2010-01-05 04:56:19 +01:00
|
|
|
def test_func(self):
|
2012-10-09 13:34:03 +02:00
|
|
|
try:
|
|
|
|
self.assertEqual(expected, validator(value))
|
|
|
|
except ValidationError as e:
|
|
|
|
self.fail("Validation of '%s' failed. Error message was: %s" % (
|
|
|
|
value, str(e)))
|
2010-01-05 04:56:19 +01:00
|
|
|
if isinstance(validator, types.FunctionType):
|
|
|
|
val_name = validator.__name__
|
|
|
|
else:
|
|
|
|
val_name = validator.__class__.__name__
|
|
|
|
test_name = test_mask % (val_name, num)
|
|
|
|
return test_name, test_func
|
|
|
|
|
|
|
|
# Dynamically assemble a test class with the contents of TEST_DATA
|
|
|
|
|
2013-11-02 22:34:05 +01:00
|
|
|
|
2010-01-05 04:56:19 +01:00
|
|
|
class TestSimpleValidators(TestCase):
|
2010-04-15 13:19:59 +02:00
|
|
|
def test_single_message(self):
|
|
|
|
v = ValidationError('Not Valid')
|
2012-06-07 18:08:47 +02:00
|
|
|
self.assertEqual(str(v), str_prefix("[%(_)s'Not Valid']"))
|
|
|
|
self.assertEqual(repr(v), str_prefix("ValidationError([%(_)s'Not Valid'])"))
|
2010-04-15 13:19:59 +02:00
|
|
|
|
|
|
|
def test_message_list(self):
|
|
|
|
v = ValidationError(['First Problem', 'Second Problem'])
|
2012-06-07 18:08:47 +02:00
|
|
|
self.assertEqual(str(v), str_prefix("[%(_)s'First Problem', %(_)s'Second Problem']"))
|
|
|
|
self.assertEqual(repr(v), str_prefix("ValidationError([%(_)s'First Problem', %(_)s'Second Problem'])"))
|
2010-04-15 13:19:59 +02:00
|
|
|
|
|
|
|
def test_message_dict(self):
|
2013-04-04 21:21:57 +02:00
|
|
|
v = ValidationError({'first': ['First Problem']})
|
2013-06-05 20:55:05 +02:00
|
|
|
self.assertEqual(str(v), str_prefix("{%(_)s'first': [%(_)s'First Problem']}"))
|
|
|
|
self.assertEqual(repr(v), str_prefix("ValidationError({%(_)s'first': [%(_)s'First Problem']})"))
|
2010-01-05 04:56:19 +01:00
|
|
|
|
|
|
|
test_counter = 0
|
|
|
|
for validator, value, expected in TEST_DATA:
|
|
|
|
name, method = create_simple_test_method(validator, expected, value, test_counter)
|
|
|
|
setattr(TestSimpleValidators, name, method)
|
|
|
|
test_counter += 1
|