def testRemoveEmptySector(self): from OFS.SimpleItem import SimpleItem survey = self.createSurvey() clientcountry = self.portal.client["nl"] clientcountry["other"] = SimpleItem("other") self.handleSurveyUnpublish(survey, None) self.assertEqual(self.portal.client["nl"].keys(), ["other"])
def test__init__userid_in_request_form_for_manager(self): # should allow for privileged users context = makerequest(SimpleItem('foo')) context.portal_membership = DummyPortalMembership(True) context.REQUEST.form['userid'] = 'bob' adapter = AccountPanelSchemaAdapter(context) self.assertEqual('bob', adapter.context)
def test_standard_error_message_is_called(self): from zExceptions import BadRequest from OFS.SimpleItem import SimpleItem # handle_errors should default to True. It is a flag used for # functional doctests. See ZPublisher/Test.py and # ZPublisher/Publish.py. class REQUEST(object): class RESPONSE(object): handle_errors = True class StandardErrorMessage(object): def __init__(self): self.kw = {} def __call__(self, **kw): self.kw.clear() self.kw.update(kw) item = SimpleItem() item.standard_error_message = sem = StandardErrorMessage() try: raise BadRequest("1") except Exception: item.raise_standardErrorMessage(client=item, REQUEST=REQUEST()) self.assertEqual(sem.kw.get('error_type'), 'BadRequest')
def persistent_load(ref): assert ref == 'ignored' # Return a placeholder object that will be replaced by # removeNonVersionedData(). placeholder = SimpleItem() placeholder.id = "ignored_subobject" return placeholder
def test_setObject_set_owner_with_emergency_user(self): om = self._makeOne() newSecurityManager(None, emergency_user) si = SimpleItem('should_fail') self.assertEqual(si.__ac_local_roles__, None) self.assertRaises(EmergencyUserCannotOwn, om._setObject, 'should_fail', si)
def testPublishedSurvey(self): from OFS.SimpleItem import SimpleItem survey = self.createSurvey() clientsector = self.portal.client["nl"]["ict"] clientsector["other"] = SimpleItem("other") self.handleSurveyUnpublish(survey, None) self.assertEqual(self.portal.client["nl"]["ict"].keys(), ["other"])
def test_setObject_no_set_owner_with_anonymous_user(self): om = self._makeOne() newSecurityManager(None, nobody) si = SimpleItem('should_be_okay') self.assertEqual(si.__ac_local_roles__, None) om._setObject('should_be_okay', si, set_owner=0) self.assertEqual(si.__ac_local_roles__, None)
def test_setObject_set_owner_with_system_user(self): om = self._makeOne() newSecurityManager(None, system) si = SimpleItem('system') self.assertEqual(si.__ac_local_roles__, None) om._setObject('system', si) self.assertEqual(si.__ac_local_roles__, None)
def test__init__userid_in_request_form_for_non_manager(self): # disallow for non-privileged users context = makerequest(SimpleItem('foo')) context.portal_membership = DummyPortalMembership(False) context.REQUEST.form['userid'] = 'bob' adapter = AccountPanelSchemaAdapter(context) self.assertEqual('(authenticated)', adapter.context)
def test_get_hit(self): om = self._makeOne() si1 = SimpleItem('1') om['1'] = si1 got = om.get('1') self.assertTrue(aq_self(got) is si1) self.assertTrue(got.__parent__ is om)
def test_withTwoOnlyOneWired(self): parent = Folder() parent._setObject('plugins', DummyPluginRegistry()) scriptable_plugin = self._makeOne().__of__(parent) faux_method = SimpleItem('faux_method') whatever = SimpleItem('whatever') scriptable_plugin._setObject('faux_method', faux_method) scriptable_plugin._setObject('whatever', whatever) scriptable_plugin.manage_updateInterfaces(['IFaux']) self.assertTrue(IFaux in providedBy(scriptable_plugin))
def test_manage_propertiesForm_allows_adding(self): from OFS.SimpleItem import SimpleItem def _header(*args, **kw): return 'HEADER' def _footer(*args, **kw): return 'HEADER' container = SimpleItem() container.REQUEST = request = DummyRequest() request.set('manage_page_header', _header) request.set('manage_page_footer', _footer) request.set('BASEPATH1', '/one/two') setattr(request, 'URL1', '/one/two') request._steps = ['one', 'two'] prd = {'ac_permissions': ('a', 'b')} container._getProductRegistryData = prd.get a = self._makeOne('extensible').__of__(container) form_html = a.manage_propertiesForm(request) self.assertTrue('value=" Add "' in form_html)
def test___getitem___hit(self): om = self._makeOne() si1 = SimpleItem('1') om['1'] = si1 got = om['1'] self.assertTrue(got.aq_self is si1) self.assertTrue(got.aq_parent is om)
def _makeOne(self, id, since_field=None, until_field=None, caller=None, extra=None, precision_value=None): klass = self._getTargetClass() index = klass(id, since_field, until_field, caller, extra, precision_value=precision_value) class DummyZCatalog(SimpleItem): id = 'DummyZCatalog' # Build pseudo catalog and REQUEST environment catalog = makerequest(DummyZCatalog()) indexes = SimpleItem() indexes = indexes.__of__(catalog) index = index.__of__(indexes) return index
def testBoboTraverseTraversalDefault(self): from OFS.SimpleItem import SimpleItem from ZPublisher.interfaces import UseTraversalDefault class BoboTraversableUseTraversalDefault(SimpleItem): """ A BoboTraversable class which may use "UseTraversalDefault" (dependent on "name") to indicate that standard traversal should be used. """ default = 'Default' def __bobo_traverse__(self, request, name): if name == 'normal': return 'Normal' raise UseTraversalDefault bb = BoboTraversableUseTraversalDefault() # normal access -- no traversal default used self.assertEqual(bb.unrestrictedTraverse('normal'), 'Normal') # use traversal default self.assertEqual(bb.unrestrictedTraverse('default'), 'Default') # test traversal default with acqires attribute si = SimpleItem() si.default_acquire = 'Default_Acquire' si.bb = bb self.assertEqual(si.unrestrictedTraverse('bb/default_acquire'), 'Default_Acquire')
def test_withTwo(self): parent = Folder() parent._setObject('plugins', DummyPluginRegistry()) scriptable_plugin = self._makeOne().__of__(parent) faux_method = SimpleItem('faux_method') two_method = SimpleItem('two_method') scriptable_plugin._setObject('faux_method', faux_method) scriptable_plugin._setObject('two_method', two_method) scriptable_plugin.manage_updateInterfaces(['IFaux', 'IFauxTwo']) self.assertEqual(len(scriptable_plugin.__implements__), 4)
def test_walk_basic(self): from naaya.core.zope2util import ofs_walk fol = Folder('fol').__of__(Folder('root')) self.assertEqual(list(ofs_walk(fol)), []) item = SimpleItem('item') fol._setObject('item', item) self.assertEqual(list(ofs_walk(fol)), [item]) fol2 = Folder('fol2') fol._setObject('fol2', fol2) self.assertEqual(list(ofs_walk(fol)), [item, fol2]) item2 = SimpleItem('item2') fol2._setObject('item2', item2) self.assertEqual(list(ofs_walk(fol)), [item, fol2, item2])
def test_hasId(self): om = self._makeOne() request = {'id': 'test'} self.assertRaises(KeyError, om.manage_hasId, request) si = SimpleItem('test') om._setObject('test', si) om.manage_hasId(request)
def test_setObject_no_set_owner_with_faux_user(self): om = self._makeOne() user = FauxUser('user_id', 'user_login').__of__(FauxRoot()) newSecurityManager(None, user) si = SimpleItem('should_be_okay') self.assertEqual(si.__ac_local_roles__, None) om._setObject('should_be_okay', si, set_owner=0) self.assertEqual(si.__ac_local_roles__, None)
def test_withTwo(self): parent = Folder() parent._setObject('plugins', DummyPluginRegistry()) scriptable_plugin = self._makeOne().__of__(parent) faux_method = SimpleItem('faux_method') two_method = SimpleItem('two_method') scriptable_plugin._setObject('faux_method', faux_method) scriptable_plugin._setObject('two_method', two_method) scriptable_plugin.manage_updateInterfaces(['IFaux', 'IFauxTwo']) self.failUnless(IFaux in providedBy(scriptable_plugin)) self.failUnless(IFauxTwo in providedBy(scriptable_plugin))
def test_setObject_set_owner_with_faux_user(self): om = self._makeOne() user = FauxUser('user_id', 'user_login').__of__(FauxRoot()) newSecurityManager(None, user) si = SimpleItem('faux_creation') self.assertEqual(si.__ac_local_roles__, None) om._setObject('faux_creation', si) self.assertEqual(si.__ac_local_roles__, {'user_id': ['Owner']})
def test_setObject_checkId_bad(self): om = self._makeOne() si = SimpleItem('111') om._setObject('111', si) si = SimpleItem('2') self.assertRaises(BadRequest, om._setObject, 123, si) self.assertRaises(BadRequest, om._setObject, 'a\x01b', si) self.assertRaises(BadRequest, om._setObject, 'a\\b', si) self.assertRaises(BadRequest, om._setObject, 'a:b', si) self.assertRaises(BadRequest, om._setObject, 'a;b', si) self.assertRaises(BadRequest, om._setObject, '.', si) self.assertRaises(BadRequest, om._setObject, '..', si) self.assertRaises(BadRequest, om._setObject, '_foo', si) self.assertRaises(BadRequest, om._setObject, 'aq_me', si) self.assertRaises(BadRequest, om._setObject, 'bah__', si) self.assertRaises(BadRequest, om._setObject, '111', si) self.assertRaises(BadRequest, om._setObject, 'REQUEST', si) self.assertRaises(BadRequest, om._setObject, '/', si)
def test_stdout(self): # You can pass a stdout arg and it's used by the response. import io out = io.BytesIO() item = makerequest(SimpleItem(), stdout=out) item.REQUEST.RESPONSE.write('aaa') out.seek(0) written = out.read() self.assertTrue(written.startswith('Status: 200 OK\r\n')) self.assertTrue(written.endswith('\naaa'))
def test_stdout(self): # You can pass a stdout arg and it's used by the response. import cStringIO out = cStringIO.StringIO() item = makerequest(SimpleItem(), stdout=out) item.REQUEST.RESPONSE.write('aaa') out.seek(0) written = out.read() self.failUnless(written.startswith('Status: 200 OK\n')) self.failUnless(written.endswith('\naaa'))
def test_migrate_members_default_layout(self): members = self.portal['Members'] from OFS.SimpleItem import SimpleItem members._setOb('index_html', SimpleItem()) self.assertIsNotNone(members.get('index_html', None)) from plone.app.upgrade.v50.alphas import migrate_members_default_view migrate_members_default_view(self.portal) self.assertIsNone(members.get('index_html', None)) self.assertEqual(members.getLayout(), '@@member-search')
def test_manage_get_sortedObjects_quote_id(self): # manage_get_sortedObjects now returns a urlquoted version # of the object ID to create correct links in the ZMI om = self._makeOne() hash_id = '#999' om._setObject(hash_id, SimpleItem(hash_id)) result = om.manage_get_sortedObjects('id', 'asc') self.assertEqual(len(result), 1) self.assertEqual(result[0]['id'], hash_id) self.assertEqual(result[0]['quoted_id'], quote(hash_id))
def _initRegistry(self, plugin_type_info=(), plugins={}): from OFS.Folder import Folder from OFS.SimpleItem import SimpleItem from Products.PluginRegistry.PluginRegistry import PluginRegistry app = Folder() app.getPhysicalPath = lambda: () app.getPhysicalRoot = lambda: app app._setObject('foo_plugin_1', SimpleItem()) app._setObject('foo_plugin_2', SimpleItem()) registry = PluginRegistry(plugin_type_info) registry._plugins = {} # it is usually lazy for plugin_type, registered in plugins.items(): registry._plugins[plugin_type] = registered app._setObject('plugin_registry', registry) registry = app._getOb('plugin_registry') return app, registry
def testNotFoundIsRaised(self): from OFS.SimpleItem import SimpleItem from zExceptions import NotFound from operator import getitem self.folder1._setObject('foo', SimpleItem('foo')) self.assertRaises(AttributeError, getitem, self.folder1.foo, 'doesntexist') self.assertRaises(NotFound, self.folder1.unrestrictedTraverse, 'foo/doesntexist') self.assertRaises(TypeError, getitem, self.folder1.foo.isPrincipiaFolderish, 'doesntexist') self.assertRaises(NotFound, self.folder1.unrestrictedTraverse, 'foo/isPrincipiaFolderish/doesntexist')
def test_setObject_checkId_bad(self): om = self._makeOne() si = SimpleItem('111') om._setObject('111', si) si = SimpleItem('2') self.assertRaises(BadRequest, om._setObject, 123, si) self.assertRaises(BadRequest, om._setObject, 'a\x01b', si) self.assertRaises(BadRequest, om._setObject, '.', si) self.assertRaises(BadRequest, om._setObject, '..', si) self.assertRaises(BadRequest, om._setObject, '_foo', si) self.assertRaises(BadRequest, om._setObject, 'aq_me', si) self.assertRaises(BadRequest, om._setObject, 'bah__', si) self.assertRaises(BadRequest, om._setObject, '111', si) self.assertRaises(BadRequest, om._setObject, 'REQUEST', si) self.assertRaises(BadRequest, om._setObject, '/', si) self.assertRaises(BadRequest, om._setObject, 'get', si) self.assertRaises(BadRequest, om._setObject, 'items', si) self.assertRaises(BadRequest, om._setObject, 'keys', si) self.assertRaises(BadRequest, om._setObject, 'values', si) self.assertRaises(BadRequest, om._setObject, 'foo&bar', si) self.assertRaises(BadRequest, om._setObject, 'foo>bar', si) self.assertRaises(BadRequest, om._setObject, 'foo<bar', si) self.assertRaises(BadRequest, om._setObject, 'foo/bar', si)
def test_restrict_traversal(self): from naaya.core.zope2util import ofs_walk fol = Folder('fol').__of__(Folder('root')) fol2 = Folder('fol2') item = SimpleItem('item') item2 = SimpleItem('item2') fol._setObject('item', item) fol._setObject('fol2', fol2) fol2._setObject('item2', item2) class IMyContainer(interface.Interface): pass class IMyCont2(interface.Interface): pass # we call with 2 interfaces to make sure "OR" logic is applied walk = lambda: list(ofs_walk(fol, containers=[IMyContainer, IMyCont2])) self.assertEqual(walk(), [item, fol2]) interface.alsoProvides(fol2, IMyContainer) self.assertEqual(walk(), [item, fol2, item2])