commit | author | age
|
4f0b02
|
1 |
import unittest |
CM |
2 |
|
c7337b
|
3 |
from pyramid import testing |
CM |
4 |
|
4f0b02
|
5 |
from pyramid.compat import text_ |
CM |
6 |
|
0c29cf
|
7 |
|
4f0b02
|
8 |
class TestXHRPredicate(unittest.TestCase): |
CM |
9 |
def _makeOne(self, val): |
c7974f
|
10 |
from pyramid.predicates import XHRPredicate |
0c29cf
|
11 |
|
9c8ec5
|
12 |
return XHRPredicate(val, None) |
0c29cf
|
13 |
|
4f0b02
|
14 |
def test___call___true(self): |
CM |
15 |
inst = self._makeOne(True) |
|
16 |
request = Dummy() |
|
17 |
request.is_xhr = True |
|
18 |
result = inst(None, request) |
|
19 |
self.assertTrue(result) |
0c29cf
|
20 |
|
4f0b02
|
21 |
def test___call___false(self): |
CM |
22 |
inst = self._makeOne(True) |
|
23 |
request = Dummy() |
|
24 |
request.is_xhr = False |
|
25 |
result = inst(None, request) |
|
26 |
self.assertFalse(result) |
|
27 |
|
4d2602
|
28 |
def test_text(self): |
4f0b02
|
29 |
inst = self._makeOne(True) |
4d2602
|
30 |
self.assertEqual(inst.text(), 'xhr = True') |
4f0b02
|
31 |
|
4d2602
|
32 |
def test_phash(self): |
4f0b02
|
33 |
inst = self._makeOne(True) |
4d2602
|
34 |
self.assertEqual(inst.phash(), 'xhr = True') |
4f0b02
|
35 |
|
0c29cf
|
36 |
|
4f0b02
|
37 |
class TestRequestMethodPredicate(unittest.TestCase): |
CM |
38 |
def _makeOne(self, val): |
c7974f
|
39 |
from pyramid.predicates import RequestMethodPredicate |
0c29cf
|
40 |
|
9c8ec5
|
41 |
return RequestMethodPredicate(val, None) |
d98612
|
42 |
|
CM |
43 |
def test_ctor_get_but_no_head(self): |
|
44 |
inst = self._makeOne('GET') |
|
45 |
self.assertEqual(inst.val, ('GET', 'HEAD')) |
0c29cf
|
46 |
|
4f0b02
|
47 |
def test___call___true_single(self): |
CM |
48 |
inst = self._makeOne('GET') |
|
49 |
request = Dummy() |
|
50 |
request.method = 'GET' |
|
51 |
result = inst(None, request) |
|
52 |
self.assertTrue(result) |
0c29cf
|
53 |
|
4f0b02
|
54 |
def test___call___true_multi(self): |
0c29cf
|
55 |
inst = self._makeOne(('GET', 'HEAD')) |
4f0b02
|
56 |
request = Dummy() |
CM |
57 |
request.method = 'GET' |
|
58 |
result = inst(None, request) |
|
59 |
self.assertTrue(result) |
|
60 |
|
|
61 |
def test___call___false(self): |
0c29cf
|
62 |
inst = self._makeOne(('GET', 'HEAD')) |
4f0b02
|
63 |
request = Dummy() |
CM |
64 |
request.method = 'POST' |
|
65 |
result = inst(None, request) |
|
66 |
self.assertFalse(result) |
|
67 |
|
4d2602
|
68 |
def test_text(self): |
0c29cf
|
69 |
inst = self._makeOne(('HEAD', 'GET')) |
4d2602
|
70 |
self.assertEqual(inst.text(), 'request_method = GET,HEAD') |
4f0b02
|
71 |
|
4d2602
|
72 |
def test_phash(self): |
0c29cf
|
73 |
inst = self._makeOne(('HEAD', 'GET')) |
4d2602
|
74 |
self.assertEqual(inst.phash(), 'request_method = GET,HEAD') |
0c29cf
|
75 |
|
4f0b02
|
76 |
|
CM |
77 |
class TestPathInfoPredicate(unittest.TestCase): |
|
78 |
def _makeOne(self, val): |
c7974f
|
79 |
from pyramid.predicates import PathInfoPredicate |
0c29cf
|
80 |
|
9c8ec5
|
81 |
return PathInfoPredicate(val, None) |
4f0b02
|
82 |
|
CM |
83 |
def test_ctor_compilefail(self): |
|
84 |
from pyramid.exceptions import ConfigurationError |
0c29cf
|
85 |
|
4f0b02
|
86 |
self.assertRaises(ConfigurationError, self._makeOne, '\\') |
0c29cf
|
87 |
|
4f0b02
|
88 |
def test___call___true(self): |
CM |
89 |
inst = self._makeOne(r'/\d{2}') |
|
90 |
request = Dummy() |
|
91 |
request.upath_info = text_('/12') |
|
92 |
result = inst(None, request) |
|
93 |
self.assertTrue(result) |
0c29cf
|
94 |
|
4f0b02
|
95 |
def test___call___false(self): |
CM |
96 |
inst = self._makeOne(r'/\d{2}') |
|
97 |
request = Dummy() |
|
98 |
request.upath_info = text_('/n12') |
|
99 |
result = inst(None, request) |
|
100 |
self.assertFalse(result) |
|
101 |
|
4d2602
|
102 |
def test_text(self): |
4f0b02
|
103 |
inst = self._makeOne('/') |
4d2602
|
104 |
self.assertEqual(inst.text(), 'path_info = /') |
4f0b02
|
105 |
|
4d2602
|
106 |
def test_phash(self): |
4f0b02
|
107 |
inst = self._makeOne('/') |
4d2602
|
108 |
self.assertEqual(inst.phash(), 'path_info = /') |
4f0b02
|
109 |
|
0c29cf
|
110 |
|
4f0b02
|
111 |
class TestRequestParamPredicate(unittest.TestCase): |
CM |
112 |
def _makeOne(self, val): |
c7974f
|
113 |
from pyramid.predicates import RequestParamPredicate |
0c29cf
|
114 |
|
9c8ec5
|
115 |
return RequestParamPredicate(val, None) |
4f0b02
|
116 |
|
CM |
117 |
def test___call___true_exists(self): |
|
118 |
inst = self._makeOne('abc') |
|
119 |
request = Dummy() |
0c29cf
|
120 |
request.params = {'abc': 1} |
4f0b02
|
121 |
result = inst(None, request) |
CM |
122 |
self.assertTrue(result) |
|
123 |
|
|
124 |
def test___call___true_withval(self): |
|
125 |
inst = self._makeOne('abc=1') |
|
126 |
request = Dummy() |
0c29cf
|
127 |
request.params = {'abc': '1'} |
4f0b02
|
128 |
result = inst(None, request) |
CM |
129 |
self.assertTrue(result) |
|
130 |
|
3fb934
|
131 |
def test___call___true_multi(self): |
53c7f7
|
132 |
inst = self._makeOne(('abc', '=def =2= ')) |
3fb934
|
133 |
request = Dummy() |
0c29cf
|
134 |
request.params = {'abc': '1', '=def': '2='} |
3fb934
|
135 |
result = inst(None, request) |
MM |
136 |
self.assertTrue(result) |
|
137 |
|
|
138 |
def test___call___false_multi(self): |
|
139 |
inst = self._makeOne(('abc=3', 'def =2 ')) |
|
140 |
request = Dummy() |
0c29cf
|
141 |
request.params = {'abc': '3', 'def': '1'} |
3fb934
|
142 |
result = inst(None, request) |
MM |
143 |
self.assertFalse(result) |
|
144 |
|
4f0b02
|
145 |
def test___call___false(self): |
CM |
146 |
inst = self._makeOne('abc') |
|
147 |
request = Dummy() |
|
148 |
request.params = {} |
|
149 |
result = inst(None, request) |
|
150 |
self.assertFalse(result) |
|
151 |
|
4d2602
|
152 |
def test_text_exists(self): |
4f0b02
|
153 |
inst = self._makeOne('abc') |
4d2602
|
154 |
self.assertEqual(inst.text(), 'request_param abc') |
4f0b02
|
155 |
|
00f80c
|
156 |
def test_text_exists_equal_sign(self): |
TI |
157 |
inst = self._makeOne('=abc') |
|
158 |
self.assertEqual(inst.text(), 'request_param =abc') |
|
159 |
|
4d2602
|
160 |
def test_text_withval(self): |
4f0b02
|
161 |
inst = self._makeOne('abc= 1') |
5507b8
|
162 |
self.assertEqual(inst.text(), 'request_param abc=1') |
4f0b02
|
163 |
|
3fb934
|
164 |
def test_text_multi(self): |
MM |
165 |
inst = self._makeOne(('abc= 1', 'def')) |
5507b8
|
166 |
self.assertEqual(inst.text(), 'request_param abc=1,def') |
3fb934
|
167 |
|
00f80c
|
168 |
def test_text_multi_equal_sign(self): |
afa8a7
|
169 |
inst = self._makeOne(('abc= 1', '=def= 2')) |
TI |
170 |
self.assertEqual(inst.text(), 'request_param =def=2,abc=1') |
00f80c
|
171 |
|
4d2602
|
172 |
def test_phash_exists(self): |
4f0b02
|
173 |
inst = self._makeOne('abc') |
4d2602
|
174 |
self.assertEqual(inst.phash(), 'request_param abc') |
4f0b02
|
175 |
|
00f80c
|
176 |
def test_phash_exists_equal_sign(self): |
TI |
177 |
inst = self._makeOne('=abc') |
|
178 |
self.assertEqual(inst.phash(), 'request_param =abc') |
|
179 |
|
4d2602
|
180 |
def test_phash_withval(self): |
4f0b02
|
181 |
inst = self._makeOne('abc= 1') |
5507b8
|
182 |
self.assertEqual(inst.phash(), "request_param abc=1") |
4f0b02
|
183 |
|
0c29cf
|
184 |
|
4d2602
|
185 |
class TestMatchParamPredicate(unittest.TestCase): |
CM |
186 |
def _makeOne(self, val): |
c7974f
|
187 |
from pyramid.predicates import MatchParamPredicate |
0c29cf
|
188 |
|
9c8ec5
|
189 |
return MatchParamPredicate(val, None) |
4d2602
|
190 |
|
CM |
191 |
def test___call___true_single(self): |
|
192 |
inst = self._makeOne('abc=1') |
|
193 |
request = Dummy() |
0c29cf
|
194 |
request.matchdict = {'abc': '1'} |
4d2602
|
195 |
result = inst(None, request) |
CM |
196 |
self.assertTrue(result) |
|
197 |
|
|
198 |
def test___call___true_multi(self): |
|
199 |
inst = self._makeOne(('abc=1', 'def=2')) |
|
200 |
request = Dummy() |
0c29cf
|
201 |
request.matchdict = {'abc': '1', 'def': '2'} |
4d2602
|
202 |
result = inst(None, request) |
CM |
203 |
self.assertTrue(result) |
0c29cf
|
204 |
|
4d2602
|
205 |
def test___call___false(self): |
CM |
206 |
inst = self._makeOne('abc=1') |
|
207 |
request = Dummy() |
|
208 |
request.matchdict = {} |
|
209 |
result = inst(None, request) |
|
210 |
self.assertFalse(result) |
|
211 |
|
267dbd
|
212 |
def test___call___matchdict_is_None(self): |
CM |
213 |
inst = self._makeOne('abc=1') |
|
214 |
request = Dummy() |
|
215 |
request.matchdict = None |
|
216 |
result = inst(None, request) |
|
217 |
self.assertFalse(result) |
|
218 |
|
4d2602
|
219 |
def test_text(self): |
CM |
220 |
inst = self._makeOne(('def= 1', 'abc =2')) |
|
221 |
self.assertEqual(inst.text(), 'match_param abc=2,def=1') |
|
222 |
|
|
223 |
def test_phash(self): |
|
224 |
inst = self._makeOne(('def= 1', 'abc =2')) |
|
225 |
self.assertEqual(inst.phash(), 'match_param abc=2,def=1') |
|
226 |
|
0c29cf
|
227 |
|
4d2602
|
228 |
class TestCustomPredicate(unittest.TestCase): |
CM |
229 |
def _makeOne(self, val): |
c7974f
|
230 |
from pyramid.predicates import CustomPredicate |
0c29cf
|
231 |
|
9c8ec5
|
232 |
return CustomPredicate(val, None) |
4d2602
|
233 |
|
CM |
234 |
def test___call___true(self): |
|
235 |
def func(context, request): |
|
236 |
self.assertEqual(context, None) |
|
237 |
self.assertEqual(request, None) |
|
238 |
return True |
0c29cf
|
239 |
|
4d2602
|
240 |
inst = self._makeOne(func) |
CM |
241 |
result = inst(None, None) |
|
242 |
self.assertTrue(result) |
0c29cf
|
243 |
|
4d2602
|
244 |
def test___call___false(self): |
CM |
245 |
def func(context, request): |
|
246 |
self.assertEqual(context, None) |
|
247 |
self.assertEqual(request, None) |
|
248 |
return False |
0c29cf
|
249 |
|
4d2602
|
250 |
inst = self._makeOne(func) |
CM |
251 |
result = inst(None, None) |
|
252 |
self.assertFalse(result) |
|
253 |
|
|
254 |
def test_text_func_has___text__(self): |
|
255 |
pred = predicate() |
|
256 |
pred.__text__ = 'text' |
|
257 |
inst = self._makeOne(pred) |
|
258 |
self.assertEqual(inst.text(), 'text') |
0c29cf
|
259 |
|
4d2602
|
260 |
def test_text_func_repr(self): |
CM |
261 |
pred = predicate() |
|
262 |
inst = self._makeOne(pred) |
859e94
|
263 |
self.assertEqual(inst.text(), 'custom predicate: object predicate') |
4d2602
|
264 |
|
CM |
265 |
def test_phash(self): |
|
266 |
pred = predicate() |
|
267 |
inst = self._makeOne(pred) |
9c8ec5
|
268 |
self.assertEqual(inst.phash(), 'custom:1') |
4d2602
|
269 |
|
0c29cf
|
270 |
|
4d2602
|
271 |
class TestTraversePredicate(unittest.TestCase): |
CM |
272 |
def _makeOne(self, val): |
c7974f
|
273 |
from pyramid.predicates import TraversePredicate |
0c29cf
|
274 |
|
9c8ec5
|
275 |
return TraversePredicate(val, None) |
0c29cf
|
276 |
|
4d2602
|
277 |
def test___call__traverse_has_remainder_already(self): |
CM |
278 |
inst = self._makeOne('/1/:a/:b') |
0c29cf
|
279 |
info = {'traverse': 'abc'} |
4d2602
|
280 |
request = Dummy() |
CM |
281 |
result = inst(info, request) |
|
282 |
self.assertEqual(result, True) |
0c29cf
|
283 |
self.assertEqual(info, {'traverse': 'abc'}) |
4d2602
|
284 |
|
CM |
285 |
def test___call__traverse_matches(self): |
|
286 |
inst = self._makeOne('/1/:a/:b') |
0c29cf
|
287 |
info = {'match': {'a': 'a', 'b': 'b'}} |
4d2602
|
288 |
request = Dummy() |
CM |
289 |
result = inst(info, request) |
|
290 |
self.assertEqual(result, True) |
0c29cf
|
291 |
self.assertEqual( |
MM |
292 |
info, {'match': {'a': 'a', 'b': 'b', 'traverse': ('1', 'a', 'b')}} |
|
293 |
) |
4d2602
|
294 |
|
CM |
295 |
def test___call__traverse_matches_with_highorder_chars(self): |
|
296 |
inst = self._makeOne(text_(b'/La Pe\xc3\xb1a/{x}', 'utf-8')) |
0c29cf
|
297 |
info = {'match': {'x': text_(b'Qu\xc3\xa9bec', 'utf-8')}} |
4d2602
|
298 |
request = Dummy() |
CM |
299 |
result = inst(info, request) |
|
300 |
self.assertEqual(result, True) |
|
301 |
self.assertEqual( |
|
302 |
info['match']['traverse'], |
0c29cf
|
303 |
( |
MM |
304 |
text_(b'La Pe\xc3\xb1a', 'utf-8'), |
|
305 |
text_(b'Qu\xc3\xa9bec', 'utf-8'), |
|
306 |
), |
|
307 |
) |
4d2602
|
308 |
|
CM |
309 |
def test_text(self): |
|
310 |
inst = self._makeOne('/abc') |
|
311 |
self.assertEqual(inst.text(), 'traverse matchdict pseudo-predicate') |
|
312 |
|
|
313 |
def test_phash(self): |
|
314 |
inst = self._makeOne('/abc') |
|
315 |
self.assertEqual(inst.phash(), '') |
|
316 |
|
0c29cf
|
317 |
|
643a83
|
318 |
class Test_CheckCSRFTokenPredicate(unittest.TestCase): |
CM |
319 |
def _makeOne(self, val, config): |
c7974f
|
320 |
from pyramid.predicates import CheckCSRFTokenPredicate |
0c29cf
|
321 |
|
643a83
|
322 |
return CheckCSRFTokenPredicate(val, config) |
CM |
323 |
|
|
324 |
def test_text(self): |
|
325 |
inst = self._makeOne(True, None) |
|
326 |
self.assertEqual(inst.text(), 'check_csrf = True') |
|
327 |
|
|
328 |
def test_phash(self): |
|
329 |
inst = self._makeOne(True, None) |
|
330 |
self.assertEqual(inst.phash(), 'check_csrf = True') |
0c29cf
|
331 |
|
643a83
|
332 |
def test_it_call_val_True(self): |
CM |
333 |
inst = self._makeOne(True, None) |
|
334 |
request = Dummy() |
0c29cf
|
335 |
|
643a83
|
336 |
def check_csrf_token(req, val, raises=True): |
CM |
337 |
self.assertEqual(req, request) |
|
338 |
self.assertEqual(val, 'csrf_token') |
|
339 |
self.assertEqual(raises, False) |
|
340 |
return True |
0c29cf
|
341 |
|
MM |
342 |
inst.check_csrf_token = check_csrf_token |
643a83
|
343 |
result = inst(None, request) |
CM |
344 |
self.assertEqual(result, True) |
|
345 |
|
|
346 |
def test_it_call_val_str(self): |
|
347 |
inst = self._makeOne('abc', None) |
|
348 |
request = Dummy() |
0c29cf
|
349 |
|
643a83
|
350 |
def check_csrf_token(req, val, raises=True): |
CM |
351 |
self.assertEqual(req, request) |
|
352 |
self.assertEqual(val, 'abc') |
|
353 |
self.assertEqual(raises, False) |
|
354 |
return True |
0c29cf
|
355 |
|
MM |
356 |
inst.check_csrf_token = check_csrf_token |
643a83
|
357 |
result = inst(None, request) |
CM |
358 |
self.assertEqual(result, True) |
|
359 |
|
|
360 |
def test_it_call_val_False(self): |
|
361 |
inst = self._makeOne(False, None) |
|
362 |
request = Dummy() |
|
363 |
result = inst(None, request) |
|
364 |
self.assertEqual(result, True) |
|
365 |
|
0c29cf
|
366 |
|
b1d2a3
|
367 |
class TestHeaderPredicate(unittest.TestCase): |
MM |
368 |
def _makeOne(self, val): |
c7974f
|
369 |
from pyramid.predicates import HeaderPredicate |
0c29cf
|
370 |
|
b1d2a3
|
371 |
return HeaderPredicate(val, None) |
MM |
372 |
|
|
373 |
def test___call___true_exists(self): |
|
374 |
inst = self._makeOne('abc') |
|
375 |
request = Dummy() |
0c29cf
|
376 |
request.headers = {'abc': 1} |
b1d2a3
|
377 |
result = inst(None, request) |
MM |
378 |
self.assertTrue(result) |
|
379 |
|
|
380 |
def test___call___true_withval(self): |
|
381 |
inst = self._makeOne('abc:1') |
|
382 |
request = Dummy() |
0c29cf
|
383 |
request.headers = {'abc': '1'} |
b1d2a3
|
384 |
result = inst(None, request) |
MM |
385 |
self.assertTrue(result) |
|
386 |
|
|
387 |
def test___call___true_withregex(self): |
|
388 |
inst = self._makeOne(r'abc:\d+') |
|
389 |
request = Dummy() |
0c29cf
|
390 |
request.headers = {'abc': '1'} |
b1d2a3
|
391 |
result = inst(None, request) |
MM |
392 |
self.assertTrue(result) |
|
393 |
|
|
394 |
def test___call___false_withregex(self): |
|
395 |
inst = self._makeOne(r'abc:\d+') |
|
396 |
request = Dummy() |
0c29cf
|
397 |
request.headers = {'abc': 'a'} |
b1d2a3
|
398 |
result = inst(None, request) |
MM |
399 |
self.assertFalse(result) |
|
400 |
|
|
401 |
def test___call___false(self): |
|
402 |
inst = self._makeOne('abc') |
|
403 |
request = Dummy() |
|
404 |
request.headers = {} |
|
405 |
result = inst(None, request) |
|
406 |
self.assertFalse(result) |
|
407 |
|
|
408 |
def test_text_exists(self): |
|
409 |
inst = self._makeOne('abc') |
|
410 |
self.assertEqual(inst.text(), 'header abc') |
|
411 |
|
|
412 |
def test_text_withval(self): |
|
413 |
inst = self._makeOne('abc:1') |
|
414 |
self.assertEqual(inst.text(), 'header abc=1') |
|
415 |
|
|
416 |
def test_text_withregex(self): |
|
417 |
inst = self._makeOne(r'abc:\d+') |
|
418 |
self.assertEqual(inst.text(), r'header abc=\d+') |
|
419 |
|
|
420 |
def test_phash_exists(self): |
|
421 |
inst = self._makeOne('abc') |
|
422 |
self.assertEqual(inst.phash(), 'header abc') |
|
423 |
|
|
424 |
def test_phash_withval(self): |
|
425 |
inst = self._makeOne('abc:1') |
|
426 |
self.assertEqual(inst.phash(), "header abc=1") |
|
427 |
|
|
428 |
def test_phash_withregex(self): |
|
429 |
inst = self._makeOne(r'abc:\d+') |
|
430 |
self.assertEqual(inst.phash(), r'header abc=\d+') |
|
431 |
|
0c29cf
|
432 |
|
c25a8f
|
433 |
class Test_PhysicalPathPredicate(unittest.TestCase): |
CM |
434 |
def _makeOne(self, val, config): |
c7974f
|
435 |
from pyramid.predicates import PhysicalPathPredicate |
0c29cf
|
436 |
|
c25a8f
|
437 |
return PhysicalPathPredicate(val, config) |
CM |
438 |
|
|
439 |
def test_text(self): |
|
440 |
inst = self._makeOne('/', None) |
|
441 |
self.assertEqual(inst.text(), "physical_path = ('',)") |
|
442 |
|
|
443 |
def test_phash(self): |
|
444 |
inst = self._makeOne('/', None) |
|
445 |
self.assertEqual(inst.phash(), "physical_path = ('',)") |
0c29cf
|
446 |
|
c25a8f
|
447 |
def test_it_call_val_tuple_True(self): |
CM |
448 |
inst = self._makeOne(('', 'abc'), None) |
|
449 |
root = Dummy() |
|
450 |
root.__name__ = '' |
|
451 |
root.__parent__ = None |
|
452 |
context = Dummy() |
|
453 |
context.__name__ = 'abc' |
|
454 |
context.__parent__ = root |
|
455 |
self.assertTrue(inst(context, None)) |
|
456 |
|
|
457 |
def test_it_call_val_list_True(self): |
|
458 |
inst = self._makeOne(['', 'abc'], None) |
|
459 |
root = Dummy() |
|
460 |
root.__name__ = '' |
|
461 |
root.__parent__ = None |
|
462 |
context = Dummy() |
|
463 |
context.__name__ = 'abc' |
|
464 |
context.__parent__ = root |
|
465 |
self.assertTrue(inst(context, None)) |
|
466 |
|
|
467 |
def test_it_call_val_str_True(self): |
|
468 |
inst = self._makeOne('/abc', None) |
|
469 |
root = Dummy() |
|
470 |
root.__name__ = '' |
|
471 |
root.__parent__ = None |
|
472 |
context = Dummy() |
|
473 |
context.__name__ = 'abc' |
|
474 |
context.__parent__ = root |
|
475 |
self.assertTrue(inst(context, None)) |
|
476 |
|
|
477 |
def test_it_call_False(self): |
|
478 |
inst = self._makeOne('/', None) |
|
479 |
root = Dummy() |
|
480 |
root.__name__ = '' |
|
481 |
root.__parent__ = None |
|
482 |
context = Dummy() |
|
483 |
context.__name__ = 'abc' |
|
484 |
context.__parent__ = root |
|
485 |
self.assertFalse(inst(context, None)) |
|
486 |
|
267dbd
|
487 |
def test_it_call_context_has_no_name(self): |
CM |
488 |
inst = self._makeOne('/', None) |
|
489 |
context = Dummy() |
|
490 |
self.assertFalse(inst(context, None)) |
|
491 |
|
0c29cf
|
492 |
|
c7337b
|
493 |
class Test_EffectivePrincipalsPredicate(unittest.TestCase): |
CM |
494 |
def setUp(self): |
|
495 |
self.config = testing.setUp() |
|
496 |
|
|
497 |
def tearDown(self): |
|
498 |
testing.tearDown() |
0c29cf
|
499 |
|
c7337b
|
500 |
def _makeOne(self, val, config): |
c7974f
|
501 |
from pyramid.predicates import EffectivePrincipalsPredicate |
0c29cf
|
502 |
|
c7337b
|
503 |
return EffectivePrincipalsPredicate(val, config) |
CM |
504 |
|
|
505 |
def test_text(self): |
|
506 |
inst = self._makeOne(('verna', 'fred'), None) |
0c29cf
|
507 |
self.assertEqual( |
MM |
508 |
inst.text(), "effective_principals = ['fred', 'verna']" |
|
509 |
) |
c7337b
|
510 |
|
CM |
511 |
def test_text_noniter(self): |
|
512 |
inst = self._makeOne('verna', None) |
0c29cf
|
513 |
self.assertEqual(inst.text(), "effective_principals = ['verna']") |
c7337b
|
514 |
|
CM |
515 |
def test_phash(self): |
|
516 |
inst = self._makeOne(('verna', 'fred'), None) |
0c29cf
|
517 |
self.assertEqual( |
MM |
518 |
inst.phash(), "effective_principals = ['fred', 'verna']" |
|
519 |
) |
c7337b
|
520 |
|
CM |
521 |
def test_it_call_no_authentication_policy(self): |
|
522 |
request = testing.DummyRequest() |
|
523 |
inst = self._makeOne(('verna', 'fred'), None) |
|
524 |
context = Dummy() |
|
525 |
self.assertFalse(inst(context, request)) |
|
526 |
|
|
527 |
def test_it_call_authentication_policy_provides_superset(self): |
|
528 |
request = testing.DummyRequest() |
|
529 |
self.config.testing_securitypolicy('fred', groupids=('verna', 'bambi')) |
|
530 |
inst = self._makeOne(('verna', 'fred'), None) |
|
531 |
context = Dummy() |
|
532 |
self.assertTrue(inst(context, request)) |
|
533 |
|
|
534 |
def test_it_call_authentication_policy_provides_superset_implicit(self): |
|
535 |
from pyramid.security import Authenticated |
0c29cf
|
536 |
|
c7337b
|
537 |
request = testing.DummyRequest() |
CM |
538 |
self.config.testing_securitypolicy('fred', groupids=('verna', 'bambi')) |
|
539 |
inst = self._makeOne(Authenticated, None) |
|
540 |
context = Dummy() |
|
541 |
self.assertTrue(inst(context, request)) |
|
542 |
|
|
543 |
def test_it_call_authentication_policy_doesnt_provide_superset(self): |
|
544 |
request = testing.DummyRequest() |
|
545 |
self.config.testing_securitypolicy('fred') |
|
546 |
inst = self._makeOne(('verna', 'fred'), None) |
|
547 |
context = Dummy() |
|
548 |
self.assertFalse(inst(context, request)) |
|
549 |
|
52fde9
|
550 |
|
MM |
551 |
class TestNotted(unittest.TestCase): |
|
552 |
def _makeOne(self, predicate): |
|
553 |
from pyramid.predicates import Notted |
0c29cf
|
554 |
|
52fde9
|
555 |
return Notted(predicate) |
MM |
556 |
|
|
557 |
def test_it_with_phash_val(self): |
|
558 |
pred = DummyPredicate('val') |
|
559 |
inst = self._makeOne(pred) |
|
560 |
self.assertEqual(inst.text(), '!val') |
|
561 |
self.assertEqual(inst.phash(), '!val') |
|
562 |
self.assertEqual(inst(None, None), False) |
|
563 |
|
|
564 |
def test_it_without_phash_val(self): |
|
565 |
pred = DummyPredicate('') |
|
566 |
inst = self._makeOne(pred) |
|
567 |
self.assertEqual(inst.text(), '') |
|
568 |
self.assertEqual(inst.phash(), '') |
|
569 |
self.assertEqual(inst(None, None), True) |
|
570 |
|
0c29cf
|
571 |
|
4d2602
|
572 |
class predicate(object): |
CM |
573 |
def __repr__(self): |
|
574 |
return 'predicate' |
0c29cf
|
575 |
|
4d2602
|
576 |
def __hash__(self): |
CM |
577 |
return 1 |
52fde9
|
578 |
|
0c29cf
|
579 |
|
4f0b02
|
580 |
class Dummy(object): |
52fde9
|
581 |
pass |
MM |
582 |
|
0c29cf
|
583 |
|
52fde9
|
584 |
class DummyPredicate(object): |
MM |
585 |
def __init__(self, result): |
|
586 |
self.result = result |
|
587 |
|
|
588 |
def text(self): |
|
589 |
return self.result |
|
590 |
|
|
591 |
phash = text |
|
592 |
|
|
593 |
def __call__(self, context, request): |
|
594 |
return True |