Back to index

plone3  3.1.7
test_action_mail.py
Go to the documentation of this file.
00001 # -*- coding: UTF-8 -*-
00002 
00003 from email.MIMEText import MIMEText
00004 from zope.component import getUtility, getMultiAdapter, getSiteManager
00005 from zope.component.interfaces import IObjectEvent
00006 from zope.interface import implements
00007 
00008 from plone.app.contentrules.rule import Rule
00009 from plone.app.contentrules.tests.base import ContentRulesTestCase
00010 from plone.app.contentrules.actions.mail import MailAction, MailEditForm, MailAddForm
00011 from plone.contentrules.engine.interfaces import IRuleStorage
00012 from plone.contentrules.rule.interfaces import IRuleAction, IExecutable
00013 
00014 from Products.MailHost.interfaces import IMailHost
00015 from Products.SecureMailHost.SecureMailHost import SecureMailHost
00016 
00017 class DummyEvent(object):
00018     implements(IObjectEvent)
00019 
00020     def __init__(self, object):
00021         self.object = object
00022 
00023 class DummySecureMailHost(SecureMailHost):
00024     meta_type = 'Dummy secure Mail Host'
00025     def __init__(self, id):
00026         self.id = id
00027         self.sent = []
00028 
00029     def _send(self, mfrom, mto, messageText, debug=False):
00030         self.sent.append(messageText)
00031 
00032 
00033 class TestMailAction(ContentRulesTestCase):
00034 
00035     def afterSetUp(self):
00036         self.setRoles(('Manager',))
00037         self.portal.invokeFactory('Folder', 'target')
00038         self.folder.invokeFactory('Document', 'd1',
00039             title=unicode('Wälkommen', 'utf-8'))
00040 
00041     def testRegistered(self):
00042         element = getUtility(IRuleAction, name='plone.actions.Mail')
00043         self.assertEquals('plone.actions.Mail', element.addview)
00044         self.assertEquals('edit', element.editview)
00045         self.assertEquals(None, element.for_)
00046         self.assertEquals(IObjectEvent, element.event)
00047 
00048     def testInvokeAddView(self):
00049         element = getUtility(IRuleAction, name='plone.actions.Mail')
00050         storage = getUtility(IRuleStorage)
00051         storage[u'foo'] = Rule()
00052         rule = self.portal.restrictedTraverse('++rule++foo')
00053 
00054         adding = getMultiAdapter((rule, self.portal.REQUEST), name='+action')
00055         addview = getMultiAdapter((adding, self.portal.REQUEST),
00056                                   name=element.addview)
00057         self.failUnless(isinstance(addview,MailAddForm))
00058 
00059         addview.createAndAdd(data={'subject' : 'My Subject',
00060                                    'source': 'foo@bar.be',
00061                                    'recipients': 'foo@bar.be,bar@foo.be',
00062                                    'message': 'Hey, Oh!'})
00063 
00064         e = rule.actions[0]
00065         self.failUnless(isinstance(e, MailAction))
00066         self.assertEquals('My Subject', e.subject)
00067         self.assertEquals('foo@bar.be', e.source)
00068         self.assertEquals('foo@bar.be,bar@foo.be', e.recipients)
00069         self.assertEquals('Hey, Oh!', e.message)
00070 
00071     def testInvokeEditView(self):
00072         element = getUtility(IRuleAction, name='plone.actions.Mail')
00073         e = MailAction()
00074         editview = getMultiAdapter((e, self.folder.REQUEST),
00075                                    name=element.editview)
00076         self.failUnless(isinstance(editview, MailEditForm))
00077 
00078     def testExecute(self):
00079         self.loginAsPortalOwner()
00080         sm = getSiteManager(self.portal)
00081         sm.unregisterUtility(provided=IMailHost)
00082         dummyMailHost = DummySecureMailHost('dMailhost')
00083         sm.registerUtility(dummyMailHost, IMailHost)
00084         e = MailAction()
00085         e.source = "foo@bar.be"
00086         e.recipients = "bar@foo.be"
00087         e.message = u"Päge '${title}' created in ${url} !"
00088         ex = getMultiAdapter((self.folder, e, DummyEvent(self.folder.d1)),
00089                              IExecutable)
00090         ex()
00091         self.failUnless(isinstance(dummyMailHost.sent[0], MIMEText))
00092         mailSent = dummyMailHost.sent[0]
00093         self.assertEqual('text/plain; charset="utf-8"',
00094                         mailSent.get('Content-Type'))
00095         self.assertEqual("bar@foo.be", mailSent.get('To'))
00096         self.assertEqual("foo@bar.be", mailSent.get('From'))
00097         self.assertEqual("P\xc3\xa4ge 'W\xc3\xa4lkommen' created in \
00098 http://nohost/plone/Members/test_user_1_/d1 !",
00099                          mailSent.get_payload(decode=True))
00100 
00101     def testExecuteNoSource(self):
00102         self.loginAsPortalOwner()
00103         sm = getSiteManager(self.portal)
00104         sm.unregisterUtility(provided=IMailHost)
00105         dummyMailHost = DummySecureMailHost('dMailhost')
00106         sm.registerUtility(dummyMailHost, IMailHost)
00107         e = MailAction()
00108         e.recipients = 'bar@foo.be,foo@bar.be'
00109         e.message = 'Document created !'
00110         ex = getMultiAdapter((self.folder, e, DummyEvent(self.folder.d1)),
00111                              IExecutable)
00112         self.assertRaises(ValueError, ex)
00113         # if we provide a site mail address this won't fail anymore
00114         sm.manage_changeProperties({'email_from_address': 'manager@portal.be'})
00115         ex()
00116         self.failUnless(isinstance(dummyMailHost.sent[0], MIMEText))
00117         mailSent = dummyMailHost.sent[0]
00118         self.assertEqual('text/plain; charset="utf-8"',
00119                         mailSent.get('Content-Type'))
00120         self.assertEqual("bar@foo.be", mailSent.get('To'))
00121         self.assertEqual("Site Administrator <manager@portal.be>",
00122                          mailSent.get('From'))
00123         self.assertEqual("Document created !",
00124                          mailSent.get_payload(decode=True))
00125 
00126     def testExecuteMultiRecipients(self):
00127         self.loginAsPortalOwner()
00128         sm = getSiteManager(self.portal)
00129         sm.unregisterUtility(provided=IMailHost)
00130         dummyMailHost = DummySecureMailHost('dMailhost')
00131         sm.registerUtility(dummyMailHost, IMailHost)
00132         e = MailAction()
00133         e.source = 'foo@bar.be'
00134         e.recipients = 'bar@foo.be,foo@bar.be'
00135         e.message = 'Document created !'
00136         ex = getMultiAdapter((self.folder, e, DummyEvent(self.folder.d1)),
00137                              IExecutable)
00138         ex()
00139         self.assertEqual(len(dummyMailHost.sent), 2)
00140         self.failUnless(isinstance(dummyMailHost.sent[0], MIMEText))
00141         mailSent = dummyMailHost.sent[0]
00142         self.assertEqual('text/plain; charset="utf-8"',
00143                         mailSent.get('Content-Type'))
00144         self.assertEqual('bar@foo.be', mailSent.get('To'))
00145         self.assertEqual('foo@bar.be', mailSent.get('From'))
00146         self.assertEqual('Document created !',mailSent.get_payload(decode=True))
00147         mailSent = dummyMailHost.sent[1]
00148         self.assertEqual('text/plain; charset="utf-8"',
00149                         mailSent.get('Content-Type'))
00150         self.assertEqual('foo@bar.be', mailSent.get('To'))
00151         self.assertEqual('foo@bar.be', mailSent.get('From'))
00152         self.assertEqual('Document created !',mailSent.get_payload(decode=True))
00153 
00154 def test_suite():
00155     from unittest import TestSuite, makeSuite
00156     suite = TestSuite()
00157     suite.addTest(makeSuite(TestMailAction))
00158     return suite