2015-01-28 20:35:27 +08:00
|
|
|
from functools import update_wrapper, wraps
|
2021-12-13 15:57:19 +08:00
|
|
|
from unittest import TestCase, mock
|
2008-02-25 14:02:35 +08:00
|
|
|
|
2010-10-11 20:55:17 +08:00
|
|
|
from django.contrib.admin.views.decorators import staff_member_required
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.contrib.auth.decorators import (
|
|
|
|
login_required,
|
|
|
|
permission_required,
|
|
|
|
user_passes_test,
|
|
|
|
)
|
|
|
|
from django.http import HttpRequest, HttpResponse, HttpResponseNotAllowed
|
2011-10-14 02:51:33 +08:00
|
|
|
from django.middleware.clickjacking import XFrameOptionsMiddleware
|
2015-07-22 04:54:37 +08:00
|
|
|
from django.test import SimpleTestCase
|
2010-10-11 20:55:17 +08:00
|
|
|
from django.utils.decorators import method_decorator
|
2016-12-31 22:52:31 +08:00
|
|
|
from django.utils.functional import keep_lazy, keep_lazy_text, lazy
|
2016-06-03 05:11:43 +08:00
|
|
|
from django.utils.safestring import mark_safe
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.views.decorators.cache import cache_control, cache_page, never_cache
|
|
|
|
from django.views.decorators.clickjacking import (
|
|
|
|
xframe_options_deny,
|
|
|
|
xframe_options_exempt,
|
|
|
|
xframe_options_sameorigin,
|
|
|
|
)
|
|
|
|
from django.views.decorators.http import (
|
|
|
|
condition,
|
|
|
|
require_GET,
|
|
|
|
require_http_methods,
|
|
|
|
require_POST,
|
|
|
|
require_safe,
|
|
|
|
)
|
|
|
|
from django.views.decorators.vary import vary_on_cookie, vary_on_headers
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2008-02-25 14:02:35 +08:00
|
|
|
|
|
|
|
def fully_decorated(request):
|
|
|
|
"""Expected __doc__"""
|
|
|
|
return HttpResponse("<html><body>dummy</body></html>")
|
2016-11-13 01:11:23 +08:00
|
|
|
|
|
|
|
|
2008-02-25 14:02:35 +08:00
|
|
|
fully_decorated.anything = "Expected __dict__"
|
|
|
|
|
|
|
|
|
2011-05-24 00:08:36 +08:00
|
|
|
def compose(*functions):
|
|
|
|
# compose(f, g)(*args, **kwargs) == f(g(*args, **kwargs))
|
|
|
|
functions = list(reversed(functions))
|
2013-10-22 18:21:07 +08:00
|
|
|
|
2011-05-24 00:08:36 +08:00
|
|
|
def _inner(*args, **kwargs):
|
|
|
|
result = functions[0](*args, **kwargs)
|
|
|
|
for f in functions[1:]:
|
|
|
|
result = f(result)
|
|
|
|
return result
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2011-05-24 00:08:36 +08:00
|
|
|
return _inner
|
|
|
|
|
|
|
|
|
|
|
|
full_decorator = compose(
|
|
|
|
# django.views.decorators.http
|
|
|
|
require_http_methods(["GET"]),
|
|
|
|
require_GET,
|
|
|
|
require_POST,
|
|
|
|
require_safe,
|
2012-02-10 02:57:06 +08:00
|
|
|
condition(lambda r: None, lambda r: None),
|
2011-05-24 00:08:36 +08:00
|
|
|
# django.views.decorators.vary
|
|
|
|
vary_on_headers("Accept-language"),
|
|
|
|
vary_on_cookie,
|
|
|
|
# django.views.decorators.cache
|
2013-11-03 06:50:35 +08:00
|
|
|
cache_page(60 * 15),
|
2011-05-24 00:08:36 +08:00
|
|
|
cache_control(private=True),
|
|
|
|
never_cache,
|
|
|
|
# django.contrib.auth.decorators
|
|
|
|
# Apply user_passes_test twice to check #9474
|
2013-10-27 03:15:03 +08:00
|
|
|
user_passes_test(lambda u: True),
|
2011-05-24 00:08:36 +08:00
|
|
|
login_required,
|
|
|
|
permission_required("change_world"),
|
|
|
|
# django.contrib.admin.views.decorators
|
|
|
|
staff_member_required,
|
|
|
|
# django.utils.functional
|
2015-11-07 21:30:20 +08:00
|
|
|
keep_lazy(HttpResponse),
|
|
|
|
keep_lazy_text,
|
2011-05-24 00:08:36 +08:00
|
|
|
lazy,
|
2016-06-03 05:11:43 +08:00
|
|
|
# django.utils.safestring
|
|
|
|
mark_safe,
|
2011-05-24 00:08:36 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
fully_decorated = full_decorator(fully_decorated)
|
2010-02-09 23:02:39 +08:00
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2008-02-25 14:02:35 +08:00
|
|
|
class DecoratorsTest(TestCase):
|
|
|
|
def test_attributes(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
Built-in decorators set certain attributes of the wrapped function.
|
2008-02-25 14:02:35 +08:00
|
|
|
"""
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(fully_decorated.__name__, "fully_decorated")
|
|
|
|
self.assertEqual(fully_decorated.__doc__, "Expected __doc__")
|
|
|
|
self.assertEqual(fully_decorated.__dict__["anything"], "Expected __dict__")
|
2009-04-02 00:17:38 +08:00
|
|
|
|
|
|
|
def test_user_passes_test_composition(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
The user_passes_test decorator can be applied multiple times (#9474).
|
2009-04-02 00:17:38 +08:00
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2009-04-02 00:17:38 +08:00
|
|
|
def test1(user):
|
|
|
|
user.decorators_applied.append("test1")
|
|
|
|
return True
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2009-04-02 00:17:38 +08:00
|
|
|
def test2(user):
|
|
|
|
user.decorators_applied.append("test2")
|
|
|
|
return True
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2009-04-02 00:17:38 +08:00
|
|
|
def callback(request):
|
|
|
|
return request.user.decorators_applied
|
|
|
|
|
|
|
|
callback = user_passes_test(test1)(callback)
|
|
|
|
callback = user_passes_test(test2)(callback)
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class DummyUser:
|
2013-10-17 16:17:41 +08:00
|
|
|
pass
|
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class DummyRequest:
|
2013-10-17 16:17:41 +08:00
|
|
|
pass
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2009-04-02 00:17:38 +08:00
|
|
|
request = DummyRequest()
|
|
|
|
request.user = DummyUser()
|
|
|
|
request.user.decorators_applied = []
|
|
|
|
response = callback(request)
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2009-04-02 00:17:38 +08:00
|
|
|
self.assertEqual(response, ["test2", "test1"])
|
2009-09-22 06:31:51 +08:00
|
|
|
|
2016-10-27 15:53:39 +08:00
|
|
|
def test_cache_page(self):
|
2009-09-22 06:31:51 +08:00
|
|
|
def my_view(request):
|
|
|
|
return "response"
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2009-09-22 06:31:51 +08:00
|
|
|
my_view_cached = cache_page(123)(my_view)
|
|
|
|
self.assertEqual(my_view_cached(HttpRequest()), "response")
|
2009-09-29 05:54:54 +08:00
|
|
|
my_view_cached2 = cache_page(123, key_prefix="test")(my_view)
|
|
|
|
self.assertEqual(my_view_cached2(HttpRequest()), "response")
|
2009-09-22 06:31:51 +08:00
|
|
|
|
2011-04-28 21:04:16 +08:00
|
|
|
def test_require_safe_accepts_only_safe_methods(self):
|
|
|
|
"""
|
|
|
|
Test for the require_safe decorator.
|
|
|
|
A view returns either a response or an exception.
|
|
|
|
Refs #15637.
|
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2011-04-28 21:04:16 +08:00
|
|
|
def my_view(request):
|
|
|
|
return HttpResponse("OK")
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2011-04-28 21:04:16 +08:00
|
|
|
my_safe_view = require_safe(my_view)
|
|
|
|
request = HttpRequest()
|
|
|
|
request.method = "GET"
|
2013-05-21 17:42:15 +08:00
|
|
|
self.assertIsInstance(my_safe_view(request), HttpResponse)
|
2011-04-28 21:04:16 +08:00
|
|
|
request.method = "HEAD"
|
2013-05-21 17:42:15 +08:00
|
|
|
self.assertIsInstance(my_safe_view(request), HttpResponse)
|
2011-04-28 21:04:16 +08:00
|
|
|
request.method = "POST"
|
2013-05-21 17:42:15 +08:00
|
|
|
self.assertIsInstance(my_safe_view(request), HttpResponseNotAllowed)
|
2011-04-28 21:04:16 +08:00
|
|
|
request.method = "PUT"
|
2013-05-21 17:42:15 +08:00
|
|
|
self.assertIsInstance(my_safe_view(request), HttpResponseNotAllowed)
|
2011-04-28 21:04:16 +08:00
|
|
|
request.method = "DELETE"
|
2013-05-21 17:42:15 +08:00
|
|
|
self.assertIsInstance(my_safe_view(request), HttpResponseNotAllowed)
|
2011-04-28 21:04:16 +08:00
|
|
|
|
2010-02-09 23:02:39 +08: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):
|
2022-05-22 14:26:21 +08:00
|
|
|
@wraps(func)
|
2010-02-09 23:02:39 +08:00
|
|
|
def wrapper(arg):
|
|
|
|
return func("test:" + arg)
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2022-05-22 14:26:21 +08:00
|
|
|
return wrapper
|
2010-02-09 23:02:39 +08:00
|
|
|
|
2016-11-13 01:11:23 +08:00
|
|
|
|
2010-02-09 23:02:39 +08:00
|
|
|
simple_dec_m = method_decorator(simple_dec)
|
|
|
|
|
|
|
|
|
2010-10-21 22:56:49 +08:00
|
|
|
# For testing method_decorator, two decorators that add an attribute to the function
|
|
|
|
def myattr_dec(func):
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
return func(*args, **kwargs)
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2010-10-21 22:56:49 +08:00
|
|
|
wrapper.myattr = True
|
2018-04-17 01:38:37 +08:00
|
|
|
return wrapper
|
2010-10-21 22:56:49 +08:00
|
|
|
|
2016-11-13 01:11:23 +08:00
|
|
|
|
2010-10-21 22:56:49 +08:00
|
|
|
myattr_dec_m = method_decorator(myattr_dec)
|
|
|
|
|
|
|
|
|
|
|
|
def myattr2_dec(func):
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
return func(*args, **kwargs)
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2010-10-21 22:56:49 +08:00
|
|
|
wrapper.myattr2 = True
|
2018-04-17 01:38:37 +08:00
|
|
|
return wrapper
|
2010-10-21 22:56:49 +08:00
|
|
|
|
2016-11-13 01:11:23 +08:00
|
|
|
|
2010-10-21 22:56:49 +08:00
|
|
|
myattr2_dec_m = method_decorator(myattr2_dec)
|
|
|
|
|
2014-02-09 19:13:10 +08:00
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class ClsDec:
|
2013-11-26 20:00:50 +08:00
|
|
|
def __init__(self, myattr):
|
|
|
|
self.myattr = myattr
|
|
|
|
|
|
|
|
def __call__(self, f):
|
2022-05-22 14:20:29 +08:00
|
|
|
def wrapper():
|
2013-11-26 20:00:50 +08:00
|
|
|
return f() and self.myattr
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2022-05-22 14:20:29 +08:00
|
|
|
return update_wrapper(wrapper, f)
|
2013-11-26 20:00:50 +08:00
|
|
|
|
2010-10-21 22:56:49 +08:00
|
|
|
|
2015-07-22 04:54:37 +08:00
|
|
|
class MethodDecoratorTests(SimpleTestCase):
|
2010-02-09 23:02:39 +08:00
|
|
|
"""
|
|
|
|
Tests for method_decorator
|
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2010-10-21 22:56:49 +08:00
|
|
|
def test_preserve_signature(self):
|
2017-01-19 15:39:46 +08:00
|
|
|
class Test:
|
2010-02-09 23:02:39 +08:00
|
|
|
@simple_dec_m
|
|
|
|
def say(self, arg):
|
|
|
|
return arg
|
|
|
|
|
|
|
|
self.assertEqual("test:hello", Test().say("hello"))
|
2010-10-21 22:56:49 +08:00
|
|
|
|
|
|
|
def test_preserve_attributes(self):
|
|
|
|
# Sanity check myattr_dec and myattr2_dec
|
2018-04-17 01:38:37 +08:00
|
|
|
@myattr_dec
|
|
|
|
def func():
|
|
|
|
pass
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2018-04-17 01:38:37 +08:00
|
|
|
self.assertIs(getattr(func, "myattr", False), True)
|
|
|
|
|
|
|
|
@myattr2_dec
|
|
|
|
def func():
|
|
|
|
pass
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2018-04-17 01:38:37 +08:00
|
|
|
self.assertIs(getattr(func, "myattr2", False), True)
|
|
|
|
|
2010-10-21 22:56:49 +08:00
|
|
|
@myattr_dec
|
|
|
|
@myattr2_dec
|
|
|
|
def func():
|
|
|
|
pass
|
|
|
|
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIs(getattr(func, "myattr", False), True)
|
2018-04-17 01:38:37 +08:00
|
|
|
self.assertIs(getattr(func, "myattr2", False), False)
|
2010-10-21 22:56:49 +08:00
|
|
|
|
2015-08-11 19:35:50 +08:00
|
|
|
# Decorate using method_decorator() on the method.
|
2017-01-19 15:39:46 +08:00
|
|
|
class TestPlain:
|
2010-10-21 22:56:49 +08:00
|
|
|
@myattr_dec_m
|
|
|
|
@myattr2_dec_m
|
|
|
|
def method(self):
|
|
|
|
"A method"
|
|
|
|
pass
|
|
|
|
|
2015-08-11 19:35:50 +08:00
|
|
|
# Decorate using method_decorator() on both the class and the method.
|
|
|
|
# The decorators applied to the methods are applied before the ones
|
|
|
|
# applied to the class.
|
|
|
|
@method_decorator(myattr_dec_m, "method")
|
2017-01-19 15:39:46 +08:00
|
|
|
class TestMethodAndClass:
|
2015-08-11 19:35:50 +08:00
|
|
|
@method_decorator(myattr2_dec_m)
|
|
|
|
def method(self):
|
|
|
|
"A method"
|
|
|
|
pass
|
2010-10-21 22:56:49 +08:00
|
|
|
|
2018-04-17 01:38:37 +08:00
|
|
|
# Decorate using an iterable of function decorators.
|
|
|
|
@method_decorator((myattr_dec, myattr2_dec), "method")
|
|
|
|
class TestFunctionIterable:
|
|
|
|
def method(self):
|
|
|
|
"A method"
|
|
|
|
pass
|
|
|
|
|
|
|
|
# Decorate using an iterable of method decorators.
|
2015-08-11 19:35:50 +08:00
|
|
|
decorators = (myattr_dec_m, myattr2_dec_m)
|
2010-10-21 22:56:49 +08:00
|
|
|
|
2015-08-11 19:35:50 +08:00
|
|
|
@method_decorator(decorators, "method")
|
2018-04-17 01:38:37 +08:00
|
|
|
class TestMethodIterable:
|
2015-08-11 19:35:50 +08:00
|
|
|
def method(self):
|
|
|
|
"A method"
|
|
|
|
pass
|
|
|
|
|
2018-04-17 01:38:37 +08:00
|
|
|
tests = (
|
|
|
|
TestPlain,
|
|
|
|
TestMethodAndClass,
|
|
|
|
TestFunctionIterable,
|
|
|
|
TestMethodIterable,
|
|
|
|
)
|
2018-04-17 00:47:06 +08:00
|
|
|
for Test in tests:
|
|
|
|
with self.subTest(Test=Test):
|
|
|
|
self.assertIs(getattr(Test().method, "myattr", False), True)
|
|
|
|
self.assertIs(getattr(Test().method, "myattr2", False), True)
|
|
|
|
self.assertIs(getattr(Test.method, "myattr", False), True)
|
|
|
|
self.assertIs(getattr(Test.method, "myattr2", False), True)
|
|
|
|
self.assertEqual(Test.method.__doc__, "A method")
|
|
|
|
self.assertEqual(Test.method.__name__, "method")
|
2015-08-11 19:35:50 +08:00
|
|
|
|
2018-06-27 23:46:07 +08:00
|
|
|
def test_new_attribute(self):
|
|
|
|
"""A decorator that sets a new attribute on the method."""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2018-06-27 23:46:07 +08:00
|
|
|
def decorate(func):
|
|
|
|
func.x = 1
|
|
|
|
return func
|
|
|
|
|
|
|
|
class MyClass:
|
|
|
|
@method_decorator(decorate)
|
|
|
|
def method(self):
|
|
|
|
return True
|
|
|
|
|
|
|
|
obj = MyClass()
|
|
|
|
self.assertEqual(obj.method.x, 1)
|
|
|
|
self.assertIs(obj.method(), True)
|
|
|
|
|
2015-08-11 19:35:50 +08:00
|
|
|
def test_bad_iterable(self):
|
|
|
|
decorators = {myattr_dec_m, myattr2_dec_m}
|
2017-01-22 09:02:00 +08:00
|
|
|
msg = "'set' object is not subscriptable"
|
|
|
|
with self.assertRaisesMessage(TypeError, msg):
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2015-08-11 19:35:50 +08:00
|
|
|
@method_decorator(decorators, "method")
|
2017-01-19 15:39:46 +08:00
|
|
|
class TestIterable:
|
2015-08-11 19:35:50 +08:00
|
|
|
def method(self):
|
|
|
|
"A method"
|
|
|
|
pass
|
2011-05-31 06:27:47 +08:00
|
|
|
|
2013-11-26 20:00:50 +08:00
|
|
|
# Test for argumented decorator
|
|
|
|
def test_argumented(self):
|
2017-01-19 15:39:46 +08:00
|
|
|
class Test:
|
2013-11-26 20:00:50 +08:00
|
|
|
@method_decorator(ClsDec(False))
|
|
|
|
def method(self):
|
|
|
|
return True
|
|
|
|
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIs(Test().method(), False)
|
2014-02-09 19:13:10 +08:00
|
|
|
|
2014-02-09 19:10:05 +08:00
|
|
|
def test_descriptors(self):
|
|
|
|
def original_dec(wrapped):
|
|
|
|
def _wrapped(arg):
|
|
|
|
return wrapped(arg)
|
|
|
|
|
|
|
|
return _wrapped
|
|
|
|
|
|
|
|
method_dec = method_decorator(original_dec)
|
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class bound_wrapper:
|
2014-02-09 19:10:05 +08:00
|
|
|
def __init__(self, wrapped):
|
|
|
|
self.wrapped = wrapped
|
|
|
|
self.__name__ = wrapped.__name__
|
|
|
|
|
|
|
|
def __call__(self, arg):
|
|
|
|
return self.wrapped(arg)
|
|
|
|
|
2015-10-26 23:31:16 +08:00
|
|
|
def __get__(self, instance, cls=None):
|
2014-02-09 19:10:05 +08:00
|
|
|
return self
|
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class descriptor_wrapper:
|
2014-02-09 19:10:05 +08:00
|
|
|
def __init__(self, wrapped):
|
|
|
|
self.wrapped = wrapped
|
|
|
|
self.__name__ = wrapped.__name__
|
|
|
|
|
2015-10-26 23:31:16 +08:00
|
|
|
def __get__(self, instance, cls=None):
|
|
|
|
return bound_wrapper(self.wrapped.__get__(instance, cls))
|
2014-02-09 19:10:05 +08:00
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class Test:
|
2014-02-09 19:10:05 +08:00
|
|
|
@method_dec
|
|
|
|
@descriptor_wrapper
|
|
|
|
def method(self, arg):
|
|
|
|
return arg
|
|
|
|
|
|
|
|
self.assertEqual(Test().method(1), 1)
|
|
|
|
|
2015-07-22 04:54:37 +08:00
|
|
|
def test_class_decoration(self):
|
|
|
|
"""
|
|
|
|
@method_decorator can be used to decorate a class and its methods.
|
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2015-07-22 04:54:37 +08:00
|
|
|
def deco(func):
|
|
|
|
def _wrapper(*args, **kwargs):
|
|
|
|
return True
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2015-07-22 04:54:37 +08:00
|
|
|
return _wrapper
|
|
|
|
|
|
|
|
@method_decorator(deco, name="method")
|
2017-01-19 15:39:46 +08:00
|
|
|
class Test:
|
2015-07-22 04:54:37 +08:00
|
|
|
def method(self):
|
|
|
|
return False
|
|
|
|
|
|
|
|
self.assertTrue(Test().method())
|
|
|
|
|
2015-08-11 19:35:50 +08:00
|
|
|
def test_tuple_of_decorators(self):
|
|
|
|
"""
|
|
|
|
@method_decorator can accept a tuple of decorators.
|
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2015-08-11 19:35:50 +08:00
|
|
|
def add_question_mark(func):
|
|
|
|
def _wrapper(*args, **kwargs):
|
|
|
|
return func(*args, **kwargs) + "?"
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2015-08-11 19:35:50 +08:00
|
|
|
return _wrapper
|
|
|
|
|
|
|
|
def add_exclamation_mark(func):
|
|
|
|
def _wrapper(*args, **kwargs):
|
|
|
|
return func(*args, **kwargs) + "!"
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2015-08-11 19:35:50 +08:00
|
|
|
return _wrapper
|
|
|
|
|
|
|
|
# The order should be consistent with the usual order in which
|
|
|
|
# decorators are applied, e.g.
|
|
|
|
# @add_exclamation_mark
|
|
|
|
# @add_question_mark
|
|
|
|
# def func():
|
|
|
|
# ...
|
|
|
|
decorators = (add_exclamation_mark, add_question_mark)
|
|
|
|
|
|
|
|
@method_decorator(decorators, name="method")
|
2017-01-19 15:39:46 +08:00
|
|
|
class TestFirst:
|
2015-08-11 19:35:50 +08:00
|
|
|
def method(self):
|
|
|
|
return "hello world"
|
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class TestSecond:
|
2015-08-11 19:35:50 +08:00
|
|
|
@method_decorator(decorators)
|
|
|
|
def method(self):
|
|
|
|
return "hello world"
|
|
|
|
|
|
|
|
self.assertEqual(TestFirst().method(), "hello world?!")
|
|
|
|
self.assertEqual(TestSecond().method(), "hello world?!")
|
|
|
|
|
2015-07-22 04:54:37 +08:00
|
|
|
def test_invalid_non_callable_attribute_decoration(self):
|
|
|
|
"""
|
|
|
|
@method_decorator on a non-callable attribute raises an error.
|
|
|
|
"""
|
|
|
|
msg = (
|
|
|
|
"Cannot decorate 'prop' as it isn't a callable attribute of "
|
|
|
|
"<class 'Test'> (1)"
|
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(TypeError, msg):
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2015-07-22 04:54:37 +08:00
|
|
|
@method_decorator(lambda: None, name="prop")
|
2017-01-19 15:39:46 +08:00
|
|
|
class Test:
|
2015-07-22 04:54:37 +08:00
|
|
|
prop = 1
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def __module__(cls):
|
|
|
|
return "tests"
|
|
|
|
|
|
|
|
def test_invalid_method_name_to_decorate(self):
|
|
|
|
"""
|
|
|
|
@method_decorator on a nonexistent method raises an error.
|
|
|
|
"""
|
|
|
|
msg = (
|
|
|
|
"The keyword argument `name` must be the name of a method of the "
|
2017-02-03 09:43:21 +08:00
|
|
|
"decorated class: <class 'Test'>. Got 'nonexistent_method' instead"
|
2015-07-22 04:54:37 +08:00
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2017-02-03 09:43:21 +08:00
|
|
|
@method_decorator(lambda: None, name="nonexistent_method")
|
2017-01-19 15:39:46 +08:00
|
|
|
class Test:
|
2015-07-22 04:54:37 +08:00
|
|
|
@classmethod
|
|
|
|
def __module__(cls):
|
|
|
|
return "tests"
|
|
|
|
|
2021-08-23 20:58:38 +08:00
|
|
|
def test_wrapper_assignments(self):
|
|
|
|
"""@method_decorator preserves wrapper assignments."""
|
|
|
|
func_name = None
|
|
|
|
func_module = None
|
|
|
|
|
|
|
|
def decorator(func):
|
|
|
|
@wraps(func)
|
|
|
|
def inner(*args, **kwargs):
|
|
|
|
nonlocal func_name, func_module
|
|
|
|
func_name = getattr(func, "__name__", None)
|
|
|
|
func_module = getattr(func, "__module__", None)
|
|
|
|
return func(*args, **kwargs)
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2021-08-23 20:58:38 +08:00
|
|
|
return inner
|
|
|
|
|
|
|
|
class Test:
|
|
|
|
@method_decorator(decorator)
|
|
|
|
def method(self):
|
|
|
|
return "tests"
|
|
|
|
|
|
|
|
Test().method()
|
|
|
|
self.assertEqual(func_name, "method")
|
|
|
|
self.assertIsNotNone(func_module)
|
|
|
|
|
2011-05-31 06:27:47 +08:00
|
|
|
|
|
|
|
class XFrameOptionsDecoratorsTests(TestCase):
|
|
|
|
"""
|
|
|
|
Tests for the X-Frame-Options decorators.
|
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2011-05-31 06:27:47 +08:00
|
|
|
def test_deny_decorator(self):
|
|
|
|
"""
|
|
|
|
Ensures @xframe_options_deny properly sets the X-Frame-Options header.
|
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2011-05-31 06:27:47 +08:00
|
|
|
@xframe_options_deny
|
|
|
|
def a_view(request):
|
|
|
|
return HttpResponse()
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2011-05-31 06:27:47 +08:00
|
|
|
r = a_view(HttpRequest())
|
2020-07-14 19:32:24 +08:00
|
|
|
self.assertEqual(r.headers["X-Frame-Options"], "DENY")
|
2011-05-31 06:27:47 +08:00
|
|
|
|
|
|
|
def test_sameorigin_decorator(self):
|
|
|
|
"""
|
|
|
|
Ensures @xframe_options_sameorigin properly sets the X-Frame-Options
|
|
|
|
header.
|
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2011-05-31 06:27:47 +08:00
|
|
|
@xframe_options_sameorigin
|
|
|
|
def a_view(request):
|
|
|
|
return HttpResponse()
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2011-05-31 06:27:47 +08:00
|
|
|
r = a_view(HttpRequest())
|
2020-07-14 19:32:24 +08:00
|
|
|
self.assertEqual(r.headers["X-Frame-Options"], "SAMEORIGIN")
|
2011-05-31 06:27:47 +08:00
|
|
|
|
|
|
|
def test_exempt_decorator(self):
|
|
|
|
"""
|
|
|
|
Ensures @xframe_options_exempt properly instructs the
|
|
|
|
XFrameOptionsMiddleware to NOT set the header.
|
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2011-05-31 06:27:47 +08:00
|
|
|
@xframe_options_exempt
|
|
|
|
def a_view(request):
|
|
|
|
return HttpResponse()
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2011-05-31 06:27:47 +08:00
|
|
|
req = HttpRequest()
|
|
|
|
resp = a_view(req)
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIsNone(resp.get("X-Frame-Options", None))
|
2011-05-31 06:27:47 +08:00
|
|
|
self.assertTrue(resp.xframe_options_exempt)
|
|
|
|
|
|
|
|
# Since the real purpose of the exempt decorator is to suppress
|
|
|
|
# the middleware's functionality, let's make sure it actually works...
|
2019-09-27 01:06:35 +08:00
|
|
|
r = XFrameOptionsMiddleware(a_view)(req)
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIsNone(r.get("X-Frame-Options", None))
|
2015-04-28 05:56:02 +08:00
|
|
|
|
|
|
|
|
2021-12-17 03:13:17 +08:00
|
|
|
class HttpRequestProxy:
|
|
|
|
def __init__(self, request):
|
|
|
|
self._request = request
|
|
|
|
|
|
|
|
def __getattr__(self, attr):
|
|
|
|
"""Proxy to the underlying HttpRequest object."""
|
|
|
|
return getattr(self._request, attr)
|
|
|
|
|
|
|
|
|
2021-02-22 15:32:37 +08:00
|
|
|
class NeverCacheDecoratorTest(SimpleTestCase):
|
2021-12-13 15:57:19 +08:00
|
|
|
@mock.patch("time.time")
|
|
|
|
def test_never_cache_decorator_headers(self, mocked_time):
|
2015-04-28 05:56:02 +08:00
|
|
|
@never_cache
|
|
|
|
def a_view(request):
|
|
|
|
return HttpResponse()
|
2021-12-13 15:57:19 +08:00
|
|
|
|
|
|
|
mocked_time.return_value = 1167616461.0
|
|
|
|
response = a_view(HttpRequest())
|
|
|
|
self.assertEqual(
|
|
|
|
response.headers["Expires"],
|
|
|
|
"Mon, 01 Jan 2007 01:54:21 GMT",
|
|
|
|
)
|
2015-04-28 05:56:02 +08:00
|
|
|
self.assertEqual(
|
2021-12-13 15:57:19 +08:00
|
|
|
response.headers["Cache-Control"],
|
|
|
|
"max-age=0, no-cache, no-store, must-revalidate, private",
|
2015-04-28 05:56:02 +08:00
|
|
|
)
|
2021-02-22 15:32:37 +08:00
|
|
|
|
2021-12-13 15:57:19 +08:00
|
|
|
def test_never_cache_decorator_expires_not_overridden(self):
|
|
|
|
@never_cache
|
|
|
|
def a_view(request):
|
|
|
|
return HttpResponse(headers={"Expires": "tomorrow"})
|
|
|
|
|
|
|
|
response = a_view(HttpRequest())
|
|
|
|
self.assertEqual(response.headers["Expires"], "tomorrow")
|
|
|
|
|
2021-02-22 15:32:37 +08:00
|
|
|
def test_never_cache_decorator_http_request(self):
|
|
|
|
class MyClass:
|
|
|
|
@never_cache
|
|
|
|
def a_view(self, request):
|
|
|
|
return HttpResponse()
|
2021-12-17 03:13:17 +08:00
|
|
|
|
|
|
|
request = HttpRequest()
|
2021-02-22 15:32:37 +08:00
|
|
|
msg = (
|
|
|
|
"never_cache didn't receive an HttpRequest. If you are decorating "
|
|
|
|
"a classmethod, be sure to use @method_decorator."
|
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(TypeError, msg):
|
2021-12-17 03:13:17 +08:00
|
|
|
MyClass().a_view(request)
|
|
|
|
with self.assertRaisesMessage(TypeError, msg):
|
|
|
|
MyClass().a_view(HttpRequestProxy(request))
|
|
|
|
|
|
|
|
def test_never_cache_decorator_http_request_proxy(self):
|
|
|
|
class MyClass:
|
|
|
|
@method_decorator(never_cache)
|
|
|
|
def a_view(self, request):
|
|
|
|
return HttpResponse()
|
|
|
|
|
|
|
|
request = HttpRequest()
|
|
|
|
response = MyClass().a_view(HttpRequestProxy(request))
|
|
|
|
self.assertIn("Cache-Control", response.headers)
|
|
|
|
self.assertIn("Expires", response.headers)
|
2021-02-22 15:32:37 +08:00
|
|
|
|
|
|
|
|
|
|
|
class CacheControlDecoratorTest(SimpleTestCase):
|
|
|
|
def test_cache_control_decorator_http_request(self):
|
|
|
|
class MyClass:
|
|
|
|
@cache_control(a="b")
|
|
|
|
def a_view(self, request):
|
|
|
|
return HttpResponse()
|
|
|
|
|
|
|
|
msg = (
|
|
|
|
"cache_control didn't receive an HttpRequest. If you are "
|
|
|
|
"decorating a classmethod, be sure to use @method_decorator."
|
|
|
|
)
|
2021-12-17 03:13:17 +08:00
|
|
|
request = HttpRequest()
|
2021-02-22 15:32:37 +08:00
|
|
|
with self.assertRaisesMessage(TypeError, msg):
|
2021-12-17 03:13:17 +08:00
|
|
|
MyClass().a_view(request)
|
|
|
|
with self.assertRaisesMessage(TypeError, msg):
|
|
|
|
MyClass().a_view(HttpRequestProxy(request))
|
|
|
|
|
|
|
|
def test_cache_control_decorator_http_request_proxy(self):
|
|
|
|
class MyClass:
|
|
|
|
@method_decorator(cache_control(a="b"))
|
|
|
|
def a_view(self, request):
|
|
|
|
return HttpResponse()
|
|
|
|
|
|
|
|
request = HttpRequest()
|
|
|
|
response = MyClass().a_view(HttpRequestProxy(request))
|
|
|
|
self.assertEqual(response.headers["Cache-Control"], "a=b")
|