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:
Ramiro Morales 2011-06-25 13:14:31 +00:00
parent 2d6dec24c4
commit 25c5251acd
2 changed files with 33 additions and 12 deletions

View File

@ -4,6 +4,7 @@ from django.forms import *
from django.test import TestCase
from django.utils.safestring import mark_safe
from django.utils import unittest
from regressiontests.forms.tests.fields import verify_exists_urls
class AssertFormErrorsMixin(object):
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', ''))
@verify_exists_urls()
def test_urlfield(self):
e = {
'required': 'REQUIRED',

View File

@ -25,11 +25,11 @@ Other than that, the Field subclasses have class-specific options for
__init__(). For example, CharField has a max_length option.
"""
import datetime
import time
import re
import os
import urllib2
from decimal import Decimal
from functools import wraps
from django.core.files.uploadedfile import SimpleUploadedFile
from django.forms import *
@ -48,6 +48,28 @@ def fix_os_paths(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):
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://.com')
@verify_exists_urls(('http://www.google.com/',))
def test_urlfield_3(self):
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.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
@ -611,10 +634,11 @@ class FieldsTests(TestCase):
except ValidationError, 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):
f = URLField(verify_exists=True, required=False)
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):
f = URLField(min_length=15, max_length=20)
@ -663,17 +687,12 @@ class FieldsTests(TestCase):
except ValidationError, 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):
# UTF-8 char in path, enclosed by a monkey-patch to make sure
# the encoding is passed to urllib2.urlopen
# UTF-8 char in path
f = URLField(verify_exists=True)
try:
_orig_urlopen = urllib2.urlopen
urllib2.urlopen = lambda req: True
url = u'http://t\xfcr.djangoproject.com/'
self.assertEqual(url, f.clean(url))
finally:
urllib2.urlopen = _orig_urlopen
url = u'http://t\xfcr.djangoproject.com/'
self.assertEqual(url, f.clean(url))
# BooleanField ################################################################