1
|
|
|
# coding=utf-8 |
2
|
|
|
import django |
3
|
|
|
from django.test import TestCase |
4
|
|
|
from django.http import HttpRequest |
5
|
|
|
from django.core.exceptions import PermissionDenied |
6
|
|
|
from permission.utils.handlers import registry |
7
|
|
|
from permission.decorators.functionbase import permission_required as f |
8
|
|
|
from permission.decorators.methodbase import permission_required as m |
9
|
|
|
from permission.decorators.classbase import permission_required as c |
10
|
|
|
from permission.decorators import permission_required as p |
11
|
|
|
from permission.tests.compat import skipIf |
12
|
|
|
from permission.tests.compat import MagicMock |
13
|
|
|
from permission.tests.test_decorators.utils import create_mock_handler |
14
|
|
|
from permission.tests.test_decorators.utils import create_mock_request |
15
|
|
|
from permission.tests.test_decorators.utils import create_mock_queryset |
16
|
|
|
from permission.tests.test_decorators.utils import create_mock_model |
17
|
|
|
from permission.tests.test_decorators.utils import create_mock_view_func |
18
|
|
|
from permission.tests.test_decorators.utils import create_mock_view_class |
19
|
|
|
|
20
|
|
|
p = p('permission.add_article') |
21
|
|
|
c = c('permission.add_article') |
22
|
|
|
m = m('permission.add_article') |
23
|
|
|
f = f('permission.add_article') |
24
|
|
|
|
25
|
|
|
model = create_mock_model() |
26
|
|
|
instance = model() |
27
|
|
|
|
28
|
|
|
def view_func(request, *args, **kwargs): |
29
|
|
|
assert isinstance(request, HttpRequest) |
30
|
|
|
try: |
31
|
|
|
from django.views.generic import View as BaseView |
32
|
|
|
except ImportError: |
33
|
|
|
# Classbased generic view related test will not be run so never mind. |
34
|
|
|
BaseView = object |
35
|
|
|
|
36
|
|
|
class View(BaseView): |
37
|
|
|
def dispatch(self, request, *args, **kwargs): |
38
|
|
|
assert isinstance(self, View) |
39
|
|
|
assert isinstance(request, HttpRequest) |
40
|
|
|
def get_object(self, queryset=None): |
41
|
|
|
return instance |
42
|
|
|
|
43
|
|
|
class PermissionDecoratorsTestCase(TestCase): |
44
|
|
|
|
45
|
|
|
def setUp(self): |
46
|
|
|
self.handler = create_mock_handler() |
47
|
|
|
self.request = create_mock_request(self.handler) |
48
|
|
|
self.queryset = create_mock_queryset(instance) |
49
|
|
|
|
50
|
|
|
# store original registry |
51
|
|
|
self._original_registry = registry._registry |
52
|
|
|
|
53
|
|
|
# clear registry and register mock handler |
54
|
|
|
registry._registry = {} |
55
|
|
|
registry.register( |
56
|
|
|
model, |
57
|
|
|
self.handler, |
58
|
|
|
) |
59
|
|
|
|
60
|
|
|
def tearDown(self): |
61
|
|
|
# restore original registry |
62
|
|
|
registry._registry = self._original_registry |
63
|
|
|
|
64
|
|
|
def test_function_views(self): |
65
|
|
|
|
66
|
|
|
if django.VERSION >= (1, 3): |
67
|
|
|
# class decorator cannot handle |
68
|
|
|
self.assertRaises(AttributeError, c, view_func) |
69
|
|
|
# method decorator can handle |
70
|
|
|
method_view = m(view_func) |
71
|
|
|
method_view(self.request, self.queryset, object_id=1) |
72
|
|
|
|
73
|
|
|
# function decorator can handle |
74
|
|
|
function_view = f(view_func) |
75
|
|
|
function_view(self.request, self.queryset, object_id=1) |
76
|
|
|
|
77
|
|
|
@skipIf( |
78
|
|
|
django.VERSION < (1, 3), |
79
|
|
|
'Classbased generic view is not supported in this version') |
80
|
|
|
def test_method_views(self): |
81
|
|
|
view_method = View.dispatch |
82
|
|
|
|
83
|
|
|
# class decorator cannot handle |
84
|
|
|
self.assertRaises(AttributeError, c, View.dispatch) |
85
|
|
|
|
86
|
|
|
# method decorator can handle |
87
|
|
|
method_view = m(View.dispatch) |
88
|
|
|
method_view(View(), self.request, pk=1) |
89
|
|
|
|
90
|
|
|
# function decorators cannot handle |
91
|
|
|
function_view = f(View.dispatch) |
92
|
|
|
self.assertRaises(AttributeError, function_view, |
93
|
|
|
View(), self.request, pk=1) |
94
|
|
|
|
95
|
|
|
@skipIf( |
96
|
|
|
django.VERSION < (1, 3), |
97
|
|
|
'Classbased generic view is not supported in this version') |
98
|
|
|
def test_class_views(self): |
99
|
|
|
# class decorator can handle |
100
|
|
|
class_view = c(View) |
101
|
|
|
class_view.as_view()(self.request, pk=1) |
102
|
|
|
|
103
|
|
|
# method decorator cannot handle |
104
|
|
|
method_view = m(View) |
105
|
|
|
self.assertFalse(hasattr(method_view, 'as_view')) |
106
|
|
|
|
107
|
|
|
# function decorator cannot handle |
108
|
|
|
function_view = f(View) |
109
|
|
|
self.assertFalse(hasattr(method_view, 'as_view')) |
110
|
|
|
|
111
|
|
|
@skipIf( |
112
|
|
|
django.VERSION < (1, 3), |
113
|
|
|
'Classbased generic view is not supported in this version') |
114
|
|
|
def test_permission_required(self): |
115
|
|
|
# function |
116
|
|
|
functional_view = p(view_func) |
117
|
|
|
functional_view(self.request, queryset=self.queryset, object_id=1) |
118
|
|
|
|
119
|
|
|
# method |
120
|
|
|
method_view = p(View.dispatch) |
121
|
|
|
method_view(View(), self.request, pk=1) |
122
|
|
|
|
123
|
|
|
# class |
124
|
|
|
class_view = p(View) |
125
|
|
|
class_view.as_view()(self.request, pk=1) |
126
|
|
|
|