Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #16816 -- Tweaked the test mock for `URLField.verify_exists` to…

… allow tests to pass when there is no Internet connection available. Many thanks to Ramiro Morales, Aymeric Augustin and Florian Apolloner for the patch reviews.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17059 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 977316e0cb1a266b62112f58226f0a447f71336a 1 parent 422f6e8
Julien Phalip authored October 31, 2011
6  tests/modeltests/validation/tests.py
@@ -6,9 +6,13 @@
6 6
 from django.core.exceptions import NON_FIELD_ERRORS
7 7
 from django.test import TestCase
8 8
 
  9
+# Import the verify_exists_urls from the 'forms' test app
  10
+from regressiontests.forms.tests.fields import verify_exists_urls
  11
+
9 12
 from . import ValidationTestCase
10 13
 from .models import (Author, Article, ModelToValidate,
11 14
     GenericIPAddressTestModel, GenericIPAddrUnpackUniqueTest)
  15
+
12 16
 # Import other tests for this package.
13 17
 from .test_custom_messages import CustomMessagesTest
14 18
 from .test_error_messages import ValidationMessagesTest
@@ -71,10 +75,12 @@ def test_correct_url_but_nonexisting_gives_404(self):
71 75
         mtv = ModelToValidate(number=10, name='Some Name', url_verify='http://qa-dev.w3.org/link-testsuite/http.php?code=404')
72 76
         self.assertFieldFailsValidationWithMessage(mtv.full_clean, 'url_verify', [u'This URL appears to be a broken link.'])
73 77
 
  78
+    @verify_exists_urls(existing_urls=('http://www.google.com/',))
74 79
     def test_correct_url_value_passes(self):
75 80
         mtv = ModelToValidate(number=10, name='Some Name', url_verify='http://www.google.com/')
76 81
         self.assertEqual(None, mtv.full_clean()) # This will fail if there's no Internet connection
77 82
 
  83
+    @verify_exists_urls(existing_urls=('http://qa-dev.w3.org/link-testsuite/http.php?code=301',))
78 84
     def test_correct_url_with_redirect(self):
79 85
         mtv = ModelToValidate(number=10, name='Some Name', url_verify='http://qa-dev.w3.org/link-testsuite/http.php?code=301') #example.com is a redirect to iana.org now
80 86
         self.assertEqual(None, mtv.full_clean()) # This will fail if there's no Internet connection
23  tests/regressiontests/forms/tests/fields.py
@@ -49,23 +49,27 @@ def fix_os_paths(x):
49 49
 
50 50
 
51 51
 def verify_exists_urls(existing_urls=()):
  52
+    """
  53
+    Patches urllib to simulate the availability of some urls even when there
  54
+    is no Internet connection. This hack should be removed alongside with
  55
+    `URLField.verify_exists` in Django 1.5.
  56
+    """
52 57
     def decorator(func):
53 58
         @wraps(func)
54 59
         def wrapper(*args, **kwargs):
55 60
             from django.core import validators
56  
-            # patch urllib2
57  
-            original_urlopen = validators.urllib2.urlopen
58  
-            def urlopen(req):
59  
-                url = req.get_full_url()
60  
-                if url in existing_urls:
  61
+            # patch urllib2.OpenerDirector
  62
+            original_open = validators.urllib2.OpenerDirector.open
  63
+            def custom_open(self, req, data=None, timeout=None):
  64
+                if req.get_full_url() in existing_urls:
61 65
                     return True
62 66
                 raise Exception()
63 67
             try:
64  
-                urllib2.urlopen = urlopen
  68
+                urllib2.OpenerDirector.open = custom_open
65 69
                 func(*args, **kwargs)
66 70
             finally:
67  
-                # unpatch urllib2
68  
-                validators.urllib2.urlopen = original_urlopen
  71
+                # unpatch urllib2.OpenerDirector
  72
+                validators.urllib2.OpenerDirector.open = original_open
69 73
         return wrapper
70 74
     return decorator
71 75
 
@@ -690,8 +694,9 @@ def test_urlfield_9(self):
690 694
         except ValidationError, e:
691 695
             self.assertEqual("[u'This URL appears to be a broken link.']", str(e))
692 696
 
  697
+    @verify_exists_urls((u'http://xn--hxargifdar.idn.icann.org/%CE%91%CF%81%CF%87%CE%B9%CE%BA%CE%AE_%CF%83%CE%B5%CE%BB%CE%AF%CE%B4%CE%B1',))
693 698
     def test_urlfield_10(self):
694  
-        # UTF-8 in the domain. 
  699
+        # UTF-8 in the domain.
695 700
         f = URLField(verify_exists=True)
696 701
         url = u'http://\u03b5\u03bb\u03bb\u03b7\u03bd\u03b9\u03ba\u03ac.idn.icann.org/\u0391\u03c1\u03c7\u03b9\u03ba\u03ae_\u03c3\u03b5\u03bb\u03af\u03b4\u03b1'
697 702
         self.assertEqual(url, f.clean(url))

0 notes on commit 977316e

Please sign in to comment.
Something went wrong with that request. Please try again.