tests.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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 allow_lazy, lazy
  12. from django.views.decorators.cache import (
  13. cache_control, cache_page, never_cache,
  14. )
  15. from django.views.decorators.clickjacking import (
  16. xframe_options_deny, xframe_options_exempt, xframe_options_sameorigin,
  17. )
  18. from django.views.decorators.http import (
  19. condition, require_GET, require_http_methods, require_POST, require_safe,
  20. )
  21. from django.views.decorators.vary import vary_on_cookie, vary_on_headers
  22. def fully_decorated(request):
  23. """Expected __doc__"""
  24. return HttpResponse('<html><body>dummy</body></html>')
  25. fully_decorated.anything = "Expected __dict__"
  26. def compose(*functions):
  27. # compose(f, g)(*args, **kwargs) == f(g(*args, **kwargs))
  28. functions = list(reversed(functions))
  29. def _inner(*args, **kwargs):
  30. result = functions[0](*args, **kwargs)
  31. for f in functions[1:]:
  32. result = f(result)
  33. return result
  34. return _inner
  35. full_decorator = compose(
  36. # django.views.decorators.http
  37. require_http_methods(["GET"]),
  38. require_GET,
  39. require_POST,
  40. require_safe,
  41. condition(lambda r: None, lambda r: None),
  42. # django.views.decorators.vary
  43. vary_on_headers('Accept-language'),
  44. vary_on_cookie,
  45. # django.views.decorators.cache
  46. cache_page(60 * 15),
  47. cache_control(private=True),
  48. never_cache,
  49. # django.contrib.auth.decorators
  50. # Apply user_passes_test twice to check #9474
  51. user_passes_test(lambda u: True),
  52. login_required,
  53. permission_required('change_world'),
  54. # django.contrib.admin.views.decorators
  55. staff_member_required,
  56. # django.utils.functional
  57. allow_lazy,
  58. lazy,
  59. )
  60. fully_decorated = full_decorator(fully_decorated)
  61. class DecoratorsTest(TestCase):
  62. def test_attributes(self):
  63. """
  64. Tests that django decorators set certain attributes of the wrapped
  65. function.
  66. """
  67. self.assertEqual(fully_decorated.__name__, 'fully_decorated')
  68. self.assertEqual(fully_decorated.__doc__, 'Expected __doc__')
  69. self.assertEqual(fully_decorated.__dict__['anything'], 'Expected __dict__')
  70. def test_user_passes_test_composition(self):
  71. """
  72. Test that the user_passes_test decorator can be applied multiple times
  73. (#9474).
  74. """
  75. def test1(user):
  76. user.decorators_applied.append('test1')
  77. return True
  78. def test2(user):
  79. user.decorators_applied.append('test2')
  80. return True
  81. def callback(request):
  82. return request.user.decorators_applied
  83. callback = user_passes_test(test1)(callback)
  84. callback = user_passes_test(test2)(callback)
  85. class DummyUser(object):
  86. pass
  87. class DummyRequest(object):
  88. pass
  89. request = DummyRequest()
  90. request.user = DummyUser()
  91. request.user.decorators_applied = []
  92. response = callback(request)
  93. self.assertEqual(response, ['test2', 'test1'])
  94. def test_cache_page_new_style(self):
  95. """
  96. Test that we can call cache_page the new way
  97. """
  98. def my_view(request):
  99. return "response"
  100. my_view_cached = cache_page(123)(my_view)
  101. self.assertEqual(my_view_cached(HttpRequest()), "response")
  102. my_view_cached2 = cache_page(123, key_prefix="test")(my_view)
  103. self.assertEqual(my_view_cached2(HttpRequest()), "response")
  104. def test_require_safe_accepts_only_safe_methods(self):
  105. """
  106. Test for the require_safe decorator.
  107. A view returns either a response or an exception.
  108. Refs #15637.
  109. """
  110. def my_view(request):
  111. return HttpResponse("OK")
  112. my_safe_view = require_safe(my_view)
  113. request = HttpRequest()
  114. request.method = 'GET'
  115. self.assertIsInstance(my_safe_view(request), HttpResponse)
  116. request.method = 'HEAD'
  117. self.assertIsInstance(my_safe_view(request), HttpResponse)
  118. request.method = 'POST'
  119. self.assertIsInstance(my_safe_view(request), HttpResponseNotAllowed)
  120. request.method = 'PUT'
  121. self.assertIsInstance(my_safe_view(request), HttpResponseNotAllowed)
  122. request.method = 'DELETE'
  123. self.assertIsInstance(my_safe_view(request), HttpResponseNotAllowed)
  124. # For testing method_decorator, a decorator that assumes a single argument.
  125. # We will get type arguments if there is a mismatch in the number of arguments.
  126. def simple_dec(func):
  127. def wrapper(arg):
  128. return func("test:" + arg)
  129. return wraps(func)(wrapper)
  130. simple_dec_m = method_decorator(simple_dec)
  131. # For testing method_decorator, two decorators that add an attribute to the function
  132. def myattr_dec(func):
  133. def wrapper(*args, **kwargs):
  134. return func(*args, **kwargs)
  135. wrapper.myattr = True
  136. return wraps(func)(wrapper)
  137. myattr_dec_m = method_decorator(myattr_dec)
  138. def myattr2_dec(func):
  139. def wrapper(*args, **kwargs):
  140. return func(*args, **kwargs)
  141. wrapper.myattr2 = True
  142. return wraps(func)(wrapper)
  143. myattr2_dec_m = method_decorator(myattr2_dec)
  144. class ClsDec(object):
  145. def __init__(self, myattr):
  146. self.myattr = myattr
  147. def __call__(self, f):
  148. def wrapped():
  149. return f() and self.myattr
  150. return update_wrapper(wrapped, f)
  151. class MethodDecoratorTests(SimpleTestCase):
  152. """
  153. Tests for method_decorator
  154. """
  155. def test_preserve_signature(self):
  156. class Test(object):
  157. @simple_dec_m
  158. def say(self, arg):
  159. return arg
  160. self.assertEqual("test:hello", Test().say("hello"))
  161. def test_preserve_attributes(self):
  162. # Sanity check myattr_dec and myattr2_dec
  163. @myattr_dec
  164. @myattr2_dec
  165. def func():
  166. pass
  167. self.assertEqual(getattr(func, 'myattr', False), True)
  168. self.assertEqual(getattr(func, 'myattr2', False), True)
  169. # Now check method_decorator
  170. class Test(object):
  171. @myattr_dec_m
  172. @myattr2_dec_m
  173. def method(self):
  174. "A method"
  175. pass
  176. self.assertEqual(getattr(Test().method, 'myattr', False), True)
  177. self.assertEqual(getattr(Test().method, 'myattr2', False), True)
  178. self.assertEqual(getattr(Test.method, 'myattr', False), True)
  179. self.assertEqual(getattr(Test.method, 'myattr2', False), True)
  180. self.assertEqual(Test.method.__doc__, 'A method')
  181. self.assertEqual(Test.method.__name__, 'method')
  182. # Test for argumented decorator
  183. def test_argumented(self):
  184. class Test(object):
  185. @method_decorator(ClsDec(False))
  186. def method(self):
  187. return True
  188. self.assertEqual(Test().method(), False)
  189. def test_descriptors(self):
  190. def original_dec(wrapped):
  191. def _wrapped(arg):
  192. return wrapped(arg)
  193. return _wrapped
  194. method_dec = method_decorator(original_dec)
  195. class bound_wrapper(object):
  196. def __init__(self, wrapped):
  197. self.wrapped = wrapped
  198. self.__name__ = wrapped.__name__
  199. def __call__(self, arg):
  200. return self.wrapped(arg)
  201. def __get__(self, instance, owner):
  202. return self
  203. class descriptor_wrapper(object):
  204. def __init__(self, wrapped):
  205. self.wrapped = wrapped
  206. self.__name__ = wrapped.__name__
  207. def __get__(self, instance, owner):
  208. return bound_wrapper(self.wrapped.__get__(instance, owner))
  209. class Test(object):
  210. @method_dec
  211. @descriptor_wrapper
  212. def method(self, arg):
  213. return arg
  214. self.assertEqual(Test().method(1), 1)
  215. def test_class_decoration(self):
  216. """
  217. @method_decorator can be used to decorate a class and its methods.
  218. """
  219. def deco(func):
  220. def _wrapper(*args, **kwargs):
  221. return True
  222. return _wrapper
  223. @method_decorator(deco, name="method")
  224. class Test(object):
  225. def method(self):
  226. return False
  227. self.assertTrue(Test().method())
  228. def test_invalid_non_callable_attribute_decoration(self):
  229. """
  230. @method_decorator on a non-callable attribute raises an error.
  231. """
  232. msg = (
  233. "Cannot decorate 'prop' as it isn't a callable attribute of "
  234. "<class 'Test'> (1)"
  235. )
  236. with self.assertRaisesMessage(TypeError, msg):
  237. @method_decorator(lambda: None, name="prop")
  238. class Test(object):
  239. prop = 1
  240. @classmethod
  241. def __module__(cls):
  242. return "tests"
  243. def test_invalid_method_name_to_decorate(self):
  244. """
  245. @method_decorator on a nonexistent method raises an error.
  246. """
  247. msg = (
  248. "The keyword argument `name` must be the name of a method of the "
  249. "decorated class: <class 'Test'>. Got 'non_existing_method' instead"
  250. )
  251. with self.assertRaisesMessage(ValueError, msg):
  252. @method_decorator(lambda: None, name="non_existing_method")
  253. class Test(object):
  254. @classmethod
  255. def __module__(cls):
  256. return "tests"
  257. class XFrameOptionsDecoratorsTests(TestCase):
  258. """
  259. Tests for the X-Frame-Options decorators.
  260. """
  261. def test_deny_decorator(self):
  262. """
  263. Ensures @xframe_options_deny properly sets the X-Frame-Options header.
  264. """
  265. @xframe_options_deny
  266. def a_view(request):
  267. return HttpResponse()
  268. r = a_view(HttpRequest())
  269. self.assertEqual(r['X-Frame-Options'], 'DENY')
  270. def test_sameorigin_decorator(self):
  271. """
  272. Ensures @xframe_options_sameorigin properly sets the X-Frame-Options
  273. header.
  274. """
  275. @xframe_options_sameorigin
  276. def a_view(request):
  277. return HttpResponse()
  278. r = a_view(HttpRequest())
  279. self.assertEqual(r['X-Frame-Options'], 'SAMEORIGIN')
  280. def test_exempt_decorator(self):
  281. """
  282. Ensures @xframe_options_exempt properly instructs the
  283. XFrameOptionsMiddleware to NOT set the header.
  284. """
  285. @xframe_options_exempt
  286. def a_view(request):
  287. return HttpResponse()
  288. req = HttpRequest()
  289. resp = a_view(req)
  290. self.assertEqual(resp.get('X-Frame-Options', None), None)
  291. self.assertTrue(resp.xframe_options_exempt)
  292. # Since the real purpose of the exempt decorator is to suppress
  293. # the middleware's functionality, let's make sure it actually works...
  294. r = XFrameOptionsMiddleware().process_response(req, resp)
  295. self.assertEqual(r.get('X-Frame-Options', None), None)
  296. class NeverCacheDecoratorTest(TestCase):
  297. def test_never_cache_decorator(self):
  298. @never_cache
  299. def a_view(request):
  300. return HttpResponse()
  301. r = a_view(HttpRequest())
  302. self.assertEqual(
  303. set(r['Cache-Control'].split(', ')),
  304. {'max-age=0', 'no-cache', 'no-store', 'must-revalidate'},
  305. )