Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MockMailHostLayer: clean up the registry on test tear down #67

Merged
merged 1 commit into from
Feb 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion news/62.bugfix
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Properly configure the mail sender setting the appropriate registry records (Fixes #62)
MockMailHostLayer configures the mail sender setting the appropriate registry records (Fixes #62)
33 changes: 24 additions & 9 deletions src/plone/app/testing/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,28 +388,43 @@ def testSetUp(self):
with zope.zopeApp() as app:
portal = app[PLONE_SITE_ID]
registry = getUtility(IRegistry, context=portal)

if not registry["plone.email_from_address"]:
portal._original_email_address = registry["plone.email_from_address"] # noqa: E501
registry["plone.email_from_address"] = "noreply@example.com"

if not registry["plone.email_from_name"]:
portal._original_email_name = registry["plone.email_from_name"]
registry["plone.email_from_name"] = u"Plone site"

portal._original_MailHost = portal.MailHost
portal.MailHost = mailhost = MockMailHost('MailHost')

sm = getSiteManager(context=portal)
sm.unregisterUtility(provided=IMailHost)
sm.registerUtility(mailhost, provided=IMailHost)

def testTearDown(self):

with zope.zopeApp() as app:
portal = app[PLONE_SITE_ID]
_o_mailhost = getattr(portal, '_original_MailHost', None)
if _o_mailhost:
portal.MailHost = portal._original_MailHost
sm = getSiteManager(context=portal)
sm.unregisterUtility(provided=IMailHost)
sm.registerUtility(
aq_base(portal._original_MailHost),
provided=IMailHost
)
registry = getUtility(IRegistry, context=portal)

portal.MailHost = portal._original_MailHost

sm = getSiteManager(context=portal)
sm.unregisterUtility(provided=IMailHost)
sm.registerUtility(aq_base(portal.MailHost), provided=IMailHost)

if hasattr(portal, "_original_email_name"):
registry["plone.email_from_name"] = portal._original_email_name
delattr(portal, "_original_email_name")

if hasattr(portal, "_original_email_address"):
registry["plone.email_from_address"] = portal._original_email_address # noqa: E501
delattr(portal, "_original_email_address")

delattr(portal, "_original_MailHost")


MOCK_MAILHOST_FIXTURE = MockMailHostLayer()
Expand Down
5 changes: 4 additions & 1 deletion src/plone/app/testing/layers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,8 @@ The list can be reset:
... portal.MailHost.messages
[]

When the test is torn down the original MaiHost is restored:
When the test is torn down the original MaiHost is restored
and the registry is cleaned up:

>>> layers.MOCK_MAILHOST_FIXTURE.testTearDown()
>>> zope.STARTUP.testTearDown()
Expand All @@ -469,6 +470,8 @@ When the test is torn down the original MaiHost is restored:
...
AttributeError: 'RequestContainer' object has no attribute 'messages'

>>> registry["plone.email_from_address"]
>>> registry["plone.email_from_name"]
>>> runner.tear_down_unneeded(options, [], setupLayers)
Tear down plone.app.testing.layers.MockMailHostLayer in ... seconds.
Tear down plone.app.testing.layers.PloneFixture in ... seconds.
Expand Down