| 1 |
from unittest import TestCase |
|---|
| 2 |
from sys import version_info |
|---|
| 3 |
|
|---|
| 4 |
from django.http import HttpResponse |
|---|
| 5 |
from django.utils.functional import allow_lazy, lazy, memoize |
|---|
| 6 |
from django.views.decorators.http import require_http_methods, require_GET, require_POST |
|---|
| 7 |
from django.views.decorators.vary import vary_on_headers, vary_on_cookie |
|---|
| 8 |
from django.views.decorators.cache import cache_page, never_cache, cache_control |
|---|
| 9 |
from django.contrib.auth.decorators import login_required, permission_required, user_passes_test |
|---|
| 10 |
from django.contrib.admin.views.decorators import staff_member_required |
|---|
| 11 |
|
|---|
| 12 |
def fully_decorated(request): |
|---|
| 13 |
"""Expected __doc__""" |
|---|
| 14 |
return HttpResponse('<html><body>dummy</body></html>') |
|---|
| 15 |
fully_decorated.anything = "Expected __dict__" |
|---|
| 16 |
|
|---|
| 17 |
# django.views.decorators.http |
|---|
| 18 |
fully_decorated = require_http_methods(["GET"])(fully_decorated) |
|---|
| 19 |
fully_decorated = require_GET(fully_decorated) |
|---|
| 20 |
fully_decorated = require_POST(fully_decorated) |
|---|
| 21 |
|
|---|
| 22 |
# django.views.decorators.vary |
|---|
| 23 |
fully_decorated = vary_on_headers('Accept-language')(fully_decorated) |
|---|
| 24 |
fully_decorated = vary_on_cookie(fully_decorated) |
|---|
| 25 |
|
|---|
| 26 |
# django.views.decorators.cache |
|---|
| 27 |
fully_decorated = cache_page(60*15)(fully_decorated) |
|---|
| 28 |
fully_decorated = cache_control(private=True)(fully_decorated) |
|---|
| 29 |
fully_decorated = never_cache(fully_decorated) |
|---|
| 30 |
|
|---|
| 31 |
# django.contrib.auth.decorators |
|---|
| 32 |
fully_decorated = user_passes_test(lambda u:True)(fully_decorated) |
|---|
| 33 |
fully_decorated = login_required(fully_decorated) |
|---|
| 34 |
fully_decorated = permission_required('change_world')(fully_decorated) |
|---|
| 35 |
|
|---|
| 36 |
# django.contrib.admin.views.decorators |
|---|
| 37 |
fully_decorated = staff_member_required(fully_decorated) |
|---|
| 38 |
|
|---|
| 39 |
# django.utils.functional |
|---|
| 40 |
fully_decorated = memoize(fully_decorated, {}, 1) |
|---|
| 41 |
fully_decorated = allow_lazy(fully_decorated) |
|---|
| 42 |
fully_decorated = lazy(fully_decorated) |
|---|
| 43 |
|
|---|
| 44 |
class DecoratorsTest(TestCase): |
|---|
| 45 |
|
|---|
| 46 |
def test_attributes(self): |
|---|
| 47 |
""" |
|---|
| 48 |
Tests that django decorators set certain attributes of the wrapped |
|---|
| 49 |
function. |
|---|
| 50 |
""" |
|---|
| 51 |
# Only check __name__ on Python 2.4 or later since __name__ can't be |
|---|
| 52 |
# assigned to in earlier Python versions. |
|---|
| 53 |
if version_info[0] >= 2 and version_info[1] >= 4: |
|---|
| 54 |
self.assertEquals(fully_decorated.__name__, 'fully_decorated') |
|---|
| 55 |
self.assertEquals(fully_decorated.__doc__, 'Expected __doc__') |
|---|
| 56 |
self.assertEquals(fully_decorated.__dict__['anything'], 'Expected __dict__') |
|---|