File:  [Repository] / ExtFile / tests / ExtFileTestCase.py
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Wed Jan 24 16:53:50 2007 UTC (17 years, 5 months ago) by dwinter
Branches: first, MAIN
CVS tags: release, HEAD
Auf der Basis http://www.zope.org/Members/shh/ExtFile Version 1.5.4

mit zlog ersetzt durch logging


    1: 
    2: from Testing import ZopeTestCase
    3: 
    4: import os, sys, tempfile
    5: 
    6: from Products.ExtFile import transaction
    7: 
    8: # Repository configuration
    9: from Products.ExtFile import ExtFile, Config
   10: ExtFile.ExtFile._repository = ['reposit']
   11: ExtFile.REPOSITORY = Config.FLAT
   12: ExtFile.NORMALIZE_CASE = Config.KEEP
   13: ExtFile.ZODB_PATH = Config.VIRTUAL
   14: ExtFile.REPOSITORY_EXTENSIONS = Config.MIMETYPE_REPLACE
   15: ExtFile.UNDO_POLICY = Config.BACKUP_ON_DELETE
   16:             
   17: # File names of test data
   18: from Globals import package_home
   19: here = package_home(globals())
   20: gifImage = os.path.join(here, 'data', 'Folder_icon.gif')
   21: jpegImage = os.path.join(here, 'data', 'Teneriffa_small.jpg')
   22: tiffImage = os.path.join(here, 'data', 'Mountain_cmyk.tif')
   23: notImage = os.path.join(here, 'data', 'Binary.foo')
   24: 
   25: # Define some permission sets
   26: standard_perms = ZopeTestCase.standard_permissions
   27: copymove_perms = ['View management screens', 'Add ExtImages', 'Add ExtFiles', 'Delete objects']
   28: access_perms = ['View management screens']
   29: change_perms = ['Change ExtFile/ExtImage']
   30: 
   31: # Put some Zope objects into the test ZODB
   32: app = ZopeTestCase.app()
   33: factory = app.manage_addProduct['OFSP']
   34: factory.manage_addImage('GifImage', file=open(gifImage, 'rb'))
   35: factory.manage_addImage('JpegImage', file=open(jpegImage, 'rb'), content_type='image/jpeg')
   36: factory.manage_addImage('TiffImage', file=open(tiffImage, 'rb'), content_type='image/tiff')
   37: factory.manage_addFile('NotImage', file=open(notImage, 'rb'))
   38: transaction.commit()
   39: ZopeTestCase.close(app)
   40: 
   41: # Load ZCML to get events configured
   42: try:
   43:     import Products.Five
   44:     import OFS.subscribers
   45: except ImportError:
   46:     pass
   47: else:
   48:     from Products.Five import zcml
   49:     zcml.load_config('configure.zcml', Products.Five)
   50:             
   51:         
   52: class LocalInstanceHome:
   53:             
   54:     local_home = here #tempfile.gettempdir()
   55:             
   56:     def afterSetUp(self):
   57:         try:
   58:             import App.config
   59:         except ImportError:
   60:             # Modify builtins
   61:             b = getattr(__builtins__, '__dict__', __builtins__)
   62:             self._ih = INSTANCE_HOME
   63:             b['INSTANCE_HOME'] = self.local_home
   64:         else:
   65:             # Zope 2.7+
   66:             cfg = App.config.getConfiguration()
   67:             self._ih = cfg.instancehome
   68:             cfg.instancehome = self.local_home
   69:             App.config.setConfiguration(cfg)
   70: 
   71:     def afterClear(self):
   72:         try:
   73:             import App.config
   74:         except ImportError:
   75:             # Restore builtins
   76:             b = getattr(__builtins__, '__dict__', __builtins__)
   77:             if hasattr(self, '_ih'):
   78:                 b['INSTANCE_HOME'] = self._ih
   79:         else:
   80:             # Zope 2.7+
   81:             cfg = App.config.getConfiguration()
   82:             if hasattr(self, '_ih'):
   83:                 cfg.instancehome = self._ih
   84:             App.config.setConfiguration(cfg)
   85: 
   86: 
   87: class ExtFileTestCase(LocalInstanceHome, ZopeTestCase.ZopeTestCase):
   88:         
   89:     def afterSetUp(self):
   90:         LocalInstanceHome.afterSetUp(self)
   91:         self._nuke_reposit = 1
   92: 
   93:     def afterClear(self):
   94:         if getattr(self, '_nuke_reposit', 0):
   95:             # Remove repository
   96:             repository = os.path.join(INSTANCE_HOME, 'reposit')
   97:             if os.path.isdir(repository): 
   98:                 import shutil
   99:                 shutil.rmtree(repository, 1)
  100:             del self._nuke_reposit
  101:         LocalInstanceHome.afterClear(self)
  102:         
  103:     def _fsname(self, id):
  104:         return os.path.join(INSTANCE_HOME, 'reposit', id)
  105:         
  106:     def _exists(self, id):
  107:         return os.path.isfile(self._fsname(id))
  108:         
  109:     def _listdir(self):
  110:         return os.listdir(os.path.join(INSTANCE_HOME, 'reposit'))
  111:         
  112:     def _fsize(self, id):
  113:         return os.stat(id)[6]
  114:         
  115:     def addExtFile(self, id, file, content_type='', folder=None):
  116:         # Add an ExtFile
  117:         if folder is None:
  118:             folder = self.folder
  119:         id = folder.manage_addProduct['ExtFile'].manage_addExtFile(id=id, file=file, content_type=content_type)
  120:         self.file = folder[id]
  121:         return self.file
  122:         
  123:     def addExtImage(self, id, file, content_type='', folder=None):
  124:         # Add an ExtImage
  125:         if folder is None:
  126:             folder = self.folder
  127:         id = folder.manage_addProduct['ExtFile'].manage_addExtImage(id=id, file=file, content_type=content_type)
  128:         self.image = folder[id]
  129:         return self.image
  130: 
  131: 
  132: # FileUpload factory
  133: from ZPublisher.HTTPRequest import FileUpload
  134: 
  135: class DummyFieldStorage:
  136:     '''Quacks like a FieldStorage'''
  137: 
  138:     def __init__(self, file, filename, headers):
  139:         self.file = file
  140:         self.filename = filename
  141:         self.headers = headers
  142: 
  143: def makeFileUpload(file, content_type='', filename=''):
  144:     headers = {}
  145:     if type(file) == type(''):
  146:         file = open(file, 'rb')
  147:     if content_type:
  148:         headers['content-type'] = content_type
  149:     fs = DummyFieldStorage(file, filename, headers)
  150:     return FileUpload(fs)
  151: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>