tests.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. from functools import wraps, update_wrapper
  2. from unittest import TestCase
  3. from django.contrib.admin.views.decorators import staff_member_required
  4. from django.contrib.auth.decorators import login_required, permission_required, user_passes_test
  5. from django.http import HttpResponse, HttpRequest, HttpResponseNotAllowed
  6. from django.middleware.clickjacking import XFrameOptionsMiddleware
  7. from django.utils.decorators import method_decorator
  8. from django.utils.functional import allow_lazy, lazy
  9. from django.views.decorators.cache import cache_page, never_cache, cache_control
  10. from django.views.decorators.clickjacking import xframe_options_deny, xframe_options_sameorigin, xframe_options_exempt
  11. from django.views.decorators.http import require_http_methods, require_GET, require_POST, require_safe, condition
  12. from django.views.decorators.vary import vary_on_headers, vary_on_cookie
  13. def fully_decorated(request):
  14. """Expected __doc__"""
  15. return HttpResponse('<html><body>dummy</body></html>')
  16. fully_decorated.anything = "Expected __dict__"
  17. def compose(*functions):
  18. # compose(f, g)(*args, **kwargs) == f(g(*args, **kwargs))
  19. functions = list(reversed(functions))
  20. def _inner(*args, **kwargs):
  21. result = functions[0](*args, **kwargs)
  22. for f in functions[1:]:
  23. result = f(result)
  24. return result
  25. return _inner
  26. full_decorator = compose(
  27. # django.views.decorators.http
  28. require_http_methods(["GET"]),
  29. require_GET,
  30. require_POST,
  31. require_safe,
  32. condition(lambda r: None, lambda r: None),
  33. # django.views.decorators.vary
  34. vary_on_headers('Accept-language'),
  35. vary_on_cookie,
  36. # django.views.decorators.cache
  37. cache_page(60 * 15),
  38. cache_control(private=True),
  39. never_cache,
  40. # django.contrib.auth.decorators
  41. # Apply user_passes_test twice to check #9474
  42. user_passes_test(lambda u: True),
  43. login_required,
  44. permission_required('change_world'),
  45. # django.contrib.admin.views.decorators
  46. staff_member_required,
  47. # django.utils.functional
  48. allow_lazy,
  49. lazy,
  50. )
  51. fully_decorated = full_decorator(fully_decorated)
  52. class DecoratorsTest(TestCase):
  53. def test_attributes(self):
  54. """
  55. Tests that django decorators set certain attributes of the wrapped
  56. function.
  57. """
  58. self.assertEqual(fully_decorated.__name__, 'fully_decorated')
  59. self.assertEqual(fully_decorated.__doc__, 'Expected __doc__')
  60. self.assertEqual(fully_decorated.__dict__['anything'], 'Expected __dict__')
  61. def test_user_passes_test_composition(self):
  62. """
  63. Test that the user_passes_test decorator can be applied multiple times
  64. (#9474).
  65. """
  66. def test1(user):
  67. user.decorators_applied.append('test1')
  68. return True
  69. def test2(user):
  70. user.decorators_applied.append('test2')
  71. return True
  72. def callback(request):
  73. return request.user.decorators_applied
  74. callback = user_passes_test(test1)(callback)
  75. callback = user_passes_test(test2)(callback)
  76. class DummyUser(object):
  77. pass
  78. class DummyRequest(object):
  79. pass
  80. request = DummyRequest()
  81. request.user = DummyUser()
  82. request.user.decorators_applied = []
  83. response = callback(request)
  84. self.assertEqual(response, ['test2', 'test1'])
  85. def test_cache_page_new_style(self):
  86. """
  87. Test that we can call cache_page the new way
  88. """
  89. def my_view(request):
  90. return "response"
  91. my_view_cached = cache_page(123)(my_view)
  92. self.assertEqual(my_view_cached(HttpRequest()), "response")
  93. my_view_cached2 = cache_page(123, key_prefix="test")(my_view)
  94. self.assertEqual(my_view_cached2(HttpRequest()), "response")
  95. def test_require_safe_accepts_only_safe_methods(self):
  96. """
  97. Test for the require_safe decorator.
  98. A view returns either a response or an exception.
  99. Refs #15637.
  100. """
  101. def my_view(request):
  102. return HttpResponse("OK")
  103. my_safe_view = require_safe(my_view)
  104. request = HttpRequest()
  105. request.method = 'GET'
  106. self.assertIsInstance(my_safe_view(request), HttpResponse)
  107. request.method = 'HEAD'
  108. self.assertIsInstance(my_safe_view(request), HttpResponse)
  109. request.method = 'POST'
  110. self.assertIsInstance(my_safe_view(request), HttpResponseNotAllowed)
  111. request.method = 'PUT'
  112. self.assertIsInstance(my_safe_view(request), HttpResponseNotAllowed)
  113. request.method = 'DELETE'
  114. self.assertIsInstance(my_safe_view(request), HttpResponseNotAllowed)
  115. # For testing method_decorator, a decorator that assumes a single argument.
  116. # We will get type arguments if there is a mismatch in the number of arguments.
  117. def simple_dec(func):
  118. def wrapper(arg):
  119. return func("test:" + arg)
  120. return wraps(func)(wrapper)
  121. simple_dec_m = method_decorator(simple_dec)
  122. # For testing method_decorator, two decorators that add an attribute to the function
  123. def myattr_dec(func):
  124. def wrapper(*args, **kwargs):
  125. return func(*args, **kwargs)
  126. wrapper.myattr = True
  127. return wraps(func)(wrapper)
  128. myattr_dec_m = method_decorator(myattr_dec)
  129. def myattr2_dec(func):
  130. def wrapper(*args, **kwargs):
  131. return func(*args, **kwargs)
  132. wrapper.myattr2 = True
  133. return wraps(func)(wrapper)
  134. myattr2_dec_m = method_decorator(myattr2_dec)
  135. class ClsDec(object):
  136. def __init__(self, myattr):
  137. self.myattr = myattr
  138. def __call__(self, f):
  139. def wrapped():
  140. return f() and self.myattr
  141. return update_wrapper(wrapped, f)
  142. class MethodDecoratorTests(TestCase):
  143. """
  144. Tests for method_decorator
  145. """
  146. def test_preserve_signature(self):
  147. class Test(object):
  148. @simple_dec_m
  149. def say(self, arg):
  150. return arg
  151. self.assertEqual("test:hello", Test().say("hello"))
  152. def test_preserve_attributes(self):
  153. # Sanity check myattr_dec and myattr2_dec
  154. @myattr_dec
  155. @myattr2_dec
  156. def func():
  157. pass
  158. self.assertEqual(getattr(func, 'myattr', False), True)
  159. self.assertEqual(getattr(func, 'myattr2', False), True)
  160. # Now check method_decorator
  161. class Test(object):
  162. @myattr_dec_m
  163. @myattr2_dec_m
  164. def method(self):
  165. "A method"
  166. pass
  167. self.assertEqual(getattr(Test().method, 'myattr', False), True)
  168. self.assertEqual(getattr(Test().method, 'myattr2', False), True)
  169. self.assertEqual(getattr(Test.method, 'myattr', False), True)
  170. self.assertEqual(getattr(Test.method, 'myattr2', False), True)
  171. self.assertEqual(Test.method.__doc__, 'A method')
  172. self.assertEqual(Test.method.__name__, 'method')
  173. # Test for argumented decorator
  174. def test_argumented(self):
  175. class Test(object):
  176. @method_decorator(ClsDec(False))
  177. def method(self):
  178. return True
  179. self.assertEqual(Test().method(), False)
  180. def test_descriptors(self):
  181. def original_dec(wrapped):
  182. def _wrapped(arg):
  183. return wrapped(arg)
  184. return _wrapped
  185. method_dec = method_decorator(original_dec)
  186. class bound_wrapper(object):
  187. def __init__(self, wrapped):
  188. self.wrapped = wrapped
  189. self.__name__ = wrapped.__name__
  190. def __call__(self, arg):
  191. return self.wrapped(arg)
  192. def __get__(self, instance, owner):
  193. return self
  194. class descriptor_wrapper(object):
  195. def __init__(self, wrapped):
  196. self.wrapped = wrapped
  197. self.__name__ = wrapped.__name__
  198. def __get__(self, instance, owner):
  199. return bound_wrapper(self.wrapped.__get__(instance, owner))
  200. class Test(object):
  201. @method_dec
  202. @descriptor_wrapper
  203. def method(self, arg):
  204. return arg
  205. self.assertEqual(Test().method(1), 1)
  206. class XFrameOptionsDecoratorsTests(TestCase):
  207. """
  208. Tests for the X-Frame-Options decorators.
  209. """
  210. def test_deny_decorator(self):
  211. """
  212. Ensures @xframe_options_deny properly sets the X-Frame-Options header.
  213. """
  214. @xframe_options_deny
  215. def a_view(request):
  216. return HttpResponse()
  217. r = a_view(HttpRequest())
  218. self.assertEqual(r['X-Frame-Options'], 'DENY')
  219. def test_sameorigin_decorator(self):
  220. """
  221. Ensures @xframe_options_sameorigin properly sets the X-Frame-Options
  222. header.
  223. """
  224. @xframe_options_sameorigin
  225. def a_view(request):
  226. return HttpResponse()
  227. r = a_view(HttpRequest())
  228. self.assertEqual(r['X-Frame-Options'], 'SAMEORIGIN')
  229. def test_exempt_decorator(self):
  230. """
  231. Ensures @xframe_options_exempt properly instructs the
  232. XFrameOptionsMiddleware to NOT set the header.
  233. """
  234. @xframe_options_exempt
  235. def a_view(request):
  236. return HttpResponse()
  237. req = HttpRequest()
  238. resp = a_view(req)
  239. self.assertEqual(resp.get('X-Frame-Options', None), None)
  240. self.assertTrue(resp.xframe_options_exempt)
  241. # Since the real purpose of the exempt decorator is to suppress
  242. # the middleware's functionality, let's make sure it actually works...
  243. r = XFrameOptionsMiddleware().process_response(req, resp)
  244. self.assertEqual(r.get('X-Frame-Options', None), None)