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 /docs | |
| 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 'docs')
| -rw-r--r-- | docs/ref/settings.txt | 10 | ||||
| -rw-r--r-- | docs/releases/1.11.txt | 5 | ||||
| -rw-r--r-- | docs/spelling_wordlist | 1 | ||||
| -rw-r--r-- | docs/topics/testing/advanced.txt | 55 |
4 files changed, 68 insertions, 3 deletions
diff --git a/docs/ref/settings.txt b/docs/ref/settings.txt index 01cecd44a4..7b9c8e89fb 100644 --- a/docs/ref/settings.txt +++ b/docs/ref/settings.txt @@ -90,14 +90,18 @@ If the ``Host`` header (or ``X-Forwarded-Host`` if list, the :meth:`django.http.HttpRequest.get_host()` method will raise :exc:`~django.core.exceptions.SuspiciousOperation`. -When :setting:`DEBUG` is ``True`` or when running tests, host validation is -disabled; any host will be accepted. Thus it's usually only necessary to set it -in production. +When :setting:`DEBUG` is ``True``, host validation is disabled; any host will +be accepted. ``ALLOWED_HOSTS`` is :ref:`checked when running tests +<topics-testing-advanced-multiple-hosts>`. This validation only applies via :meth:`~django.http.HttpRequest.get_host()`; if your code accesses the ``Host`` header directly from ``request.META`` you are bypassing this security protection. +.. versionchanged:: 1.11 + + In older versions, ``ALLOWED_HOSTS`` wasn't checked when running tests. + .. setting:: APPEND_SLASH ``APPEND_SLASH`` diff --git a/docs/releases/1.11.txt b/docs/releases/1.11.txt index 019b52f0f8..9a1cc81d8d 100644 --- a/docs/releases/1.11.txt +++ b/docs/releases/1.11.txt @@ -262,6 +262,11 @@ Miscellaneous * CSRF failures are logged to the ``django.security.csrf ``` logger instead of ``django.request``. +* :setting:`ALLOWED_HOSTS` validation is no longer disabled when running tests. + If your application includes tests with custom host names, you must include + those host names in :setting:`ALLOWED_HOSTS`. See + :ref:`topics-testing-advanced-multiple-hosts`. + * Using a foreign key's id (e.g. ``'field_id'``) in ``ModelAdmin.list_display`` displays the related object's ID. Remove the ``_id`` suffix if you want the old behavior of the string representation of the object. diff --git a/docs/spelling_wordlist b/docs/spelling_wordlist index 4355bbd065..557d139a3e 100644 --- a/docs/spelling_wordlist +++ b/docs/spelling_wordlist @@ -498,6 +498,7 @@ multiline multilinestring multipart multipolygon +multitenancy multithreaded multithreading Mumbai diff --git a/docs/topics/testing/advanced.txt b/docs/topics/testing/advanced.txt index 754904e1d0..d69e95e5a8 100644 --- a/docs/topics/testing/advanced.txt +++ b/docs/topics/testing/advanced.txt @@ -67,6 +67,61 @@ The following is a simple unit test using the request factory:: response = MyView.as_view()(request) self.assertEqual(response.status_code, 200) +.. _topics-testing-advanced-multiple-hosts: + +Tests and multiple host names +============================= + +The :setting:`ALLOWED_HOSTS` setting is validated when running tests. This +allows the test client to differentiate between internal and external URLs. + +Projects that support multitenancy or otherwise alter business logic based on +the request's host and use custom host names in tests must include those hosts +in :setting:`ALLOWED_HOSTS`. + +The first and simplest option to do so is to add the hosts to your settings +file. For example, the test suite for docs.djangoproject.com includes the +following:: + + from django.test import TestCase + + class SearchFormTestCase(TestCase): + def test_empty_get(self): + response = self.client.get('/en/dev/search/', HTTP_HOST='docs.djangoproject.dev:8000') + self.assertEqual(response.status_code, 200) + +and the settings file includes a list of the domains supported by the project:: + + ALLOWED_HOSTS = [ + 'www.djangoproject.dev', + 'docs.djangoproject.dev', + ... + ] + +Another option is to add the required hosts to :setting:`ALLOWED_HOSTS` using +:meth:`~django.test.override_settings()` or +:attr:`~django.test.SimpleTestCase.modify_settings()`. This option may be +preferable in standalone apps that can't package their own settings file or +for projects where the list of domains is not static (e.g., subdomains for +multitenancy). For example, you could write a test for the domain +``http://otherserver/`` as follows:: + + from django.test import TestCase, override_settings + + class MultiDomainTestCase(TestCase): + @override_settings(ALLOWED_HOSTS=['otherserver']) + def test_other_domain(self): + response = self.client.get('http://otherserver/foo/bar/') + +Disabling :setting:`ALLOWED_HOSTS` checking (``ALLOWED_HOSTS = ['*']``) when +running tests prevents the test client from raising a helpful error message if +you follow a redirect to an external URL. + +.. versionchanged:: 1.11 + + Older versions didn't validate ``ALLOWED_HOSTS`` while testing so these + techniques weren't necessary. + .. _topics-testing-advanced-multidb: Tests and multiple databases |
