[Grok-dev] Re: ComponentLookupError with IAuthentication

Christian Klinger cklinger at novareto.de
Thu Jul 3 05:30:39 EDT 2008


Hi Peter,

in some grok.tests it´s important to set the site manually:

Here is an example from the ftests/utlity ...

"""
Local Utilities can be registered on subclasses of grok.Site using
grok.local_utility:

   >>> cave = Cave()
   >>> getRootFolder()["cave"] = cave

   >>> from zope import component
   >>> from zope.app.component.hooks import getSite, setSite
   >>> setSite(cave)


Maybe this helps you

Christian



> Perhaps this is related to the email I sent a few minutes ago about
> running a functional test to create users.
> Now I'm trying the same in a unit test and I get a different error.
> 
> Error in test test_save_user (thegrokway.unit_tests.app.AppTest)
> Traceback (most recent call last):
>   File "/usr/lib/python2.4/unittest.py", line 260, in run
>     testMethod()
>   File "/home/peterbe/dev/GROK/TheGrokWay/src/thegrokway/unit_tests/app.py",
> line 69, in test_save_user
>     user = app.save_user(login, password, name, email)
>   File "/home/peterbe/dev/GROK/TheGrokWay/src/thegrokway/app.py", line
> 133, in save_user
>     pau = getUtility(IAuthentication)
>   File "/home/peterbe/buildout-eggs/zope.component-3.4.0-py2.4.egg/zope/component/_api.py",
> line 207, in getUtility
>     raise ComponentLookupError(interface, name)
> ComponentLookupError: (<InterfaceClass
> zope.app.security.interfaces.IAuthentication>, '')
> 
> 
> The test is really simple:
> 
>     def test_save_user(self):
>         """ save_user is a function that creates a IUser object """
> 
>         login = u'foo'
>         password = u'secret'
>         name = u'Pelle Svansl\xf6s'
>         email = 'pelle at svanslos.com'
>         user = save_user(login, password, name, email)
> 
> 
> And the function save_user() is defined outside the app (which does
> the grok.local_utility() stuff)
> 
> 
> 
> def save_user(login, password, name, email):
>     pau = getUtility(IAuthentication)
>     principals = pau['principals']
>     # create an instance of InternalPrincipal
>     principal = InternalPrincipal(login, password, name,
>                                   passwordManagerName='SHA1')
> 
>     # add principal to principal folder; we may assume that the login
>     # name is unique because of validation on the IUser interface
>     # but to be doubly sure, we assert this
>     assert login not in principals, "Login already in use"
>     principals[login] = principal
>     # save the e-mail
> 
>     user = IUser(principal)
>     user.email = email
>     # grant the user permission to view the member listing
>     permission_mngr = IPrincipalPermissionManager(grok.getSite())
>     permission_mngr.grantPermissionToPrincipal(
>            ADD_RECIPE_PERMISSION,
>            principals.prefix + login)
>     permission_mngr.grantPermissionToPrincipal(
>            EDIT_RECIPE_PERMISSION,
>            principals.prefix + login)
> 
>     return user
> 
> 
> I guess it's because I haven't registered the IAuthentication
> interface as a utility. My application class looks like this::
> 
> class TheGrokWay(grok.Application, grok.Container, Base):
> 
>     # register the authentication utility; see setup_pau for settings
>     grok.local_utility(PluggableAuthentication, IAuthentication,
>                        setup=setup_pau)
> 
> 
> But the function save_user() is defined outside the class. How do I
> register the utility when the application is not involved?
> 
> Peter
> 



More information about the Grok-dev mailing list