commit | author | age
|
30d74a
|
1 |
import unittest |
CM |
2 |
|
79ef3d
|
3 |
from pyramid.exceptions import ConfigurationExecutionError |
CM |
4 |
from pyramid.exceptions import ConfigurationError |
|
5 |
|
30d74a
|
6 |
class ConfiguratorSecurityMethodsTests(unittest.TestCase): |
CM |
7 |
def _makeOne(self, *arg, **kw): |
|
8 |
from pyramid.config import Configurator |
|
9 |
config = Configurator(*arg, **kw) |
|
10 |
return config |
|
11 |
|
|
12 |
def test_set_authentication_policy_no_authz_policy(self): |
|
13 |
config = self._makeOne() |
|
14 |
policy = object() |
|
15 |
config.set_authentication_policy(policy) |
|
16 |
self.assertRaises(ConfigurationExecutionError, config.commit) |
|
17 |
|
|
18 |
def test_set_authentication_policy_no_authz_policy_autocommit(self): |
|
19 |
config = self._makeOne(autocommit=True) |
|
20 |
policy = object() |
f67ba4
|
21 |
self.assertRaises(ConfigurationError, |
CM |
22 |
config.set_authentication_policy, policy) |
30d74a
|
23 |
|
CM |
24 |
def test_set_authentication_policy_with_authz_policy(self): |
|
25 |
from pyramid.interfaces import IAuthenticationPolicy |
|
26 |
from pyramid.interfaces import IAuthorizationPolicy |
|
27 |
config = self._makeOne() |
|
28 |
authn_policy = object() |
|
29 |
authz_policy = object() |
|
30 |
config.registry.registerUtility(authz_policy, IAuthorizationPolicy) |
|
31 |
config.set_authentication_policy(authn_policy) |
|
32 |
config.commit() |
|
33 |
self.assertEqual( |
|
34 |
config.registry.getUtility(IAuthenticationPolicy), authn_policy) |
|
35 |
|
|
36 |
def test_set_authentication_policy_with_authz_policy_autocommit(self): |
|
37 |
from pyramid.interfaces import IAuthenticationPolicy |
|
38 |
from pyramid.interfaces import IAuthorizationPolicy |
|
39 |
config = self._makeOne(autocommit=True) |
|
40 |
authn_policy = object() |
|
41 |
authz_policy = object() |
|
42 |
config.registry.registerUtility(authz_policy, IAuthorizationPolicy) |
|
43 |
config.set_authentication_policy(authn_policy) |
|
44 |
config.commit() |
|
45 |
self.assertEqual( |
|
46 |
config.registry.getUtility(IAuthenticationPolicy), authn_policy) |
|
47 |
|
|
48 |
def test_set_authorization_policy_no_authn_policy(self): |
|
49 |
config = self._makeOne() |
|
50 |
policy = object() |
|
51 |
config.set_authorization_policy(policy) |
|
52 |
self.assertRaises(ConfigurationExecutionError, config.commit) |
|
53 |
|
|
54 |
def test_set_authorization_policy_no_authn_policy_autocommit(self): |
f67ba4
|
55 |
from pyramid.interfaces import IAuthorizationPolicy |
30d74a
|
56 |
config = self._makeOne(autocommit=True) |
CM |
57 |
policy = object() |
f67ba4
|
58 |
config.set_authorization_policy(policy) |
CM |
59 |
self.assertEqual( |
|
60 |
config.registry.getUtility(IAuthorizationPolicy), policy) |
30d74a
|
61 |
|
CM |
62 |
def test_set_authorization_policy_with_authn_policy(self): |
|
63 |
from pyramid.interfaces import IAuthorizationPolicy |
|
64 |
from pyramid.interfaces import IAuthenticationPolicy |
|
65 |
config = self._makeOne() |
|
66 |
authn_policy = object() |
|
67 |
authz_policy = object() |
|
68 |
config.registry.registerUtility(authn_policy, IAuthenticationPolicy) |
|
69 |
config.set_authorization_policy(authz_policy) |
f67ba4
|
70 |
config.commit() |
30d74a
|
71 |
self.assertEqual( |
CM |
72 |
config.registry.getUtility(IAuthorizationPolicy), authz_policy) |
|
73 |
|
|
74 |
def test_set_authorization_policy_with_authn_policy_autocommit(self): |
|
75 |
from pyramid.interfaces import IAuthorizationPolicy |
|
76 |
from pyramid.interfaces import IAuthenticationPolicy |
|
77 |
config = self._makeOne(autocommit=True) |
|
78 |
authn_policy = object() |
|
79 |
authz_policy = object() |
|
80 |
config.registry.registerUtility(authn_policy, IAuthenticationPolicy) |
|
81 |
config.set_authorization_policy(authz_policy) |
|
82 |
self.assertEqual( |
|
83 |
config.registry.getUtility(IAuthorizationPolicy), authz_policy) |
|
84 |
|
6251e0
|
85 |
def test_set_default_permission(self): |
CM |
86 |
from pyramid.interfaces import IDefaultPermission |
|
87 |
config = self._makeOne(autocommit=True) |
|
88 |
config.set_default_permission('view') |
|
89 |
self.assertEqual(config.registry.getUtility(IDefaultPermission), |
|
90 |
'view') |
|
91 |
|
6b180c
|
92 |
def test_add_permission(self): |
CM |
93 |
config = self._makeOne(autocommit=True) |
|
94 |
config.add_permission('perm') |
|
95 |
cat = config.registry.introspector.get_category('permissions') |
|
96 |
self.assertEqual(len(cat), 1) |
|
97 |
D = cat[0] |
|
98 |
intr = D['introspectable'] |
|
99 |
self.assertEqual(intr['value'], 'perm') |
|
100 |
|
de3d0c
|
101 |
def test_set_default_csrf_options(self): |
MM |
102 |
from pyramid.interfaces import IDefaultCSRFOptions |
|
103 |
config = self._makeOne(autocommit=True) |
|
104 |
config.set_default_csrf_options() |
|
105 |
result = config.registry.getUtility(IDefaultCSRFOptions) |
|
106 |
self.assertEqual(result.require_csrf, True) |
|
107 |
self.assertEqual(result.token, 'csrf_token') |
|
108 |
self.assertEqual(result.header, 'X-CSRF-Token') |
|
109 |
self.assertEqual(list(sorted(result.safe_methods)), |
|
110 |
['GET', 'HEAD', 'OPTIONS', 'TRACE']) |
17fa5e
|
111 |
self.assertTrue(result.callback is None) |
de3d0c
|
112 |
|
MM |
113 |
def test_changing_set_default_csrf_options(self): |
|
114 |
from pyramid.interfaces import IDefaultCSRFOptions |
|
115 |
config = self._makeOne(autocommit=True) |
17fa5e
|
116 |
def callback(request): return True |
de3d0c
|
117 |
config.set_default_csrf_options( |
17fa5e
|
118 |
require_csrf=False, token='DUMMY', header=None, |
MM |
119 |
safe_methods=('PUT',), callback=callback) |
de3d0c
|
120 |
result = config.registry.getUtility(IDefaultCSRFOptions) |
MM |
121 |
self.assertEqual(result.require_csrf, False) |
|
122 |
self.assertEqual(result.token, 'DUMMY') |
|
123 |
self.assertEqual(result.header, None) |
|
124 |
self.assertEqual(list(sorted(result.safe_methods)), ['PUT']) |
17fa5e
|
125 |
self.assertTrue(result.callback is callback) |