parts/django/tests/regressiontests/decorators/tests.py
changeset 307 c6bca38c1cbf
equal deleted inserted replaced
306:5ff1fc726848 307:c6bca38c1cbf
       
     1 from unittest import TestCase
       
     2 from sys import version_info
       
     3 try:
       
     4     from functools import wraps
       
     5 except ImportError:
       
     6     from django.utils.functional import wraps  # Python 2.4 fallback.
       
     7 
       
     8 from django.http import HttpResponse, HttpRequest
       
     9 from django.utils.functional import allow_lazy, lazy, memoize
       
    10 from django.views.decorators.http import require_http_methods, require_GET, require_POST
       
    11 from django.views.decorators.vary import vary_on_headers, vary_on_cookie
       
    12 from django.views.decorators.cache import cache_page, never_cache, cache_control
       
    13 from django.utils.decorators import method_decorator
       
    14 from django.contrib.auth.decorators import login_required, permission_required, user_passes_test
       
    15 from django.contrib.admin.views.decorators import staff_member_required
       
    16 
       
    17 def fully_decorated(request):
       
    18     """Expected __doc__"""
       
    19     return HttpResponse('<html><body>dummy</body></html>')
       
    20 fully_decorated.anything = "Expected __dict__"
       
    21 
       
    22 # django.views.decorators.http
       
    23 fully_decorated = require_http_methods(["GET"])(fully_decorated)
       
    24 fully_decorated = require_GET(fully_decorated)
       
    25 fully_decorated = require_POST(fully_decorated)
       
    26 
       
    27 # django.views.decorators.vary
       
    28 fully_decorated = vary_on_headers('Accept-language')(fully_decorated)
       
    29 fully_decorated = vary_on_cookie(fully_decorated)
       
    30 
       
    31 # django.views.decorators.cache
       
    32 fully_decorated = cache_page(60*15)(fully_decorated)
       
    33 fully_decorated = cache_control(private=True)(fully_decorated)
       
    34 fully_decorated = never_cache(fully_decorated)
       
    35 
       
    36 # django.contrib.auth.decorators
       
    37 # Apply user_passes_test twice to check #9474
       
    38 fully_decorated = user_passes_test(lambda u:True)(fully_decorated)
       
    39 fully_decorated = login_required(fully_decorated)
       
    40 fully_decorated = permission_required('change_world')(fully_decorated)
       
    41 
       
    42 # django.contrib.admin.views.decorators
       
    43 fully_decorated = staff_member_required(fully_decorated)
       
    44 
       
    45 # django.utils.functional
       
    46 fully_decorated = memoize(fully_decorated, {}, 1)
       
    47 fully_decorated = allow_lazy(fully_decorated)
       
    48 fully_decorated = lazy(fully_decorated)
       
    49 
       
    50 
       
    51 class DecoratorsTest(TestCase):
       
    52 
       
    53     def test_attributes(self):
       
    54         """
       
    55         Tests that django decorators set certain attributes of the wrapped
       
    56         function.
       
    57         """
       
    58         # Only check __name__ on Python 2.4 or later since __name__ can't be
       
    59         # assigned to in earlier Python versions.
       
    60         if version_info[0] >= 2 and version_info[1] >= 4:
       
    61             self.assertEquals(fully_decorated.__name__, 'fully_decorated')
       
    62         self.assertEquals(fully_decorated.__doc__, 'Expected __doc__')
       
    63         self.assertEquals(fully_decorated.__dict__['anything'], 'Expected __dict__')
       
    64 
       
    65     def test_user_passes_test_composition(self):
       
    66         """
       
    67         Test that the user_passes_test decorator can be applied multiple times
       
    68         (#9474).
       
    69         """
       
    70         def test1(user):
       
    71             user.decorators_applied.append('test1')
       
    72             return True
       
    73             
       
    74         def test2(user):
       
    75             user.decorators_applied.append('test2')
       
    76             return True
       
    77             
       
    78         def callback(request):
       
    79             return request.user.decorators_applied
       
    80 
       
    81         callback = user_passes_test(test1)(callback)
       
    82         callback = user_passes_test(test2)(callback)
       
    83         
       
    84         class DummyUser(object): pass
       
    85         class DummyRequest(object): pass
       
    86         
       
    87         request = DummyRequest()
       
    88         request.user = DummyUser()
       
    89         request.user.decorators_applied = []
       
    90         response = callback(request)
       
    91         
       
    92         self.assertEqual(response, ['test2', 'test1'])
       
    93 
       
    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 
       
   105     def test_cache_page_old_style(self):
       
   106         """
       
   107         Test that we can call cache_page the old way
       
   108         """
       
   109         def my_view(request):
       
   110             return "response"
       
   111         my_view_cached = cache_page(my_view, 123)
       
   112         self.assertEqual(my_view_cached(HttpRequest()), "response")
       
   113         my_view_cached2 = cache_page(my_view, 123, key_prefix="test")
       
   114         self.assertEqual(my_view_cached2(HttpRequest()), "response")
       
   115         my_view_cached3 = cache_page(my_view)
       
   116         self.assertEqual(my_view_cached3(HttpRequest()), "response")
       
   117         my_view_cached4 = cache_page()(my_view)
       
   118         self.assertEqual(my_view_cached4(HttpRequest()), "response")
       
   119 
       
   120 
       
   121 # For testing method_decorator, a decorator that assumes a single argument.
       
   122 # We will get type arguments if there is a mismatch in the number of arguments.
       
   123 def simple_dec(func):
       
   124     def wrapper(arg):
       
   125         return func("test:" + arg)
       
   126     return wraps(func)(wrapper)
       
   127 
       
   128 simple_dec_m = method_decorator(simple_dec)
       
   129 
       
   130 
       
   131 class MethodDecoratorTests(TestCase):
       
   132     """
       
   133     Tests for method_decorator
       
   134     """
       
   135     def test_method_decorator(self):
       
   136         class Test(object):
       
   137             @simple_dec_m
       
   138             def say(self, arg):
       
   139                 return arg
       
   140 
       
   141         self.assertEqual("test:hello", Test().say("hello"))