Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added a test for issue 654 (leaking of extra_context), could not repr…

…oduce the issue
  • Loading branch information...
commit a02b915a7f019b44871b3f0b72049c4136ec59c9 1 parent 9b47e0d
@ojii ojii authored
View
39 cms/test/util/context_managers.py
@@ -6,6 +6,9 @@
import StringIO
import sys
+class NULL:
+ pass
+
class SettingsOverride(object):
"""
Overrides Django settings within a context and resets them to their inital
@@ -16,8 +19,6 @@ class SettingsOverride(object):
with SettingsOverride(DEBUG=True):
# do something
"""
- class DoesNotExist:
- pass
def __init__(self, **overrides):
self.overrides = overrides
@@ -25,12 +26,12 @@ def __init__(self, **overrides):
def __enter__(self):
self.old = {}
for key, value in self.overrides.items():
- self.old[key] = getattr(settings, key, self.DoesNotExist)
+ self.old[key] = getattr(settings, key, NULL)
setattr(settings, key, value)
def __exit__(self, type, value, traceback):
for key, value in self.old.items():
- if value is not self.DoesNotExist:
+ if value is not NULL:
setattr(settings, key, value)
else:
del settings[key] # do not pollute the context!
@@ -96,6 +97,7 @@ def cleanup(self):
def __exit__(self, exc, value, tb):
self.cleanup()
+
class UserLoginContext(object):
def __init__(self, testcase, user):
self.testcase = testcase
@@ -106,4 +108,31 @@ def __enter__(self):
def __exit__(self, exc, value, tb):
self.testcase.user = None
- self.testcase.client.logout()
+ self.testcase.client.logout()
+
+
+class ChangeModel(object):
+ """
+ Changes attributes on a model while within the context.
+
+ These changes *ARE* saved to the database for the context!
+ """
+ def __init__(self, instance, **overrides):
+ self.instance = instance
+ self.overrides = overrides
+
+ def __enter__(self):
+ self.old = {}
+ for key, value in self.overrides.items():
+ self.old[key] = getattr(self.instance, key, NULL)
+ setattr(self.instance, key, value)
+ self.instance.save()
+
+ def __exit__(self, exc, value, tb):
+ for key in self.overrides.keys():
+ old_value = self.old[key]
+ if old_value is NULL:
+ delattr(self.instance, key)
+ else:
+ setattr(self.instance, key, old_value)
+ self.instance.save()
View
9 cms/tests/rendering.py
@@ -5,13 +5,14 @@
from cms.plugin_rendering import render_plugins, PluginContext
from cms.plugins.text.models import Text
from cms.test.testcases import CMSTestCase
-from cms.test.util.context_managers import SettingsOverride
+from cms.test.util.context_managers import SettingsOverride, ChangeModel
from django.conf import settings
from django.contrib.auth.models import User
from django.contrib.sites.models import Site
from django.forms.widgets import Media
from django.http import Http404, HttpResponseRedirect
from django.template import Template, RequestContext
+from django.template.loader import render_to_string
TEMPLATE_NAME = 'tests/rendering/base.html'
@@ -285,4 +286,8 @@ class Mock:
response = details(request, slug=self.test_page.get_slug())
self.assertTrue(isinstance(response,HttpResponseRedirect))
-
+
+ def test_12_extra_context_isolation(self):
+ with ChangeModel(self.test_page, template='extra_context.html'):
+ response = self.client.get(self.test_page.get_absolute_url())
+ self.assertTrue('width' not in response.context)
View
5 tests/testapp/settings.py
@@ -159,6 +159,11 @@
'TextPlugin', 'SnippetPlugin','GoogleMapPlugin',),
'name': gettext("right column")
},
+ 'extra_context': {
+ "plugins": ('TextPlugin',),
+ "extra_context": {"width": 250},
+ "name": "extra context"
+ },
}
CMS_SOFTROOT = True
View
1  tests/testapp/templates/extra_context.html
@@ -0,0 +1 @@
+{% load cms_tags %}{% placeholder "extra_context" %}
Please sign in to comment.
Something went wrong with that request. Please try again.