def test_schema_exception(self): # Mock FTI fti_mock = DexterityFTI(u"testtype") fti_mock.lookupSchema = Mock(side_effect=AttributeError) fti_mock.behaviors = () self.mock_utility(fti_mock, IDexterityFTI, u'testtype') # Content item item = Item('test') item.portal_type = u"testtype" item.test = u"foo" item.foo = u"bar" SCHEMA_CACHE.clear() self.assertTrue( item.__allow_access_to_unprotected_subobjects__('test', u"foo") ) self.assertTrue( item.__allow_access_to_unprotected_subobjects__('foo', u"bar") ) self.assertTrue( item.__allow_access_to_unprotected_subobjects__('random', u"stuff") )
def test_clear_all_caches(self): class ISchema1(Interface): pass fti1 = DexterityFTI(u"testtype") fti1.lookupSchema = Mock(return_value=ISchema1) self.mock_utility(fti1, IDexterityFTI, name=u"testtype1") fti2 = DexterityFTI(u"testtype") fti2.lookupSchema = Mock(return_value=ISchema1) self.mock_utility(fti2, IDexterityFTI, name=u"testtype2") # reset schemacache counter SCHEMA_CACHE.invalidations = 0 # fill cache should call lookupschema one time schema1 = SCHEMA_CACHE.get(u"testtype1") schema2 = SCHEMA_CACHE.get(u"testtype2") self.assertTrue(schema1 is schema2 is ISchema1) # clear SCHEMA_CACHE.clear() self.assertEqual(SCHEMA_CACHE.invalidations, 2) # check invalidations # fill cache again should call lookupschema one time schema1 = SCHEMA_CACHE.get(u"testtype1") schema2 = SCHEMA_CACHE.get(u"testtype2") self.assertTrue(schema1 is schema2 is ISchema1)
def test_clear_all_caches(self): class ISchema1(Interface): pass # FTI mock fti_mock1 = self.mocker.proxy(DexterityFTI(u"testtype")) self.expect(fti_mock1.lookupSchema()).result(ISchema1).count(2) self.mock_utility(fti_mock1, IDexterityFTI, name=u"testtype1") fti_mock2 = self.mocker.proxy(DexterityFTI(u"testtype")) self.expect(fti_mock2.lookupSchema()).result(ISchema1).count(2) self.mock_utility(fti_mock2, IDexterityFTI, name=u"testtype2") self.replay() # reset schemacache counter SCHEMA_CACHE.invalidations = 0 # fill cache should call lookupschema one time schema1 = SCHEMA_CACHE.get(u"testtype1") schema2 = SCHEMA_CACHE.get(u"testtype2") self.assertTrue(schema1 is schema2 is ISchema1) # clear SCHEMA_CACHE.clear() self.assertEqual(SCHEMA_CACHE.invalidations, 2) # check invalidations # fill cache again should call lookupschema one time schema1 = SCHEMA_CACHE.get(u"testtype1") schema2 = SCHEMA_CACHE.get(u"testtype2") self.assertTrue(schema1 is schema2 is ISchema1)
def test_no_tagged_value(self): # Mock schema model class ITestSchema(Interface): test = zope.schema.TextLine(title=u"Test") # Mock FTI fti_mock = self.mocker.proxy(DexterityFTI(u"testtype")) self.expect(fti_mock.lookupSchema()).result(ITestSchema) self.expect(fti_mock.behaviors).result(tuple()) self.mock_utility(fti_mock, IDexterityFTI, u"testtype") # Content item item = Item("test") item.portal_type = u"testtype" item.test = u"foo" item.foo = u"bar" self.mocker.replay() SCHEMA_CACHE.clear() # Everything allowed self.assertTrue(item.__allow_access_to_unprotected_subobjects__("test", u"foo")) self.assertTrue(item.__allow_access_to_unprotected_subobjects__("foo", u"bar")) # Unknown attributes are allowed self.assertTrue(item.__allow_access_to_unprotected_subobjects__("random", u"stuff"))
def test_no_tagged_value(self): # Mock schema model class ITestSchema(Interface): test = zope.schema.TextLine(title=u"Test") # Mock FTI fti_mock = DexterityFTI(u"testtype") fti_mock.lookupSchema = Mock(return_value=ITestSchema) fti_mock.behaviors = () self.mock_utility(fti_mock, IDexterityFTI, u'testtype') # Content item item = Item('test') item.portal_type = u"testtype" item.test = u"foo" item.foo = u"bar" SCHEMA_CACHE.clear() # Everything allowed self.assertTrue( item.__allow_access_to_unprotected_subobjects__('test', u"foo") ) self.assertTrue( item.__allow_access_to_unprotected_subobjects__('foo', u"bar") ) # Unknown attributes are allowed self.assertTrue( item.__allow_access_to_unprotected_subobjects__('random', u"stuff") )
def test_no_tagged_value(self): # Mock schema model class ITestSchema(Interface): test = zope.schema.TextLine(title=u"Test") # Mock FTI fti_mock = self.mocker.proxy(DexterityFTI(u"testtype")) self.expect(fti_mock.lookupSchema()).result(ITestSchema) self.expect(fti_mock.behaviors).result(tuple()) self.mock_utility(fti_mock, IDexterityFTI, u'testtype') # Content item item = Item('test') item.portal_type = u"testtype" item.test = u"foo" item.foo = u"bar" self.mocker.replay() SCHEMA_CACHE.clear() # Everything allowed self.assertTrue( item.__allow_access_to_unprotected_subobjects__('test', u"foo")) self.assertTrue( item.__allow_access_to_unprotected_subobjects__('foo', u"bar")) # Unknown attributes are allowed self.assertTrue( item.__allow_access_to_unprotected_subobjects__( 'random', u"stuff"))
def test_schema_exception(self): # Mock FTI fti_mock = self.mocker.proxy(DexterityFTI(u"testtype")) self.expect(fti_mock.lookupSchema()).throw(AttributeError) self.expect(fti_mock.behaviors).result(tuple()) self.mock_utility(fti_mock, IDexterityFTI, u'testtype') # Content item item = Item('test') item.portal_type = u"testtype" item.test = u"foo" item.foo = u"bar" self.mocker.replay() SCHEMA_CACHE.clear() self.assertTrue( item.__allow_access_to_unprotected_subobjects__('test', u"foo")) self.assertTrue( item.__allow_access_to_unprotected_subobjects__('foo', u"bar")) self.assertTrue( item.__allow_access_to_unprotected_subobjects__( 'random', u"stuff"))
def migrate_to_pa_event(context): # Install plone.app.event context.runAllImportStepsFromProfile('profile-plone.app.event:default') # Re-import types to get newest Event type context.runImportStepFromProfile( 'profile-plone.app.contenttypes:default', 'typeinfo' ) portal = getSite() migrate(portal, DXOldEventMigrator) SCHEMA_CACHE.clear()
def test_empty_name(self): # Mock FTI fti_mock = DexterityFTI(u"testtype") self.mock_utility(fti_mock, IDexterityFTI, u'testtype') # Content item item = Item('test') item.portal_type = u"testtype" SCHEMA_CACHE.clear() self.assertTrue( item.__allow_access_to_unprotected_subobjects__('', u"foo"))
def test_empty_name(self): # Mock FTI fti_mock = DexterityFTI(u"testtype") self.mock_utility(fti_mock, IDexterityFTI, u'testtype') # Content item item = Item('test') item.portal_type = u"testtype" SCHEMA_CACHE.clear() self.assertTrue( item.__allow_access_to_unprotected_subobjects__('', u"foo") )
def test_no_read_permission(self): # Mock schema model class ITestSchema(Interface): test = zope.schema.TextLine(title=u"Test") ITestSchema.setTaggedValue(READ_PERMISSIONS_KEY, dict(foo='foo.View')) # Mock FTI fti_mock = self.mocker.proxy(DexterityFTI(u"testtype")) self.expect(fti_mock.lookupSchema()).result(ITestSchema) self.expect(fti_mock.behaviors).result(tuple()) self.mock_utility(fti_mock, IDexterityFTI, u'testtype') # Mock permissions self.mock_utility(Permission(u'foo.View', u"View foo"), IPermission, u'foo.View') # Content item item = Item('test') item.portal_type = u"testtype" item.test = u"foo" item.foo = u"bar" # Check permission securityManager_mock = self.mocker.mock() self.expect(securityManager_mock.checkPermission("View foo", item)).result(True) getSecurityManager_mock = self.mocker.replace( 'AccessControl.getSecurityManager') self.expect( getSecurityManager_mock()).result(securityManager_mock).count(1) self.mocker.replay() SCHEMA_CACHE.clear() self.assertTrue( item.__allow_access_to_unprotected_subobjects__('test', u"foo")) self.assertTrue( item.__allow_access_to_unprotected_subobjects__('foo', u"bar")) # Unknown attributes are allowed self.assertTrue( item.__allow_access_to_unprotected_subobjects__( 'random', u"stuff"))
def test_readfile_mimetype_no_message_no_primary_field(self): class ITest(Interface): title = schema.TextLine() SCHEMA_CACHE.clear() fti_mock = DexterityFTI(u'testtype') fti_mock.lookupSchema = Mock(return_value=ITest) fti_mock.behaviors = [] self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype") item = Item('item') item.portal_type = 'testtype' readfile = DefaultReadFile(item) self.assertEqual('text/plain', readfile.mimeType)
def test_empty_name(self): # Mock FTI fti_mock = self.mocker.proxy(DexterityFTI(u"testtype")) self.expect(fti_mock.lookupSchema()).count(0) self.expect(fti_mock.behaviors).count(0) self.mock_utility(fti_mock, IDexterityFTI, u"testtype") # Content item item = Item("test") item.portal_type = u"testtype" self.mocker.replay() SCHEMA_CACHE.clear() self.assertTrue(item.__allow_access_to_unprotected_subobjects__("", u"foo"))
def test_no_read_permission(self): # Mock schema model class ITestSchema(Interface): test = zope.schema.TextLine(title=u"Test") ITestSchema.setTaggedValue(READ_PERMISSIONS_KEY, dict(foo='foo.View')) # Mock FTI fti_mock = DexterityFTI(u"testtype") fti_mock.lookupSchema = Mock(return_value=ITestSchema) fti_mock.behaviors = () self.mock_utility(fti_mock, IDexterityFTI, u'testtype') # Mock permissions self.mock_utility( Permission(u'foo.View', u"View foo"), IPermission, u'foo.View' ) # Content item item = Item('test') item.portal_type = u"testtype" item.test = u"foo" item.foo = u"bar" # Check permission security_manager_mock = Mock() security_manager_mock.checkPermission = Mock(return_value=True) from AccessControl import getSecurityManager self.patch_global( getSecurityManager, return_value=security_manager_mock) SCHEMA_CACHE.clear() self.assertTrue( item.__allow_access_to_unprotected_subobjects__('test', u"foo") ) self.assertTrue( item.__allow_access_to_unprotected_subobjects__('foo', u"bar") ) # Unknown attributes are allowed self.assertTrue( item.__allow_access_to_unprotected_subobjects__('random', u"stuff") )
def test_empty_name(self): # Mock FTI fti_mock = self.mocker.proxy(DexterityFTI(u"testtype")) self.expect(fti_mock.lookupSchema()).count(0) self.expect(fti_mock.behaviors).count(0) self.mock_utility(fti_mock, IDexterityFTI, u'testtype') # Content item item = Item('test') item.portal_type = u"testtype" self.mocker.replay() SCHEMA_CACHE.clear() self.assertTrue( item.__allow_access_to_unprotected_subobjects__('', u"foo"))
def test_no_read_permission(self): # Mock schema model class ITestSchema(Interface): test = zope.schema.TextLine(title=u"Test") ITestSchema.setTaggedValue(READ_PERMISSIONS_KEY, dict(foo='foo.View')) # Mock FTI fti_mock = DexterityFTI(u"testtype") fti_mock.lookupSchema = Mock(return_value=ITestSchema) fti_mock.behaviors = () self.mock_utility(fti_mock, IDexterityFTI, u'testtype') # Mock permissions self.mock_utility(Permission(u'foo.View', u"View foo"), IPermission, u'foo.View') # Content item item = Item('test') item.portal_type = u"testtype" item.test = u"foo" item.foo = u"bar" # Check permission security_manager_mock = Mock() security_manager_mock.checkPermission = Mock(return_value=True) from AccessControl import getSecurityManager self.patch_global(getSecurityManager, return_value=security_manager_mock) SCHEMA_CACHE.clear() self.assertTrue( item.__allow_access_to_unprotected_subobjects__('test', u"foo")) self.assertTrue( item.__allow_access_to_unprotected_subobjects__('foo', u"bar")) # Unknown attributes are allowed self.assertTrue( item.__allow_access_to_unprotected_subobjects__( 'random', u"stuff"))
def test_readfile_mimetype_additional_schemata(self): # This is mostly a test that utils.iterSchemata takes # IBehaviorAssignable into account. class ITest(Interface): title = schema.TextLine() class ITestAdditional(Interface): # Additional behavior on an item body = schema.Text() stuff = schema.Bytes() alsoProvides(ITestAdditional['body'], IPrimaryField) alsoProvides(ITestAdditional['stuff'], IPrimaryField) alsoProvides(ITestAdditional, IFormFieldProvider) class MockBehavior(object): def __init__(self, iface): self.interface = iface class MockBehaviorAssignable(object): def __init__(self, context): self.context = context def enumerateBehaviors(self): yield MockBehavior(ITestAdditional) SCHEMA_CACHE.clear() fti_mock = self.mocker.mock(DexterityFTI) self.expect(fti_mock.lookupSchema()).result(ITest) self.expect(fti_mock.lookupSchema()).result(ITest) self.mock_adapter(MockBehaviorAssignable, IBehaviorAssignable, (Item, )) self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype") item = Item('item') item.portal_type = 'testtype' readfile = DefaultReadFile(item) self.replay() self.assertEquals('message/rfc822', readfile.mimeType)
def test_readfile_mimetype_no_message_no_primary_field(self): class ITest(Interface): title = schema.TextLine() SCHEMA_CACHE.clear() fti_mock = self.mocker.proxy(DexterityFTI(u'testtype')) self.expect(fti_mock.lookupSchema()).result(ITest) self.expect(fti_mock.behaviors).result([]) self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype") item = Item('item') item.portal_type = 'testtype' readfile = DefaultReadFile(item) self.replay() self.assertEqual('text/plain', readfile.mimeType)
def test_readfile_mimetype_no_message_no_primary_field(self): class ITest(Interface): title = schema.TextLine() SCHEMA_CACHE.clear() fti_mock = self.mocker.proxy(DexterityFTI(u"testtype")) self.expect(fti_mock.lookupSchema()).result(ITest) self.expect(fti_mock.behaviors).result([]) self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype") item = Item("item") item.portal_type = "testtype" readfile = DefaultReadFile(item) self.replay() self.assertEqual("text/plain", readfile.mimeType)
def test_readfile_mimetype_no_message_multiple_primary_fields(self): class ITest(Interface): title = schema.TextLine() body = schema.Text() stuff = schema.Bytes() alsoProvides(ITest['body'], IPrimaryField) alsoProvides(ITest['stuff'], IPrimaryField) SCHEMA_CACHE.clear() fti_mock = DexterityFTI(u'testtype') fti_mock.lookupSchema = Mock(return_value=ITest) self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype") item = Item('item') item.portal_type = 'testtype' readfile = DefaultReadFile(item) self.assertEqual('message/rfc822', readfile.mimeType)
def test_readfile_mimetype_no_message_no_fields(self): class ITest(Interface): pass fti_mock = self.mocker.proxy(DexterityFTI(u'testtype')) SCHEMA_CACHE.clear() self.expect(fti_mock.lookupSchema()).result(ITest) self.expect(fti_mock.behaviors).result([]) self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype") item = Item('item') item.portal_type = 'testtype' readfile = DefaultReadFile(item) self.replay() self.assertEqual('text/plain', readfile.mimeType)
def test_readfile_mimetype_additional_schemata(self): # This is mostly a test that utils.iterSchemata takes # IBehaviorAssignable into account. class ITest(Interface): title = schema.TextLine() class ITestAdditional(Interface): # Additional behavior on an item body = schema.Text() stuff = schema.Bytes() alsoProvides(ITestAdditional["body"], IPrimaryField) alsoProvides(ITestAdditional["stuff"], IPrimaryField) alsoProvides(ITestAdditional, IFormFieldProvider) class MockBehavior(object): def __init__(self, iface): self.interface = iface class MockBehaviorAssignable(object): def __init__(self, context): self.context = context def enumerateBehaviors(self): yield MockBehavior(ITestAdditional) SCHEMA_CACHE.clear() fti_mock = self.mocker.proxy(DexterityFTI(u"testtype")) self.expect(fti_mock.lookupSchema()).result(ITest) self.mock_adapter(MockBehaviorAssignable, IBehaviorAssignable, (Item,)) self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype") item = Item("item") item.portal_type = "testtype" readfile = DefaultReadFile(item) self.replay() self.assertEqual("message/rfc822", readfile.mimeType)
def test_no_schema(self): # Mock FTI fti_mock = self.mocker.proxy(DexterityFTI(u"testtype")) self.expect(fti_mock.lookupSchema()).result(None) self.expect(fti_mock.behaviors).result(tuple()) self.mock_utility(fti_mock, IDexterityFTI, u"testtype") # Content item item = Item("test") item.portal_type = u"testtype" item.test = u"foo" item.foo = u"bar" self.mocker.replay() SCHEMA_CACHE.clear() self.assertTrue(item.__allow_access_to_unprotected_subobjects__("test", u"foo")) self.assertTrue(item.__allow_access_to_unprotected_subobjects__("foo", u"bar")) self.assertTrue(item.__allow_access_to_unprotected_subobjects__("random", u"stuff"))
def test_no_read_permission(self): # Mock schema model class ITestSchema(Interface): test = zope.schema.TextLine(title=u"Test") ITestSchema.setTaggedValue(READ_PERMISSIONS_KEY, dict(foo="foo.View")) # Mock FTI fti_mock = self.mocker.proxy(DexterityFTI(u"testtype")) self.expect(fti_mock.lookupSchema()).result(ITestSchema) self.expect(fti_mock.behaviors).result(tuple()) self.mock_utility(fti_mock, IDexterityFTI, u"testtype") # Mock permissions self.mock_utility(Permission(u"foo.View", u"View foo"), IPermission, u"foo.View") # Content item item = Item("test") item.portal_type = u"testtype" item.test = u"foo" item.foo = u"bar" # Check permission securityManager_mock = self.mocker.mock() self.expect(securityManager_mock.checkPermission("View foo", item)).result(True) getSecurityManager_mock = self.mocker.replace("AccessControl.getSecurityManager") self.expect(getSecurityManager_mock()).result(securityManager_mock).count(1) self.mocker.replay() SCHEMA_CACHE.clear() self.assertTrue(item.__allow_access_to_unprotected_subobjects__("test", u"foo")) self.assertTrue(item.__allow_access_to_unprotected_subobjects__("foo", u"bar")) # Unknown attributes are allowed self.assertTrue(item.__allow_access_to_unprotected_subobjects__("random", u"stuff"))
def test_readfile_mimetype_no_message_multiple_primary_fields(self): class ITest(Interface): title = schema.TextLine() body = schema.Text() stuff = schema.Bytes() alsoProvides(ITest["body"], IPrimaryField) alsoProvides(ITest["stuff"], IPrimaryField) SCHEMA_CACHE.clear() fti_mock = self.mocker.proxy(DexterityFTI(u"testtype")) self.expect(fti_mock.lookupSchema()).result(ITest) self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype") item = Item("item") item.portal_type = "testtype" readfile = DefaultReadFile(item) self.replay() self.assertEqual("message/rfc822", readfile.mimeType)
def test_readfile_mimetype_no_message_single_primary_field(self): class ITest(Interface): title = schema.TextLine() body = schema.Text() alsoProvides(ITest['body'], IPrimaryField) SCHEMA_CACHE.clear() fti_mock = self.mocker.proxy(DexterityFTI(u'testtype')) self.expect(fti_mock.lookupSchema()).result(ITest) self.expect(fti_mock.behaviors).result([]) self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype") item = Item('item') item.portal_type = 'testtype' readfile = DefaultReadFile(item) self.replay() self.assertEqual('text/plain', readfile.mimeType)
def test_no_schema(self): # Mock FTI fti_mock = DexterityFTI(u"testtype") fti_mock.lookupSchema = Mock(return_value=None) fti_mock.behaviors = () self.mock_utility(fti_mock, IDexterityFTI, u'testtype') # Content item item = Item('test') item.portal_type = u"testtype" item.test = u"foo" item.foo = u"bar" SCHEMA_CACHE.clear() self.assertTrue( item.__allow_access_to_unprotected_subobjects__('test', u"foo")) self.assertTrue( item.__allow_access_to_unprotected_subobjects__('foo', u"bar")) self.assertTrue( item.__allow_access_to_unprotected_subobjects__( 'random', u"stuff"))
def test_no_schema(self): # Mock FTI fti_mock = self.mocker.proxy(DexterityFTI('testtype')) self.expect(fti_mock.lookupSchema()).result(None) self.expect(fti_mock.behaviors).result(tuple()) self.mock_utility(fti_mock, IDexterityFTI, 'testtype') # Content item item = Item('test') item.portal_type = 'testtype' item.test = 'foo' item.foo = 'bar' self.mocker.replay() SCHEMA_CACHE.clear() self.assertTrue( item.__allow_access_to_unprotected_subobjects__('test', 'foo')) self.assertTrue( item.__allow_access_to_unprotected_subobjects__('foo', 'bar')) self.assertTrue( item.__allow_access_to_unprotected_subobjects__('random', 'stuff'))
def test_item(self): # Mock schema model class ITestSchema(Interface): test = zope.schema.TextLine(title=u"Test") ITestSchema.setTaggedValue(READ_PERMISSIONS_KEY, dict(test="zope2.View", foo="foo.View")) from plone.autoform.interfaces import IFormFieldProvider @provider(IFormFieldProvider) class ITestBehavior(Interface): test2 = zope.schema.TextLine(title=u"Test") ITestBehavior.setTaggedValue(READ_PERMISSIONS_KEY, dict(test2="zope2.View", foo2="foo.View")) # Mock a test behavior from plone.behavior.registration import BehaviorRegistration registration = BehaviorRegistration( title=u"Test Behavior", description=u"Provides test behavior", interface=ITestBehavior, marker=Interface, factory=None, ) from plone.behavior.interfaces import IBehavior self.mock_utility(registration, IBehavior, ITestBehavior.__identifier__) from plone.dexterity.behavior import DexterityBehaviorAssignable from plone.dexterity.interfaces import IDexterityContent from plone.behavior.interfaces import IBehaviorAssignable self.mock_adapter(DexterityBehaviorAssignable, IBehaviorAssignable, (IDexterityContent,)) # Mock FTI fti_mock = self.mocker.proxy(DexterityFTI(u"testtype")) self.mock_utility(fti_mock, IDexterityFTI, u"testtype") # Mock permissions self.mock_utility(Permission(u"zope2.View", u"View"), IPermission, u"zope2.View") self.mock_utility(Permission(u"foo.View", u"View foo"), IPermission, u"foo.View") # Content item item = Item("test") item.portal_type = u"testtype" item.test = u"foo" item.foo = u"bar" # mock security manager securityManager_mock = self.mocker.mock() getSecurityManager_mock = self.mocker.replace("AccessControl.getSecurityManager") # expectations # run 1 # lookupSchema is always called twice: cache and __providedBy__ self.expect(fti_mock.lookupSchema()).result(ITestSchema) self.expect(fti_mock.behaviors).result(tuple()) self.expect(securityManager_mock.checkPermission("View", item)).result(False) # run 2 self.expect(fti_mock.lookupSchema()).result(ITestSchema) self.expect(fti_mock.behaviors).result(tuple()) self.expect(securityManager_mock.checkPermission("View foo", item)).result(True) # run 3 self.expect(fti_mock.lookupSchema()).result(ITestSchema) self.expect(fti_mock.behaviors).result(tuple()) # # run 4 self.expect(fti_mock.lookupSchema()).result(ITestSchema) self.expect(fti_mock.behaviors).result([ITestBehavior.__identifier__]) self.expect(securityManager_mock.checkPermission("View", item)).result(True) # # run 5 self.expect(fti_mock.lookupSchema()).result(None) self.expect(fti_mock.behaviors).result([ITestBehavior.__identifier__]) self.expect(securityManager_mock.checkPermission("View", item)).result(True) # for all 5 runs self.expect(getSecurityManager_mock()).result(securityManager_mock).count(4) self.mocker.replay() # run 1: schema and no behavior access to schema protected attribute SCHEMA_CACHE.clear() self.assertFalse(item.__allow_access_to_unprotected_subobjects__("test", u"foo")) # # run 2: schema and no behavior access to known non schema attribute SCHEMA_CACHE.clear() self.assertTrue(item.__allow_access_to_unprotected_subobjects__("foo", u"bar")) # # run 3: schema and no behavior, unknown attributes are allowed SCHEMA_CACHE.clear() self.assertTrue(item.__allow_access_to_unprotected_subobjects__("random", u"stuff")) # # run 4: schema and behavior SCHEMA_CACHE.clear() self.assertTrue(item.__allow_access_to_unprotected_subobjects__("test2", u"foo2")) # run 5: no schema but behavior SCHEMA_CACHE.clear() self.assertTrue(item.__allow_access_to_unprotected_subobjects__("test2", u"foo2"))
def setUp(self): SCHEMA_CACHE.clear()
def test_item(self): # Mock schema model class ITestSchema(Interface): test = zope.schema.TextLine(title=u"Test") ITestSchema.setTaggedValue(READ_PERMISSIONS_KEY, dict(test='zope2.View', foo='foo.View')) from plone.autoform.interfaces import IFormFieldProvider @provider(IFormFieldProvider) class ITestBehavior(Interface): test2 = zope.schema.TextLine(title=u"Test") ITestBehavior.setTaggedValue(READ_PERMISSIONS_KEY, dict(test2='zope2.View', foo2='foo.View')) # Mock a test behavior from plone.behavior.registration import BehaviorRegistration registration = BehaviorRegistration( title=u"Test Behavior", description=u"Provides test behavior", interface=ITestBehavior, marker=Interface, factory=None) from plone.behavior.interfaces import IBehavior self.mock_utility(registration, IBehavior, ITestBehavior.__identifier__) from plone.dexterity.behavior import DexterityBehaviorAssignable from plone.dexterity.interfaces import IDexterityContent from plone.behavior.interfaces import IBehaviorAssignable self.mock_adapter(DexterityBehaviorAssignable, IBehaviorAssignable, (IDexterityContent, )) # Mock FTI fti_mock = self.mocker.proxy(DexterityFTI(u"testtype")) self.mock_utility(fti_mock, IDexterityFTI, u'testtype') # Mock permissions self.mock_utility(Permission(u'zope2.View', u"View"), IPermission, u'zope2.View') self.mock_utility(Permission(u'foo.View', u"View foo"), IPermission, u'foo.View') # Content item item = Item('test') item.portal_type = u"testtype" item.test = u"foo" item.foo = u"bar" # mock security manager securityManager_mock = self.mocker.mock() getSecurityManager_mock = self.mocker.replace( 'AccessControl.getSecurityManager') # expectations # run 1 # lookupSchema is always called twice: cache and __providedBy__ self.expect(fti_mock.lookupSchema()).result(ITestSchema) self.expect(fti_mock.behaviors).result(tuple()) self.expect(securityManager_mock.checkPermission("View", item)).result(False) # run 2 self.expect(fti_mock.lookupSchema()).result(ITestSchema) self.expect(fti_mock.behaviors).result(tuple()) self.expect(securityManager_mock.checkPermission("View foo", item)).result(True) # run 3 self.expect(fti_mock.lookupSchema()).result(ITestSchema) self.expect(fti_mock.behaviors).result(tuple()) # # run 4 self.expect(fti_mock.lookupSchema()).result(ITestSchema) self.expect(fti_mock.behaviors).result([ITestBehavior.__identifier__]) self.expect(securityManager_mock.checkPermission("View", item)).result(True) # # run 5 self.expect(fti_mock.lookupSchema()).result(None) self.expect(fti_mock.behaviors).result([ITestBehavior.__identifier__]) self.expect(securityManager_mock.checkPermission("View", item)).result(True) # for all 5 runs self.expect( getSecurityManager_mock()).result(securityManager_mock).count(4) self.mocker.replay() # run 1: schema and no behavior access to schema protected attribute SCHEMA_CACHE.clear() self.assertFalse( item.__allow_access_to_unprotected_subobjects__('test', u"foo")) # # run 2: schema and no behavior access to known non schema attribute SCHEMA_CACHE.clear() self.assertTrue( item.__allow_access_to_unprotected_subobjects__('foo', u"bar")) # # run 3: schema and no behavior, unknown attributes are allowed SCHEMA_CACHE.clear() self.assertTrue( item.__allow_access_to_unprotected_subobjects__( 'random', u"stuff")) # # run 4: schema and behavior SCHEMA_CACHE.clear() self.assertTrue( item.__allow_access_to_unprotected_subobjects__('test2', u"foo2")) # run 5: no schema but behavior SCHEMA_CACHE.clear() self.assertTrue( item.__allow_access_to_unprotected_subobjects__('test2', u"foo2"))
def setUp(self): SCHEMA_CACHE.clear() SCHEMA_CACHE.cache_enabled = False
def test_item(self): # Mock schema model class ITestSchema(Interface): test = zope.schema.TextLine(title=u"Test") ITestSchema.setTaggedValue(READ_PERMISSIONS_KEY, dict(test='zope2.View', foo='foo.View')) from plone.autoform.interfaces import IFormFieldProvider @provider(IFormFieldProvider) class ITestBehavior(Interface): test2 = zope.schema.TextLine(title=u"Test") ITestBehavior.setTaggedValue(READ_PERMISSIONS_KEY, dict(test2='zope2.View', foo2='foo.View')) # Mock a test behavior from plone.behavior.registration import BehaviorRegistration registration = BehaviorRegistration( title=u"Test Behavior", description=u"Provides test behavior", interface=ITestBehavior, marker=Interface, factory=None) from plone.behavior.interfaces import IBehavior self.mock_utility(registration, IBehavior, ITestBehavior.__identifier__) from plone.dexterity.behavior import DexterityBehaviorAssignable from plone.dexterity.interfaces import IDexterityContent from plone.behavior.interfaces import IBehaviorAssignable self.mock_adapter(DexterityBehaviorAssignable, IBehaviorAssignable, (IDexterityContent, )) # Mock FTI fti_mock = DexterityFTI(u"testtype") fti_mock.behaviors = () fti_mock.lookupSchema = Mock(return_value=ITestSchema) self.mock_utility(fti_mock, IDexterityFTI, u'testtype') # Mock permissions self.mock_utility(Permission(u'zope2.View', u"View"), IPermission, u'zope2.View') self.mock_utility(Permission(u'foo.View', u"View foo"), IPermission, u'foo.View') # Content item item = Item('test') item.portal_type = u"testtype" item.test = u"foo" item.foo = u"bar" # mock security manager security_manager_mock = Mock() from AccessControl import getSecurityManager self.patch_global(getSecurityManager, return_value=security_manager_mock) # run 1: schema and no behavior access to schema protected attribute security_manager_mock.checkPermission = Mock(return_value=False) SCHEMA_CACHE.clear() self.assertFalse( item.__allow_access_to_unprotected_subobjects__('test', u"foo")) security_manager_mock.checkPermission.assert_called_with('View', item) # run 2: schema and no behavior access to known non schema attribute security_manager_mock.checkPermission = Mock(return_value=True) SCHEMA_CACHE.clear() self.assertTrue( item.__allow_access_to_unprotected_subobjects__('foo', u"bar")) security_manager_mock.checkPermission.assert_called_with( 'View foo', item) # run 3: schema and no behavior, unknown attributes are allowed SCHEMA_CACHE.clear() self.assertTrue( item.__allow_access_to_unprotected_subobjects__( 'random', u"stuff")) # run 4: schema and behavior security_manager_mock.checkPermission = Mock(return_value=True) fti_mock.behaviors = [ITestBehavior.__identifier__] SCHEMA_CACHE.clear() self.assertTrue( item.__allow_access_to_unprotected_subobjects__('test2', u"foo2")) security_manager_mock.checkPermission.assert_called_with('View', item) # run 5: no schema but behavior security_manager_mock.checkPermission = Mock(return_value=True) fti_mock.lookupSchema = Mock(return_value=None) SCHEMA_CACHE.clear() self.assertTrue( item.__allow_access_to_unprotected_subobjects__('test2', u"foo2")) security_manager_mock.checkPermission.assert_called_with('View', item)
def test_item(self): # Mock schema model class ITestSchema(Interface): test = zope.schema.TextLine(title=u"Test") ITestSchema.setTaggedValue( READ_PERMISSIONS_KEY, dict(test='zope2.View', foo='foo.View') ) from plone.autoform.interfaces import IFormFieldProvider @provider(IFormFieldProvider) class ITestBehavior(Interface): test2 = zope.schema.TextLine(title=u"Test") ITestBehavior.setTaggedValue( READ_PERMISSIONS_KEY, dict(test2='zope2.View', foo2='foo.View') ) # Mock a test behavior from plone.behavior.registration import BehaviorRegistration registration = BehaviorRegistration( title=u"Test Behavior", description=u"Provides test behavior", interface=ITestBehavior, marker=Interface, factory=None ) from plone.behavior.interfaces import IBehavior self.mock_utility( registration, IBehavior, ITestBehavior.__identifier__ ) from plone.dexterity.behavior import DexterityBehaviorAssignable from plone.dexterity.interfaces import IDexterityContent from plone.behavior.interfaces import IBehaviorAssignable self.mock_adapter( DexterityBehaviorAssignable, IBehaviorAssignable, (IDexterityContent,) ) # Mock FTI fti_mock = DexterityFTI(u"testtype") fti_mock.behaviors = () fti_mock.lookupSchema = Mock(return_value=ITestSchema) self.mock_utility(fti_mock, IDexterityFTI, u'testtype') # Mock permissions self.mock_utility( Permission(u'zope2.View', u"View"), IPermission, u'zope2.View' ) self.mock_utility( Permission(u'foo.View', u"View foo"), IPermission, u'foo.View' ) # Content item item = Item('test') item.portal_type = u"testtype" item.test = u"foo" item.foo = u"bar" # mock security manager security_manager_mock = Mock() from AccessControl import getSecurityManager self.patch_global( getSecurityManager, return_value=security_manager_mock) # run 1: schema and no behavior access to schema protected attribute security_manager_mock.checkPermission = Mock(return_value=False) SCHEMA_CACHE.clear() self.assertFalse( item.__allow_access_to_unprotected_subobjects__('test', u"foo") ) security_manager_mock.checkPermission.assert_called_with('View', item) # run 2: schema and no behavior access to known non schema attribute security_manager_mock.checkPermission = Mock(return_value=True) SCHEMA_CACHE.clear() self.assertTrue( item.__allow_access_to_unprotected_subobjects__('foo', u"bar") ) security_manager_mock.checkPermission.assert_called_with( 'View foo', item) # run 3: schema and no behavior, unknown attributes are allowed SCHEMA_CACHE.clear() self.assertTrue( item.__allow_access_to_unprotected_subobjects__('random', u"stuff") ) # run 4: schema and behavior security_manager_mock.checkPermission = Mock(return_value=True) fti_mock.behaviors = [ITestBehavior.__identifier__] SCHEMA_CACHE.clear() self.assertTrue( item.__allow_access_to_unprotected_subobjects__('test2', u"foo2") ) security_manager_mock.checkPermission.assert_called_with('View', item) # run 5: no schema but behavior security_manager_mock.checkPermission = Mock(return_value=True) fti_mock.lookupSchema = Mock(return_value=None) SCHEMA_CACHE.clear() self.assertTrue( item.__allow_access_to_unprotected_subobjects__('test2', u"foo2") ) security_manager_mock.checkPermission.assert_called_with('View', item)
def test_provided_by_behavior_subtype(self): # Dummy type class MyItem(Item): pass class IMarkerCustom(Interface): pass # Fake data manager class FauxDataManager(object): def setstate(self, obj): pass def oldstate(self, obj, tid): pass def register(self, obj): pass # Dummy instance item = MyItem(id=u'id') item.portal_type = 'testtype' # Without a persistence jar, the _p_changed check doesn't work. In # this case, the cache is a bit slower. item._p_jar = FauxDataManager() # Dummy schema class ISchema(Interface): foo = zope.schema.TextLine(title=u"foo", default=u"foo_default") bar = zope.schema.TextLine(title=u"bar") # Schema is not implemented by class or provided by instance self.assertFalse(ISchema.implementedBy(MyItem)) self.assertFalse(ISchema.providedBy(item)) # Behaviors - one with a subtype and one without self.mock_adapter( DexterityBehaviorAssignable, IBehaviorAssignable, (IDexterityContent,) ) class IBehavior1(Interface): pass behavior1 = BehaviorRegistration( u"Behavior1", "", IBehavior1, None, None ) self.mock_utility(behavior1, IBehavior, name="behavior1") class IBehavior2(Interface): baz = zope.schema.TextLine(title=u"baz", default=u"baz") class IMarker2(Interface): pass behavior2 = BehaviorRegistration( u"Behavior2", "", IBehavior2, IMarker2, None ) self.mock_utility(behavior2, IBehavior, name="behavior2") # FTI mock fti_mock = Mock(wraps=DexterityFTI(u"testtype")) fti_mock.lookupSchema = Mock(return_value=ISchema) fti_mock.behaviors = ['behavior1', 'behavior2'] self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype") alsoProvides(fti_mock, IDexterityFTI) # start clean SCHEMA_CACHE.clear() # implementedBy does not look into the fti self.assertFalse(ISchema.implementedBy(MyItem)) # Main schema as looked up in FTI is now provided by item ... self.assertTrue(ISchema.providedBy(item)) # behavior1 does not provide a marker, the schema interface # is NOT used as a marker self.assertFalse(IBehavior1.providedBy(item)) # behavior2 provides a marker, so it is used as a marker self.assertTrue(IMarker2.providedBy(item)) # Subtypes provide field defaults. self.assertEqual(u"baz", getattr(item, "baz", None)) # We also need to ensure that the _v_ attribute doesn't hide any # interface set directly on the instance with alsoProvides() or # directlyProvides(). This is done by clearing the cache when these # are invoked. alsoProvides(item, IMarkerCustom) self.assertTrue(IMarkerCustom.providedBy(item)) # after directly setting an interface the main-schema and behavior # interfaces are still there self.assertTrue(ISchema.providedBy(item)) self.assertFalse(IBehavior1.providedBy(item)) self.assertTrue(IMarker2.providedBy(item))
def test_provided_by_behavior_subtype(self): # Dummy type class MyItem(Item): pass class IMarkerCustom(Interface): pass # Fake data manager class FauxDataManager(object): def setstate(self, obj): pass def oldstate(self, obj, tid): pass def register(self, obj): pass # Dummy instance item = MyItem(id=u'id') item.portal_type = 'testtype' # Without a persistence jar, the _p_changed check doesn't work. In # this case, the cache is a bit slower. item._p_jar = FauxDataManager() # Dummy schema class ISchema(Interface): foo = zope.schema.TextLine(title=u"foo", default=u"foo_default") bar = zope.schema.TextLine(title=u"bar") # Schema is not implemented by class or provided by instance self.assertFalse(ISchema.implementedBy(MyItem)) self.assertFalse(ISchema.providedBy(item)) # Behaviors - one with a subtype and one without self.mock_adapter(DexterityBehaviorAssignable, IBehaviorAssignable, (IDexterityContent, )) class IBehavior1(Interface): pass behavior1 = BehaviorRegistration(u"Behavior1", "", IBehavior1, None, None) self.mock_utility(behavior1, IBehavior, name="behavior1") class IBehavior2(Interface): baz = zope.schema.TextLine(title=u"baz", default=u"baz") class IMarker2(Interface): pass behavior2 = BehaviorRegistration(u"Behavior2", "", IBehavior2, IMarker2, None) self.mock_utility(behavior2, IBehavior, name="behavior2") # FTI mock fti_mock = Mock(wraps=DexterityFTI(u"testtype")) fti_mock.lookupSchema = Mock(return_value=ISchema) fti_mock.behaviors = ['behavior1', 'behavior2'] self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype") alsoProvides(fti_mock, IDexterityFTI) # start clean SCHEMA_CACHE.clear() # implementedBy does not look into the fti self.assertFalse(ISchema.implementedBy(MyItem)) # Main schema as looked up in FTI is now provided by item ... self.assertTrue(ISchema.providedBy(item)) # behavior1 does not provide a marker, the schema interface # is NOT used as a marker self.assertFalse(IBehavior1.providedBy(item)) # behavior2 provides a marker, so it is used as a marker self.assertTrue(IMarker2.providedBy(item)) # Subtypes provide field defaults. self.assertEqual(u"baz", getattr(item, "baz", None)) # We also need to ensure that the _v_ attribute doesn't hide any # interface set directly on the instance with alsoProvides() or # directlyProvides(). This is done by clearing the cache when these # are invoked. alsoProvides(item, IMarkerCustom) self.assertTrue(IMarkerCustom.providedBy(item)) # after directly setting an interface the main-schema and behavior # interfaces are still there self.assertTrue(ISchema.providedBy(item)) self.assertFalse(IBehavior1.providedBy(item)) self.assertTrue(IMarker2.providedBy(item))
def setUp(self): SCHEMA_CACHE.clear() provideAdapter(DefaultOrdering) provideAdapter(AttributeAnnotations)