def test_page_security(self): self.failIf(hasattr(self.dummy1, '__ac_permissions__')) decl = """ <configure xmlns="http://namespaces.zope.org/zope" xmlns:browser="http://namespaces.zope.org/browser"> <browser:page for="Products.Five.tests.dummy.IDummy" class="Products.Five.tests.dummy.DummyView" attribute="foo" name="foo.txt" permission="zope2.ViewManagementScreens" /> </configure> """ zcml.string(decl) request = FakeRequest() view = getView(Dummy1(), 'foo.txt', request) ac = getattr(view, '__ac_permissions__') ex_ac = (('View management screens', ('foo',)),) self.assertEquals(ac, ex_ac) foo_roles = getattr(view, 'foo__roles__', None) self.failIf(foo_roles is None) self.failIf(foo_roles == ()) self.assertEquals(foo_roles.__of__(view), ('Manager',))
def test_page_security(self): decl = """ <configure xmlns="http://namespaces.zope.org/zope" xmlns:browser="http://namespaces.zope.org/browser"> <browser:page for="Products.Five.tests.dummy.IDummy" class="Products.Five.tests.dummy.DummyView" attribute="foo" name="test_page_security" permission="zope2.ViewManagementScreens" /> </configure> """ zcml.load_string(decl) request = FakeRequest() # Wrap into an acquisition so that imPermissionRole objects # can be evaluated. view = getView(Dummy1(), 'test_page_security', request) ac = getattr(view, '__ac_permissions__') # It's protecting the object with the permission, and not the # attribute, so we get ('',) instead of ('foo',). ex_ac = (('View management screens', ('',)),) self.assertEquals(ac, ex_ac) # Wrap into an acquisition so that imPermissionRole objects # can be evaluated. __roles__ is a imPermissionRole object. view = view.__of__(self.folder) view_roles = getattr(view, '__roles__', None) self.failIf(view_roles is None) self.failIf(view_roles == ()) self.assertEquals(view_roles, ('Manager',))
def testViewWithContextArgument(self): # Basically the same as testView, but exercising the context # argument. As this only tests global views, the context # argument is pretty much a no-operation. from zope.component import getView, queryView, getService from zope.component.exceptions import ComponentLookupError self.assertRaises(ComponentLookupError, getView, ob, 'foo', Request(I1), context=ob) self.assertRaises(ComponentLookupError, getView, ob, 'foo', Request(I2), context=ob) self.assertEquals(queryView(ob, 'foo', Request(I2), Test, context=ob), Test) getService(Adapters, ob).register((I1, I2), Interface, 'foo', Comp) c = getView(ob, 'foo', Request(I2), context=ob) self.assertEquals(c.__class__, Comp) self.assertEquals(c.context, ob) self.assertRaises(ComponentLookupError, getView, ob, 'foo2', Request(I1), context=ob) self.assertRaises(ComponentLookupError, getView, ob, 'foo2', Request(I2), context=ob) self.assertEquals(queryView(ob, 'foo2', Request(I2), Test, context=ob), Test) self.assertEquals(queryView(ob, 'foo2', Request(I1), None, context=ob), None)
def testView_w_provided(self): from zope.component import getView, queryView, getService from zope.component.exceptions import ComponentLookupError self.assertRaises(ComponentLookupError, getView, ob, 'foo', Request(I1), providing=I3) self.assertRaises(ComponentLookupError, getView, ob, 'foo', Request(I2), providing=I3) self.assertEquals( queryView(ob, 'foo', Request(I2), Test, providing=I3), Test) getService(Adapters).register([I1, I2], Interface, 'foo', Comp) self.assertRaises(ComponentLookupError, getView, ob, 'foo', Request(I1), providing=I3) self.assertRaises(ComponentLookupError, getView, ob, 'foo', Request(I2), providing=I3) self.assertEquals( queryView(ob, 'foo', Request(I2), Test, providing=I3), Test) getService(Adapters).register([I1, I2], I3, 'foo', Comp) c = getView(ob, 'foo', Request(I2), providing=I3) self.assertEquals(c.__class__, Comp) self.assertEquals(c.context, ob)
def traverse(self, name, furtherPath): context = self._subject __traceback_info__ = (context, name, furtherPath) # Find the REQUEST REQUEST = getattr(context, 'REQUEST', None) if not IBrowserRequest.providedBy(REQUEST): REQUEST = FakeRequest() # Try to lookup a view try: return getView(context, name, REQUEST) except ComponentLookupError: pass
def __getitem__(self, key): key = self.aliases.get(key, key) context = self.context request = self.request for name in self.macro_pages: page = getView(context, name, request) try: v = page[key] except KeyError: pass else: return v raise KeyError, key
def traverse(self, name, furtherPath): context = self._subject __traceback_info__ = (context, name, furtherPath) # Find the REQUEST REQUEST = getattr(context, 'REQUEST', None) if not IBrowserRequest.providedBy(REQUEST): REQUEST = FakeRequest() # Try to lookup a view first try: return getView(context, name, REQUEST) except ComponentLookupError: pass # If a view can't be found, then use default traversable return super(FiveTraversable, self).traverse(name, furtherPath)
def test_five_product_with_no_Products_namespace(self): basket = self._makeOne() from Products.Basket import monkeypatches monkeypatches.patch_findProducts(basket) basket.pdist_fname = os.path.join(self.fixtures, 'pdist-fiveproduct2.txt') sys.path.append(self.fixtures) self.working_set.add_entry(self.fixtures) self.failIf(sys.modules.has_key('fiveproduct2')) productcontext = DummyProductContext('Basket') basket.preinitialize() result = basket.initialize(productcontext) import fiveproduct2 self.failUnless(sys.modules.has_key('fiveproduct2')) from Products.Five.fiveconfigure import findProducts from zope.configuration import xmlconfig products = findProducts() self.assert_(fiveproduct2 in products) # do what Five.loadProduct does sitezcml = """\ <configure xmlns="http://namespaces.zope.org/zope" xmlns:five="http://namespaces.zope.org/five"> <include package="Products.Five" /> <include package="fiveproduct2"/> </configure>""" xmlconfig.string(sitezcml) # verify that the zcml had the correct effect context = None view = getView(object(), 'example_view', FakeRequest()) self.failUnless(view is not None)
def __bobo_traverse__(self, REQUEST, name): """Hook for Zope 2 traversal This method is called by Zope 2's ZPublisher upon traversal. It allows us to trick it into publishing Zope 3-style views. """ if not IBrowserRequest.providedBy(REQUEST): REQUEST = FakeRequest() try: return getView(self, name, REQUEST).__of__(self) except ComponentLookupError: pass try: return getattr(self, name) except AttributeError: pass try: return self[name] except (AttributeError, KeyError): pass return self.__fallback_traverse__(REQUEST, name)