tests.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. from functools import update_wrapper, wraps
  2. from unittest import TestCase
  3. from django.contrib.admin.views.decorators import staff_member_required
  4. from django.contrib.auth.decorators import (
  5. login_required, permission_required, user_passes_test,
  6. )
  7. from django.http import HttpRequest, HttpResponse, HttpResponseNotAllowed
  8. from django.middleware.clickjacking import XFrameOptionsMiddleware
  9. from django.test import SimpleTestCase
  10. from django.utils.decorators import method_decorator
  11. from django.utils.functional import keep_lazy, keep_lazy_text, lazy
  12. from django.utils.safestring import mark_safe
  13. from django.views.decorators.cache import (
  14. cache_control, cache_page, never_cache,
  15. )
  16. from django.views.decorators.clickjacking import (
  17. xframe_options_deny, xframe_options_exempt, xframe_options_sameorigin,
  18. )
  19. from django.views.decorators.http import (
  20. condition, require_GET, require_http_methods, require_POST, require_safe,
  21. )
  22. from django.views.decorators.vary import vary_on_cookie, vary_on_headers
  23. def fully_decorated(request):
  24. """Expected __doc__"""
  25. return HttpResponse('<html><body>dummy</body></html>')
  26. fully_decorated.anything = "Expected __dict__"
  27. def compose(*functions):
  28. # compose(f, g)(*args, **kwargs) == f(g(*args, **kwargs))
  29. functions = list(reversed(functions))
  30. def _inner(*args, **kwargs):
  31. result = functions[0](*args, **kwargs)
  32. for f in functions[1:]:
  33. result = f(result)
  34. return result
  35. return _inner
  36. full_decorator = compose(
  37. # django.views.decorators.http
  38. require_http_methods(["GET"]),
  39. require_GET,
  40. require_POST,
  41. require_safe,
  42. condition(lambda r: None, lambda r: None),
  43. # django.views.decorators.vary
  44. vary_on_headers('Accept-language'),
  45. vary_on_cookie,
  46. # django.views.decorators.cache
  47. cache_page(60 * 15),
  48. cache_control(private=True),
  49. never_cache,
  50. # django.contrib.auth.decorators
  51. # Apply user_passes_test twice to check #9474
  52. user_passes_test(lambda u: True),
  53. login_required,
  54. permission_required('change_world'),
  55. # django.contrib.admin.views.decorators
  56. staff_member_required,
  57. # django.utils.functional
  58. keep_lazy(HttpResponse),
  59. keep_lazy_text,
  60. lazy,
  61. # django.utils.safestring
  62. mark_safe,
  63. )
  64. fully_decorated = full_decorator(fully_decorated)
  65. class DecoratorsTest(TestCase):
  66. def test_attributes(self):
  67. """
  68. Built-in decorators set certain attributes of the wrapped function.
  69. """
  70. self.assertEqual(fully_decorated.__name__, 'fully_decorated')
  71. self.assertEqual(fully_decorated.__doc__, 'Expected __doc__')
  72. self.assertEqual(fully_decorated.__dict__['anything'], 'Expected __dict__')
  73. def test_user_passes_test_composition(self):
  74. """
  75. The user_passes_test decorator can be applied multiple times (#9474).
  76. """
  77. def test1(user):
  78. user.decorators_applied.append('test1')
  79. return True
  80. def test2(user):
  81. user.decorators_applied.append('test2')
  82. return True
  83. def callback(request):
  84. return request.user.decorators_applied
  85. callback = user_passes_test(test1)(callback)
  86. callback = user_passes_test(test2)(callback)
  87. class DummyUser:
  88. pass
  89. class DummyRequest:
  90. pass
  91. request = DummyRequest()
  92. request.user = DummyUser()
  93. request.user.decorators_applied = []
  94. response = callback(request)
  95. self.assertEqual(response, ['test2', 'test1'])
  96. def test_cache_page(self):
  97. def my_view(request):
  98. return "response"
  99. my_view_cached = cache_page(123)(my_view)
  100. self.assertEqual(my_view_cached(HttpRequest()), "response")
  101. my_view_cached2 = cache_page(123, key_prefix="test")(my_view)
  102. self.assertEqual(my_view_cached2(HttpRequest()), "response")
  103. def test_require_safe_accepts_only_safe_methods(self):
  104. """
  105. Test for the require_safe decorator.
  106. A view returns either a response or an exception.
  107. Refs #15637.
  108. """
  109. def my_view(request):
  110. return HttpResponse("OK")
  111. my_safe_view = require_safe(my_view)
  112. request = HttpRequest()
  113. request.method = 'GET'
  114. self.assertIsInstance(my_safe_view(request), HttpResponse)
  115. request.method = 'HEAD'
  116. self.assertIsInstance(my_safe_view(request), HttpResponse)
  117. request.method = 'POST'
  118. self.assertIsInstance(my_safe_view(request), HttpResponseNotAllowed)
  119. request.method = 'PUT'
  120. self.assertIsInstance(my_safe_view(request), HttpResponseNotAllowed)
  121. request.method = 'DELETE'
  122. self.assertIsInstance(my_safe_view(request), HttpResponseNotAllowed)
  123. # For testing method_decorator, a decorator that assumes a single argument.
  124. # We will get type arguments if there is a mismatch in the number of arguments.
  125. def simple_dec(func):
  126. def wrapper(arg):
  127. return func("test:" + arg)
  128. return wraps(func)(wrapper)
  129. simple_dec_m = method_decorator(simple_dec)
  130. # For testing method_decorator, two decorators that add an attribute to the function
  131. def myattr_dec(func):
  132. def wrapper(*args, **kwargs):
  133. return func(*args, **kwargs)
  134. wrapper.myattr = True
  135. return wrapper
  136. myattr_dec_m = method_decorator(myattr_dec)
  137. def myattr2_dec(func):
  138. def wrapper(*args, **kwargs):
  139. return func(*args, **kwargs)
  140. wrapper.myattr2 = True
  141. return wrapper
  142. myattr2_dec_m = method_decorator(myattr2_dec)
  143. class ClsDec:
  144. def __init__(self, myattr):
  145. self.myattr = myattr
  146. def __call__(self, f):
  147. def wrapped():
  148. return f() and self.myattr
  149. return update_wrapper(wrapped, f)
  150. class MethodDecoratorTests(SimpleTestCase):
  151. """
  152. Tests for method_decorator
  153. """
  154. def test_preserve_signature(self):
  155. class Test:
  156. @simple_dec_m
  157. def say(self, arg):
  158. return arg
  159. self.assertEqual("test:hello", Test().say("hello"))
  160. def test_preserve_attributes(self):
  161. # Sanity check myattr_dec and myattr2_dec
  162. @myattr_dec
  163. def func():
  164. pass
  165. self.assertIs(getattr(func, 'myattr', False), True)
  166. @myattr2_dec
  167. def func():
  168. pass
  169. self.assertIs(getattr(func, 'myattr2', False), True)
  170. @myattr_dec
  171. @myattr2_dec
  172. def func():
  173. pass
  174. self.assertIs(getattr(func, 'myattr', False), True)
  175. self.assertIs(getattr(func, 'myattr2', False), False)
  176. # Decorate using method_decorator() on the method.
  177. class TestPlain:
  178. @myattr_dec_m
  179. @myattr2_dec_m
  180. def method(self):
  181. "A method"
  182. pass
  183. # Decorate using method_decorator() on both the class and the method.
  184. # The decorators applied to the methods are applied before the ones
  185. # applied to the class.
  186. @method_decorator(myattr_dec_m, "method")
  187. class TestMethodAndClass:
  188. @method_decorator(myattr2_dec_m)
  189. def method(self):
  190. "A method"
  191. pass
  192. # Decorate using an iterable of function decorators.
  193. @method_decorator((myattr_dec, myattr2_dec), 'method')
  194. class TestFunctionIterable:
  195. def method(self):
  196. "A method"
  197. pass
  198. # Decorate using an iterable of method decorators.
  199. decorators = (myattr_dec_m, myattr2_dec_m)
  200. @method_decorator(decorators, "method")
  201. class TestMethodIterable:
  202. def method(self):
  203. "A method"
  204. pass
  205. tests = (TestPlain, TestMethodAndClass, TestFunctionIterable, TestMethodIterable)
  206. for Test in tests:
  207. with self.subTest(Test=Test):
  208. self.assertIs(getattr(Test().method, 'myattr', False), True)
  209. self.assertIs(getattr(Test().method, 'myattr2', False), True)
  210. self.assertIs(getattr(Test.method, 'myattr', False), True)
  211. self.assertIs(getattr(Test.method, 'myattr2', False), True)
  212. self.assertEqual(Test.method.__doc__, 'A method')
  213. self.assertEqual(Test.method.__name__, 'method')
  214. def test_new_attribute(self):
  215. """A decorator that sets a new attribute on the method."""
  216. def decorate(func):
  217. func.x = 1
  218. return func
  219. class MyClass:
  220. @method_decorator(decorate)
  221. def method(self):
  222. return True
  223. obj = MyClass()
  224. self.assertEqual(obj.method.x, 1)
  225. self.assertIs(obj.method(), True)
  226. def test_bad_iterable(self):
  227. decorators = {myattr_dec_m, myattr2_dec_m}
  228. msg = "'set' object is not subscriptable"
  229. with self.assertRaisesMessage(TypeError, msg):
  230. @method_decorator(decorators, "method")
  231. class TestIterable:
  232. def method(self):
  233. "A method"
  234. pass
  235. # Test for argumented decorator
  236. def test_argumented(self):
  237. class Test:
  238. @method_decorator(ClsDec(False))
  239. def method(self):
  240. return True
  241. self.assertIs(Test().method(), False)
  242. def test_descriptors(self):
  243. def original_dec(wrapped):
  244. def _wrapped(arg):
  245. return wrapped(arg)
  246. return _wrapped
  247. method_dec = method_decorator(original_dec)
  248. class bound_wrapper:
  249. def __init__(self, wrapped):
  250. self.wrapped = wrapped
  251. self.__name__ = wrapped.__name__
  252. def __call__(self, arg):
  253. return self.wrapped(arg)
  254. def __get__(self, instance, cls=None):
  255. return self
  256. class descriptor_wrapper:
  257. def __init__(self, wrapped):
  258. self.wrapped = wrapped
  259. self.__name__ = wrapped.__name__
  260. def __get__(self, instance, cls=None):
  261. return bound_wrapper(self.wrapped.__get__(instance, cls))
  262. class Test:
  263. @method_dec
  264. @descriptor_wrapper
  265. def method(self, arg):
  266. return arg
  267. self.assertEqual(Test().method(1), 1)
  268. def test_class_decoration(self):
  269. """
  270. @method_decorator can be used to decorate a class and its methods.
  271. """
  272. def deco(func):
  273. def _wrapper(*args, **kwargs):
  274. return True
  275. return _wrapper
  276. @method_decorator(deco, name="method")
  277. class Test:
  278. def method(self):
  279. return False
  280. self.assertTrue(Test().method())
  281. def test_tuple_of_decorators(self):
  282. """
  283. @method_decorator can accept a tuple of decorators.
  284. """
  285. def add_question_mark(func):
  286. def _wrapper(*args, **kwargs):
  287. return func(*args, **kwargs) + "?"
  288. return _wrapper
  289. def add_exclamation_mark(func):
  290. def _wrapper(*args, **kwargs):
  291. return func(*args, **kwargs) + "!"
  292. return _wrapper
  293. # The order should be consistent with the usual order in which
  294. # decorators are applied, e.g.
  295. # @add_exclamation_mark
  296. # @add_question_mark
  297. # def func():
  298. # ...
  299. decorators = (add_exclamation_mark, add_question_mark)
  300. @method_decorator(decorators, name="method")
  301. class TestFirst:
  302. def method(self):
  303. return "hello world"
  304. class TestSecond:
  305. @method_decorator(decorators)
  306. def method(self):
  307. return "hello world"
  308. self.assertEqual(TestFirst().method(), "hello world?!")
  309. self.assertEqual(TestSecond().method(), "hello world?!")
  310. def test_invalid_non_callable_attribute_decoration(self):
  311. """
  312. @method_decorator on a non-callable attribute raises an error.
  313. """
  314. msg = (
  315. "Cannot decorate 'prop' as it isn't a callable attribute of "
  316. "<class 'Test'> (1)"
  317. )
  318. with self.assertRaisesMessage(TypeError, msg):
  319. @method_decorator(lambda: None, name="prop")
  320. class Test:
  321. prop = 1
  322. @classmethod
  323. def __module__(cls):
  324. return "tests"
  325. def test_invalid_method_name_to_decorate(self):
  326. """
  327. @method_decorator on a nonexistent method raises an error.
  328. """
  329. msg = (
  330. "The keyword argument `name` must be the name of a method of the "
  331. "decorated class: <class 'Test'>. Got 'nonexistent_method' instead"
  332. )
  333. with self.assertRaisesMessage(ValueError, msg):
  334. @method_decorator(lambda: None, name='nonexistent_method')
  335. class Test:
  336. @classmethod
  337. def __module__(cls):
  338. return "tests"
  339. class XFrameOptionsDecoratorsTests(TestCase):
  340. """
  341. Tests for the X-Frame-Options decorators.
  342. """
  343. def test_deny_decorator(self):
  344. """
  345. Ensures @xframe_options_deny properly sets the X-Frame-Options header.
  346. """
  347. @xframe_options_deny
  348. def a_view(request):
  349. return HttpResponse()
  350. r = a_view(HttpRequest())
  351. self.assertEqual(r['X-Frame-Options'], 'DENY')
  352. def test_sameorigin_decorator(self):
  353. """
  354. Ensures @xframe_options_sameorigin properly sets the X-Frame-Options
  355. header.
  356. """
  357. @xframe_options_sameorigin
  358. def a_view(request):
  359. return HttpResponse()
  360. r = a_view(HttpRequest())
  361. self.assertEqual(r['X-Frame-Options'], 'SAMEORIGIN')
  362. def test_exempt_decorator(self):
  363. """
  364. Ensures @xframe_options_exempt properly instructs the
  365. XFrameOptionsMiddleware to NOT set the header.
  366. """
  367. @xframe_options_exempt
  368. def a_view(request):
  369. return HttpResponse()
  370. req = HttpRequest()
  371. resp = a_view(req)
  372. self.assertIsNone(resp.get('X-Frame-Options', None))
  373. self.assertTrue(resp.xframe_options_exempt)
  374. # Since the real purpose of the exempt decorator is to suppress
  375. # the middleware's functionality, let's make sure it actually works...
  376. r = XFrameOptionsMiddleware(a_view)(req)
  377. self.assertIsNone(r.get('X-Frame-Options', None))
  378. class NeverCacheDecoratorTest(TestCase):
  379. def test_never_cache_decorator(self):
  380. @never_cache
  381. def a_view(request):
  382. return HttpResponse()
  383. r = a_view(HttpRequest())
  384. self.assertEqual(
  385. set(r['Cache-Control'].split(', ')),
  386. {'max-age=0', 'no-cache', 'no-store', 'must-revalidate', 'private'},
  387. )