tests.py 14 KB

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