Fixed #20636 -- Stopped stuffing values in the settings.

In Django < 1.6, override_settings restores the settings module that was
active when the override_settings call was executed, not when it was
run. This can make a difference when override_settings is applied to a
class, since it's executed when the module is imported, not when the
test case is run.

In addition, if the settings module for tests is stored alongside the
tests themselves, importing the settings module can trigger an import
of the tests. Since the settings module isn't fully imported yet,
class-level override_settings statements may store a reference to an
incorrect settings module. Eventually this will result in a crash during
test teardown because the settings module restored by override_settings
won't the one that was active during test setup.

While Django should prevent this situation in the future by failing
loudly in such dubious import sequences, that change won't be backported
to 1.5 and 1.4. However, these versions received the "allowed hosts"
patch and they're prone to "AttributeError: 'Settings' object has no
attribute '_original_allowed_hosts'". To mitigate this regression, this
commits stuffs _original_allowed_hosts on a random module instead of the
settings module.

This problem shouldn't occur in Django 1.6, see #20290, but this patch
will be forward-ported for extra safety.

Also tweaked backup variable names for consistency.

Forward port of 0261922 from stable/1.5.x.

Conflicts:
	django/test/utils.py
This commit is contained in:
Aymeric Augustin 2013-06-24 19:48:23 +02:00
parent e161e4ff11
commit 5a6f12182e
1 changed files with 13 additions and 9 deletions

View File

@ -10,6 +10,7 @@ from django.conf import settings, UserSettingsHolder
from django.core import mail from django.core import mail
from django.core.signals import request_started from django.core.signals import request_started
from django.db import reset_queries from django.db import reset_queries
from django.http import request
from django.template import Template, loader, TemplateDoesNotExist from django.template import Template, loader, TemplateDoesNotExist
from django.template.loaders import cached from django.template.loaders import cached
from django.test.signals import template_rendered, setting_changed from django.test.signals import template_rendered, setting_changed
@ -87,13 +88,16 @@ def setup_test_environment():
- Set the email backend to the locmem email backend. - Set the email backend to the locmem email backend.
- Setting the active locale to match the LANGUAGE_CODE setting. - Setting the active locale to match the LANGUAGE_CODE setting.
""" """
Template.original_render = Template._render Template._original_render = Template._render
Template._render = instrumented_test_render Template._render = instrumented_test_render
mail.original_email_backend = settings.EMAIL_BACKEND # Storing previous values in the settings module itself is problematic.
# Store them in arbitrary (but related) modules instead. See #20636.
mail._original_email_backend = settings.EMAIL_BACKEND
settings.EMAIL_BACKEND = 'django.core.mail.backends.locmem.EmailBackend' settings.EMAIL_BACKEND = 'django.core.mail.backends.locmem.EmailBackend'
settings._original_allowed_hosts = settings.ALLOWED_HOSTS request._original_allowed_hosts = settings.ALLOWED_HOSTS
settings.ALLOWED_HOSTS = ['*'] settings.ALLOWED_HOSTS = ['*']
mail.outbox = [] mail.outbox = []
@ -108,14 +112,14 @@ def teardown_test_environment():
- Restoring the email sending functions - Restoring the email sending functions
""" """
Template._render = Template.original_render Template._render = Template._original_render
del Template.original_render del Template._original_render
settings.EMAIL_BACKEND = mail.original_email_backend settings.EMAIL_BACKEND = mail._original_email_backend
del mail.original_email_backend del mail._original_email_backend
settings.ALLOWED_HOSTS = settings._original_allowed_hosts settings.ALLOWED_HOSTS = request._original_allowed_hosts
del settings._original_allowed_hosts del request._original_allowed_hosts
del mail.outbox del mail.outbox