summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAymeric Augustin <aymeric.augustin@m4x.org>2013-06-24 19:48:23 +0200
committerAymeric Augustin <aymeric.augustin@m4x.org>2013-06-24 20:42:42 +0200
commite3b6fed320f6e67b7e5dc28bab610f342a81f7d7 (patch)
treed6cce3951f632a246ab8084f478a1513e2f7ca49
parentc97cc85b748524d2e0d66c770d485ffeded8e950 (diff)
[1.4.x] 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. Backport of 0261922 from stable/1.5.x. Conflicts: django/test/utils.py
-rw-r--r--django/test/utils.py22
1 files changed, 13 insertions, 9 deletions
diff --git a/django/test/utils.py b/django/test/utils.py
index 6d6b6e177f..e31654e5c6 100644
--- a/django/test/utils.py
+++ b/django/test/utils.py
@@ -4,6 +4,7 @@ import warnings
from django.conf import settings, UserSettingsHolder
from django.core import mail
from django.test.signals import template_rendered, setting_changed
+from django.http import request
from django.template import Template, loader, TemplateDoesNotExist
from django.template.loaders import cached
from django.utils.translation import deactivate
@@ -69,13 +70,16 @@ def setup_test_environment():
- Set the email backend to the locmem email backend.
- 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
- 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._original_allowed_hosts = settings.ALLOWED_HOSTS
+ request._original_allowed_hosts = settings.ALLOWED_HOSTS
settings.ALLOWED_HOSTS = ['*']
mail.outbox = []
@@ -90,14 +94,14 @@ def teardown_test_environment():
- Restoring the email sending functions
"""
- Template._render = Template.original_render
- del Template.original_render
+ Template._render = Template._original_render
+ del Template._original_render
- settings.EMAIL_BACKEND = mail.original_email_backend
- del mail.original_email_backend
+ settings.EMAIL_BACKEND = mail._original_email_backend
+ del mail._original_email_backend
- settings.ALLOWED_HOSTS = settings._original_allowed_hosts
- del settings._original_allowed_hosts
+ settings.ALLOWED_HOSTS = request._original_allowed_hosts
+ del request._original_allowed_hosts
del mail.outbox