[Grok-dev] ComponentLookupError with IAuthentication

Peter Bengtsson peter at fry-it.com
Thu Jul 3 04:49:28 EDT 2008


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

-- 
Peter Bengtsson,
work www.fry-it.com
home www.peterbe.com
hobby www.issuetrackerproduct.com


More information about the Grok-dev mailing list