Commit cc7b1372 authored by Julien Muchembled's avatar Julien Muchembled

Always use dummy MailHost while running unit tests

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@42905 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 79a40fd4
...@@ -101,6 +101,7 @@ class ERP5TypeLiveTestCase(ERP5TypeTestCaseMixin): ...@@ -101,6 +101,7 @@ class ERP5TypeLiveTestCase(ERP5TypeTestCaseMixin):
'''Closes the ZODB connection.''' '''Closes the ZODB connection.'''
revert = transaction.get().__hash__() != self.initial_transaction_hash revert = transaction.get().__hash__() != self.initial_transaction_hash
transaction.abort() transaction.abort()
self._restoreMailHost()
if revert: if revert:
if self.activity_tool_subscribed: if self.activity_tool_subscribed:
self.portal.portal_activities.subscribe() self.portal.portal_activities.subscribe()
...@@ -115,6 +116,7 @@ class ERP5TypeLiveTestCase(ERP5TypeTestCaseMixin): ...@@ -115,6 +116,7 @@ class ERP5TypeLiveTestCase(ERP5TypeTestCaseMixin):
self.activity_tool_subscribed = self.getPortalObject()\ self.activity_tool_subscribed = self.getPortalObject()\
.portal_activities.isSubscribed() .portal_activities.isSubscribed()
self.portal.portal_activities.unsubscribe() self.portal.portal_activities.unsubscribe()
self._setUpDummyMailHost()
def setUp(self): def setUp(self):
'''Sets up the fixture. Do not override, '''Sets up the fixture. Do not override,
......
...@@ -73,7 +73,7 @@ from Products.ERP5Type.Accessor.Constant import PropertyGetter as ConstantGetter ...@@ -73,7 +73,7 @@ from Products.ERP5Type.Accessor.Constant import PropertyGetter as ConstantGetter
from zLOG import LOG, DEBUG from zLOG import LOG, DEBUG
from Products.ERP5Type.tests.backportUnittest import SetupSiteError from Products.ERP5Type.tests.backportUnittest import SetupSiteError
from Products.ERP5Type.tests.utils import DummyMailHost, parseListeningAddress from Products.ERP5Type.tests.utils import DummyMailHostMixin, parseListeningAddress
# Quiet messages when installing products # Quiet messages when installing products
install_product_quiet = 1 install_product_quiet = 1
...@@ -322,6 +322,20 @@ class ERP5TypeTestCaseMixin(ProcessingNodeTestCase, PortalTestCase): ...@@ -322,6 +322,20 @@ class ERP5TypeTestCaseMixin(ProcessingNodeTestCase, PortalTestCase):
if not uf.getUser(user_name): if not uf.getUser(user_name):
uf._doAddUser(user_name, 'secret', ['Member'], []) uf._doAddUser(user_name, 'secret', ['Member'], [])
def _setUpDummyMailHost(self):
"""Replace Original Mail Host by Dummy Mail Host in a non-persistent way
"""
cls = self.portal.MailHost.__class__
assert not issubclass(cls, DummyMailHostMixin)
cls.__bases__ = (DummyMailHostMixin,) + cls.__bases__
def _restoreMailHost(self):
"""Restore original Mail Host
"""
cls = self.portal.MailHost.__class__
assert cls.__bases__[0] is DummyMailHostMixin
cls.__bases__ = cls.__bases__[1:]
def getDefaultSitePreferenceId(self): def getDefaultSitePreferenceId(self):
"""Default id, usefull method to override """Default id, usefull method to override
""" """
...@@ -768,7 +782,6 @@ class ERP5TypeCommandLineTestCase(ERP5TypeTestCaseMixin): ...@@ -768,7 +782,6 @@ class ERP5TypeCommandLineTestCase(ERP5TypeTestCaseMixin):
BaseMessage.max_retry = property(lambda self: BaseMessage.max_retry = property(lambda self:
self.activity_kw.get('max_retry', 1)) self.activity_kw.get('max_retry', 1))
use_dummy_mail_host = os.environ.get('use_dummy_mail_host', 0)
template_list = self.getBusinessTemplateList() template_list = self.getBusinessTemplateList()
erp5_catalog_storage = os.environ.get('erp5_catalog_storage', erp5_catalog_storage = os.environ.get('erp5_catalog_storage',
'erp5_mysql_innodb_catalog') 'erp5_mysql_innodb_catalog')
...@@ -801,8 +814,7 @@ class ERP5TypeCommandLineTestCase(ERP5TypeTestCaseMixin): ...@@ -801,8 +814,7 @@ class ERP5TypeCommandLineTestCase(ERP5TypeTestCaseMixin):
create_activities=create_activities, create_activities=create_activities,
quiet=install_bt5_quiet, quiet=install_bt5_quiet,
hot_reindexing=hot_reindexing, hot_reindexing=hot_reindexing,
erp5_catalog_storage=erp5_catalog_storage, erp5_catalog_storage=erp5_catalog_storage)
use_dummy_mail_host=use_dummy_mail_host)
PortalTestCase.setUp(self) PortalTestCase.setUp(self)
def afterSetUp(self): def afterSetUp(self):
...@@ -826,13 +838,6 @@ class ERP5TypeCommandLineTestCase(ERP5TypeTestCaseMixin): ...@@ -826,13 +838,6 @@ class ERP5TypeCommandLineTestCase(ERP5TypeTestCaseMixin):
connection_name = connection_string_name.replace('_string', '') connection_name = connection_string_name.replace('_string', '')
getattr(self.portal, connection_name).edit('', connection_string) getattr(self.portal, connection_name).edit('', connection_string)
def _setUpDummyMailHost(self):
"""Replace Original Mail Host by Dummy Mail Host.
"""
if 'MailHost' in self.portal.objectIds():
self.portal.manage_delObjects(['MailHost'])
self.portal._setObject('MailHost', DummyMailHost('MailHost'))
def _updateConversionServerConfiguration(self): def _updateConversionServerConfiguration(self):
"""Update conversion server (Oood) at default site preferences. """Update conversion server (Oood) at default site preferences.
""" """
...@@ -909,8 +914,7 @@ class ERP5TypeCommandLineTestCase(ERP5TypeTestCaseMixin): ...@@ -909,8 +914,7 @@ class ERP5TypeCommandLineTestCase(ERP5TypeTestCaseMixin):
light_install=1, light_install=1,
create_activities=1, create_activities=1,
hot_reindexing=1, hot_reindexing=1,
erp5_catalog_storage='erp5_mysql_innodb_catalog', erp5_catalog_storage='erp5_mysql_innodb_catalog'):
use_dummy_mail_host=0):
''' '''
Creates an ERP5 site. Creates an ERP5 site.
business_template_list must be specified correctly business_template_list must be specified correctly
...@@ -978,6 +982,7 @@ class ERP5TypeCommandLineTestCase(ERP5TypeTestCaseMixin): ...@@ -978,6 +982,7 @@ class ERP5TypeCommandLineTestCase(ERP5TypeTestCaseMixin):
self.portal = portal = self.getPortal() self.portal = portal = self.getPortal()
if len(setup_done) == 1: # make sure it is run only once if len(setup_done) == 1: # make sure it is run only once
self._setUpDummyMailHost()
try: try:
from Products import DeadlockDebugger from Products import DeadlockDebugger
except ImportError: except ImportError:
...@@ -997,9 +1002,6 @@ class ERP5TypeCommandLineTestCase(ERP5TypeTestCaseMixin): ...@@ -997,9 +1002,6 @@ class ERP5TypeCommandLineTestCase(ERP5TypeTestCaseMixin):
'Assignor', 'Author', 'Auditor', 'Associate'], []) 'Assignor', 'Author', 'Auditor', 'Associate'], [])
user = uf.getUserById('ERP5TypeTestCase').__of__(uf) user = uf.getUserById('ERP5TypeTestCase').__of__(uf)
if use_dummy_mail_host:
self._setUpDummyMailHost()
setup_once = getattr(self, 'setUpOnce', None) setup_once = getattr(self, 'setUpOnce', None)
if setup_once is not None and \ if setup_once is not None and \
not getattr(portal, 'set_up_once_called', 0): not getattr(portal, 'set_up_once_called', 0):
......
...@@ -98,9 +98,6 @@ Options: ...@@ -98,9 +98,6 @@ Options:
Port number used to connect to conversion server Port number used to connect to conversion server
(Oood), the value will be stored at default preference. (Oood), the value will be stored at default preference.
By default 8008 is used. By default 8008 is used.
--use_dummy_mail_host Replace the MailHost by DummyMailHost.
This prevent the instance send emails.
By default Original MailHost is used.
--random_activity_priority=[SEED] --random_activity_priority=[SEED]
Force activities to have a random priority, to make Force activities to have a random priority, to make
random failures (due to bad activity dependencies) random failures (due to bad activity dependencies)
...@@ -610,7 +607,6 @@ def main(argument_list=None): ...@@ -610,7 +607,6 @@ def main(argument_list=None):
"enable_full_indexing=", "enable_full_indexing=",
"run_only=", "run_only=",
"update_only=", "update_only=",
"use_dummy_mail_host",
"update_business_templates", "update_business_templates",
"random_activity_priority=", "random_activity_priority=",
"activity_node=", "activity_node=",
...@@ -698,8 +694,6 @@ def main(argument_list=None): ...@@ -698,8 +694,6 @@ def main(argument_list=None):
os.environ["erp5_dump_sql"] = "0" os.environ["erp5_dump_sql"] = "0"
os.environ["erp5_tests_data_fs_path"] = os.path.join( os.environ["erp5_tests_data_fs_path"] = os.path.join(
live_instance_path, 'var', 'Data.fs') live_instance_path, 'var', 'Data.fs')
elif opt == "--use_dummy_mail_host":
os.environ["use_dummy_mail_host"] = "1"
elif opt == "--random_activity_priority": elif opt == "--random_activity_priority":
os.environ["random_activity_priority"] = arg or \ os.environ["random_activity_priority"] = arg or \
str(random.randrange(0, 1<<16)) str(random.randrange(0, 1<<16))
......
...@@ -54,25 +54,23 @@ class FileUpload(file): ...@@ -54,25 +54,23 @@ class FileUpload(file):
self.headers = {} self.headers = {}
# dummy objects # dummy objects
class DummyMailHost(MailHost): class DummyMailHostMixin(object):
"""Dummy Mail Host that doesn't really send messages and keep a copy in """Dummy Mail Host that doesn't really send messages and keep a copy in
_last_message attribute. _last_message attribute.
To use it, you have to replace existing mailhost in afterSetUp:
if 'MailHost' in portal.objectIds():
portal.manage_delObjects(['MailHost'])
portal._setObject('MailHost', DummyMailHost('MailHost'))
""" """
_last_message = () _last_message = ()
_previous_message = () _previous_message = ()
_message_list = [] _message_list = []
def _send( self, mfrom, mto, messageText, immediate=False ):
@classmethod
def _send(cls, mfrom, mto, messageText, immediate=False):
"""Record message in _last_message.""" """Record message in _last_message."""
self._previous_message = self._last_message cls._previous_message = cls._last_message
self._last_message = (mfrom, mto, messageText) cls._last_message = (mfrom, mto, messageText)
self._message_list.append(self._last_message) cls._message_list.append(cls._last_message)
def _decodeMessage(self, messageText): @staticmethod
def _decodeMessage(messageText):
""" Decode message""" """ Decode message"""
message_text = messageText message_text = messageText
for part in message_from_string(messageText).walk(): for part in message_from_string(messageText).walk():
...@@ -81,15 +79,20 @@ class DummyMailHost(MailHost): ...@@ -81,15 +79,20 @@ class DummyMailHost(MailHost):
message_text = part.get_payload(decode=1) message_text = part.get_payload(decode=1)
return message_text return message_text
def getMessageList(self, decode=True): @classmethod
def getMessageList(cls, decode=True):
""" Return message list""" """ Return message list"""
if decode: if decode:
return [ (m[0], m[1], self._decodeMessage(m[2])) for m in self._message_list] return [(m[0], m[1], cls._decodeMessage(m[2])) for m in cls._message_list]
return self._message_list return cls._message_list
def getLastLog(self): @classmethod
def getLastLog(cls):
""" Return last message """ """ Return last message """
return self._last_message return cls._last_message
class DummyMailHost(DummyMailHostMixin, MailHost):
pass
class DummyTranslationService: class DummyTranslationService:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment