def installUi(self): '''Installs the user interface.''' # Some useful imports from OFS.Folder import manage_addFolder from OFS.Image import manage_addImage, manage_addFile from Products.PythonScripts.PythonScript import PythonScript from Products.PageTemplates.ZopePageTemplate import \ manage_addPageTemplate # Delete the existing folder if it existed. zopeContent = self.app.objectIds() if 'ui' in zopeContent: self.app.manage_delObjects(['ui']) manage_addFolder(self.app, 'ui') # Browse the physical ui folders (the Appy one and an app-specific, if # the app defines one) and create the corresponding objects in the Zope # folder. In the case of files having the same name in both folders, # the one from the app-specific folder is chosen. j = os.path.join uiFolders = [j(j(appy.getPath(), 'gen'), 'ui')] appUi = j(self.config.diskFolder, 'ui') if os.path.exists(appUi): uiFolders.insert(0, appUi) for ui in uiFolders: for root, dirs, files in os.walk(ui): folderName = root[len(ui):] # Get the Zope folder that corresponds to this name zopeFolder = self.app.ui if folderName: for name in folderName.strip(os.sep).split(os.sep): zopeFolder = zopeFolder._getOb(name) # Create sub-folders at this level for name in dirs: if not hasattr(zopeFolder.aq_base, name): manage_addFolder(zopeFolder, name) # Create files at this level for name in files: zopeName, ext = os.path.splitext(name) if ext not in ('.pt', '.py'): # In the ZODB, pages and scripts have their name without # their extension. zopeName = name if hasattr(zopeFolder.aq_base, zopeName): continue f = file(j(root, name)) if ext in gen.File.imageExts: manage_addImage(zopeFolder, zopeName, f) elif ext == '.pt': manage_addPageTemplate(zopeFolder,zopeName,'',f.read()) elif ext == '.py': obj = PythonScript(zopeName) zopeFolder._setObject(zopeName, obj) zopeFolder._getOb(zopeName).write(f.read()) else: manage_addFile(zopeFolder, zopeName, f) f.close() # Update the home page if 'index_html' in zopeContent: self.app.manage_delObjects(['index_html']) manage_addPageTemplate(self.app, 'index_html', '', homePage) # Update the error page if 'standard_error_message' in zopeContent: self.app.manage_delObjects(['standard_error_message']) manage_addPageTemplate(self.app, 'standard_error_message', '',errorPage)
def testPasteMultiSameID( self ): self.failUnless( 'file' in self.folder1.objectIds() ) self.failIf( 'file1' in self.folder1.objectIds() ) manage_addFile(self.folder1, 'file1', file='', content_type='text/plain') self.failIf( 'file2' in self.folder1.objectIds() ) manage_addFile(self.folder1, 'file2', file='', content_type='text/plain') self.failIf( 'file' in self.folder2.objectIds() ) manage_addFile(self.folder2, 'file', file='', content_type='text/plain') self.failIf( 'file1' in self.folder2.objectIds() ) manage_addFile(self.folder2, 'file1', file='', content_type='text/plain') self.failIf( 'file2' in self.folder2.objectIds() ) manage_addFile(self.folder2, 'file2', file='', content_type='text/plain') cookie = self.folder1.manage_copyObjects( ids=('file','file1','file2',) ) result = self.folder2.manage_pasteObjects( cookie ) self.failUnless( 'file' in self.folder1.objectIds() ) self.failUnless( 'file1' in self.folder1.objectIds() ) self.failUnless( 'file2' in self.folder1.objectIds() ) self.failUnless( 'file' in self.folder2.objectIds() ) self.failUnless( 'file1' in self.folder2.objectIds() ) self.failUnless( 'file2' in self.folder2.objectIds() ) self.failUnless( 'copy_of_file' in self.folder2.objectIds() ) self.failUnless( 'copy_of_file1' in self.folder2.objectIds() ) self.failUnless( 'copy_of_file2' in self.folder2.objectIds() ) self.failUnless( result == [{'id':'file', 'new_id':'copy_of_file'}, {'id':'file1', 'new_id':'copy_of_file1'}, {'id':'file2', 'new_id':'copy_of_file2'}])
def _initFolders(self): self.connection = makeConnection() try: r = self.connection.root() a = Application() r['Application'] = a self.root = a responseOut = self.responseOut = cStringIO.StringIO() self.app = makerequest( self.root, stdout=responseOut ) manage_addFolder( self.app, 'folder1' ) manage_addFolder( self.app, 'folder2' ) folder1 = getattr( self.app, 'folder1' ) folder2 = getattr( self.app, 'folder2' ) manage_addFile( folder1, 'file' , file='', content_type='text/plain') # Hack, we need a _p_mtime for the file, so we make sure that it # has one. We use a subtransaction, which means we can rollback # later and pretend we didn't touch the ZODB. transaction.commit() except: self.connection.close() raise transaction.begin() return self.app._getOb( 'folder1' ), self.app._getOb( 'folder2' )
def test_copy_cant_copy_invisible_items(self): # User can view folder1. # User cannot view private file in folder1. # When user copies folder1, the private file should not be copied, # because the user would get the Owner role on the copy, # and be able to view it anyway. from AccessControl.Permissions import add_folders from AccessControl.Permissions import view folder1, folder2 = self._initFolders() manage_addFile(folder1, 'private', file=b'', content_type='text/plain') manage_addFile(folder1, 'public', file=b'', content_type='text/plain') folder1.private.manage_permission(view, roles=(), acquire=0) folder2.manage_permission(add_folders, roles=('Anonymous', ), acquire=1) copy_info = folder2.manage_pasteObjects( self.app.manage_copyObjects('folder1')) new_id = copy_info[0]['new_id'] new_folder = folder2[new_id] # The private item should not be in the copy. self.assertTrue('private' not in new_folder.objectIds()) # There is nothing wrong with copying the public item. self.assertTrue('public' in new_folder.objectIds())
def setUp(self): self.responseOut = cStringIO.StringIO() self.connection = makeConnection() try: r = self.connection.root() a = Application() r['Application'] = a self.root = a self.app = makerequest(self.root, stdout=self.responseOut) try: self.app._delObject(TESTFOLDER_NAME) except AttributeError: pass manage_addFolder(self.app, TESTFOLDER_NAME) folder = getattr( self.app, TESTFOLDER_NAME ) data = string.letters manage_addFile( folder, 'file' , file=data, content_type='text/plain') self.file = folder.file self.data = data # Hack, we need a _p_mtime for the file, so we make sure that it # has one. We use a subtransaction, which means we can rollback # later and pretend we didn't touch the ZODB. transaction.commit() except: self.connection.close() raise
def _initFolders( self ): from Products.CMFCore.PortalFolder import PortalFolder self.connection = makeConnection() try: r = self.connection.root() a = Application() r['Application'] = a self.root = a responseOut = self.responseOut = cStringIO.StringIO() self.app = makerequest( self.root, stdout=responseOut ) self.app._setObject( 'folder1', PortalFolder( 'folder1' ) ) self.app._setObject( 'folder2', PortalFolder( 'folder2' ) ) folder1 = getattr( self.app, 'folder1' ) folder2 = getattr( self.app, 'folder2' ) manage_addFile( folder1, 'file' , file='', content_type='text/plain') # Hack, we need a _p_mtime for the file, so we make sure that it # has one. We use a subtransaction, which means we can rollback # later and pretend we didn't touch the ZODB. get_transaction().commit() except: self.connection.close() raise get_transaction().begin() return self.app._getOb( 'folder1' ), self.app._getOb( 'folder2' )
def _initFolders(self): self.connection = makeConnection() try: r = self.connection.root() a = Application() r['Application'] = a self.root = a responseOut = self.responseOut = io.BytesIO() self.app = makerequest(self.root, stdout=responseOut) manage_addFolder(self.app, 'folder1') manage_addFolder(self.app, 'folder2') folder1 = getattr(self.app, 'folder1') manage_addFile(folder1, 'file', file=b'', content_type='text/plain') # Hack, we need a _p_mtime for the file, so we make sure that it # has one. We use a subtransaction, which means we can rollback # later and pretend we didn't touch the ZODB. transaction.commit() except Exception: transaction.abort() self.connection.close() raise transaction.begin() return self.app._getOb('folder1'), self.app._getOb('folder2')
def test_copy_cant_copy_invisible_items(self): # User can view folder1. # User cannot view private file in folder1. # When user copies folder1, the private file should not be copied, # because the user would get the Owner role on the copy, # and be able to view it anyway. from AccessControl.Permissions import add_folders from AccessControl.Permissions import view folder1, folder2 = self._initFolders() manage_addFile(folder1, 'private', file=b'', content_type='text/plain') manage_addFile(folder1, 'public', file=b'', content_type='text/plain') folder1.private.manage_permission(view, roles=(), acquire=0) folder2.manage_permission(add_folders, roles=('Anonymous',), acquire=1) copy_info = folder2.manage_pasteObjects( self.app.manage_copyObjects('folder1') ) new_id = copy_info[0]['new_id'] new_folder = folder2[new_id] # The private item should not be in the copy. self.assertTrue('private' not in new_folder.objectIds()) # There is nothing wrong with copying the public item. self.assertTrue('public' in new_folder.objectIds())
def testPasteMultiSameID(self): self.assertTrue('file' in self.folder1.objectIds()) self.assertFalse('file1' in self.folder1.objectIds()) manage_addFile(self.folder1, 'file1', file=b'', content_type='text/plain') self.assertFalse('file2' in self.folder1.objectIds()) manage_addFile(self.folder1, 'file2', file=b'', content_type='text/plain') self.assertFalse('file' in self.folder2.objectIds()) manage_addFile(self.folder2, 'file', file=b'', content_type='text/plain') self.assertFalse('file1' in self.folder2.objectIds()) manage_addFile(self.folder2, 'file1', file=b'', content_type='text/plain') self.assertFalse('file2' in self.folder2.objectIds()) manage_addFile(self.folder2, 'file2', file=b'', content_type='text/plain') cookie = self.folder1.manage_copyObjects( ids=('file', 'file1', 'file2',)) result = self.folder2.manage_pasteObjects(cookie) self.assertTrue('file' in self.folder1.objectIds()) self.assertTrue('file1' in self.folder1.objectIds()) self.assertTrue('file2' in self.folder1.objectIds()) self.assertTrue('file' in self.folder2.objectIds()) self.assertTrue('file1' in self.folder2.objectIds()) self.assertTrue('file2' in self.folder2.objectIds()) self.assertTrue('copy_of_file' in self.folder2.objectIds()) self.assertTrue('copy_of_file1' in self.folder2.objectIds()) self.assertTrue('copy_of_file2' in self.folder2.objectIds()) self.assertEqual(result, [ {'id': 'file', 'new_id': 'copy_of_file'}, {'id': 'file1', 'new_id': 'copy_of_file1'}, {'id': 'file2', 'new_id': 'copy_of_file2'}, ])
def test_file_preserves_content_type(self): # Verify that a file's content_type is preserved. # Note that there is some contention between content_type # guessing and the content_type property. data = ( '\n' 'This is not just text\n' 'In a frivolous file test\n' 'But a wise practice.\n' ) ct = 'text/x-haiku' conn = self.db.open() try: app = conn.root()['Application'] manage_addFile(app, 'file', StringIO(data)) app.file.content_type = ct transaction.commit() conn2 = self.db.open() try: app = conn2.root()['Application'] self.assertEqual(app.file.content_type, ct) finally: conn2.close() finally: conn.close()
def _initFolders( self ): from Products.CMFCore.PortalFolder import PortalFolder self.connection = makeConnection() try: r = self.connection.root() a = Application() r['Application'] = a self.root = a responseOut = self.responseOut = cStringIO.StringIO() self.app = makerequest( self.root, stdout=responseOut ) self.app._setObject( 'folder1', PortalFolder( 'folder1' ) ) self.app._setObject( 'folder2', PortalFolder( 'folder2' ) ) folder1 = getattr( self.app, 'folder1' ) folder2 = getattr( self.app, 'folder2' ) manage_addFile( folder1, 'file' , file='', content_type='text/plain') # Hack, we need a _p_mtime for the file, so we make sure that it # has one. We use a subtransaction, which means we can rollback # later and pretend we didn't touch the ZODB. transaction.savepoint(optimistic=True) except: self.connection.close() raise return self.app._getOb( 'folder1' ), self.app._getOb( 'folder2' )
def installUi(self): '''Installs the user interface''' # Some useful imports from OFS.Folder import manage_addFolder from OFS.Image import manage_addImage, manage_addFile # Delete the existing folder if it existed zopeContent = self.app.objectIds() for name in ('ui', self.favIcon): if name in zopeContent: self.app.manage_delObjects([name]) manage_addFolder(self.app, 'ui') # Browse the physical ui folders (the Appy one and an app-specific, if # the app defines one) and create the corresponding objects in the Zope # folder. In the case of files having the same name in both folders, # the one from the app-specific folder is chosen. j = os.path.join uiFolders = [j(j(appy.getPath(), 'gen'), 'ui')] for uiFolder in self.config.appConfig.uiFolders: if uiFolder.startswith('..'): folder = j(os.path.dirname(self.config.diskFolder),uiFolder[3:]) else: folder = j(self.config.diskFolder, uiFolder) if os.path.exists(folder): uiFolders.insert(0, folder) for ui in uiFolders: for root, dirs, files in os.walk(ui): folderName = root[len(ui):] # Get the Zope folder that corresponds to this name zopeFolder = self.app.ui if folderName: for name in folderName.strip(os.sep).split(os.sep): zopeFolder = zopeFolder._getOb(name) # Create sub-folders at this level for name in dirs: if not hasattr(zopeFolder.aq_base, name): manage_addFolder(zopeFolder, name) # Create files at this level for name in files: ext = os.path.splitext(name)[1] if hasattr(zopeFolder.aq_base, name): continue f = file(j(root, name)) if name == self.favIcon: if not hasattr(self.app, name): # Copy it at the root. Else, IE won't notice it. manage_addImage(self.app, name, f) elif ext in gen.File.imageExts: manage_addImage(zopeFolder, name, f) else: manage_addFile(zopeFolder, name, f) f.close() # Update home and error pages from Products.PageTemplates.ZopePageTemplate import \ manage_addPageTemplate if 'index_html' in zopeContent: self.app.manage_delObjects(['index_html']) manage_addPageTemplate(self.app, 'index_html', '', homePage) if 'standard_error_message' in zopeContent: self.app.manage_delObjects(['standard_error_message']) manage_addPageTemplate(self.app, 'standard_error_message', '', errorPage)
def testPasteSpecialName(self): manage_addFile(self.folder1, 'copy_of_', file='', content_type='text/plain') cookie = self.folder1.manage_copyObjects(ids=('copy_of_',)) result = self.folder1.manage_pasteObjects(cookie) self.assertEqual(self.folder1.objectIds(), ['file', 'copy_of_', 'copy2_of_']) self.assertEqual(result, [{'id':'copy_of_', 'new_id':'copy2_of_'}])
def testCopyNewObject(self): self.failIf('newfile' in self.folder1.objectIds()) manage_addFile(self.folder1, 'newfile', file='', content_type='text/plain') cookie = self.folder1.manage_copyObjects(ids=('newfile',)) self.folder2.manage_pasteObjects(cookie) self.failUnless('newfile' in self.folder1.objectIds()) self.failUnless('newfile' in self.folder2.objectIds())
def installUi(self): '''Installs the user interface.''' # Some useful imports. from OFS.Folder import manage_addFolder from OFS.Image import manage_addImage, manage_addFile # Delete the existing folder if it existed. zopeContent = self.app.objectIds() if 'ui' in zopeContent: self.app.manage_delObjects(['ui']) manage_addFolder(self.app, 'ui') # Browse the physical ui folders (the Appy one and an app-specific, if # the app defines one) and create the corresponding objects in the Zope # folder. In the case of files having the same name in both folders, # the one from the app-specific folder is chosen. j = os.path.join uiFolders = [j(j(appy.getPath(), 'gen'), 'ui')] for uiFolder in self.config.appConfig.uiFolders: if uiFolder.startswith('..'): folder = j(os.path.dirname(self.config.diskFolder), uiFolder[3:]) else: folder = j(self.config.diskFolder, uiFolder) if os.path.exists(folder): uiFolders.insert(0, folder) for ui in uiFolders: for root, dirs, files in os.walk(ui): folderName = root[len(ui):] # Get the Zope folder that corresponds to this name zopeFolder = self.app.ui if folderName: for name in folderName.strip(os.sep).split(os.sep): zopeFolder = zopeFolder._getOb(name) # Create sub-folders at this level for name in dirs: if not hasattr(zopeFolder.aq_base, name): manage_addFolder(zopeFolder, name) # Create files at this level for name in files: ext = os.path.splitext(name)[1] if hasattr(zopeFolder.aq_base, name): continue f = file(j(root, name)) if name == 'favicon.ico': if not hasattr(self.app, name): # Copy it at the root. Else, IE won't notice it. manage_addImage(self.app, name, f) elif ext in gen.File.imageExts: manage_addImage(zopeFolder, name, f) else: manage_addFile(zopeFolder, name, f) f.close() # Update the home page if 'index_html' in zopeContent: self.app.manage_delObjects(['index_html']) from Products.PageTemplates.ZopePageTemplate import \ manage_addPageTemplate manage_addPageTemplate(self.app, 'index_html', '', homePage) # Remove the error page. if 'standard_error_message' in zopeContent: self.app.manage_delObjects(['standard_error_message'])
def test_can_handle_non_ascii_URLs(self): from OFS.Image import manage_addFile manage_addFile(self.app, 'täst', u'çöńtêñt'.encode('utf-8')) browser = Testing.testbrowser.Browser() browser.login('manager', 'manager_pass') browser.open('http://localhost/{}'.format(quote('täst'))) self.assertEqual(browser.contents.decode('utf-8'), u'çöńtêñt')
def testPasteMultiSameID(self): self.assertTrue('file' in self.folder1.objectIds()) self.assertFalse('file1' in self.folder1.objectIds()) manage_addFile(self.folder1, 'file1', file=b'', content_type='text/plain') self.assertFalse('file2' in self.folder1.objectIds()) manage_addFile(self.folder1, 'file2', file=b'', content_type='text/plain') self.assertFalse('file' in self.folder2.objectIds()) manage_addFile(self.folder2, 'file', file=b'', content_type='text/plain') self.assertFalse('file1' in self.folder2.objectIds()) manage_addFile(self.folder2, 'file1', file=b'', content_type='text/plain') self.assertFalse('file2' in self.folder2.objectIds()) manage_addFile(self.folder2, 'file2', file=b'', content_type='text/plain') cookie = self.folder1.manage_copyObjects(ids=( 'file', 'file1', 'file2', )) result = self.folder2.manage_pasteObjects(cookie) self.assertTrue('file' in self.folder1.objectIds()) self.assertTrue('file1' in self.folder1.objectIds()) self.assertTrue('file2' in self.folder1.objectIds()) self.assertTrue('file' in self.folder2.objectIds()) self.assertTrue('file1' in self.folder2.objectIds()) self.assertTrue('file2' in self.folder2.objectIds()) self.assertTrue('copy_of_file' in self.folder2.objectIds()) self.assertTrue('copy_of_file1' in self.folder2.objectIds()) self.assertTrue('copy_of_file2' in self.folder2.objectIds()) self.assertEqual(result, [ { 'id': 'file', 'new_id': 'copy_of_file' }, { 'id': 'file1', 'new_id': 'copy_of_file1' }, { 'id': 'file2', 'new_id': 'copy_of_file2' }, ])
def testCopyNewObject(self): self.assertFalse('newfile' in self.folder1.objectIds()) manage_addFile(self.folder1, 'newfile', file=b'', content_type='text/plain') cookie = self.folder1.manage_copyObjects(ids=('newfile', )) self.folder2.manage_pasteObjects(cookie) self.assertTrue('newfile' in self.folder1.objectIds()) self.assertTrue('newfile' in self.folder2.objectIds())
def testPasteSpecialName(self): manage_addFile(self.folder1, 'copy_of_', file=b'', content_type='text/plain') cookie = self.folder1.manage_copyObjects(ids=('copy_of_', )) result = self.folder1.manage_pasteObjects(cookie) self.assertEqual(self.folder1.objectIds(), ['file', 'copy_of_', 'copy2_of_']) self.assertEqual(result, [{'id': 'copy_of_', 'new_id': 'copy2_of_'}])
def test_not_icontentish__acquired(self): from OFS.Image import manage_addFile manage_addFile(self.portal, id='a_file') self.assertTrue('a_file' in self.portal.objectIds()) self.portal.invokeFactory('Document', 'a_page') self.assertTrue('a_page' in self.portal.objectIds()) request = self.layer['request'] request.set('MIGHT_REDIRECT', True) request.traverse('/plone/a_file/a_page') request.traverse('/plone/a_page/a_file')
def process_file_upload(self, file='', REQUEST=None): """ Handles the upload of a new file. @param file: uploaded file @param REQUEST: I{optional} parameter to do the redirect """ if file != '': if hasattr(file, 'filename'): if file.filename != '': manage_addFile(self, '', file) if REQUEST: REQUEST.RESPONSE.redirect('%s/toolbox_html' % self.absolute_url())
def testPasteSingleSameID( self ): self.assertTrue( 'file' in self.folder1.objectIds() ) self.assertFalse( 'file' in self.folder2.objectIds() ) manage_addFile(self.folder2, 'file', file='', content_type='text/plain') cookie = self.folder1.manage_copyObjects( ids=('file',) ) result = self.folder2.manage_pasteObjects( cookie ) self.assertTrue( 'file' in self.folder1.objectIds() ) self.assertTrue( 'file' in self.folder2.objectIds() ) self.assertTrue( 'copy_of_file' in self.folder2.objectIds() ) self.assertTrue( result == [{'id':'file', 'new_id':'copy_of_file'}])
def test_can_handle_non_ascii_URLs(self): from OFS.Image import manage_addFile manage_addFile(self.app, 'täst', u'çöńtêñt'.encode('utf-8')) browser = Testing.testbrowser.Browser() browser.addHeader( 'Authorization', 'basic {}'.format( codecs.encode(b'manager:manager_pass', 'base64').decode())) browser.open('http://localhost/{}'.format(quote('täst'))) self.assertEqual(browser.contents.decode('utf-8'), u'çöńtêñt')
def testPasteMultiSameID(self): self.failUnless('file' in self.folder1.objectIds()) self.failIf('file1' in self.folder1.objectIds()) manage_addFile(self.folder1, 'file1', file='', content_type='text/plain') self.failIf('file2' in self.folder1.objectIds()) manage_addFile(self.folder1, 'file2', file='', content_type='text/plain') self.failIf('file' in self.folder2.objectIds()) manage_addFile(self.folder2, 'file', file='', content_type='text/plain') self.failIf('file1' in self.folder2.objectIds()) manage_addFile(self.folder2, 'file1', file='', content_type='text/plain') self.failIf('file2' in self.folder2.objectIds()) manage_addFile(self.folder2, 'file2', file='', content_type='text/plain') cookie = self.folder1.manage_copyObjects(ids=( 'file', 'file1', 'file2', )) result = self.folder2.manage_pasteObjects(cookie) self.failUnless('file' in self.folder1.objectIds()) self.failUnless('file1' in self.folder1.objectIds()) self.failUnless('file2' in self.folder1.objectIds()) self.failUnless('file' in self.folder2.objectIds()) self.failUnless('file1' in self.folder2.objectIds()) self.failUnless('file2' in self.folder2.objectIds()) self.failUnless('copy_of_file' in self.folder2.objectIds()) self.failUnless('copy_of_file1' in self.folder2.objectIds()) self.failUnless('copy_of_file2' in self.folder2.objectIds()) self.failUnless(result == [{ 'id': 'file', 'new_id': 'copy_of_file' }, { 'id': 'file1', 'new_id': 'copy_of_file1' }, { 'id': 'file2', 'new_id': 'copy_of_file2' }])
def _initFolders(self): from ..PortalFolder import PortalFolder self.app._setObject('folder1', PortalFolder('folder1')) self.app._setObject('folder2', PortalFolder('folder2')) folder1 = getattr(self.app, 'folder1') manage_addFile(folder1, 'file', file='', content_type='text/plain') # Hack, we need a _p_mtime for the file, so we make sure that it # has one. We use a subtransaction, which means we can rollback # later and pretend we didn't touch the ZODB. transaction.savepoint(optimistic=True) return self.app._getOb('folder1'), self.app._getOb('folder2')
def testPasteSingleSameID(self): self.assertTrue('file' in self.folder1.objectIds()) self.assertFalse('file' in self.folder2.objectIds()) manage_addFile(self.folder2, 'file', file='', content_type='text/plain') cookie = self.folder1.manage_copyObjects(ids=('file', )) result = self.folder2.manage_pasteObjects(cookie) self.assertTrue('file' in self.folder1.objectIds()) self.assertTrue('file' in self.folder2.objectIds()) self.assertTrue('copy_of_file' in self.folder2.objectIds()) self.assertTrue(result == [{'id': 'file', 'new_id': 'copy_of_file'}])
def setUp(self): import io import transaction from AccessControl import SecurityManager from AccessControl.SecurityManagement import newSecurityManager from OFS.Application import Application from OFS.Folder import manage_addFolder from OFS.Image import manage_addFile from Testing.makerequest import makerequest from ZODB.DB import DB from ZODB.DemoStorage import DemoStorage s = DemoStorage() self.connection = DB(s).open() try: r = self.connection.root() a = Application() r['Application'] = a self.root = a responseOut = self.responseOut = io.BytesIO() self.app = makerequest(self.root, stdout=responseOut) manage_addFolder(self.app, 'folder1') folder1 = getattr(self.app, 'folder1') setattr(folder1, '+something', 'plus') folder1.all_meta_types = ({ 'name': 'File', 'action': 'manage_addFile', 'permission': 'Add images and files' }, ) manage_addFile(folder1, 'file', file=b'', content_type='text/plain') # Hack, we need a _p_mtime for the file, so we make sure that it # has one. We use a subtransaction, which means we can rollback # later and pretend we didn't touch the ZODB. transaction.commit() except Exception: self.connection.close() raise transaction.begin() self.folder1 = getattr(self.app, 'folder1') self.policy = UnitTestSecurityPolicy() self.oldPolicy = SecurityManager.setSecurityPolicy(self.policy) newSecurityManager(None, self._makeUser().__of__(self.root))
def setUp( self ): import cStringIO import transaction from AccessControl import SecurityManager from AccessControl.SecurityManagement import newSecurityManager from OFS.Application import Application from OFS.Folder import manage_addFolder from OFS.Image import manage_addFile from Testing.makerequest import makerequest from ZODB.DB import DB from ZODB.DemoStorage import DemoStorage s = DemoStorage() self.connection = DB(s).open() try: r = self.connection.root() a = Application() r['Application'] = a self.root = a responseOut = self.responseOut = cStringIO.StringIO() self.app = makerequest( self.root, stdout=responseOut ) manage_addFolder( self.app, 'folder1' ) folder1 = getattr( self.app, 'folder1' ) setattr(folder1, '+something', 'plus') folder1.all_meta_types = \ ({ 'name' : 'File' , 'action' : 'manage_addFile' , 'permission' : 'Add images and files' } , ) manage_addFile( folder1, 'file' , file='', content_type='text/plain') # Hack, we need a _p_mtime for the file, so we make sure that it # has one. We use a subtransaction, which means we can rollback # later and pretend we didn't touch the ZODB. transaction.commit() except: self.connection.close() raise transaction.begin() self.folder1 = getattr( self.app, 'folder1' ) self.policy = UnitTestSecurityPolicy() self.oldPolicy = SecurityManager.setSecurityPolicy( self.policy ) newSecurityManager( None, self._makeUser().__of__( self.root ) )
def test_instance_security(self): # Make sure instances' Zope permission settings are respected from AccessControl.Permissions import view from OFS.Folder import Folder from OFS.Image import manage_addFile folder = Folder('folder') manage_addFile(folder, 'file1') folder.file1.manage_permission(view, ('Manager',), 0) manage_addFile(folder, 'file2') folder.file2.manage_permission(view, ('Manager', 'Anonymous'), 0) faux = FauxResponse() response = self._makeOne(faux) response.setBody(folder) data, method = xmlrpc.client.loads(faux._body) self.assertFalse('file1' in data[0].keys()) self.assertTrue('file2' in data[0].keys())
def test_large_file(self): # Verify that 256K file objects can be serialized/deserialized. # Zope splits files larger than 64K into chunks. data = 'data' * 65536 conn = self.db.open() try: app = conn.root()['Application'] manage_addFile(app, 'file', StringIO(data)) transaction.commit() self.assertEqual(str(app.file), data) conn2 = self.db.open() try: app = conn2.root()['Application'] self.assertEqual(str(app.file), data) transaction.abort() finally: conn2.close() finally: conn.close()
def setUp(self): import io import string import transaction from OFS.Application import Application from OFS.Folder import manage_addFolder from OFS.Image import manage_addFile from Testing.makerequest import makerequest self.responseOut = io.BytesIO() self.connection = makeConnection() try: r = self.connection.root() a = Application() r['Application'] = a self.root = a self.app = makerequest(self.root, stdout=self.responseOut) try: self.app._delObject(TESTFOLDER_NAME) except AttributeError: pass manage_addFolder(self.app, TESTFOLDER_NAME) folder = getattr(self.app, TESTFOLDER_NAME) data = string.ascii_letters.encode('ascii') manage_addFile(folder, 'file', file=data, content_type='text/plain') self.file = folder.file self.data = data # Hack, we need a _p_mtime for the file, so we make sure that it # has one. We use a subtransaction, which means we can rollback # later and pretend we didn't touch the ZODB. transaction.commit() except Exception: self.connection.close() raise
def test_corrected_file_extension(self): # Verify that certain content_types use the correct filename # extension. data = 'Hello, world!' conn = self.db.open() try: app = conn.root()['Application'] manage_addFile(app, 'hello', StringIO(data), content_type='text/plain') manage_addFile(app, 'world.dat', StringIO(data), content_type='text/plain') manage_addFile(app, 'binary_file', StringIO(data), content_type='application/octet-stream') transaction.commit() dir = self.conn.basepath names = os.listdir(dir) self.assert_(('hello.txt') in names, names) self.assert_(('world.dat') in names, names) self.assert_(('hello') not in names, names) self.assert_(('binary_file') in names, names) finally: conn.close()
def setUp( self ): self.connection = makeConnection() try: r = self.connection.root() a = Application() r['Application'] = a self.root = a responseOut = self.responseOut = cStringIO.StringIO() self.app = makerequest( self.root, stdout=responseOut ) manage_addFolder( self.app, 'folder1' ) folder1 = getattr( self.app, 'folder1' ) folder1.all_meta_types = \ ( { 'name' : 'File' , 'action' : 'manage_addFile' , 'permission' : 'Add images and files' } , ) manage_addFile( folder1, 'file' , file='', content_type='text/plain') # Hack, we need a _p_mtime for the file, so we make sure that it # has one. We use a subtransaction, which means we can rollback # later and pretend we didn't touch the ZODB. get_transaction().commit() except: self.connection.close() raise get_transaction().begin() self.folder1 = getattr( self.app, 'folder1' ) self.policy = UnitTestSecurityPolicy() self.oldPolicy = SecurityManager.setSecurityPolicy( self.policy ) newSecurityManager( None, UnitTestUser().__of__( self.root ) )
def setUp(self): self.connection = makeConnection() try: r = self.connection.root() a = Application() r['Application'] = a self.root = a responseOut = self.responseOut = cStringIO.StringIO() self.app = makerequest(self.root, stdout=responseOut) manage_addFolder(self.app, 'folder1') folder1 = getattr(self.app, 'folder1') setattr(folder1, '+something', 'plus') folder1.all_meta_types = \ ( { 'name' : 'File' , 'action' : 'manage_addFile' , 'permission' : 'Add images and files' } , ) manage_addFile(folder1, 'file', file='', content_type='text/plain') # Hack, we need a _p_mtime for the file, so we make sure that it # has one. We use a subtransaction, which means we can rollback # later and pretend we didn't touch the ZODB. transaction.commit() except: self.connection.close() raise transaction.begin() self.folder1 = getattr(self.app, 'folder1') self.policy = UnitTestSecurityPolicy() self.oldPolicy = SecurityManager.setSecurityPolicy(self.policy) newSecurityManager(None, UnitTestUser().__of__(self.root))
def setUp(self): import io import string import transaction from OFS.Application import Application from OFS.Folder import manage_addFolder from OFS.Image import manage_addFile from Testing.makerequest import makerequest self.responseOut = io.BytesIO() self.connection = makeConnection() try: r = self.connection.root() a = Application() r['Application'] = a self.root = a self.app = makerequest(self.root, stdout=self.responseOut) try: self.app._delObject(TESTFOLDER_NAME) except AttributeError: pass manage_addFolder(self.app, TESTFOLDER_NAME) folder = getattr(self.app, TESTFOLDER_NAME) data = string.ascii_letters.encode('ascii') manage_addFile( folder, 'file', file=data, content_type='text/plain') self.file = folder.file self.data = data # Hack, we need a _p_mtime for the file, so we make sure that it # has one. We use a subtransaction, which means we can rollback # later and pretend we didn't touch the ZODB. transaction.commit() except Exception: self.connection.close() raise
def test_file_preserves_content_type(self): # Verify that a file's content_type is preserved. # Note that there is some contention between content_type # guessing and the content_type property. data = ('\n' 'This is not just text\n' 'In a frivolous file test\n' 'But a wise practice.\n') ct = 'text/x-haiku' conn = self.db.open() try: app = conn.root()['Application'] manage_addFile(app, 'file', StringIO(data)) app.file.content_type = ct transaction.commit() conn2 = self.db.open() try: app = conn2.root()['Application'] self.assertEqual(app.file.content_type, ct) finally: conn2.close() finally: conn.close()