Tres Seaver
2010-04-26 a349a240a116319a72dde88007aa37b4b3d14e4e
Added ``repoze.who.config:make_api_factory_with_config``.

This is a convenience function for applications which want to set up their
own API Factory from a configuration file.

4 files modified
106 ■■■■ changed files
CHANGES.txt 4 ●●●● patch | view | raw | blame | history
docs/api.rst 22 ●●●●● patch | view | raw | blame | history
repoze/who/config.py 18 ●●●●● patch | view | raw | blame | history
repoze/who/tests/test_config.py 62 ●●●●● patch | view | raw | blame | history
CHANGES.txt
@@ -4,6 +4,10 @@
After 2.0a2 (unreleased)
-----------------------
- Added ``repoze.who.config:make_api_factory_with_config``, a convenience
  method for applications which want to set up their own API Factory from
  a configuration file.
- Fixed example call to ``repoze.who.config:make_middleware_with_config``
  (added missing ``global_config`` argument).  See
  http://bugs.repoze.org/issue114
docs/api.rst
@@ -15,27 +15,19 @@
  instance, populating it with a request classifier, a challenge decider,
  and a set of plugins.  It can do this process imperatively
  (see :ref:`imperative_configuration`), or using a declarative
  configuration file (see :ref:`declarative_configuration`).
  configuration file (see :ref:`declarative_configuration`).  For the latter
  case, there is a convenience function,
  :func:`repoze.who.config.make_api_factory_with_config`:
.. code-block:: python
   # myapp/run.py
   from repoze.who.api import APIFactory
   from repoze.who.config import make_api_factory_with_config
   who_api_factory = None
   def startup(global_conf):
       global who_api_factory
       parser = WhoConfig(global_conf['here'])
       parser.parse(open(global_conf['who_config']))
       who_api_factory = APIFactory(
                            parser.identifiers,
                            parser.authenticators,
                            parser.challengers,
                            parser.mdproviders,
                            parser.request_classifier,
                            parser.challenge_decider,
                            parser.remote_user_key,
                          )
       who_api_factory = make_api_factory_with_config(global_conf,
                                                      '/path/to/who.config')
- When it needs to use the API, it must call the ``APIFactory``, passing
  the WSGI environment to it.  The ``APIFactory`` returns an object
repoze/who/config.py
@@ -6,6 +6,7 @@
from pkg_resources import EntryPoint
import sys
from repoze.who.api import APIFactory
from repoze.who.interfaces import IAuthenticator
from repoze.who.interfaces import IChallengeDecider
from repoze.who.interfaces import IChallenger
@@ -132,6 +133,23 @@
           'error': logging.ERROR,
          }
def make_api_factory_with_config(global_conf,
                                 config_file,
                                 remote_user_key = 'REMOTE_USER',
                                 logger=None,
                                ):
    parser = WhoConfig(global_conf['here'])
    parser.parse(open(config_file))
    return APIFactory(parser.identifiers,
                      parser.authenticators,
                      parser.challengers,
                      parser.mdproviders,
                      parser.request_classifier,
                      parser.challenge_decider,
                      remote_user_key,
                      logger,
                     )
def make_middleware_with_config(app, global_conf, config_file,
                                log_file=None, log_level=None):
    parser = WhoConfig(global_conf['here'])
repoze/who/tests/test_config.py
@@ -407,6 +407,68 @@
        self.assertEqual(middleware.logger.getEffectiveLevel(), logging.INFO)
        logging.shutdown()
class Test_make_api_with_config(unittest.TestCase):
    tempdir = None
    def setUp(self):
        pass
    def tearDown(self):
        if self.tempdir is not None:
            import shutil
            shutil.rmtree(self.tempdir)
    def _getFactory(self):
        from repoze.who.config import make_api_factory_with_config
        return make_api_factory_with_config
    def _getTempfile(self, text):
        import os
        import tempfile
        tempdir = self.tempdir = tempfile.mkdtemp()
        path = os.path.join(tempdir, 'who.ini')
        config = open(path, 'w')
        config.write(text)
        config.flush()
        config.close()
        return path
    def test_sample_config_no_logger(self):
        factory = self._getFactory()
        path = self._getTempfile(SAMPLE_CONFIG)
        global_conf = {'here': '/'}
        api_factory = factory(global_conf, config_file=path)
        self.assertEqual(len(api_factory.identifiers), 3)
        self.assertEqual(len(api_factory.authenticators), 1)
        self.assertEqual(len(api_factory.challengers), 2)
        self.assertEqual(len(api_factory.mdproviders), 0)
        self.assertEqual(api_factory.remote_user_key, 'REMOTE_USER')
        self.failUnless(api_factory.logger is None)
    def test_sample_config_w_remote_user_key(self):
        factory = self._getFactory()
        path = self._getTempfile(SAMPLE_CONFIG)
        global_conf = {'here': '/'}
        api_factory = factory(global_conf, config_file=path,
                              remote_user_key = 'X-OTHER-USER')
        self.assertEqual(len(api_factory.identifiers), 3)
        self.assertEqual(len(api_factory.authenticators), 1)
        self.assertEqual(len(api_factory.challengers), 2)
        self.assertEqual(len(api_factory.mdproviders), 0)
        self.assertEqual(api_factory.remote_user_key, 'X-OTHER-USER')
    def test_sample_config_w_logger(self):
        factory = self._getFactory()
        path = self._getTempfile(SAMPLE_CONFIG)
        global_conf = {'here': '/'}
        logger = object()
        api_factory = factory(global_conf, config_file=path, logger=logger)
        self.assertEqual(len(api_factory.identifiers), 3)
        self.assertEqual(len(api_factory.authenticators), 1)
        self.assertEqual(len(api_factory.challengers), 2)
        self.assertEqual(len(api_factory.mdproviders), 0)
        self.failUnless(api_factory.logger is logger)
SAMPLE_CONFIG = """\
[plugin:form]
use = repoze.who.plugins.form:make_plugin