Skip to content

Commit

Permalink
Merge pull request #55 from zopefoundation/dataflake/issue_54
Browse files Browse the repository at this point in the history
Respect permission value for utility factory registrations
  • Loading branch information
dataflake authored Mar 18, 2021
2 parents a4f0c3e + 2ad4491 commit 41cafd3
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
5 changes: 4 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
Changes
=========

4.7.0 (unreleased)
5.0.0 (unreleased)
==================

- Respect permission value for utility factory registrations
(`#54 <https://github.com/zopefoundation/zope.component/issues/54>`_)

- Add support for Python 3.9


Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def read(*rnames):

setup(
name='zope.component',
version='4.7.0.dev0',
version='5.0.0.dev0',
url='https://github.com/zopefoundation/zope.component',
project_urls={
'Documentation': 'https://zopecomponent.readthedocs.io/',
Expand Down
37 changes: 37 additions & 0 deletions src/zope/component/tests/test_zcml.py
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,43 @@ class Foo(object):
self.assertEqual(action['discriminator'], None)
self.assertEqual(action['args'], ('', IFoo))

@skipIfNoSecurity
def test_w_factory_w_provides_w_permission(self):
from zope.interface import Interface
from zope.component.interface import provideInterface
from zope.component.zcml import handler
class IFoo(Interface):
pass
class Foo(object):
pass
_cfg_ctx = _makeConfigContext()
self._callFUT(_cfg_ctx, factory=Foo,
provides=IFoo, permission='testing')
self.assertEqual(len(_cfg_ctx._actions), 2)
self.assertEqual(_cfg_ctx._actions[0][0], ())
# Register the utility
action =_cfg_ctx._actions[0][1]
self.assertEqual(action['callable'], handler)
self.assertEqual(action['discriminator'], ('utility', IFoo, ''))
self.assertEqual(action['args'][0], 'registerUtility')
self.assertEqual(action['args'][1], None)
self.assertEqual(action['args'][2], IFoo)
self.assertEqual(action['args'][3], '')
self.assertEqual(action['args'][4], 'TESTING')
fctry = action['kw']['factory']
# Factory is wrapped
self.assertNotEqual(fctry, Foo)
self.assertEqual(fctry.factory, Foo)
secured = fctry()
self.assertIsInstance(secured, Foo)
# Register the provided interface
self.assertEqual(_cfg_ctx._actions[1][0], ())
action =_cfg_ctx._actions[1][1]
self.assertEqual(action['callable'], provideInterface)
self.assertEqual(action['discriminator'], None)
self.assertEqual(action['args'], ('', IFoo))


class Test_interface(unittest.TestCase):

def _callFUT(self, *args, **kw):
Expand Down
6 changes: 5 additions & 1 deletion src/zope/component/zcml.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,11 @@ def utility(_context, provides=None, component=None, factory=None,
name = getName(component)

if permission is not None:
component = proxify(component, provides=provides, permission=permission)
if component:
component = proxify(component, provides=provides,
permission=permission)
if factory:
factory = protectedFactory(factory, provides, permission)

_context.action(
discriminator = ('utility', provides, name),
Expand Down

0 comments on commit 41cafd3

Please sign in to comment.