toutpt
toutpt

Reputation: 5220

How to setup plone tests using Products.membrane addon

Context: In my current project I'm trying to setup tests environ using plone.app.testing. The code is available on github: https://github.com/collective/collective.rcse

I'm trying to add some members in the setup but it raises the following exception:

Traceback (most recent call last):
  File "/Uses/toutpt/myproject/buildout-cache/eggs/zope.testing-3.9.7-py2.7.egg/zope/testing/testrunner/runner.py", line 366, in run_layer
    setup_layer(options, layer, setup_layers)
  File "/Uses/toutpt/myproject/buildout-cache/eggs/zope.testing-3.9.7-py2.7.egg/zope/testing/testrunner/runner.py", line 628, in setup_layer
    setup_layer(options, base, setup_layers)
  File "/Uses/toutpt/myproject/buildout-cache/eggs/zope.testing-3.9.7-py2.7.egg/zope/testing/testrunner/runner.py", line 633, in setup_layer
    layer.setUp()
  File "/Uses/toutpt/myproject/buildout-cache/eggs/plone.app.testing-4.2.2-py2.7.egg/plone/app/testing/helpers.py", line 343, in setUp
    self.setUpPloneSite(portal)
  File "/Uses/toutpt/myproject/src/collective.rcse/collective/rcse/testing.py", line 71, in setUpPloneSite
    self.create_user(portal, "simplemember1")
  File "/Uses/toutpt/myproject/src/collective.rcse/collective/rcse/testing.py", line 82, in create_user
    regtool.addMember(username, username)
  File "<string>", line 10, in addMember
  File "/Uses/toutpt/myproject/buildout-cache/eggs/plone.protect-2.0.2-py2.7.egg/plone/protect/utils.py", line 46, in _curried
    return callable(*args, **kw)
  File "<string>", line 10, in addMember
  File "/Uses/toutpt/myproject/buildout-cache/eggs/AccessControl-3.0.8-py2.7-macosx-10.6-x86_64.egg/AccessControl/requestmethod.py", line 70, in _curried
    return callable(*args, **kw)
  File "/Uses/toutpt/myproject/buildout-cache/eggs/Products.CMFCore-2.2.7-py2.7.egg/Products/CMFCore/RegistrationTool.py", line 160, in addMember
    membership.addMember(id, password, roles, domains, properties)
  File "/Uses/toutpt/myproject/buildout-cache/eggs/Products.PlonePAS-4.1.1-py2.7.egg/Products/PlonePAS/tools/membership.py", line 136, in addMember
    acl_users._doAddUser(id, password, roles, domains)
  File "/Uses/toutpt/myproject/buildout-cache/eggs/Products.PlonePAS-4.1.1-py2.7.egg/Products/PlonePAS/pas.py", line 42, in _doAddUser
    retval = _old_doAddUser(self, login, password, roles, domains)
  File "/Uses/toutpt/myproject/buildout-cache/eggs/Products.PluggableAuthService-1.10.0-py2.7.egg/Products/PluggableAuthService/PluggableAuthService.py", line 1004, in _doAddUser
    if useradder.doAddUser( login, password ):
  File "/Uses/toutpt/myproject/buildout-cache/eggs/Products.membrane-2.1.9-py2.7.egg/Products/membrane/plugins/usermanager.py", line 283, in doAddUser
    adder = getCurrentUserAdder(self)
  File "/Uses/toutpt/myproject/buildout-cache/eggs/Products.membrane-2.1.9-py2.7.egg/Products/membrane/utils.py", line 46, in getCurrentUserAdder
    name, adder = adders.next()
  File "/Uses/toutpt/myproject/buildout-cache/eggs/zope.component-3.9.5-py2.7.egg/zope/component/registry.py", line 172, in getUtilitiesFor
    for name, utility in self.utilities.lookupAll((), interface):
  File "/Uses/toutpt/myproject/buildout-cache/eggs/five.localsitemanager-2.0.5-py2.7.egg/five/localsitemanager/registry.py", line 77, in _uncached_lookupAll
    tmp_result[k] = _wrap(v, registry)
  File "/Uses/toutpt/myproject/buildout-cache/eggs/five.localsitemanager-2.0.5-py2.7.egg/five/localsitemanager/registry.py", line 143, in _wrap
    registry_site = registry_site.__parent__
AttributeError: 'BaseGlobalComponents' object has no attribute '__parent__'

This trace show membrane is called to add a member but when it tries to find the IUserAdder components, the component registry raise an exception in the following code:

def _wrap(comp, registry):
    """Return an aq wrapped component with the site as the parent but
    only if the comp has an aq wrapper to begin with.
    """

    # If component is stored as a ComponentPathWrapper, we traverse to
    # the component using the stored path:
    if isinstance(comp, ComponentPathWrapper):
        comp = getSite().unrestrictedTraverse(comp.path)
        if IAcquirer.providedBy(comp):
            return _rewrap(comp)
        else:
            return comp

    # BBB: The primary reason for doing this sort of wrapping of
    # returned utilities is to support CMF tool-like functionality where
    # a tool expects its aq_parent to be the portal object. New code
    # (ie new utilities) should not rely on this predictability to
    # get the portal object and should search out an alternate means
    # (possibly retrieve the ISiteRoot utility). Although in most
    # cases getting at the portal object shouldn't be the required pattern
    # but instead looking up required functionality via other (possibly
    # local) components.

    if registry.__bases__ and IAcquirer.providedBy(comp):
        current_site = getSite()
        registry_site = Acquisition.aq_base(registry.__parent__)
        if not ISite.providedBy(registry_site):
            registry_site = registry_site.__parent__

        ...

And during tests, registry_site has no parent. Here is what I have tried using PDB:

(Pdb) registry_site
<BaseGlobalComponents test-stack-3>
(Pdb) ISite.providedBy(registry_site)
False
(Pdb) registry
<zope.component.globalregistry.GlobalAdapterRegistry object at 0x105499990>
(Pdb) getSite()
<PloneSite at /plone>
(Pdb) Acquisition.aq_base(registry.__parent__)
<BaseGlobalComponents test-stack-3>
(Pdb) registry.__bases__
(<zope.component.globalregistry.GlobalAdapterRegistry object at 0x1049238d0>,)
(Pdb) registry
<zope.component.globalregistry.GlobalAdapterRegistry object at 0x105499990>

So because it happens only during testing it means I have to add something in the tests setup.

Upvotes: 2

Views: 169

Answers (2)

Gagaro
Gagaro

Reputation: 782

We found the solution.

The custom user adder we use was a SimpleItem without being persistent. five.localsitemanager was trying to acquire the parent of something actually not acquirable. Changing it to an object worked.

cf: https://github.com/collective/collective.rcse/commit/f05d9e92bf4578ca82099eec714743903d181173

No clue of why it works outside of testing though.

Upvotes: 2

Asko Soukka
Asko Soukka

Reputation: 1793

This should be pretty much how we have set it up:

def setUpZope(self, app, configurationContext):
    import collective.indexing
    import Products.membrane

    self.loadZCML(package=collective.indexing)
    self.loadZCML(package=Products.membrane)

    z2.installProduct(app, 'collective.indexing')
    z2.installProduct(app, 'Products.membrane')

    # + your dexterity.membrane product setup

def setUpPloneSite(self, portal):
    from zope.publisher.browser import TestRequest
    from zope.globalrequest import setRequest
    request = TestRequest()
    setRequest(request)

    # + your dexterity.membrane product policy
    # + create (and reindex) content (with dexterity.membrane)

    import transaction
    transaction commit()

Yet, when everythin else fails, and you are trying to do functional testing, and you are using <includeDependencies />-directive in your package, you can re-enable z3c.autoinclude for your fixture in setUpZope with:

    # Enable z3c.autoinclude
    configurationContext._features = set([
        feature for feature in configurationContext._features
        if feature != "disable-autoinclude"
    ])

Upvotes: 3

Related Questions