Permalink
Browse files

Add ZCML directive for the policy.

  • Loading branch information...
1 parent 9e6652d commit 9df7664dd1d3b6ee95c92a8cda364aa4770079ee @tseaver tseaver committed Jan 5, 2011
Showing with 123 additions and 1 deletion.
  1. +1 −1 TODO.txt
  2. +94 −0 pyramid_who/tests/test_zcml.py
  3. +28 −0 pyramid_who/zcml.py
View
@@ -4,7 +4,7 @@
- [X] Implement :class:`pyramid.interfaces.IAuthenticationPolicy` using
the :mod:`repoze.who` 2.0 API.
-- [_] Implement a custom ZCML directive which configures the policy.
+- [X] Implement a custom ZCML directive which configures the policy.
- [X] Document configuring the policy via imperative Python.
@@ -0,0 +1,94 @@
+import unittest
+
+
+class TestRepozeWhowAuthenticationPolicyDirective(unittest.TestCase):
+
+ _tempdir = None
+
+ def setUp(self):
+ from pyramid.testing import setUp
+ self.config = setUp(autocommit=False)
+ self.config._ctx = self.config._make_context()
+
+ def tearDown(self):
+ from pyramid.testing import tearDown
+ tearDown()
+ if self._tempdir is not None:
+ import shutil
+ shutil.rmtree(self._tempdir)
+
+ def _makeWhoConfig(self, name='who.ini'):
+ import os
+ import tempfile
+ tempdir = self._tempdir = tempfile.mkdtemp()
+ config_file = os.path.join(tempdir, 'who.ini')
+ open(config_file, 'w').close()
+ return config_file
+
+ def _callFUT(self, context,
+ config_file=None,
+ identifier_id='IDENTIFIER',
+ callback=None):
+ from pyramid_who.zcml import repozewho2authenticationpolicy
+ if config_file is None:
+ config_file = self._makeWhoConfig()
+ if callback is None:
+ return repozewho2authenticationpolicy(context,
+ config_file,
+ identifier_id)
+ return repozewho2authenticationpolicy(context,
+ config_file,
+ identifier_id,
+ callback)
+
+ def test_it_defaults(self):
+ from pyramid.interfaces import IAuthenticationPolicy
+ from pyramid_who.whov2 import _null_callback
+ reg = self.config.registry
+ context = self.config._ctx
+ self._callFUT(context)
+ actions = extract_actions(context.actions)
+ self.assertEqual(len(actions), 1)
+ regadapt = actions[0]
+ self.assertEqual(regadapt['discriminator'], IAuthenticationPolicy)
+ self.assertEqual(regadapt['callable'], None)
+ self.assertEqual(regadapt['args'], ())
+ policy = reg.getUtility(IAuthenticationPolicy)
+ self.assertEqual(policy._identifier_id, 'IDENTIFIER')
+ self.assertEqual(policy._callback, _null_callback)
+
+ def test_it(self):
+ reg = self.config.registry
+ from pyramid.interfaces import IAuthenticationPolicy
+ context = self.config._ctx
+ def _callback(identity, request):
+ """ """ # hide from coverage
+ config_file = self._makeWhoConfig('firstbase.ini')
+ self._callFUT(context, config_file,
+ 'something', _callback)
+ actions = extract_actions(context.actions)
+ self.assertEqual(len(actions), 1)
+ regadapt = actions[0]
+ self.assertEqual(regadapt['discriminator'], IAuthenticationPolicy)
+ self.assertEqual(regadapt['callable'], None)
+ self.assertEqual(regadapt['args'], ())
+ policy = reg.getUtility(IAuthenticationPolicy)
+ self.assertEqual(policy._config_file, config_file)
+ self.assertEqual(policy._identifier_id, 'something')
+ self.assertEqual(policy._callback, _callback)
+
+
+def extract_actions(native):
+ from zope.configuration.config import expand_action
+ L = []
+ for action in native:
+ (discriminator, callable, args, kw, includepath, info, order
+ ) = expand_action(*action)
+ d = {}
+ d['discriminator'] = discriminator
+ d['callable'] = callable
+ d['args'] = args
+ d['kw'] = kw
+ d['order'] = order
+ L.append(d)
+ return L
View
@@ -0,0 +1,28 @@
+from zope.configuration.fields import GlobalObject
+from zope.interface import Interface
+from zope.schema import ASCIILine
+from zope.schema import TextLine
+
+from pyramid.config import Configurator
+from pyramid_who.whov2 import WhoV2AuthenticationPolicy
+
+
+class IRepozeWho2AuthenticationPolicyDirective(Interface):
+ config_file = ASCIILine(title=u'config_file', required=True)
+ identifier_name = TextLine(title=u'identitfier_name', required=True)
+ callback = GlobalObject(title=u'callback', required=False)
+
+
+def repozewho2authenticationpolicy(_context,
+ config_file,
+ identifier_name,
+ callback=None):
+ if callback is None:
+ policy = WhoV2AuthenticationPolicy(config_file, identifier_name)
+ else:
+ policy = WhoV2AuthenticationPolicy(config_file, identifier_name,
+ callback=callback)
+ # authentication policies must be registered eagerly so they can
+ # be found by the view registration machinery
+ config = Configurator.with_context(_context)
+ config._set_authentication_policy(policy)

0 comments on commit 9df7664

Please sign in to comment.