mirror of https://github.com/django/django.git
Fixed #6189 -- Modified test that need Internet access so they use a mock instead. Thanks Gregor Müellegger for the patch.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@16451 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
2d6dec24c4
commit
25c5251acd
|
@ -4,6 +4,7 @@ from django.forms import *
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
from django.utils.safestring import mark_safe
|
from django.utils.safestring import mark_safe
|
||||||
from django.utils import unittest
|
from django.utils import unittest
|
||||||
|
from regressiontests.forms.tests.fields import verify_exists_urls
|
||||||
|
|
||||||
class AssertFormErrorsMixin(object):
|
class AssertFormErrorsMixin(object):
|
||||||
def assertFormErrors(self, expected, the_callable, *args, **kwargs):
|
def assertFormErrors(self, expected, the_callable, *args, **kwargs):
|
||||||
|
@ -139,6 +140,7 @@ class FormsErrorMessagesTestCase(unittest.TestCase, AssertFormErrorsMixin):
|
||||||
self.assertFormErrors([u'EMPTY FILE'], f.clean, SimpleUploadedFile('name', None))
|
self.assertFormErrors([u'EMPTY FILE'], f.clean, SimpleUploadedFile('name', None))
|
||||||
self.assertFormErrors([u'EMPTY FILE'], f.clean, SimpleUploadedFile('name', ''))
|
self.assertFormErrors([u'EMPTY FILE'], f.clean, SimpleUploadedFile('name', ''))
|
||||||
|
|
||||||
|
@verify_exists_urls()
|
||||||
def test_urlfield(self):
|
def test_urlfield(self):
|
||||||
e = {
|
e = {
|
||||||
'required': 'REQUIRED',
|
'required': 'REQUIRED',
|
||||||
|
|
|
@ -25,11 +25,11 @@ Other than that, the Field subclasses have class-specific options for
|
||||||
__init__(). For example, CharField has a max_length option.
|
__init__(). For example, CharField has a max_length option.
|
||||||
"""
|
"""
|
||||||
import datetime
|
import datetime
|
||||||
import time
|
|
||||||
import re
|
import re
|
||||||
import os
|
import os
|
||||||
import urllib2
|
import urllib2
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
|
from functools import wraps
|
||||||
|
|
||||||
from django.core.files.uploadedfile import SimpleUploadedFile
|
from django.core.files.uploadedfile import SimpleUploadedFile
|
||||||
from django.forms import *
|
from django.forms import *
|
||||||
|
@ -48,6 +48,28 @@ def fix_os_paths(x):
|
||||||
return x
|
return x
|
||||||
|
|
||||||
|
|
||||||
|
def verify_exists_urls(existing_urls=()):
|
||||||
|
def decorator(func):
|
||||||
|
@wraps(func)
|
||||||
|
def wrapper(*args, **kwargs):
|
||||||
|
from django.core import validators
|
||||||
|
# patch urllib2
|
||||||
|
original_urlopen = validators.urllib2.urlopen
|
||||||
|
def urlopen(req):
|
||||||
|
url = req.get_full_url()
|
||||||
|
if url in existing_urls:
|
||||||
|
return True
|
||||||
|
raise Exception()
|
||||||
|
try:
|
||||||
|
urllib2.urlopen = urlopen
|
||||||
|
func(*args, **kwargs)
|
||||||
|
finally:
|
||||||
|
# unpatch urllib2
|
||||||
|
validators.urllib2.urlopen = original_urlopen
|
||||||
|
return wrapper
|
||||||
|
return decorator
|
||||||
|
|
||||||
|
|
||||||
class FieldsTests(TestCase):
|
class FieldsTests(TestCase):
|
||||||
|
|
||||||
def assertRaisesErrorWithMessage(self, error, message, callable, *args, **kwargs):
|
def assertRaisesErrorWithMessage(self, error, message, callable, *args, **kwargs):
|
||||||
|
@ -595,9 +617,10 @@ class FieldsTests(TestCase):
|
||||||
self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://example.')
|
self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://example.')
|
||||||
self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://.com')
|
self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://.com')
|
||||||
|
|
||||||
|
@verify_exists_urls(('http://www.google.com/',))
|
||||||
def test_urlfield_3(self):
|
def test_urlfield_3(self):
|
||||||
f = URLField(verify_exists=True)
|
f = URLField(verify_exists=True)
|
||||||
self.assertEqual(u'http://www.google.com/', f.clean('http://www.google.com')) # This will fail if there's no Internet connection
|
self.assertEqual(u'http://www.google.com/', f.clean('http://www.google.com'))
|
||||||
self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://example')
|
self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://example')
|
||||||
self.assertRaises(ValidationError, f.clean, 'http://www.broken.djangoproject.com') # bad domain
|
self.assertRaises(ValidationError, f.clean, 'http://www.broken.djangoproject.com') # bad domain
|
||||||
self.assertRaises(ValidationError, f.clean, 'http://qa-dev.w3.org/link-testsuite/http.php?code=405') # Method not allowed
|
self.assertRaises(ValidationError, f.clean, 'http://qa-dev.w3.org/link-testsuite/http.php?code=405') # Method not allowed
|
||||||
|
@ -611,10 +634,11 @@ class FieldsTests(TestCase):
|
||||||
except ValidationError, e:
|
except ValidationError, e:
|
||||||
self.assertEqual("[u'This URL appears to be a broken link.']", str(e))
|
self.assertEqual("[u'This URL appears to be a broken link.']", str(e))
|
||||||
|
|
||||||
|
@verify_exists_urls(('http://www.google.com/',))
|
||||||
def test_urlfield_4(self):
|
def test_urlfield_4(self):
|
||||||
f = URLField(verify_exists=True, required=False)
|
f = URLField(verify_exists=True, required=False)
|
||||||
self.assertEqual(u'', f.clean(''))
|
self.assertEqual(u'', f.clean(''))
|
||||||
self.assertEqual(u'http://www.google.com/', f.clean('http://www.google.com')) # This will fail if there's no Internet connection
|
self.assertEqual(u'http://www.google.com/', f.clean('http://www.google.com'))
|
||||||
|
|
||||||
def test_urlfield_5(self):
|
def test_urlfield_5(self):
|
||||||
f = URLField(min_length=15, max_length=20)
|
f = URLField(min_length=15, max_length=20)
|
||||||
|
@ -663,17 +687,12 @@ class FieldsTests(TestCase):
|
||||||
except ValidationError, e:
|
except ValidationError, e:
|
||||||
self.assertEqual("[u'This URL appears to be a broken link.']", str(e))
|
self.assertEqual("[u'This URL appears to be a broken link.']", str(e))
|
||||||
|
|
||||||
|
@verify_exists_urls(('http://xn--tr-xka.djangoproject.com/',))
|
||||||
def test_urlfield_10(self):
|
def test_urlfield_10(self):
|
||||||
# UTF-8 char in path, enclosed by a monkey-patch to make sure
|
# UTF-8 char in path
|
||||||
# the encoding is passed to urllib2.urlopen
|
|
||||||
f = URLField(verify_exists=True)
|
f = URLField(verify_exists=True)
|
||||||
try:
|
url = u'http://t\xfcr.djangoproject.com/'
|
||||||
_orig_urlopen = urllib2.urlopen
|
self.assertEqual(url, f.clean(url))
|
||||||
urllib2.urlopen = lambda req: True
|
|
||||||
url = u'http://t\xfcr.djangoproject.com/'
|
|
||||||
self.assertEqual(url, f.clean(url))
|
|
||||||
finally:
|
|
||||||
urllib2.urlopen = _orig_urlopen
|
|
||||||
|
|
||||||
# BooleanField ################################################################
|
# BooleanField ################################################################
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue