Back to index

plone3  3.1.7
test_mailhost.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2005 Zope Corporation and Contributors. All Rights Reserved.
00004 #
00005 # This software is subject to the provisions of the Zope Public License,
00006 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
00007 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00008 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00009 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00010 # FOR A PARTICULAR PURPOSE.
00011 #
00012 ##############################################################################
00013 """Mailhost setup handler unit tests.
00014 
00015 $Id: test_mailhost.py 73035 2007-03-07 16:41:03Z jens $
00016 """
00017 
00018 import unittest
00019 import Testing
00020 
00021 from OFS.Folder import Folder
00022 from Products.MailHost.interfaces import IMailHost
00023 
00024 from zope.component import getSiteManager
00025 
00026 from Products.GenericSetup.tests.common import BaseRegistryTests
00027 from Products.GenericSetup.tests.common import DummyExportContext
00028 from Products.GenericSetup.tests.common import DummyImportContext
00029 
00030 from Products.CMFCore.testing import ExportImportZCMLLayer
00031 
00032 _DEFAULT_EXPORT = """\
00033 <?xml version="1.0"?>
00034 <object name="MailHost" meta_type="Mail Host" smtp_host="localhost"
00035    smtp_port="25" smtp_pwd="" smtp_uid=""/>
00036 """
00037 
00038 _CHANGED_EXPORT = """\
00039 <?xml version="1.0"?>
00040 <object name="MailHost" meta_type="Mail Host" smtp_host="value2"
00041    smtp_port="1" smtp_pwd="value1" smtp_uid="value3"/>
00042 """
00043 
00044 
00045 class _MailHostSetup(BaseRegistryTests):
00046 
00047     def _initSite(self, use_changed=False):
00048         from Products.MailHost.MailHost import MailHost
00049 
00050         self.root.site = Folder(id='site')
00051         site = self.root.site
00052         mh = site.MailHost = MailHost('MailHost')
00053         sm = getSiteManager()
00054         sm.registerUtility(site.MailHost, IMailHost)
00055  
00056         if use_changed:
00057            mh.smtp_port='1'
00058            mh.smtp_pwd="value1"
00059            mh.smtp_host="value2"
00060            mh.smtp_uid="value3"
00061 
00062         return site
00063 
00064 
00065 class exportMailHostTests(_MailHostSetup):
00066 
00067     layer = ExportImportZCMLLayer
00068 
00069     def test_unchanged(self):
00070         from Products.CMFCore.exportimport.mailhost import exportMailHost
00071 
00072         site = self._initSite(use_changed=False)
00073         context = DummyExportContext(site)
00074         exportMailHost(context)
00075 
00076         self.assertEqual(len(context._wrote), 1)
00077         filename, text, content_type = context._wrote[0]
00078         self.assertEqual(filename, 'mailhost.xml')
00079         self._compareDOM(text, _DEFAULT_EXPORT)
00080         self.assertEqual(content_type, 'text/xml')
00081 
00082     def test_changed(self):
00083         from Products.CMFCore.exportimport.mailhost import exportMailHost
00084 
00085         site = self._initSite(use_changed=True)
00086         context = DummyExportContext(site)
00087         exportMailHost(context)
00088 
00089         self.assertEqual(len(context._wrote), 1)
00090         filename, text, content_type = context._wrote[0]
00091         self.assertEqual(filename, 'mailhost.xml')
00092         self._compareDOM(text, _CHANGED_EXPORT)
00093         self.assertEqual(content_type, 'text/xml')
00094 
00095 
00096 class importMailHostTests(_MailHostSetup):
00097 
00098     layer = ExportImportZCMLLayer
00099 
00100     def test_normal(self):
00101         from Products.CMFCore.exportimport.mailhost import importMailHost
00102 
00103         site = self._initSite()
00104         mh = site.MailHost
00105 
00106         context = DummyImportContext(site)
00107         context._files['mailhost.xml'] = _CHANGED_EXPORT
00108         importMailHost(context)
00109 
00110         self.assertEqual( mh.smtp_pwd, 'value1' )
00111         self.assertEqual( mh.smtp_host, 'value2' )
00112         self.assertEqual( mh.smtp_uid, 'value3' )
00113         self.assertEqual( mh.smtp_port, 1 )
00114 
00115 
00116 def test_suite():
00117     return unittest.TestSuite((
00118         unittest.makeSuite(exportMailHostTests),
00119         unittest.makeSuite(importMailHostTests),
00120         ))
00121 
00122 if __name__ == '__main__':
00123     from Products.CMFCore.testing import run
00124     run(test_suite())