diff options
| author | Tobias McNulty <tobias@caktusgroup.com> | 2016-06-03 15:02:38 -0700 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2016-06-20 11:07:46 -0400 |
| commit | 17e661641ddaf8266e7430d83cfb2039abc55df7 (patch) | |
| tree | faab4310a86fed5682ff343efcffc9a9816a2eb3 /tests | |
| parent | 00551c3eff5cedcb9cc7ce5af97b948d62713d97 (diff) | |
Refs #26666 -- Added ALLOWED_HOSTS validation when running tests.
Also used ALLOWED_HOSTS to check for external hosts in assertRedirects().
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/cache/tests.py | 1 | ||||
| -rw-r--r-- | tests/sites_tests/tests.py | 5 | ||||
| -rw-r--r-- | tests/staticfiles_tests/test_liveserver.py | 2 | ||||
| -rw-r--r-- | tests/test_client/tests.py | 8 | ||||
| -rw-r--r-- | tests/test_client_regress/tests.py | 10 |
5 files changed, 22 insertions, 4 deletions
diff --git a/tests/cache/tests.py b/tests/cache/tests.py index 9224434dfe..9099402f43 100644 --- a/tests/cache/tests.py +++ b/tests/cache/tests.py @@ -1393,6 +1393,7 @@ class DefaultNonExpiringCacheKeyTests(SimpleTestCase): }, }, USE_I18N=False, + ALLOWED_HOSTS=['.example.com'], ) class CacheUtils(SimpleTestCase): """TestCase for django.utils.cache functions.""" diff --git a/tests/sites_tests/tests.py b/tests/sites_tests/tests.py index 7a4215cc4a..8c540f82ea 100644 --- a/tests/sites_tests/tests.py +++ b/tests/sites_tests/tests.py @@ -142,6 +142,7 @@ class SitesFrameworkTests(TestCase): with self.assertRaises(ValidationError): site.full_clean() + @override_settings(ALLOWED_HOSTS=['example.com']) def test_clear_site_cache(self): request = HttpRequest() request.META = { @@ -162,7 +163,7 @@ class SitesFrameworkTests(TestCase): clear_site_cache(Site, instance=self.site, using='default') self.assertEqual(models.SITE_CACHE, {}) - @override_settings(SITE_ID='') + @override_settings(SITE_ID='', ALLOWED_HOSTS=['example2.com']) def test_clear_site_cache_domain(self): site = Site.objects.create(name='example2.com', domain='example2.com') request = HttpRequest() @@ -191,6 +192,7 @@ class SitesFrameworkTests(TestCase): self.assertEqual(Site.objects.get_by_natural_key(self.site.domain), self.site) self.assertEqual(self.site.natural_key(), (self.site.domain,)) + @override_settings(ALLOWED_HOSTS=['example.com']) def test_requestsite_save_notimplemented_msg(self): # Test response msg for RequestSite.save NotImplementedError request = HttpRequest() @@ -201,6 +203,7 @@ class SitesFrameworkTests(TestCase): with self.assertRaisesMessage(NotImplementedError, msg): RequestSite(request).save() + @override_settings(ALLOWED_HOSTS=['example.com']) def test_requestsite_delete_notimplemented_msg(self): # Test response msg for RequestSite.delete NotImplementedError request = HttpRequest() diff --git a/tests/staticfiles_tests/test_liveserver.py b/tests/staticfiles_tests/test_liveserver.py index ceabaaa5d9..2c9d7b71db 100644 --- a/tests/staticfiles_tests/test_liveserver.py +++ b/tests/staticfiles_tests/test_liveserver.py @@ -35,9 +35,9 @@ class LiveServerBase(StaticLiveServerTestCase): @classmethod def tearDownClass(cls): + super(LiveServerBase, cls).tearDownClass() # Restore original settings cls.settings_override.disable() - super(LiveServerBase, cls).tearDownClass() class StaticLiveServerChecks(LiveServerBase): diff --git a/tests/test_client/tests.py b/tests/test_client/tests.py index 1c43a2957e..3db42cb1c2 100644 --- a/tests/test_client/tests.py +++ b/tests/test_client/tests.py @@ -601,7 +601,13 @@ class ClientTest(TestCase): a relevant ValueError rather than a non-descript AssertionError. """ response = self.client.get('/django_project_redirect/') - with self.assertRaisesMessage(ValueError, 'unable to fetch'): + msg = ( + "The test client is unable to fetch remote URLs (got " + "https://www.djangoproject.com/). If the host is served by Django, " + "add 'www.djangoproject.com' to ALLOWED_HOSTS. " + "Otherwise, use assertRedirects(..., fetch_redirect_response=False)." + ) + with self.assertRaisesMessage(ValueError, msg): self.assertRedirects(response, 'https://www.djangoproject.com/') def test_session_modifying_view(self): diff --git a/tests/test_client_regress/tests.py b/tests/test_client_regress/tests.py index ea33d66a02..d03b2d14e9 100644 --- a/tests/test_client_regress/tests.py +++ b/tests/test_client_regress/tests.py @@ -15,7 +15,8 @@ from django.template import ( ) from django.template.response import SimpleTemplateResponse from django.test import ( - Client, SimpleTestCase, TestCase, ignore_warnings, override_settings, + Client, SimpleTestCase, TestCase, ignore_warnings, modify_settings, + override_settings, ) from django.test.client import RedirectCycleError, RequestFactory, encode_file from django.test.utils import ContextList, str_prefix @@ -455,6 +456,7 @@ class AssertRedirectsTests(SimpleTestCase): self.assertRedirects(response, '/no_template_view/', 302, 200) self.assertEqual(len(response.redirect_chain), 3) + @modify_settings(ALLOWED_HOSTS={'append': 'otherserver'}) def test_redirect_to_different_host(self): "The test client will preserve scheme, host and port changes" response = self.client.get('/redirect_other_host/', follow=True) @@ -467,6 +469,12 @@ class AssertRedirectsTests(SimpleTestCase): self.assertEqual(response.request.get('wsgi.url_scheme'), 'https') self.assertEqual(response.request.get('SERVER_NAME'), 'otherserver') self.assertEqual(response.request.get('SERVER_PORT'), '8443') + # assertRedirects() can follow redirect to 'otherserver' too. + response = self.client.get('/redirect_other_host/', follow=False) + self.assertRedirects( + response, 'https://otherserver:8443/no_template_view/', + status_code=302, target_status_code=200 + ) def test_redirect_chain_on_non_redirect_page(self): "An assertion is raised if the original page couldn't be retrieved as expected" |
