|
1
|
|
|
# coding=utf-8 |
|
2
|
|
|
from django.test import TestCase, override_settings |
|
3
|
|
|
from ..handlers import PermissionHandler |
|
4
|
|
|
from ..handlers import LogicalPermissionHandler |
|
5
|
|
|
from .utils import create_user, create_article |
|
6
|
|
|
from .models import Article |
|
7
|
|
|
from .compat import MagicMock |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
@override_settings( |
|
11
|
|
|
PERMISSION_DEFAULT_PERMISSION_HANDLER=PermissionHandler |
|
12
|
|
|
) |
|
13
|
|
|
class PermissionPermissionHandlersTestCase(TestCase): |
|
14
|
|
|
def setUp(self): |
|
15
|
|
|
self.handler = PermissionHandler |
|
16
|
|
|
self.user = create_user('john') |
|
17
|
|
|
self.perm1 = 'permission.add_article' |
|
18
|
|
|
self.perm2 = 'permission.change_article' |
|
19
|
|
|
self.perm3 = 'permission.delete_article' |
|
20
|
|
|
self.article = create_article('test') |
|
21
|
|
|
|
|
22
|
|
|
def test_constructor_with_model(self): |
|
23
|
|
|
instance = self.handler(Article) |
|
24
|
|
|
self.assertEqual(instance.app_label, 'permission') |
|
25
|
|
|
self.assertEqual(instance.model, Article) |
|
26
|
|
|
# backward reference |
|
27
|
|
|
self.assertEqual(Article._permission_handler, instance) |
|
28
|
|
|
|
|
29
|
|
|
def test_constructor_with_app_label(self): |
|
30
|
|
|
instance = self.handler('permission') |
|
31
|
|
|
self.assertEqual(instance.app_label, 'permission') |
|
32
|
|
|
self.assertEqual(instance.model, None) |
|
33
|
|
|
|
|
34
|
|
|
def test__get_app_perms_with_app_label(self): |
|
35
|
|
|
instance = self.handler('permission') |
|
36
|
|
|
perms = instance._get_app_perms() |
|
37
|
|
|
self.assertEquals(perms, set([ |
|
38
|
|
|
'permission.add_article', |
|
39
|
|
|
'permission.change_article', |
|
40
|
|
|
'permission.delete_article', |
|
41
|
|
|
'permission.add_bridge', |
|
42
|
|
|
'permission.change_bridge', |
|
43
|
|
|
'permission.delete_bridge', |
|
44
|
|
|
])) |
|
45
|
|
|
|
|
46
|
|
|
def test__get_app_perms_with_model(self): |
|
47
|
|
|
instance = self.handler(Article) |
|
48
|
|
|
perms = instance._get_app_perms() |
|
49
|
|
|
self.assertEquals(perms, set([ |
|
50
|
|
|
'permission.add_article', |
|
51
|
|
|
'permission.change_article', |
|
52
|
|
|
'permission.delete_article', |
|
53
|
|
|
'permission.add_bridge', |
|
54
|
|
|
'permission.change_bridge', |
|
55
|
|
|
'permission.delete_bridge', |
|
56
|
|
|
])) |
|
57
|
|
|
|
|
58
|
|
|
def test__get_model_perms(self): |
|
59
|
|
|
instance = self.handler(Article) |
|
60
|
|
|
perms = instance._get_model_perms() |
|
61
|
|
|
self.assertEquals(perms, set([ |
|
62
|
|
|
'permission.add_article', |
|
63
|
|
|
'permission.change_article', |
|
64
|
|
|
'permission.delete_article', |
|
65
|
|
|
])) |
|
66
|
|
|
|
|
67
|
|
|
def test_get_supported_permissions(self): |
|
68
|
|
|
instance = self.handler(Article) |
|
69
|
|
|
perms = instance.get_supported_permissions() |
|
70
|
|
|
self.assertEquals(perms, set([ |
|
71
|
|
|
'permission.add_article', |
|
72
|
|
|
'permission.change_article', |
|
73
|
|
|
'permission.delete_article', |
|
74
|
|
|
])) |
|
75
|
|
|
|
|
76
|
|
|
def test_get_supported_permissions_with_includes(self): |
|
77
|
|
|
instance = self.handler(Article) |
|
78
|
|
|
instance.includes = [ |
|
79
|
|
|
'permission.add_article', |
|
80
|
|
|
'permission.change_article', |
|
81
|
|
|
] |
|
82
|
|
|
perms = instance.get_supported_permissions() |
|
83
|
|
|
self.assertEquals(perms, set([ |
|
84
|
|
|
'permission.add_article', |
|
85
|
|
|
'permission.change_article', |
|
86
|
|
|
])) |
|
87
|
|
|
|
|
88
|
|
|
def test_get_supported_permissions_with_includes_change(self): |
|
89
|
|
|
instance = self.handler(Article) |
|
90
|
|
|
instance.includes = [ |
|
91
|
|
|
'permission.add_article', |
|
92
|
|
|
'permission.change_article', |
|
93
|
|
|
] |
|
94
|
|
|
instance.get_supported_permissions() |
|
95
|
|
|
instance.includes = [ |
|
96
|
|
|
'permission.change_article', |
|
97
|
|
|
] |
|
98
|
|
|
perms = instance.get_supported_permissions() |
|
99
|
|
|
self.assertEquals(perms, set([ |
|
100
|
|
|
'permission.change_article', |
|
101
|
|
|
])) |
|
102
|
|
|
|
|
103
|
|
|
def test_get_supported_permissions_with_excludes(self): |
|
104
|
|
|
instance = self.handler(Article) |
|
105
|
|
|
instance.excludes = [ |
|
106
|
|
|
'permission.add_article', |
|
107
|
|
|
] |
|
108
|
|
|
perms = instance.get_supported_permissions() |
|
109
|
|
|
self.assertEquals(perms, set([ |
|
110
|
|
|
'permission.change_article', |
|
111
|
|
|
'permission.delete_article', |
|
112
|
|
|
])) |
|
113
|
|
|
|
|
114
|
|
|
def test_get_supported_permissions_with_excludes_change(self): |
|
115
|
|
|
instance = self.handler(Article) |
|
116
|
|
|
instance.excludes = [ |
|
117
|
|
|
'permission.add_article', |
|
118
|
|
|
] |
|
119
|
|
|
instance.get_supported_permissions() |
|
120
|
|
|
instance.excludes = [] |
|
121
|
|
|
perms = instance.get_supported_permissions() |
|
122
|
|
|
self.assertEquals(perms, set([ |
|
123
|
|
|
'permission.add_article', |
|
124
|
|
|
'permission.change_article', |
|
125
|
|
|
'permission.delete_article', |
|
126
|
|
|
])) |
|
127
|
|
|
|
|
128
|
|
|
def test_get_supported_app_labels(self): |
|
129
|
|
|
instance = self.handler(Article) |
|
130
|
|
|
app_labels = instance.get_supported_app_labels() |
|
131
|
|
|
self.assertEquals(app_labels, set([ |
|
132
|
|
|
'permission', |
|
133
|
|
|
])) |
|
134
|
|
|
|
|
135
|
|
|
def test_get_supported_app_labels_with_includes(self): |
|
136
|
|
|
instance = self.handler(Article) |
|
137
|
|
|
instance.includes = [ |
|
138
|
|
|
'permission.add_article', |
|
139
|
|
|
'permission.change_article', |
|
140
|
|
|
] |
|
141
|
|
|
app_labels = instance.get_supported_app_labels() |
|
142
|
|
|
self.assertEquals(app_labels, set([ |
|
143
|
|
|
'permission', |
|
144
|
|
|
])) |
|
145
|
|
|
|
|
146
|
|
|
def test_get_supported_app_labels_with_includes_change(self): |
|
147
|
|
|
instance = self.handler(Article) |
|
148
|
|
|
instance.includes = [ |
|
149
|
|
|
'permission.add_article', |
|
150
|
|
|
'permission.change_article', |
|
151
|
|
|
] |
|
152
|
|
|
instance.get_supported_app_labels() |
|
153
|
|
|
instance.includes = [ |
|
154
|
|
|
'permission.change_article', |
|
155
|
|
|
] |
|
156
|
|
|
app_labels = instance.get_supported_app_labels() |
|
157
|
|
|
self.assertEquals(app_labels, set([ |
|
158
|
|
|
'permission', |
|
159
|
|
|
])) |
|
160
|
|
|
|
|
161
|
|
|
def test_get_supported_app_labels_with_excludes(self): |
|
162
|
|
|
instance = self.handler(Article) |
|
163
|
|
|
instance.excludes = [ |
|
164
|
|
|
'permission.add_article', |
|
165
|
|
|
] |
|
166
|
|
|
app_labels = instance.get_supported_app_labels() |
|
167
|
|
|
self.assertEquals(app_labels, set([ |
|
168
|
|
|
'permission', |
|
169
|
|
|
])) |
|
170
|
|
|
|
|
171
|
|
|
def test_get_supported_app_labels_with_excludes_change(self): |
|
172
|
|
|
instance = self.handler(Article) |
|
173
|
|
|
instance.excludes = [ |
|
174
|
|
|
'permission.add_article', |
|
175
|
|
|
] |
|
176
|
|
|
instance.get_supported_app_labels() |
|
177
|
|
|
instance.excludes = [] |
|
178
|
|
|
app_labels = instance.get_supported_app_labels() |
|
179
|
|
|
self.assertEquals(app_labels, set([ |
|
180
|
|
|
'permission', |
|
181
|
|
|
])) |
|
182
|
|
|
|
|
183
|
|
|
def test_has_perm_add_wihtout_obj(self): |
|
184
|
|
|
instance = self.handler(Article) |
|
185
|
|
|
self.assertRaises( |
|
186
|
|
|
NotImplementedError, |
|
187
|
|
|
instance.has_perm, |
|
188
|
|
|
self.user, self.perm1) |
|
189
|
|
|
|
|
190
|
|
|
def test_has_perm_change_wihtout_obj(self): |
|
191
|
|
|
instance = self.handler(Article) |
|
192
|
|
|
self.assertRaises( |
|
193
|
|
|
NotImplementedError, |
|
194
|
|
|
instance.has_perm, |
|
195
|
|
|
self.user, self.perm2) |
|
196
|
|
|
|
|
197
|
|
|
def test_has_perm_delete_wihtout_obj(self): |
|
198
|
|
|
instance = self.handler(Article) |
|
199
|
|
|
self.assertRaises( |
|
200
|
|
|
NotImplementedError, |
|
201
|
|
|
instance.has_perm, |
|
202
|
|
|
self.user, self.perm3) |
|
203
|
|
|
|
|
204
|
|
|
def test_has_perm_add_wiht_obj(self): |
|
205
|
|
|
instance = self.handler(Article) |
|
206
|
|
|
self.assertRaises( |
|
207
|
|
|
NotImplementedError, |
|
208
|
|
|
instance.has_perm, |
|
209
|
|
|
self.user, self.perm1, self.article) |
|
210
|
|
|
|
|
211
|
|
|
def test_has_perm_change_wiht_obj(self): |
|
212
|
|
|
instance = self.handler(Article) |
|
213
|
|
|
self.assertRaises( |
|
214
|
|
|
NotImplementedError, |
|
215
|
|
|
instance.has_perm, |
|
216
|
|
|
self.user, self.perm2, self.article) |
|
217
|
|
|
|
|
218
|
|
|
def test_has_perm_delete_wiht_obj(self): |
|
219
|
|
|
instance = self.handler(Article) |
|
220
|
|
|
self.assertRaises( |
|
221
|
|
|
NotImplementedError, |
|
222
|
|
|
instance.has_perm, |
|
223
|
|
|
self.user, self.perm3, self.article) |
|
224
|
|
|
|
|
225
|
|
|
def test_has_module_perms_success(self): |
|
226
|
|
|
instance = self.handler(Article) |
|
227
|
|
|
user = MagicMock() |
|
228
|
|
|
user.has_perm.return_value = True |
|
229
|
|
|
self.assertTrue(instance.has_module_perms(user, 'permission')) |
|
230
|
|
|
self.assertTrue(user.has_perm.called) |
|
231
|
|
|
|
|
232
|
|
|
def test_has_module_perms_fail(self): |
|
233
|
|
|
instance = self.handler(Article) |
|
234
|
|
|
user = MagicMock() |
|
235
|
|
|
user.has_perm.return_value = True |
|
236
|
|
|
self.assertFalse(instance.has_module_perms(user, 'unknown')) |
|
237
|
|
|
self.assertFalse(user.has_perm.called) |
|
238
|
|
|
|
|
239
|
|
|
|
|
240
|
|
|
@override_settings( |
|
241
|
|
|
PERMISSION_DEFAULT_PERMISSION_HANDLER=LogicalPermissionHandler, |
|
242
|
|
|
) |
|
243
|
|
|
class PermissionLogicalPermissionHandlerTestCase(TestCase): |
|
244
|
|
|
def setUp(self): |
|
245
|
|
|
# make sure all caches are removed |
|
246
|
|
|
Article._permission_logics = set() |
|
247
|
|
|
self.handler = LogicalPermissionHandler |
|
248
|
|
|
self.user = create_user('john') |
|
249
|
|
|
self.perm1 = 'permission.add_article' |
|
250
|
|
|
self.perm2 = 'permission.change_article' |
|
251
|
|
|
self.perm3 = 'permission.delete_article' |
|
252
|
|
|
self.article = create_article('test') |
|
253
|
|
|
|
|
254
|
|
|
from permission.logics import PermissionLogic |
|
255
|
|
|
from permission import add_permission_logic |
|
256
|
|
|
self.mock_logic1 = MagicMock(spec=PermissionLogic) |
|
257
|
|
|
self.mock_logic1.has_perm = MagicMock(return_value=False) |
|
258
|
|
|
self.mock_logic2 = MagicMock(spec=PermissionLogic) |
|
259
|
|
|
self.mock_logic2.has_perm = MagicMock(return_value=False) |
|
260
|
|
|
add_permission_logic(Article, self.mock_logic1) |
|
261
|
|
|
add_permission_logic(Article, self.mock_logic2) |
|
262
|
|
|
|
|
263
|
|
|
def test_constructor_with_app_label(self): |
|
264
|
|
|
self.assertRaises(AttributeError, |
|
265
|
|
|
self.handler, 'permission') |
|
266
|
|
|
|
|
267
|
|
|
def test_has_perm_non_related_permission(self): |
|
268
|
|
|
instance = self.handler(Article) |
|
269
|
|
|
instance.get_supported_permissions = MagicMock(return_value=[ |
|
270
|
|
|
'permission.add_article', |
|
271
|
|
|
'permission.change_article', |
|
272
|
|
|
'permission.delete_article', |
|
273
|
|
|
]) |
|
274
|
|
|
self.assertFalse(instance.has_perm(self.user, 'unknown')) |
|
275
|
|
|
self.assertFalse(instance.has_perm(self.user, 'unknown', self.article)) |
|
276
|
|
|
|
|
277
|
|
|
def test_has_perm_permission_logics_called(self): |
|
278
|
|
|
instance = self.handler(Article) |
|
279
|
|
|
instance.get_supported_permissions = MagicMock(return_value=[ |
|
280
|
|
|
'permission.add_article', |
|
281
|
|
|
'permission.change_article', |
|
282
|
|
|
'permission.delete_article', |
|
283
|
|
|
]) |
|
284
|
|
|
self.assertFalse(self.mock_logic1.has_perm.called) |
|
285
|
|
|
self.assertFalse(self.mock_logic2.has_perm.called) |
|
286
|
|
|
self.assertFalse(instance.has_perm(self.user, |
|
287
|
|
|
'permission.add_article')) |
|
288
|
|
|
self.assertTrue(self.mock_logic1.has_perm.called) |
|
289
|
|
|
self.assertTrue(self.mock_logic2.has_perm.called) |
|
290
|
|
|
self.assertEqual(self.mock_logic1.has_perm.call_count, 1) |
|
291
|
|
|
self.assertEqual(self.mock_logic2.has_perm.call_count, 1) |
|
292
|
|
|
# permission check should be cached thus `has_perm` should not be |
|
293
|
|
|
# called twice for same user instance |
|
294
|
|
|
self.assertFalse(instance.has_perm(self.user, |
|
295
|
|
|
'permission.add_article')) |
|
296
|
|
|
self.assertEqual(self.mock_logic1.has_perm.call_count, 1) |
|
297
|
|
|
self.assertEqual(self.mock_logic2.has_perm.call_count, 1) |
|
298
|
|
|
|