Back to index

plone3  3.1.7
test_exportimport.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2005 Zope Corporation and Contributors. All Rights
00004 # Reserved.
00005 #
00006 # This software is subject to the provisions of the Zope Public License,
00007 # Version 2.0 (ZPL).  A copy of the ZPL should accompany this
00008 # distribution.
00009 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00010 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00011 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00012 # FOR A PARTICULAR PURPOSE
00013 #
00014 ##############################################################################
00015 """ Unit tests for GenericSetup-based export / import of PAS.
00016 
00017 $Id: test_exportimport.py 74681 2007-04-23 15:34:38Z tseaver $
00018 """
00019 import unittest
00020 from csv import reader
00021 from ConfigParser import ConfigParser
00022 from StringIO import StringIO
00023 
00024 try:
00025     import Products.GenericSetup
00026 except ImportError:  # No GenericSetup, so no tests
00027 
00028     print 'XXXX:  No GenericSetup!'
00029     def test_suite():
00030         return unittest.TestSuite()
00031 
00032 else:
00033     from Products.GenericSetup.tests.common import BaseRegistryTests
00034     from Products.PluggableAuthService.tests.utils import \
00035             _setUpDefaultTraversable
00036 
00037     from zope.interface import Interface
00038 
00039     try:
00040         from zope.app.testing.placelesssetup import PlacelessSetup
00041     except ImportError:
00042         # BBB for Zope 2.8
00043         from zope.app.tests.placelesssetup import PlacelessSetup
00044 
00045     class _TestBase(PlacelessSetup, BaseRegistryTests):
00046 
00047         def _initPAS(self, plugin_type_info=(), plugins={}):
00048             from OFS.Folder import Folder
00049             from OFS.SimpleItem import SimpleItem
00050             from Products.PluggableAuthService.PluggableAuthService \
00051                 import addPluggableAuthService
00052 
00053             app = Folder()
00054             app.getPhysicalPath = lambda: ()
00055             app.getPhysicalRoot = lambda: app
00056 
00057             addPluggableAuthService(app)
00058             pas = app._getOb('acl_users')
00059 
00060             return app, pas
00061 
00062     class Test_exportPAS(_TestBase):
00063 
00064         def _setUpAdapters(self):
00065             from zope.app.testing import ztapi
00066 
00067             try:
00068                 from OFS.interfaces import IObjectManager
00069                 from OFS.interfaces import ISimpleItem
00070                 from OFS.interfaces import IPropertyManager
00071             except ImportError: # BBB
00072                 from Products.Five.interfaces import IObjectManager
00073                 from Products.Five.interfaces import ISimpleItem
00074                 from Products.Five.interfaces import IPropertyManager
00075 
00076             from Products.GenericSetup.interfaces import IContentFactoryName
00077             from Products.GenericSetup.interfaces import ICSVAware
00078             from Products.GenericSetup.interfaces import IDAVAware
00079             from Products.GenericSetup.interfaces import IFilesystemExporter
00080             from Products.GenericSetup.interfaces import IINIAware
00081             from Products.GenericSetup.content import \
00082                 FolderishExporterImporter
00083             from Products.GenericSetup.content import \
00084                 SimpleINIAware
00085             from Products.GenericSetup.content import \
00086                 CSVAwareFileAdapter
00087             from Products.GenericSetup.content import \
00088                 INIAwareFileAdapter
00089             from Products.GenericSetup.content import \
00090                 DAVAwareFileAdapter
00091 
00092             from Products.PluginRegistry.interfaces import IPluginRegistry
00093             from Products.PluginRegistry.exportimport \
00094                 import PluginRegistryFileExportImportAdapter
00095 
00096             from Products.PluggableAuthService.exportimport \
00097                 import PAS_CF_Namer
00098 
00099             ztapi.provideAdapter(IObjectManager,
00100                                  IFilesystemExporter,
00101                                  FolderishExporterImporter,
00102                                 )
00103 
00104             ztapi.provideAdapter(IPropertyManager,
00105                                  IINIAware,
00106                                  SimpleINIAware,
00107                                 )
00108 
00109             ztapi.provideAdapter(ICSVAware,
00110                                  IFilesystemExporter,
00111                                  CSVAwareFileAdapter,
00112                                 )
00113 
00114             ztapi.provideAdapter(IINIAware,
00115                                  IFilesystemExporter,
00116                                  INIAwareFileAdapter,
00117                                 )
00118 
00119             ztapi.provideAdapter(IDAVAware,
00120                                  IFilesystemExporter,
00121                                  DAVAwareFileAdapter,
00122                                 )
00123 
00124             ztapi.provideAdapter(IPluginRegistry,
00125                                  IFilesystemExporter,
00126                                  PluginRegistryFileExportImportAdapter,
00127                                 )
00128 
00129             ztapi.provideAdapter(IPluginRegistry,
00130                                  IContentFactoryName,
00131                                  PAS_CF_Namer,
00132                                 )
00133 
00134         def test_empty(self):
00135             from Products.GenericSetup.utils import _getDottedName
00136             from Products.GenericSetup.tests.common import DummyExportContext
00137             from Products.PluginRegistry.PluginRegistry import PluginRegistry
00138             from Products.PluggableAuthService.exportimport import exportPAS
00139 
00140             _setUpDefaultTraversable()
00141 
00142             self._setUpAdapters()
00143             app, pas = self._initPAS()
00144             context = DummyExportContext(pas)
00145             exportPAS(context)
00146 
00147             self.assertEqual(len(context._wrote), 3)
00148             filename, text, content_type = context._wrote[0]
00149             self.assertEqual(filename, 'PAS/.objects')
00150             self.assertEqual(content_type, 'text/comma-separated-values')
00151             self.assertEqual(text.splitlines(),
00152                              _EMPTY_PAS_OBJECTS.splitlines())
00153 
00154             filename, text, content_type = context._wrote[1]
00155             self.assertEqual(filename, 'PAS/.properties')
00156             self.assertEqual(content_type, 'text/plain')
00157             lines = filter(None, [x.strip() for x in text.splitlines()])
00158             self.assertEqual(len(lines), 2)
00159             self.assertEqual(lines[0], '[DEFAULT]')
00160             self.assertEqual(lines[1], 'title =')
00161 
00162             filename, text, content_type = context._wrote[2]
00163             self.assertEqual(filename, 'PAS/pluginregistry.xml')
00164             self.assertEqual(content_type, 'text/xml')
00165 
00166         def test_with_contents(self):
00167             from Products.GenericSetup.tests.common import DummyExportContext
00168             from Products.GenericSetup.tests.faux_objects \
00169                 import TestCSVAware
00170             from Products.GenericSetup.utils import _getDottedName
00171             from Products.PluginRegistry.PluginRegistry import PluginRegistry
00172             from Products.PluggableAuthService.exportimport import exportPAS
00173 
00174             _setUpDefaultTraversable()
00175 
00176             self._setUpAdapters()
00177             app, pas = self._initPAS()
00178             csv_aware = TestCSVAware()
00179             csv_aware._setId('csv_plugin')
00180             pas._setObject('csv_plugin', csv_aware)
00181             context = DummyExportContext(pas)
00182             exportPAS(context)
00183 
00184             self.assertEqual(len(context._wrote), 4)
00185             filename, text, content_type = context._wrote[0]
00186             self.assertEqual(filename, 'PAS/.objects')
00187             self.assertEqual(content_type, 'text/comma-separated-values')
00188 
00189             objects = [x for x in reader(StringIO(text))]
00190             self.assertEqual(len(objects), 2)
00191 
00192             object_id, type_name = objects[0]
00193             self.assertEqual(object_id, 'plugins')
00194             self.assertEqual(type_name, 'plugins') # adapter-driven
00195 
00196             object_id, type_name = objects[1]
00197             self.assertEqual(object_id, 'csv_plugin')
00198             self.assertEqual(type_name, _getDottedName(csv_aware.__class__))
00199 
00200             filename, text, content_type = context._wrote[1]
00201             self.assertEqual(filename, 'PAS/.properties')
00202             self.assertEqual(content_type, 'text/plain')
00203             lines = filter(None, [x.strip() for x in text.splitlines()])
00204             self.assertEqual(len(lines), 2)
00205             self.assertEqual(lines[0], '[DEFAULT]')
00206             self.assertEqual(lines[1], 'title =')
00207 
00208             filename, text, content_type = context._wrote[2]
00209             self.assertEqual(filename, 'PAS/pluginregistry.xml')
00210             self.assertEqual(content_type, 'text/xml')
00211 
00212             filename, text, content_type = context._wrote[3]
00213             self.assertEqual(filename, 'PAS/csv_plugin.csv')
00214             self.assertEqual(content_type, 'text/comma-separated-values')
00215 
00216     class Test_importPAS(_TestBase):
00217 
00218         def _setUpAdapters(self):
00219             from zope.app.testing import ztapi
00220 
00221             try:
00222                 from OFS.interfaces import IObjectManager
00223                 from OFS.interfaces import IPropertyManager
00224                 from OFS.interfaces import ISimpleItem
00225             except ImportError: # BBB
00226                 from Products.Five.interfaces import IObjectManager
00227                 from Products.Five.interfaces import IPropertyManager
00228                 from Products.Five.interfaces import ISimpleItem
00229 
00230             from Products.GenericSetup.interfaces import IContentFactory
00231             from Products.GenericSetup.interfaces import ICSVAware
00232             from Products.GenericSetup.interfaces import IDAVAware
00233             from Products.GenericSetup.interfaces import IFilesystemImporter
00234             from Products.GenericSetup.interfaces import IINIAware
00235 
00236             from Products.GenericSetup.content import \
00237                 FolderishExporterImporter
00238             from Products.GenericSetup.content import \
00239                 SimpleINIAware
00240             from Products.GenericSetup.content import \
00241                 CSVAwareFileAdapter
00242             from Products.GenericSetup.content import \
00243                 INIAwareFileAdapter
00244             from Products.GenericSetup.content import \
00245                 DAVAwareFileAdapter
00246 
00247             from Products.PluginRegistry.interfaces import IPluginRegistry
00248             from Products.PluginRegistry.exportimport \
00249                 import PluginRegistryFileExportImportAdapter
00250 
00251             from Products.PluggableAuthService.interfaces.authservice \
00252                 import IPluggableAuthService
00253             from Products.PluggableAuthService.exportimport \
00254                 import PAS_PR_ContentFactory
00255 
00256             ztapi.provideAdapter(IObjectManager,
00257                                  IFilesystemImporter,
00258                                  FolderishExporterImporter,
00259                                 )
00260 
00261             ztapi.provideAdapter(IPropertyManager,
00262                                  IINIAware,
00263                                  SimpleINIAware,
00264                                 )
00265 
00266             ztapi.provideAdapter(ICSVAware,
00267                                  IFilesystemImporter,
00268                                  CSVAwareFileAdapter,
00269                                 )
00270 
00271             ztapi.provideAdapter(IINIAware,
00272                                  IFilesystemImporter,
00273                                  INIAwareFileAdapter,
00274                                 )
00275 
00276             ztapi.provideAdapter(IDAVAware,
00277                                  IFilesystemImporter,
00278                                  DAVAwareFileAdapter,
00279                                 )
00280 
00281             ztapi.provideAdapter(IPluginRegistry,
00282                                  IFilesystemImporter,
00283                                  PluginRegistryFileExportImportAdapter,
00284                                 )
00285 
00286             ztapi.provideAdapter(IPluggableAuthService,
00287                                  IContentFactory,
00288                                  PAS_PR_ContentFactory,
00289                                  name='plugins',
00290                                 )
00291 
00292         def test_empty_modifying_plugin_types(self):
00293             from Products.GenericSetup.tests.common import DummyImportContext
00294             from Products.GenericSetup.utils import _getDottedName
00295             from Products.PluginRegistry.PluginRegistry import PluginRegistry
00296             from Products.PluggableAuthService.exportimport import importPAS
00297 
00298             self._setUpAdapters()
00299             app, pas = self._initPAS()
00300 
00301             context = DummyImportContext(pas)
00302             context._files['PAS/.objects'] = _EMPTY_PAS_OBJECTS
00303             context._files['PAS/.properties'] = _EMPTY_PAS_PROPERTIES
00304             context._files['PAS/pluginregistry.xml'
00305                           ] = _EMPTY_PLUGINREGISTRY_EXPORT
00306 
00307             self.failUnless(pas.plugins.listPluginTypeInfo())
00308             importPAS(context)
00309             self.failIf(pas.plugins.listPluginTypeInfo())
00310 
00311         def test_empty_adding_plugins(self):
00312             from Products.GenericSetup.tests.common import DummyImportContext
00313             from Products.GenericSetup.tests.faux_objects \
00314                 import TestCSVAware, KNOWN_CSV
00315             from Products.GenericSetup.utils import _getDottedName
00316             from Products.PluginRegistry.PluginRegistry import PluginRegistry
00317             from Products.PluggableAuthService.exportimport import importPAS
00318 
00319             self._setUpAdapters()
00320             app, pas = self._initPAS()
00321 
00322             context = DummyImportContext(pas)
00323             context._files['PAS/.objects'] = _PAS_WITH_CSV_PLUGIN_OBJECTS
00324             context._files['PAS/.properties'] = _EMPTY_PAS_PROPERTIES
00325             context._files['PAS/pluginregistry.xml'
00326                           ] = _EMPTY_PLUGINREGISTRY_EXPORT
00327             context._files['PAS/csv_plugin.csv'
00328                           ] = KNOWN_CSV
00329 
00330             self.assertEqual(len(pas.objectIds()), 1)
00331             self.failUnless('plugins' in pas.objectIds())
00332 
00333             importPAS(context)
00334 
00335             self.assertEqual(len(pas.objectIds()), 2)
00336             self.failUnless('plugins' in pas.objectIds())
00337             self.failUnless('csv_plugin' in pas.objectIds())
00338 
00339             csv_plugin = pas._getOb('csv_plugin')
00340             self.failUnless(csv_plugin.__class__ is TestCSVAware)
00341             self.assertEqual(csv_plugin._was_put.getvalue().strip(),
00342                              KNOWN_CSV.strip())
00343 
00344 
00345     _EMPTY_PAS_OBJECTS = """\
00346 plugins,plugins
00347 """
00348 
00349     _PAS_WITH_CSV_PLUGIN_OBJECTS = """\
00350 plugins,plugins
00351 csv_plugin,Products.GenericSetup.tests.faux_objects.TestCSVAware
00352 """
00353 
00354     _EMPTY_PAS_PROPERTIES = """\
00355 [DEFAULT]
00356 title =
00357 """
00358 
00359     _EMPTY_PLUGINREGISTRY_EXPORT = """\
00360 <?xml version="1.0"?>
00361 <plugin-registry>
00362 </plugin-registry>
00363 """
00364 
00365     def test_suite():
00366         return unittest.TestSuite((
00367             unittest.makeSuite( Test_exportPAS ),
00368             unittest.makeSuite( Test_importPAS ),
00369             ))
00370 
00371 if __name__ == "__main__":
00372     unittest.main(defaultTest='test_suite')