Skip to content

Commit

Permalink
Merge pull request #60 from plone/59.fixed
Browse files Browse the repository at this point in the history
Simplify test setup by using the MOCK_MAILHOST_FIXTURE
  • Loading branch information
mauritsvanrees authored Dec 11, 2020
2 parents 94c9a1f + 2390cc0 commit 94edd29
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 37 deletions.
1 change: 1 addition & 0 deletions news/59.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Simplify test setup by using the MOCK_MAILHOST_FIXTURE
3 changes: 2 additions & 1 deletion plone/app/contentrules/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from plone.app.contenttypes.testing import PLONE_APP_CONTENTTYPES_FIXTURE
from plone.app.testing import FunctionalTesting
from plone.app.testing import IntegrationTesting
from plone.app.testing import MOCK_MAILHOST_FIXTURE
from plone.app.testing import PloneSandboxLayer

import plone.app.contentrules
Expand All @@ -19,7 +20,7 @@ def setUpZope(self, app, configurationContext):


PLONE_APP_CONTENTRULES_INTEGRATION_TESTING = IntegrationTesting(
bases=(PLONE_APP_CONTENTRULES_FIXTURE,),
bases=(PLONE_APP_CONTENTRULES_FIXTURE, MOCK_MAILHOST_FIXTURE),
name='PloneAppContentrulesLayer:IntegrationTesting',
)

Expand Down
49 changes: 13 additions & 36 deletions plone/app/contentrules/tests/test_action_mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,6 @@ def setUp(self):
# XXX: remove the manager role that was set in the base class
setRoles(self.portal, TEST_USER_ID, [])

def _setup_mockmail(self):
sm = getSiteManager(self.portal)
sm.unregisterUtility(provided=IMailHost)
dummyMailHost = MockMailHost('MailHost')
sm.registerUtility(dummyMailHost, IMailHost)
self.portal._original_MailHost = self.portal.MailHost
self.portal.MailHost = dummyMailHost
return dummyMailHost

def _teardown_mockmail(self):
self.portal.MailHost = self.portal._original_MailHost
sm = getSiteManager(context=self.portal)
sm.unregisterUtility(provided=IMailHost)
sm.registerUtility(aq_base(self.portal._original_MailHost),
provided=IMailHost)

def testRegistered(self):
element = getUtility(IRuleAction, name='plone.actions.Mail')
self.assertEqual('plone.actions.Mail', element.addview)
Expand Down Expand Up @@ -120,7 +104,6 @@ def testExecute(self):
# this avoids sending mail as currentuser@foobar.com
self.portal.portal_membership.getAuthenticatedMember().setProperties(
email='currentuser@foobar.com')
dummyMailHost = self._setup_mockmail()
e = MailAction()
e.source = '$user_email'
e.recipients = 'bar@foo.be, bar@foo.be, $reviewer_emails, ' \
Expand All @@ -130,7 +113,7 @@ def testExecute(self):
IExecutable)
ex()
sent_mails = {}
for mail_sent in dummyMailHost.messages:
for mail_sent in self.portal.MailHost.messages:
mail_sent_msg = message_from_bytes(mail_sent)
sent_mails[mail_sent_msg.get('To')] = mail_sent_msg

Expand Down Expand Up @@ -160,10 +143,8 @@ def testExecute(self):
self.assertEqual(
set(emails),
set(sent_mails.keys()))
self._teardown_mockmail()

def testExecuteNoSource(self):
dummyMailHost = self._setup_mockmail()
e = MailAction()
e.recipients = 'bar@foo.be,foo@bar.be'
e.message = 'Document created !'
Expand All @@ -172,37 +153,38 @@ def testExecuteNoSource(self):
# this no longer errors since it breaks usability
self.assertTrue(ex)
# and will return False for the unsent message
self.assertEqual(ex(), False)
# if we provide a site mail address the message sends correctly
# (happens when no sender address can be computed)
registry = getUtility(IRegistry)
mail_settings = registry.forInterface(IMailSchema, prefix='plone')
mail_settings.email_from_address = ""
self.assertEqual(ex(), False)

# if we provide a site mail address the message sends correctly
mail_settings.email_from_address = 'manager@portal.be'
mail_settings.email_from_name = u'plone@rulez'
ex()
self.assertEqual(len(dummyMailHost.messages), 2)
mailSent = message_from_bytes(dummyMailHost.messages[0])
self.assertEqual(len(self.portal.MailHost.messages), 2)
mailSent = message_from_bytes(self.portal.MailHost.messages[0])
self.assertEqual('text/plain; charset="utf-8"',
mailSent.get('Content-Type'))
self.assertIn(mailSent.get('To'), ['bar@foo.be', 'foo@bar.be'])
self.assertEqual('"plone@rulez" <manager@portal.be>',
mailSent.get('From'))
self.assertEqual('Document created !',
mailSent.get_payload())
self._teardown_mockmail()

def testExecuteMultiRecipients(self):
dummyMailHost = self._setup_mockmail()
e = MailAction()
e.source = 'foo@bar.be'
e.recipients = 'bar@foo.be,foo@bar.be'
e.message = 'Document created !'
ex = getMultiAdapter((self.folder, e, DummyEvent(self.folder.d1)),
IExecutable)
ex()
self.assertEqual(len(dummyMailHost.messages), 2)
self.assertEqual(len(self.portal.MailHost.messages), 2)
# in py3 the order of mails is non-determininistic
# because sending iterates over a set of recipients
for msg in dummyMailHost.messages:
for msg in self.portal.MailHost.messages:
if b'bar@foo.be' in msg:
mailSent1 = message_from_bytes(msg)
else:
Expand All @@ -219,10 +201,8 @@ def testExecuteMultiRecipients(self):
self.assertEqual('foo@bar.be', mailSent2.get('From'))
self.assertEqual('Document created !',
mailSent2.get_payload())
self._teardown_mockmail()

def testExecuteExcludeActor(self):
dummyMailHost = self._setup_mockmail()
self.portal.portal_membership.getAuthenticatedMember().setProperties(
email='currentuser@foobar.com')
e = MailAction()
Expand All @@ -233,24 +213,21 @@ def testExecuteExcludeActor(self):
ex = getMultiAdapter((self.folder, e, DummyEvent(self.folder.d1)),
IExecutable)
ex()
self.assertEqual(len(dummyMailHost.messages), 1)
self.assertEqual(len(self.portal.MailHost.messages), 1)

mailSent = message_from_bytes(dummyMailHost.messages[0])
mailSent = message_from_bytes(self.portal.MailHost.messages[0])
self.assertEqual('bar@foo.be', mailSent.get('To'))
self._teardown_mockmail()

def testExecuteNoRecipients(self):
# no recipient
dummyMailHost = self._setup_mockmail()
e = MailAction()
e.source = 'foo@bar.be'
e.recipients = ''
e.message = 'Document created !'
ex = getMultiAdapter((self.folder, e, DummyEvent(self.folder.d1)),
IExecutable)
ex()
self.assertEqual(len(dummyMailHost.messages), 0)
self._teardown_mockmail()
self.assertEqual(len(self.portal.MailHost.messages), 0)

@unittest.skip(
'Monkey patching does not work well with mocking. Needs fixing.'
Expand Down

0 comments on commit 94edd29

Please sign in to comment.