Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
  • Loading branch information...
commit 25c5251acd79293c8cbaef393ceb1db388262190 1 parent 2d6dec2
Ramiro Morales authored June 25, 2011
2  tests/regressiontests/forms/tests/error_messages.py
@@ -4,6 +4,7 @@
4 4
 from django.test import TestCase
5 5
 from django.utils.safestring import mark_safe
6 6
 from django.utils import unittest
  7
+from regressiontests.forms.tests.fields import verify_exists_urls
7 8
 
8 9
 class AssertFormErrorsMixin(object):
9 10
     def assertFormErrors(self, expected, the_callable, *args, **kwargs):
@@ -139,6 +140,7 @@ def test_filefield(self):
139 140
         self.assertFormErrors([u'EMPTY FILE'], f.clean, SimpleUploadedFile('name', None))
140 141
         self.assertFormErrors([u'EMPTY FILE'], f.clean, SimpleUploadedFile('name', ''))
141 142
 
  143
+    @verify_exists_urls()
142 144
     def test_urlfield(self):
143 145
         e = {
144 146
             'required': 'REQUIRED',
43  tests/regressiontests/forms/tests/fields.py
@@ -25,11 +25,11 @@
25 25
 __init__(). For example, CharField has a max_length option.
26 26
 """
27 27
 import datetime
28  
-import time
29 28
 import re
30 29
 import os
31 30
 import urllib2
32 31
 from decimal import Decimal
  32
+from functools import wraps
33 33
 
34 34
 from django.core.files.uploadedfile import SimpleUploadedFile
35 35
 from django.forms import *
@@ -48,6 +48,28 @@ def fix_os_paths(x):
48 48
         return x
49 49
 
50 50
 
  51
+def verify_exists_urls(existing_urls=()):
  52
+    def decorator(func):
  53
+        @wraps(func)
  54
+        def wrapper(*args, **kwargs):
  55
+            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
+                    return True
  62
+                raise Exception()
  63
+            try:
  64
+                urllib2.urlopen = urlopen
  65
+                func(*args, **kwargs)
  66
+            finally:
  67
+                # unpatch urllib2
  68
+                validators.urllib2.urlopen = original_urlopen
  69
+        return wrapper
  70
+    return decorator
  71
+
  72
+
51 73
 class FieldsTests(TestCase):
52 74
 
53 75
     def assertRaisesErrorWithMessage(self, error, message, callable, *args, **kwargs):
@@ -595,9 +617,10 @@ def test_urlfield_2(self):
595 617
         self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://example.')
596 618
         self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://.com')
597 619
 
  620
+    @verify_exists_urls(('http://www.google.com/',))
598 621
     def test_urlfield_3(self):
599 622
         f = URLField(verify_exists=True)
600  
-        self.assertEqual(u'http://www.google.com/', f.clean('http://www.google.com')) # This will fail if there's no Internet connection
  623
+        self.assertEqual(u'http://www.google.com/', f.clean('http://www.google.com'))
601 624
         self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://example')
602 625
         self.assertRaises(ValidationError, f.clean, 'http://www.broken.djangoproject.com') # bad domain
603 626
         self.assertRaises(ValidationError, f.clean, 'http://qa-dev.w3.org/link-testsuite/http.php?code=405') # Method not allowed
@@ -611,10 +634,11 @@ def test_urlfield_3(self):
611 634
         except ValidationError, e:
612 635
             self.assertEqual("[u'This URL appears to be a broken link.']", str(e))
613 636
 
  637
+    @verify_exists_urls(('http://www.google.com/',))
614 638
     def test_urlfield_4(self):
615 639
         f = URLField(verify_exists=True, required=False)
616 640
         self.assertEqual(u'', f.clean(''))
617  
-        self.assertEqual(u'http://www.google.com/', f.clean('http://www.google.com')) # This will fail if there's no Internet connection
  641
+        self.assertEqual(u'http://www.google.com/', f.clean('http://www.google.com'))
618 642
 
619 643
     def test_urlfield_5(self):
620 644
         f = URLField(min_length=15, max_length=20)
@@ -663,17 +687,12 @@ def test_urlfield_9(self):
663 687
         except ValidationError, e:
664 688
             self.assertEqual("[u'This URL appears to be a broken link.']", str(e))
665 689
 
  690
+    @verify_exists_urls(('http://xn--tr-xka.djangoproject.com/',))
666 691
     def test_urlfield_10(self):
667  
-        # UTF-8 char in path, enclosed by a monkey-patch to make sure
668  
-        # the encoding is passed to urllib2.urlopen
  692
+        # UTF-8 char in path
669 693
         f = URLField(verify_exists=True)
670  
-        try:
671  
-            _orig_urlopen = urllib2.urlopen
672  
-            urllib2.urlopen = lambda req: True
673  
-            url = u'http://t\xfcr.djangoproject.com/'
674  
-            self.assertEqual(url, f.clean(url))
675  
-        finally:
676  
-            urllib2.urlopen = _orig_urlopen
  694
+        url = u'http://t\xfcr.djangoproject.com/'
  695
+        self.assertEqual(url, f.clean(url))
677 696
 
678 697
     # BooleanField ################################################################
679 698
 

0 notes on commit 25c5251

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