products/CPSSubscriptions
changeset 1079:aef7376ce342
Factorized mock MailHost for reusability
author | Georges Racinet on ishtar.racinet.fr <georges@racinet.fr> |
---|---|
date | Tue, 25 Oct 2011 18:47:46 +0200 |
parents | f4e7e5ad2d08 |
children | 342128e059a0 |
files | tests/CPSSubscriptionsTestCase.py tests/testNotificationRules.py |
diffstat | 2 files changed, 67 insertions(+), 71 deletions(-) [+] |
line diff
1.1 --- a/tests/CPSSubscriptionsTestCase.py 1.2 +++ b/tests/CPSSubscriptionsTestCase.py 1.3 @@ -14,8 +14,13 @@ 1.4 # along with this program; if not, write to the Free Software 1.5 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 1.6 # 02111-1307, USA. 1.7 -# 1.8 -# $Id$ 1.9 + 1.10 +import email 1.11 +from zope.interface import implements 1.12 +from zope.app import zapi 1.13 + 1.14 +from OFS.SimpleItem import SimpleItem 1.15 +from Products.MailHost.interfaces import IMailHost 1.16 1.17 from Products.CPSDefault.tests.CPSTestCase import CPSTestCase 1.18 from Products.CPSDefault.tests.CPSTestCase import ExtensionProfileLayerClass 1.19 @@ -26,6 +31,63 @@ 1.20 1.21 CPSSubscriptionsLayer = LayerClass(__name__, 'CPSSubscriptionsLayer') 1.22 1.23 +class DummyMailHost(SimpleItem): 1.24 + """Host that stores the sent mails in a list 1.25 + 1.26 + The list can then be inspected. This way you can see who got notified. 1.27 + """ 1.28 + 1.29 + implements(IMailHost) 1.30 + 1.31 + mail_log = [] 1.32 + 1.33 + def clearLog(self): 1.34 + self.mail_log = [] 1.35 + 1.36 + def send(self, raw_message): 1.37 + message = email.message_from_string(raw_message) 1.38 + mfrom = message['From'] 1.39 + mto = message['To'] 1.40 + subject = message['Subject'] 1.41 + bcc = message['Bcc'] 1.42 + self.mail_log.append({'from': mfrom, 'to': mto, 'message': message, 1.43 + 'subject': subject, 'bcc': bcc}) 1.44 + 1.45 + def _send(self, mfrom, mto, msg): 1.46 + message = email.message_from_string(msg) 1.47 + hfrom = message['From'] 1.48 + hto = message['To'] 1.49 + hcc = message['Cc'] 1.50 + subject = message['Subject'] 1.51 + if message['Bcc'] is not None: 1.52 + raise ValueError("Bcc has ended up in message headers") 1.53 + # True bcc computation: take effective mto minus To and Cc headers 1.54 + bcc = [r.strip() for r in mto] 1.55 + if hto: 1.56 + for r in hto.split(','): 1.57 + bcc.remove(r.strip()) 1.58 + if hcc: 1.59 + for r in hto.split(','): 1.60 + bcc.remove(r.strip()) 1.61 + bcc = ','.join(bcc) 1.62 + self.mail_log.append({'smtp_from': mfrom, 'smtp_to': mto, 1.63 + 'message': message, 1.64 + 'from': hfrom, 'to': hto, 1.65 + 'subject': subject, 'bcc': bcc}) 1.66 + 1.67 1.68 class CPSSubscriptionsTestCase(CPSTestCase): 1.69 layer = CPSSubscriptionsLayer 1.70 + 1.71 + def afterSetUp(self): 1.72 + self._setupDummyMailHost() 1.73 + 1.74 + def _setupDummyMailHost(self): 1.75 + """Register the dummy mailhost instead of the standard one. 1.76 + 1.77 + No need to clean this up in beforeTearDown 1.78 + """ 1.79 + self._mh = DummyMailHost() 1.80 + zapi.getSiteManager().registerUtility(self._mh, IMailHost) 1.81 + self._mh.clearLog() 1.82 +
2.1 --- a/tests/testNotificationRules.py 2.2 +++ b/tests/testNotificationRules.py 2.3 @@ -14,90 +14,27 @@ 2.4 # along with this program; if not, write to the Free Software 2.5 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 2.6 # 02111-1307, USA. 2.7 -# 2.8 -# $Id$ 2.9 2.10 -import os 2.11 -import sys 2.12 -import email 2.13 import unittest 2.14 2.15 import transaction 2.16 -from zope.interface import implements 2.17 -from zope.app import zapi 2.18 - 2.19 from Acquisition import aq_parent, aq_inner 2.20 from OFS.SimpleItem import SimpleItem 2.21 -from Products.MailHost.interfaces import IMailHost 2.22 2.23 from Products.CPSSubscriptions.Notifications import NotificationRule 2.24 2.25 -import CPSSubscriptionsTestCase 2.26 +from CPSSubscriptionsTestCase import CPSSubscriptionsTestCase 2.27 2.28 -class DummyMailHost(SimpleItem): 2.29 - """Host that stores the sent mails in a list 2.30 - 2.31 - The list can then be inspected. This way you can see who got notified. 2.32 - """ 2.33 - 2.34 - implements(IMailHost) 2.35 - 2.36 - mail_log = [] 2.37 - 2.38 - def clearLog(self): 2.39 - self.mail_log = [] 2.40 - 2.41 - def send(self, raw_message): 2.42 - message = email.message_from_string(raw_message) 2.43 - mfrom = message['From'] 2.44 - mto = message['To'] 2.45 - subject = message['Subject'] 2.46 - bcc = message['Bcc'] 2.47 - self.mail_log.append({'from': mfrom, 'to': mto, 'message': message, 2.48 - 'subject': subject, 'bcc': bcc}) 2.49 - 2.50 - def _send(self, mfrom, mto, msg): 2.51 - message = email.message_from_string(msg) 2.52 - hfrom = message['From'] 2.53 - hto = message['To'] 2.54 - hcc = message['Cc'] 2.55 - subject = message['Subject'] 2.56 - if message['Bcc'] is not None: 2.57 - raise ValueError("Bcc has ended up in message headers") 2.58 - # True bcc computation: take effective mto minus To and Cc headers 2.59 - bcc = [r.strip() for r in mto] 2.60 - if hto: 2.61 - for r in hto.split(','): 2.62 - bcc.remove(r.strip()) 2.63 - if hcc: 2.64 - for r in hto.split(','): 2.65 - bcc.remove(r.strip()) 2.66 - bcc = ','.join(bcc) 2.67 - self.mail_log.append({'smtp_from': mfrom, 'smtp_to': mto, 2.68 - 'message': message, 2.69 - 'from': hfrom, 'to': hto, 2.70 - 'subject': subject, 'bcc': bcc}) 2.71 - 2.72 -class TestBaseNotificationRule( 2.73 - CPSSubscriptionsTestCase.CPSSubscriptionsTestCase): 2.74 +class TestBaseNotificationRule(CPSSubscriptionsTestCase): 2.75 2.76 def afterSetUp(self): 2.77 + CPSSubscriptionsTestCase.afterSetUp(self) 2.78 self.login('manager') 2.79 - self._setupDummyMailHost() 2.80 self._stool = self.portal.portal_subscriptions 2.81 # Max recipients per mail notification at a time. 2.82 # 20 is the default one. 2.83 self._stool.max_recipients_per_notification = 3 2.84 2.85 - def _setupDummyMailHost(self): 2.86 - """Register the dummy mailhost instead of the standard one. 2.87 - 2.88 - No need to clean this up in beforeTearDown 2.89 - """ 2.90 - self._mh = DummyMailHost() 2.91 - zapi.getSiteManager().registerUtility(self._mh, IMailHost) 2.92 - self._mh.clearLog() 2.93 - 2.94 def beforeTearDown(self): 2.95 self.logout() 2.96 2.97 @@ -245,6 +182,3 @@ 2.98 suite.addTest(unittest.makeSuite(TestNotificationRule)) 2.99 suite.addTest(unittest.makeSuite(TestMailNotificationRule)) 2.100 return suite 2.101 - 2.102 -if __name__ == '__main__': 2.103 - execfile(os.path.join(sys.path[0], 'framework.py'))