diff --git a/tests/modeltests/validation/tests.py b/tests/modeltests/validation/tests.py
index a7e13e7..25683b4 100644
a
|
b
|
from __future__ import absolute_import
|
2 | 2 | |
3 | 3 | import warnings |
4 | 4 | |
5 | | from django import forms |
| 5 | from django.forms import ModelForm |
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 |
… |
… |
class BaseModelValidationTests(ValidationTestCase):
|
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 |
… |
… |
class BaseModelValidationTests(ValidationTestCase):
|
88 | 94 | self.assertFailsValidation(mtv.full_clean, ['name',]) |
89 | 95 | |
90 | 96 | |
91 | | class ArticleForm(forms.ModelForm): |
| 97 | class ArticleForm(ModelForm): |
92 | 98 | class Meta: |
93 | 99 | model = Article |
94 | 100 | exclude = ['author'] |
diff --git a/tests/regressiontests/forms/tests/fields.py b/tests/regressiontests/forms/tests/fields.py
index d37ad5e..ecb6c23 100644
a
|
b
|
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 | |
… |
… |
class FieldsTests(SimpleTestCase):
|
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)) |