Package repoze :: Package who :: Package tests :: Module test_config
[hide private]

Source Code for Module repoze.who.tests.test_config

  1  import unittest 
  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
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 - def test_parse_make_plugin_names(self):
217 # see http://bugs.repoze.org/issue92 218 config = self._makeOne() 219 config.parse(MAKE_PLUGIN_ARG_NAMES) 220 self.assertEqual(len(config.plugins), 1) 221 foo = config.plugins['foo'] 222 self.failUnless(isinstance(foo, DummyPlugin)) 223 self.assertEqual(foo.iface, 'iface') 224 self.assertEqual(foo.name, 'name')
225
226 -class DummyPlugin:
227 - def __init__(self, **kw):
228 self.__dict__.update(kw)
229 230 PLUGINS_ONLY = """\ 231 [plugin:foo] 232 use = repoze.who.tests.test_config:DummyPlugin 233 234 [plugin:bar] 235 use = repoze.who.tests.test_config:DummyPlugin 236 credentials = qux 237 """ 238 239 GENERAL_ONLY = """\ 240 [general] 241 request_classifier = repoze.who.tests.test_config:DummyPlugin 242 challenge_decider = repoze.who.tests.test_config:DummyPlugin 243 remote_user_key = ANOTHER_REMOTE_USER 244 """ 245 246 GENERAL_WITH_PLUGINS = """\ 247 [general] 248 request_classifier = classifier 249 challenge_decider = decider 250 251 [plugin:classifier] 252 use = repoze.who.tests.test_config:DummyPlugin 253 254 [plugin:decider] 255 use = repoze.who.tests.test_config:DummyPlugin 256 """ 257 258 IDENTIFIERS_ONLY = """\ 259 [identifiers] 260 plugins = 261 repoze.who.tests.test_config:DummyPlugin;klass1 262 repoze.who.tests.test_config:DummyPlugin 263 """ 264 265 IDENTIFIERS_WITH_PLUGINS = """\ 266 [identifiers] 267 plugins = 268 foo;klass1 269 bar 270 271 [plugin:foo] 272 use = repoze.who.tests.test_config:DummyPlugin 273 274 [plugin:bar] 275 use = repoze.who.tests.test_config:DummyPlugin 276 """ 277 278 AUTHENTICATORS_ONLY = """\ 279 [authenticators] 280 plugins = 281 repoze.who.tests.test_config:DummyPlugin;klass1 282 repoze.who.tests.test_config:DummyPlugin 283 """ 284 285 AUTHENTICATORS_WITH_PLUGINS = """\ 286 [authenticators] 287 plugins = 288 foo;klass1 289 bar 290 291 [plugin:foo] 292 use = repoze.who.tests.test_config:DummyPlugin 293 294 [plugin:bar] 295 use = repoze.who.tests.test_config:DummyPlugin 296 """ 297 298 CHALLENGERS_ONLY = """\ 299 [challengers] 300 plugins = 301 repoze.who.tests.test_config:DummyPlugin;klass1 302 repoze.who.tests.test_config:DummyPlugin 303 """ 304 305 CHALLENGERS_WITH_PLUGINS = """\ 306 [challengers] 307 plugins = 308 foo;klass1 309 bar 310 311 [plugin:foo] 312 use = repoze.who.tests.test_config:DummyPlugin 313 314 [plugin:bar] 315 use = repoze.who.tests.test_config:DummyPlugin 316 """ 317 318 MDPROVIDERS_ONLY = """\ 319 [mdproviders] 320 plugins = 321 repoze.who.tests.test_config:DummyPlugin;klass1 322 repoze.who.tests.test_config:DummyPlugin 323 """ 324 325 MDPROVIDERS_WITH_PLUGINS = """\ 326 [mdproviders] 327 plugins = 328 foo;klass1 329 bar 330 331 [plugin:foo] 332 use = repoze.who.tests.test_config:DummyPlugin 333 334 [plugin:bar] 335 use = repoze.who.tests.test_config:DummyPlugin 336 """ 337 338 MAKE_PLUGIN_ARG_NAMES = """\ 339 [plugin:foo] 340 use = repoze.who.tests.test_config:DummyPlugin 341 name = name 342 iface = iface 343 """ 344
345 -class TestConfigMiddleware(unittest.TestCase):
346 tempdir = None 347
348 - def setUp(self):
349 pass
350
351 - def tearDown(self):
352 if self.tempdir is not None: 353 import shutil 354 shutil.rmtree(self.tempdir)
355
356 - def _getFactory(self):
359
360 - def _getTempfile(self, text):
361 import os 362 import tempfile 363 tempdir = self.tempdir = tempfile.mkdtemp() 364 path = os.path.join(tempdir, 'who.ini') 365 config = open(path, 'w') 366 config.write(text) 367 config.flush() 368 config.close() 369 return path
370
371 - def test_sample_config(self):
372 import logging 373 from repoze.who.interfaces import IIdentifier 374 from repoze.who.interfaces import IAuthenticator 375 from repoze.who.interfaces import IChallenger 376 app = DummyApp() 377 factory = self._getFactory() 378 path = self._getTempfile(SAMPLE_CONFIG) 379 global_conf = {'here': '/'} 380 middleware = factory(app, global_conf, config_file=path, 381 log_file='STDOUT', log_level='debug') 382 self.assertEqual(len(middleware.registry[IIdentifier]), 3) 383 self.assertEqual(len(middleware.registry[IAuthenticator]), 1) 384 self.assertEqual(len(middleware.registry[IChallenger]), 2) 385 self.failUnless(middleware.logger, middleware.logger) 386 self.assertEqual(middleware.logger.getEffectiveLevel(), logging.DEBUG)
387
389 import logging 390 app = DummyApp() 391 factory = self._getFactory() 392 path = self._getTempfile(SAMPLE_CONFIG) 393 global_conf = {'here': '/'} 394 middleware = factory(app, global_conf, config_file=path, 395 log_file='STDOUT') 396 self.assertEqual(middleware.logger.getEffectiveLevel(), logging.INFO)
397
399 import logging 400 import os 401 app = DummyApp() 402 factory = self._getFactory() 403 path = self._getTempfile(SAMPLE_CONFIG) 404 logfile = os.path.join(self.tempdir, 'who.log') 405 global_conf = {'here': '/'} 406 middleware = factory(app, global_conf, config_file=path, 407 log_file=logfile) 408 self.assertEqual(middleware.logger.getEffectiveLevel(), logging.INFO) 409 logging.shutdown()
410 411 SAMPLE_CONFIG = """\ 412 [plugin:form] 413 use = repoze.who.plugins.form:make_plugin 414 login_form_qs = __do_login 415 rememberer_name = auth_tkt 416 417 [plugin:auth_tkt] 418 use = repoze.who.plugins.auth_tkt:make_plugin 419 secret = s33kr1t 420 cookie_name = oatmeal 421 secure = False 422 include_ip = True 423 424 [plugin:basicauth] 425 use = repoze.who.plugins.basicauth:make_plugin 426 realm = 'sample' 427 428 [plugin:htpasswd] 429 use = repoze.who.plugins.htpasswd:make_plugin 430 filename = %(here)s/etc/passwd 431 check_fn = repoze.who.plugins.htpasswd:crypt_check 432 433 [general] 434 request_classifier = repoze.who.classifiers:default_request_classifier 435 challenge_decider = repoze.who.classifiers:default_challenge_decider 436 437 [identifiers] 438 plugins = 439 form;browser 440 auth_tkt 441 basicauth 442 443 [authenticators] 444 plugins = htpasswd 445 446 [challengers] 447 plugins = 448 form;browser 449 basicauth 450 451 [mdproviders] 452 plugins = 453 454 """ 455
456 -class DummyApp:
457 environ = None
458 - def __call__(self, environ, start_response):
459 self.environ = environ 460 return []
461