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