2008-02-25 06:02:35 +00:00
|
|
|
from sys import version_info
|
2009-09-21 22:31:51 +00:00
|
|
|
try:
|
|
|
|
from functools import wraps
|
|
|
|
except ImportError:
|
2010-05-04 14:00:30 +00:00
|
|
|
from django.utils.functional import wraps # Python 2.4 fallback.
|
2008-02-25 06:02:35 +00:00
|
|
|
|
2010-10-11 12:55:17 +00:00
|
|
|
from django.contrib.auth.decorators import login_required, permission_required, user_passes_test
|
|
|
|
from django.contrib.admin.views.decorators import staff_member_required
|
2009-09-21 22:31:51 +00:00
|
|
|
from django.http import HttpResponse, HttpRequest
|
2010-10-11 12:55:17 +00:00
|
|
|
from django.utils.decorators import method_decorator
|
2008-02-25 06:02:35 +00:00
|
|
|
from django.utils.functional import allow_lazy, lazy, memoize
|
2010-10-11 12:55:17 +00:00
|
|
|
from django.utils.unittest import TestCase
|
2008-02-25 06:02:35 +00:00
|
|
|
from django.views.decorators.http import require_http_methods, require_GET, require_POST
|
|
|
|
from django.views.decorators.vary import vary_on_headers, vary_on_cookie
|
|
|
|
from django.views.decorators.cache import cache_page, never_cache, cache_control
|
2010-10-11 12:55:17 +00:00
|
|
|
|
2008-02-25 06:02:35 +00:00
|
|
|
|
|
|
|
def fully_decorated(request):
|
|
|
|
"""Expected __doc__"""
|
|
|
|
return HttpResponse('<html><body>dummy</body></html>')
|
|
|
|
fully_decorated.anything = "Expected __dict__"
|
|
|
|
|
|
|
|
# django.views.decorators.http
|
|
|
|
fully_decorated = require_http_methods(["GET"])(fully_decorated)
|
|
|
|
fully_decorated = require_GET(fully_decorated)
|
|
|
|
fully_decorated = require_POST(fully_decorated)
|
|
|
|
|
|
|
|
# django.views.decorators.vary
|
|
|
|
fully_decorated = vary_on_headers('Accept-language')(fully_decorated)
|
|
|
|
fully_decorated = vary_on_cookie(fully_decorated)
|
|
|
|
|
|
|
|
# django.views.decorators.cache
|
|
|
|
fully_decorated = cache_page(60*15)(fully_decorated)
|
|
|
|
fully_decorated = cache_control(private=True)(fully_decorated)
|
|
|
|
fully_decorated = never_cache(fully_decorated)
|
|
|
|
|
|
|
|
# django.contrib.auth.decorators
|
2009-04-01 16:17:38 +00:00
|
|
|
# Apply user_passes_test twice to check #9474
|
2008-02-25 06:02:35 +00:00
|
|
|
fully_decorated = user_passes_test(lambda u:True)(fully_decorated)
|
|
|
|
fully_decorated = login_required(fully_decorated)
|
|
|
|
fully_decorated = permission_required('change_world')(fully_decorated)
|
|
|
|
|
|
|
|
# django.contrib.admin.views.decorators
|
|
|
|
fully_decorated = staff_member_required(fully_decorated)
|
|
|
|
|
|
|
|
# django.utils.functional
|
|
|
|
fully_decorated = memoize(fully_decorated, {}, 1)
|
|
|
|
fully_decorated = allow_lazy(fully_decorated)
|
|
|
|
fully_decorated = lazy(fully_decorated)
|
|
|
|
|
2010-02-09 15:02:39 +00:00
|
|
|
|
2008-02-25 06:02:35 +00:00
|
|
|
class DecoratorsTest(TestCase):
|
|
|
|
|
|
|
|
def test_attributes(self):
|
|
|
|
"""
|
|
|
|
Tests that django decorators set certain attributes of the wrapped
|
|
|
|
function.
|
|
|
|
"""
|
|
|
|
# Only check __name__ on Python 2.4 or later since __name__ can't be
|
|
|
|
# assigned to in earlier Python versions.
|
|
|
|
if version_info[0] >= 2 and version_info[1] >= 4:
|
|
|
|
self.assertEquals(fully_decorated.__name__, 'fully_decorated')
|
|
|
|
self.assertEquals(fully_decorated.__doc__, 'Expected __doc__')
|
|
|
|
self.assertEquals(fully_decorated.__dict__['anything'], 'Expected __dict__')
|
2009-04-01 16:17:38 +00:00
|
|
|
|
|
|
|
def test_user_passes_test_composition(self):
|
|
|
|
"""
|
|
|
|
Test that the user_passes_test decorator can be applied multiple times
|
|
|
|
(#9474).
|
|
|
|
"""
|
|
|
|
def test1(user):
|
|
|
|
user.decorators_applied.append('test1')
|
|
|
|
return True
|
2010-10-11 12:55:17 +00:00
|
|
|
|
2009-04-01 16:17:38 +00:00
|
|
|
def test2(user):
|
|
|
|
user.decorators_applied.append('test2')
|
|
|
|
return True
|
2010-10-11 12:55:17 +00:00
|
|
|
|
2009-04-01 16:17:38 +00:00
|
|
|
def callback(request):
|
|
|
|
return request.user.decorators_applied
|
|
|
|
|
|
|
|
callback = user_passes_test(test1)(callback)
|
|
|
|
callback = user_passes_test(test2)(callback)
|
2010-10-11 12:55:17 +00:00
|
|
|
|
2009-04-01 16:17:38 +00:00
|
|
|
class DummyUser(object): pass
|
|
|
|
class DummyRequest(object): pass
|
2010-10-11 12:55:17 +00:00
|
|
|
|
2009-04-01 16:17:38 +00:00
|
|
|
request = DummyRequest()
|
|
|
|
request.user = DummyUser()
|
|
|
|
request.user.decorators_applied = []
|
|
|
|
response = callback(request)
|
2010-10-11 12:55:17 +00:00
|
|
|
|
2009-04-01 16:17:38 +00:00
|
|
|
self.assertEqual(response, ['test2', 'test1'])
|
2009-09-21 22:31:51 +00:00
|
|
|
|
|
|
|
def test_cache_page_new_style(self):
|
|
|
|
"""
|
|
|
|
Test that we can call cache_page the new way
|
|
|
|
"""
|
|
|
|
def my_view(request):
|
|
|
|
return "response"
|
|
|
|
my_view_cached = cache_page(123)(my_view)
|
|
|
|
self.assertEqual(my_view_cached(HttpRequest()), "response")
|
2009-09-28 21:54:54 +00:00
|
|
|
my_view_cached2 = cache_page(123, key_prefix="test")(my_view)
|
|
|
|
self.assertEqual(my_view_cached2(HttpRequest()), "response")
|
2009-09-21 22:31:51 +00:00
|
|
|
|
|
|
|
def test_cache_page_old_style(self):
|
|
|
|
"""
|
|
|
|
Test that we can call cache_page the old way
|
|
|
|
"""
|
|
|
|
def my_view(request):
|
|
|
|
return "response"
|
2009-09-26 19:39:42 +00:00
|
|
|
my_view_cached = cache_page(my_view, 123)
|
2009-09-21 22:31:51 +00:00
|
|
|
self.assertEqual(my_view_cached(HttpRequest()), "response")
|
2009-09-28 21:54:54 +00:00
|
|
|
my_view_cached2 = cache_page(my_view, 123, key_prefix="test")
|
|
|
|
self.assertEqual(my_view_cached2(HttpRequest()), "response")
|
2010-09-21 19:32:22 +00:00
|
|
|
my_view_cached3 = cache_page(my_view)
|
|
|
|
self.assertEqual(my_view_cached3(HttpRequest()), "response")
|
|
|
|
my_view_cached4 = cache_page()(my_view)
|
|
|
|
self.assertEqual(my_view_cached4(HttpRequest()), "response")
|
2009-09-21 22:31:51 +00:00
|
|
|
|
2010-02-09 15:02:39 +00:00
|
|
|
|
|
|
|
# For testing method_decorator, a decorator that assumes a single argument.
|
|
|
|
# We will get type arguments if there is a mismatch in the number of arguments.
|
|
|
|
def simple_dec(func):
|
|
|
|
def wrapper(arg):
|
|
|
|
return func("test:" + arg)
|
|
|
|
return wraps(func)(wrapper)
|
|
|
|
|
|
|
|
simple_dec_m = method_decorator(simple_dec)
|
|
|
|
|
|
|
|
|
|
|
|
class MethodDecoratorTests(TestCase):
|
|
|
|
"""
|
|
|
|
Tests for method_decorator
|
|
|
|
"""
|
|
|
|
def test_method_decorator(self):
|
|
|
|
class Test(object):
|
|
|
|
@simple_dec_m
|
|
|
|
def say(self, arg):
|
|
|
|
return arg
|
|
|
|
|
|
|
|
self.assertEqual("test:hello", Test().say("hello"))
|