Skip to content

Commit

Permalink
Expand ~ and $(envvar} in config file path.
Browse files Browse the repository at this point in the history
  • Loading branch information
tseaver committed Jan 10, 2011
1 parent 842854e commit 40e68c2
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 17 deletions.
4 changes: 2 additions & 2 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -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.
26 changes: 20 additions & 6 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
32 changes: 24 additions & 8 deletions pyramid_who/tests/test_whov2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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',
Expand Down Expand Up @@ -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):
Expand Down
5 changes: 4 additions & 1 deletion pyramid_who/whov2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 40e68c2

Please sign in to comment.