commit | author | age
|
f7efc0
|
1 |
import unittest |
TS |
2 |
|
|
3 |
class TestWhoConfig(unittest.TestCase): |
|
4 |
|
|
5 |
def _getTargetClass(self): |
|
6 |
from repoze.who.config import WhoConfig |
|
7 |
return WhoConfig |
|
8 |
|
|
9 |
def _makeOne(self, here='/', *args, **kw): |
|
10 |
return self._getTargetClass()(here, *args, **kw) |
|
11 |
|
|
12 |
def _getDummyPluginClass(self, iface): |
|
13 |
from zope.interface import classImplements |
|
14 |
if not iface.implementedBy(DummyPlugin): |
|
15 |
classImplements(DummyPlugin, iface) |
|
16 |
return DummyPlugin |
|
17 |
|
|
18 |
def test_defaults_before_parse(self): |
|
19 |
config = self._makeOne() |
|
20 |
self.assertEqual(config.request_classifier, None) |
|
21 |
self.assertEqual(config.challenge_decider, None) |
|
22 |
self.assertEqual(config.remote_user_key, 'REMOTE_USER') |
|
23 |
self.assertEqual(len(config.plugins), 0) |
|
24 |
self.assertEqual(len(config.identifiers), 0) |
|
25 |
self.assertEqual(len(config.authenticators), 0) |
|
26 |
self.assertEqual(len(config.challengers), 0) |
|
27 |
self.assertEqual(len(config.mdproviders), 0) |
|
28 |
|
|
29 |
def test_parse_empty_string(self): |
|
30 |
config = self._makeOne() |
|
31 |
config.parse('') |
|
32 |
self.assertEqual(config.request_classifier, None) |
|
33 |
self.assertEqual(config.challenge_decider, None) |
|
34 |
self.assertEqual(config.remote_user_key, 'REMOTE_USER') |
|
35 |
self.assertEqual(len(config.plugins), 0) |
|
36 |
self.assertEqual(len(config.identifiers), 0) |
|
37 |
self.assertEqual(len(config.authenticators), 0) |
|
38 |
self.assertEqual(len(config.challengers), 0) |
|
39 |
self.assertEqual(len(config.mdproviders), 0) |
|
40 |
|
|
41 |
def test_parse_empty_file(self): |
|
42 |
from StringIO import StringIO |
|
43 |
config = self._makeOne() |
|
44 |
config.parse(StringIO()) |
|
45 |
self.assertEqual(config.request_classifier, None) |
|
46 |
self.assertEqual(config.challenge_decider, None) |
|
47 |
self.assertEqual(config.remote_user_key, 'REMOTE_USER') |
|
48 |
self.assertEqual(len(config.plugins), 0) |
|
49 |
self.assertEqual(len(config.identifiers), 0) |
|
50 |
self.assertEqual(len(config.authenticators), 0) |
|
51 |
self.assertEqual(len(config.challengers), 0) |
|
52 |
self.assertEqual(len(config.mdproviders), 0) |
|
53 |
|
|
54 |
def test_parse_plugins(self): |
|
55 |
config = self._makeOne() |
|
56 |
config.parse(PLUGINS_ONLY) |
|
57 |
self.assertEqual(len(config.plugins), 2) |
|
58 |
self.failUnless(isinstance(config.plugins['foo'], |
|
59 |
DummyPlugin)) |
|
60 |
bar = config.plugins['bar'] |
|
61 |
self.failUnless(isinstance(bar, DummyPlugin)) |
|
62 |
self.assertEqual(bar.credentials, 'qux') |
|
63 |
|
|
64 |
def test_parse_general_empty(self): |
|
65 |
config = self._makeOne() |
|
66 |
config.parse('[general]') |
|
67 |
self.assertEqual(config.request_classifier, None) |
|
68 |
self.assertEqual(config.challenge_decider, None) |
|
69 |
self.assertEqual(config.remote_user_key, 'REMOTE_USER') |
|
70 |
self.assertEqual(len(config.plugins), 0) |
|
71 |
|
|
72 |
def test_parse_general_only(self): |
|
73 |
from repoze.who.interfaces import IRequestClassifier |
|
74 |
from repoze.who.interfaces import IChallengeDecider |
|
75 |
class IDummy(IRequestClassifier, IChallengeDecider): |
|
76 |
pass |
|
77 |
PLUGIN_CLASS = self._getDummyPluginClass(IDummy) |
|
78 |
config = self._makeOne() |
|
79 |
config.parse(GENERAL_ONLY) |
|
80 |
self.failUnless(isinstance(config.request_classifier, PLUGIN_CLASS)) |
|
81 |
self.failUnless(isinstance(config.challenge_decider, PLUGIN_CLASS)) |
|
82 |
self.assertEqual(config.remote_user_key, 'ANOTHER_REMOTE_USER') |
|
83 |
self.assertEqual(len(config.plugins), 0) |
|
84 |
|
|
85 |
def test_parse_general_with_plugins(self): |
|
86 |
from repoze.who.interfaces import IRequestClassifier |
|
87 |
from repoze.who.interfaces import IChallengeDecider |
|
88 |
class IDummy(IRequestClassifier, IChallengeDecider): |
|
89 |
pass |
|
90 |
PLUGIN_CLASS = self._getDummyPluginClass(IDummy) |
|
91 |
config = self._makeOne() |
|
92 |
config.parse(GENERAL_WITH_PLUGINS) |
|
93 |
self.failUnless(isinstance(config.request_classifier, PLUGIN_CLASS)) |
|
94 |
self.failUnless(isinstance(config.challenge_decider, PLUGIN_CLASS)) |
|
95 |
|
|
96 |
def test_parse_identifiers_only(self): |
|
97 |
from repoze.who.interfaces import IIdentifier |
|
98 |
PLUGIN_CLASS = self._getDummyPluginClass(IIdentifier) |
|
99 |
config = self._makeOne() |
|
100 |
config.parse(IDENTIFIERS_ONLY) |
|
101 |
identifiers = config.identifiers |
|
102 |
self.assertEqual(len(identifiers), 2) |
|
103 |
first, second = identifiers |
|
104 |
self.assertEqual(first[0], 'repoze.who.tests.test_config:DummyPlugin') |
|
105 |
self.failUnless(isinstance(first[1], PLUGIN_CLASS)) |
|
106 |
self.assertEqual(len(first[1].classifications), 1) |
|
107 |
self.assertEqual(first[1].classifications[IIdentifier], 'klass1') |
|
108 |
self.assertEqual(second[0], 'repoze.who.tests.test_config:DummyPlugin') |
|
109 |
self.failUnless(isinstance(second[1], PLUGIN_CLASS)) |
|
110 |
|
|
111 |
def test_parse_identifiers_with_plugins(self): |
|
112 |
from repoze.who.interfaces import IIdentifier |
|
113 |
PLUGIN_CLASS = self._getDummyPluginClass(IIdentifier) |
|
114 |
config = self._makeOne() |
|
115 |
config.parse(IDENTIFIERS_WITH_PLUGINS) |
|
116 |
identifiers = config.identifiers |
|
117 |
self.assertEqual(len(identifiers), 2) |
|
118 |
first, second = identifiers |
|
119 |
self.assertEqual(first[0], 'foo') |
|
120 |
self.failUnless(isinstance(first[1], PLUGIN_CLASS)) |
|
121 |
self.assertEqual(len(first[1].classifications), 1) |
|
122 |
self.assertEqual(first[1].classifications[IIdentifier], 'klass1') |
|
123 |
self.assertEqual(second[0], 'bar') |
|
124 |
self.failUnless(isinstance(second[1], PLUGIN_CLASS)) |
|
125 |
|
|
126 |
def test_parse_authenticators_only(self): |
|
127 |
from repoze.who.interfaces import IAuthenticator |
|
128 |
PLUGIN_CLASS = self._getDummyPluginClass(IAuthenticator) |
|
129 |
config = self._makeOne() |
|
130 |
config.parse(AUTHENTICATORS_ONLY) |
|
131 |
authenticators = config.authenticators |
|
132 |
self.assertEqual(len(authenticators), 2) |
|
133 |
first, second = authenticators |
|
134 |
self.assertEqual(first[0], 'repoze.who.tests.test_config:DummyPlugin') |
|
135 |
self.failUnless(isinstance(first[1], PLUGIN_CLASS)) |
|
136 |
self.assertEqual(len(first[1].classifications), 1) |
|
137 |
self.assertEqual(first[1].classifications[IAuthenticator], 'klass1') |
|
138 |
self.assertEqual(second[0], 'repoze.who.tests.test_config:DummyPlugin') |
|
139 |
self.failUnless(isinstance(second[1], PLUGIN_CLASS)) |
|
140 |
|
|
141 |
def test_parse_authenticators_with_plugins(self): |
|
142 |
from repoze.who.interfaces import IAuthenticator |
|
143 |
PLUGIN_CLASS = self._getDummyPluginClass(IAuthenticator) |
|
144 |
config = self._makeOne() |
|
145 |
config.parse(AUTHENTICATORS_WITH_PLUGINS) |
|
146 |
authenticators = config.authenticators |
|
147 |
self.assertEqual(len(authenticators), 2) |
|
148 |
first, second = authenticators |
|
149 |
self.assertEqual(first[0], 'foo') |
|
150 |
self.failUnless(isinstance(first[1], PLUGIN_CLASS)) |
|
151 |
self.assertEqual(len(first[1].classifications), 1) |
|
152 |
self.assertEqual(first[1].classifications[IAuthenticator], 'klass1') |
|
153 |
self.assertEqual(second[0], 'bar') |
|
154 |
self.failUnless(isinstance(second[1], PLUGIN_CLASS)) |
|
155 |
|
|
156 |
def test_parse_challengers_only(self): |
|
157 |
from repoze.who.interfaces import IChallenger |
|
158 |
PLUGIN_CLASS = self._getDummyPluginClass(IChallenger) |
|
159 |
config = self._makeOne() |
|
160 |
config.parse(CHALLENGERS_ONLY) |
|
161 |
challengers = config.challengers |
|
162 |
self.assertEqual(len(challengers), 2) |
|
163 |
first, second = challengers |
|
164 |
self.assertEqual(first[0], 'repoze.who.tests.test_config:DummyPlugin') |
|
165 |
self.failUnless(isinstance(first[1], PLUGIN_CLASS)) |
|
166 |
self.assertEqual(len(first[1].classifications), 1) |
|
167 |
self.assertEqual(first[1].classifications[IChallenger], 'klass1') |
|
168 |
self.assertEqual(second[0], 'repoze.who.tests.test_config:DummyPlugin') |
|
169 |
self.failUnless(isinstance(second[1], PLUGIN_CLASS)) |
|
170 |
|
|
171 |
def test_parse_challengers_with_plugins(self): |
|
172 |
from repoze.who.interfaces import IChallenger |
|
173 |
PLUGIN_CLASS = self._getDummyPluginClass(IChallenger) |
|
174 |
config = self._makeOne() |
|
175 |
config.parse(CHALLENGERS_WITH_PLUGINS) |
|
176 |
challengers = config.challengers |
|
177 |
self.assertEqual(len(challengers), 2) |
|
178 |
first, second = challengers |
|
179 |
self.assertEqual(first[0], 'foo') |
|
180 |
self.failUnless(isinstance(first[1], PLUGIN_CLASS)) |
|
181 |
self.assertEqual(len(first[1].classifications), 1) |
|
182 |
self.assertEqual(first[1].classifications[IChallenger], 'klass1') |
|
183 |
self.assertEqual(second[0], 'bar') |
|
184 |
self.failUnless(isinstance(second[1], PLUGIN_CLASS)) |
|
185 |
|
|
186 |
def test_parse_mdproviders_only(self): |
|
187 |
from repoze.who.interfaces import IMetadataProvider |
|
188 |
PLUGIN_CLASS = self._getDummyPluginClass(IMetadataProvider) |
|
189 |
config = self._makeOne() |
|
190 |
config.parse(MDPROVIDERS_ONLY) |
|
191 |
mdproviders = config.mdproviders |
|
192 |
self.assertEqual(len(mdproviders), 2) |
|
193 |
first, second = mdproviders |
|
194 |
self.assertEqual(first[0], 'repoze.who.tests.test_config:DummyPlugin') |
|
195 |
self.failUnless(isinstance(first[1], PLUGIN_CLASS)) |
|
196 |
self.assertEqual(len(first[1].classifications), 1) |
|
197 |
self.assertEqual(first[1].classifications[IMetadataProvider], 'klass1') |
|
198 |
self.assertEqual(second[0], 'repoze.who.tests.test_config:DummyPlugin') |
|
199 |
self.failUnless(isinstance(second[1], PLUGIN_CLASS)) |
|
200 |
|
|
201 |
def test_parse_mdproviders_with_plugins(self): |
|
202 |
from repoze.who.interfaces import IMetadataProvider |
|
203 |
PLUGIN_CLASS = self._getDummyPluginClass(IMetadataProvider) |
|
204 |
config = self._makeOne() |
|
205 |
config.parse(MDPROVIDERS_WITH_PLUGINS) |
|
206 |
mdproviders = config.mdproviders |
|
207 |
self.assertEqual(len(mdproviders), 2) |
|
208 |
first, second = mdproviders |
|
209 |
self.assertEqual(first[0], 'foo') |
|
210 |
self.failUnless(isinstance(first[1], PLUGIN_CLASS)) |
|
211 |
self.assertEqual(len(first[1].classifications), 1) |
|
212 |
self.assertEqual(first[1].classifications[IMetadataProvider], 'klass1') |
|
213 |
self.assertEqual(second[0], 'bar') |
|
214 |
self.failUnless(isinstance(second[1], PLUGIN_CLASS)) |
|
215 |
|
|
216 |
class DummyPlugin: |
|
217 |
def __init__(self, **kw): |
|
218 |
self.__dict__.update(kw) |
|
219 |
|
|
220 |
PLUGINS_ONLY = """\ |
|
221 |
[plugin:foo] |
|
222 |
use = repoze.who.tests.test_config:DummyPlugin |
|
223 |
|
|
224 |
[plugin:bar] |
|
225 |
use = repoze.who.tests.test_config:DummyPlugin |
|
226 |
credentials = qux |
|
227 |
""" |
|
228 |
|
|
229 |
GENERAL_ONLY = """\ |
|
230 |
[general] |
|
231 |
request_classifier = repoze.who.tests.test_config:DummyPlugin |
|
232 |
challenge_decider = repoze.who.tests.test_config:DummyPlugin |
|
233 |
remote_user_key = ANOTHER_REMOTE_USER |
|
234 |
""" |
|
235 |
|
|
236 |
GENERAL_WITH_PLUGINS = """\ |
|
237 |
[general] |
|
238 |
request_classifier = classifier |
|
239 |
challenge_decider = decider |
|
240 |
|
|
241 |
[plugin:classifier] |
|
242 |
use = repoze.who.tests.test_config:DummyPlugin |
|
243 |
|
|
244 |
[plugin:decider] |
|
245 |
use = repoze.who.tests.test_config:DummyPlugin |
|
246 |
""" |
|
247 |
|
|
248 |
IDENTIFIERS_ONLY = """\ |
|
249 |
[identifiers] |
|
250 |
plugins = |
|
251 |
repoze.who.tests.test_config:DummyPlugin;klass1 |
|
252 |
repoze.who.tests.test_config:DummyPlugin |
|
253 |
""" |
|
254 |
|
|
255 |
IDENTIFIERS_WITH_PLUGINS = """\ |
|
256 |
[identifiers] |
|
257 |
plugins = |
|
258 |
foo;klass1 |
|
259 |
bar |
|
260 |
|
|
261 |
[plugin:foo] |
|
262 |
use = repoze.who.tests.test_config:DummyPlugin |
|
263 |
|
|
264 |
[plugin:bar] |
|
265 |
use = repoze.who.tests.test_config:DummyPlugin |
|
266 |
""" |
|
267 |
|
|
268 |
AUTHENTICATORS_ONLY = """\ |
|
269 |
[authenticators] |
|
270 |
plugins = |
|
271 |
repoze.who.tests.test_config:DummyPlugin;klass1 |
|
272 |
repoze.who.tests.test_config:DummyPlugin |
|
273 |
""" |
|
274 |
|
|
275 |
AUTHENTICATORS_WITH_PLUGINS = """\ |
|
276 |
[authenticators] |
|
277 |
plugins = |
|
278 |
foo;klass1 |
|
279 |
bar |
|
280 |
|
|
281 |
[plugin:foo] |
|
282 |
use = repoze.who.tests.test_config:DummyPlugin |
|
283 |
|
|
284 |
[plugin:bar] |
|
285 |
use = repoze.who.tests.test_config:DummyPlugin |
|
286 |
""" |
|
287 |
|
|
288 |
CHALLENGERS_ONLY = """\ |
|
289 |
[challengers] |
|
290 |
plugins = |
|
291 |
repoze.who.tests.test_config:DummyPlugin;klass1 |
|
292 |
repoze.who.tests.test_config:DummyPlugin |
|
293 |
""" |
|
294 |
|
|
295 |
CHALLENGERS_WITH_PLUGINS = """\ |
|
296 |
[challengers] |
|
297 |
plugins = |
|
298 |
foo;klass1 |
|
299 |
bar |
|
300 |
|
|
301 |
[plugin:foo] |
|
302 |
use = repoze.who.tests.test_config:DummyPlugin |
|
303 |
|
|
304 |
[plugin:bar] |
|
305 |
use = repoze.who.tests.test_config:DummyPlugin |
|
306 |
""" |
|
307 |
|
|
308 |
MDPROVIDERS_ONLY = """\ |
|
309 |
[mdproviders] |
|
310 |
plugins = |
|
311 |
repoze.who.tests.test_config:DummyPlugin;klass1 |
|
312 |
repoze.who.tests.test_config:DummyPlugin |
|
313 |
""" |
|
314 |
|
|
315 |
MDPROVIDERS_WITH_PLUGINS = """\ |
|
316 |
[mdproviders] |
|
317 |
plugins = |
|
318 |
foo;klass1 |
|
319 |
bar |
|
320 |
|
|
321 |
[plugin:foo] |
|
322 |
use = repoze.who.tests.test_config:DummyPlugin |
|
323 |
|
|
324 |
[plugin:bar] |
|
325 |
use = repoze.who.tests.test_config:DummyPlugin |
|
326 |
""" |
|
327 |
|
|
328 |
class TestConfigMiddleware(unittest.TestCase): |
|
329 |
tempfile = None |
|
330 |
|
|
331 |
def setUp(self): |
|
332 |
pass |
|
333 |
|
|
334 |
def tearDown(self): |
|
335 |
if self.tempfile is not None: |
|
336 |
self.tempfile.close() |
|
337 |
|
|
338 |
def _getFactory(self): |
|
339 |
from repoze.who.config import make_middleware_with_config |
|
340 |
return make_middleware_with_config |
|
341 |
|
|
342 |
def _getTempfile(self, text): |
|
343 |
import tempfile |
|
344 |
tf = self.tempfile = tempfile.NamedTemporaryFile() |
|
345 |
tf.write(text) |
|
346 |
tf.flush() |
|
347 |
return tf |
|
348 |
|
|
349 |
def test_sample_config(self): |
|
350 |
app = DummyApp() |
|
351 |
factory = self._getFactory() |
|
352 |
tempfile = self._getTempfile(SAMPLE_CONFIG) |
|
353 |
global_cohf = {'here': '/'} |
|
354 |
middleware = factory(app, global_cohf, config_file=tempfile.name, |
|
355 |
log_file='STDOUT', log_level='debug') |
|
356 |
from repoze.who.interfaces import IIdentifier |
|
357 |
from repoze.who.interfaces import IAuthenticator |
|
358 |
from repoze.who.interfaces import IChallenger |
|
359 |
self.assertEqual(len(middleware.registry[IIdentifier]), 3) |
|
360 |
self.assertEqual(len(middleware.registry[IAuthenticator]), 1) |
|
361 |
self.assertEqual(len(middleware.registry[IChallenger]), 2) |
|
362 |
self.failUnless(middleware.logger, middleware.logger) |
|
363 |
import logging |
|
364 |
self.assertEqual(middleware.logger.getEffectiveLevel(), logging.DEBUG) |
|
365 |
|
3b9db8
|
366 |
def test_sample_config_no_log_level(self): |
TS |
367 |
app = DummyApp() |
|
368 |
factory = self._getFactory() |
|
369 |
tempfile = self._getTempfile(SAMPLE_CONFIG) |
|
370 |
global_cohf = {'here': '/'} |
|
371 |
middleware = factory(app, global_cohf, config_file=tempfile.name, |
|
372 |
log_file='STDOUT') |
|
373 |
import logging |
|
374 |
self.assertEqual(middleware.logger.getEffectiveLevel(), logging.INFO) |
|
375 |
|
f7efc0
|
376 |
SAMPLE_CONFIG = """\ |
TS |
377 |
[plugin:form] |
|
378 |
use = repoze.who.plugins.form:make_plugin |
|
379 |
login_form_qs = __do_login |
|
380 |
rememberer_name = auth_tkt |
|
381 |
|
|
382 |
[plugin:auth_tkt] |
|
383 |
use = repoze.who.plugins.auth_tkt:make_plugin |
|
384 |
secret = s33kr1t |
|
385 |
cookie_name = oatmeal |
|
386 |
secure = False |
|
387 |
include_ip = True |
|
388 |
|
|
389 |
[plugin:basicauth] |
|
390 |
use = repoze.who.plugins.basicauth:make_plugin |
|
391 |
realm = 'sample' |
|
392 |
|
|
393 |
[plugin:htpasswd] |
|
394 |
use = repoze.who.plugins.htpasswd:make_plugin |
|
395 |
filename = %(here)s/etc/passwd |
|
396 |
check_fn = repoze.who.plugins.htpasswd:crypt_check |
|
397 |
|
|
398 |
[general] |
|
399 |
request_classifier = repoze.who.classifiers:default_request_classifier |
|
400 |
challenge_decider = repoze.who.classifiers:default_challenge_decider |
|
401 |
|
|
402 |
[identifiers] |
|
403 |
plugins = |
|
404 |
form;browser |
|
405 |
auth_tkt |
|
406 |
basicauth |
|
407 |
|
|
408 |
[authenticators] |
|
409 |
plugins = htpasswd |
|
410 |
|
|
411 |
[challengers] |
|
412 |
plugins = |
|
413 |
form;browser |
|
414 |
basicauth |
|
415 |
|
|
416 |
[mdproviders] |
|
417 |
plugins = |
|
418 |
|
|
419 |
""" |
|
420 |
|
|
421 |
class DummyApp: |
|
422 |
environ = None |
|
423 |
def __call__(self, environ, start_response): |
|
424 |
self.environ = environ |
|
425 |
return [] |
|
426 |
|
|
427 |
|