Skip to content
Browse files

Expand ~ and $(envvar} in config file path.

  • Loading branch information...
1 parent 842854e commit 40e68c2edaea5d6f1a35598a4b81b20b94087d77 @tseaver tseaver committed Jan 10, 2011
Showing with 50 additions and 17 deletions.
  1. +2 −2 CHANGES.txt
  2. +20 −6 docs/index.rst
  3. +24 −8 pyramid_who/tests/test_whov2.py
  4. +4 −1 pyramid_who/whov2.py
View
4 CHANGES.txt
@@ -1,7 +1,7 @@
Next release
------------
-0.1 (2011-01-04)
+0.1 (2011-01-10)
----------------
-- Added an authentication policy specifc to ``repoze.who >= 2.0dev``.
+- Initial public version.
View
26 docs/index.rst
@@ -19,6 +19,7 @@ Configure the :class:`WhoV2AuthenticationPolicy` into your :mod:`pyramid`
application via imperative Python code:
.. code-block:: python
+
import os
from pyramid.authorization import ACLAuthorizationPolicy
@@ -52,17 +53,30 @@ or via ZCML:
/>
+Configuration Options
+---------------------
+
``config_file``
- A fully-qualified path to a :mod:`repoze.who` configuration file.
+ *Required*. The path to a :mod:`repoze.who` configuration file. May
+ include ``~`` or ``${envvar}`` constructs, which will be expanded using
+ :func:`os.path.expanduser` and :func:`os.path.expandvars`, respectively.
+ Relative paths will be made absolute using :func:`os.path.abspath`.
+ If the path does not exist, or points to a non-file, the factory
+ function raises an error.
``identifier_id``
- The ID within that file of the :mod:`repoze.who` authentication plugin
- used to "remember" and "forget" authenticated users.
+ *Required*. The ID within that file of the :mod:`repoze.who`
+ authentication plugin used to "remember" and "forget" authenticated users.
+ E.g., if the config file configures the stock ``auth_tkt`` plugin ID
+ ``auth_tkt_plugin``, this value should be the string,
+ ``"auth_tkt_plugin"``.
``callback``
- A function taking ``identity`` (a :mod:`repoze.who` identity mapping)
- and ``request``, and returning a sequence of group IDs for the user, if
- she exists. If not, the callback must return None.
+ *Optional*. A function taking ``identity`` (a :mod:`repoze.who` identity
+ mapping) and ``request``, and returning a sequence of group IDs for the
+ user, if she exists. If not, the callback **must** return None. If no
+ callback is passed, the policy assumes that the user exists, but has
+ no additional groups.
Interaction with :mod:`repoze.who` Middleware
View
32 pyramid_who/tests/test_whov2.py
@@ -33,15 +33,24 @@ def _getTargetClass(self):
from pyramid_who.whov2 import WhoV2AuthenticationPolicy
return WhoV2AuthenticationPolicy
- def _makeOne(self, callback=None):
+ def _makeFile(self, filename, tempdir=None, text=None):
import os
- import tempfile
- tempdir = self._tempdir = tempfile.mkdtemp()
- config_file = os.path.join(tempdir, 'who.ini')
- open(config_file, 'a').close()
+ if tempdir is None:
+ import tempfile
+ tempdir = self._tempdir = tempfile.mkdtemp()
+ result = os.path.join(tempdir, filename)
+ if text is not None:
+ f = open(result, 'w')
+ f.write(text)
+ f.close()
+ return result
+
+ def _makeOne(self, config_file=None, identifier_id='test', callback=None):
+ if config_file is None:
+ config_file = self._makeFile('who.ini', text='')
if callback is None:
- return self._getTargetClass()(config_file, 'testing')
- return self._getTargetClass()(config_file, 'testing', callback)
+ return self._getTargetClass()(config_file, identifier_id)
+ return self._getTargetClass()(config_file, identifier_id, callback)
def _makeRequest(self, **kw):
from pyramid.testing import DummyRequest
@@ -57,6 +66,13 @@ def test_instance_conforms_to_IAuthenticationPolicy(self):
from pyramid.interfaces import IAuthenticationPolicy
verifyObject(IAuthenticationPolicy, self._makeOne())
+ def test_ctor_invalid_config_file_name(self):
+ self.assertRaises(Exception, self._makeOne, '/nonesuch')
+
+ def test_ctor_invalid_config_file_content(self):
+ filename = self._makeFile('not-ini.txt', text='this is not an INI file')
+ self.assertRaises(Exception, self._makeOne, filename)
+
def test_authenticated_userid_no_identity_in_environ(self):
ENVIRON = {'wsgi.version': '1.0',
'HTTP_USER_AGENT': 'testing',
@@ -157,7 +173,7 @@ def test_remember_w_api_in_environ(self):
policy = self._makeOne()
self.assertEqual(policy.remember(request, 'phred'), HEADERS)
self.assertEqual(api._remembered, {'repoze.who.userid': 'phred',
- 'identifier': 'testing',
+ 'identifier': 'test',
})
def test_forget_w_api_in_environ(self):
View
5 pyramid_who/whov2.py
@@ -29,7 +29,10 @@ class WhoV2AuthenticationPolicy(object):
def __init__(self, config_file, identifier_id, callback=_null_callback):
config_file = self._config_file = os.path.abspath(
- os.path.normpath(config_file))
+ os.path.normpath(
+ os.path.expandvars(
+ os.path.expanduser(
+ config_file))))
conf_dir, _ = os.path.split(config_file)
global_conf = {'here': conf_dir}
self._api_factory = APIFactory(global_conf, config_file)

0 comments on commit 40e68c2

Please sign in to comment.
Something went wrong with that request. Please try again.