Skip to content

Commit

Permalink
Merge pull request #4 from 4teamwork/deif-move-mailing
Browse files Browse the repository at this point in the history
Move Mailing class to its own module.
  • Loading branch information
jone committed May 7, 2013
2 parents f6e6d2e + e940fd4 commit 2aba1bd
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 90 deletions.
30 changes: 30 additions & 0 deletions README.rst
Expand Up @@ -10,6 +10,8 @@ This package provides helpers for writing tests.

Certified: 01/2013

.. contents:: Table of Contents


Browser testing with splinter
-----------------------------
Expand Down Expand Up @@ -289,6 +291,34 @@ own layer (by subclassing ``ComponentRegistryLayer``) and is not usable with
``load_zcml_file`` and ``load_zcml_string``.


Mailing test helper
-------------------
The Mailing helper object mocks the mailhost and captures sent emails.
The emails can then be easily used for assertions.

Usage:

.. code:: python
from ftw.testing.pages import Mailing
import transaction
class MyTest(TestCase):
layer = MY_FUNCTIONAL_TESTING
def setUp(self):
Mailing(self.layer['portal']).set_up()
transaction.commit()
def tearDown(self):
Mailing(self.layer['portal']).tear_down()
def test_mail_stuff(self):
portal = self.layer['portal']
do_send_email()
mail = Mailing(portal).pop()
self.assertEquals('Subject: ...', mail)
Links
-----
Expand Down
3 changes: 2 additions & 1 deletion docs/HISTORY.txt
Expand Up @@ -5,7 +5,8 @@ Changelog
1.4 (unreleased)
----------------

- Nothing changed yet.
- Move ``Mailing`` helper class to its own module ``mailing``.
[deif]


1.3 (2013-05-03)
Expand Down
2 changes: 1 addition & 1 deletion ftw/testing/implementer.py
Expand Up @@ -4,7 +4,7 @@


class Implementer(object):
"""The implementer creates a class implementing an interface dinamically.
"""The implementer creates a class implementing an interface dynamically.
"""

def __init__(self, interface):
Expand Down
68 changes: 68 additions & 0 deletions ftw/testing/mailing.py
@@ -0,0 +1,68 @@
from Products.MailHost.interfaces import IMailHost
from zope.component import getUtility


class Mailing(object):
"""The Mailing helper object mocks the mailhost and captures sent emails.
The emails can then be easily used for assertions.
"""

def __init__(self, portal):
self.portal = portal

def set_up(self, configure=True):
"""Setup a mock mail host so that emails can be catched and tested.
"""

# Do NOT move the MockMailHost import to the top!
# It patches things implicitly!
from Products.CMFPlone.tests.utils import MockMailHost

mockmailhost = MockMailHost('MailHost')
self.portal.MailHost = mockmailhost
sm = self.portal.getSiteManager()
sm.registerUtility(component=mockmailhost,
provided=IMailHost)

if configure:
mockmailhost.smtp_host = 'localhost'
self.portal.email_from_address = 'test@localhost'

def tear_down(self):
# Do NOT move the MockMailHost import to the top!
# It patches things implicitly!
from Products.CMFPlone.tests.utils import MockMailHost

sm = self.portal.getSiteManager()
mailhost = sm.getUtility(IMailHost)
if isinstance(mailhost, MockMailHost):
sm.unregisterUtility(component=mailhost, provided=IMailHost)

def get_mailhost(self):
mailhost = getUtility(IMailHost)

# Do NOT move the MockMailHost import to the top!
# It patches things implicitly!
from Products.CMFPlone.tests.utils import MockMailHost

assert isinstance(mailhost, MockMailHost), \
'The mailhost mocking was not set up properly. ' \
'Call ftw.testing.pages.Mailing().set_up() in your setUp method.'
return mailhost

def get_messages(self):
return self.get_mailhost().messages

def has_messages(self):
return len(self.get_messages()) > 0

def pop(self):
return self.get_messages().pop()

def reset(self):
# self.get_mailhost().reset()
try:
while self.pop():
pass
except IndexError:
pass
86 changes: 0 additions & 86 deletions ftw/testing/pages.py
Expand Up @@ -381,89 +381,3 @@ def get_control_panel_link(self, title):
"""
return self.get_control_panel_links().get(title)


class Mailing(object):
"""The Mailing page object mocks the mailhost and captures sent emails.
The emails can then be easily used for assertions.
Usage:
>>> from ftw.testing.pages import Mailing
>>> import transaction
>>> class MyTest(TestCase):
... layer = MY_FUNCTIONAL_TESTING
...
... def setUp(self):
... Mailing(self.layer['portal']).set_up()
... transaction.commit()
...
... def tearDown(self):
... Mailing(self.layer['portal']).set_up()
...
... def test_mail_stuff(self):
... portal = self.layer['portal']
... do_send_email()
... mail = Mailing(self.portal).pop()
... self.assertEquals('Subject: ...', mail)
"""

def __init__(self, portal):
self.portal = portal

def set_up(self, configure=True):
"""Setup a mock mail host so that emails can be catched and tested.
"""

# Do NOT move the MockMailHost import to the top!
# It patches things implicitly!
from Products.CMFPlone.tests.utils import MockMailHost

mockmailhost = MockMailHost('MailHost')
self.portal.MailHost = mockmailhost
sm = self.portal.getSiteManager()
sm.registerUtility(component=mockmailhost,
provided=IMailHost)

if configure:
mockmailhost.smtp_host = 'localhost'
self.portal.email_from_address = 'test@localhost'

def tear_down(self):
# Do NOT move the MockMailHost import to the top!
# It patches things implicitly!
from Products.CMFPlone.tests.utils import MockMailHost

sm = self.portal.getSiteManager()
mailhost = sm.getUtility(IMailHost)
if isinstance(mailhost, MockMailHost):
sm.unregisterUtility(component=mailhost, provided=IMailHost)

def get_mailhost(self):
mailhost = getUtility(IMailHost)

# Do NOT move the MockMailHost import to the top!
# It patches things implicitly!
from Products.CMFPlone.tests.utils import MockMailHost

assert isinstance(mailhost, MockMailHost), \
'The mailhost mocking was not set up properly. ' \
'Call ftw.testing.pages.Mailing().set_up() in your setUp method.'
return mailhost

def get_messages(self):
return self.get_mailhost().messages

def has_messages(self):
return len(self.get_messages()) > 0

def pop(self):
return self.get_messages().pop()

def reset(self):
# self.get_mailhost().reset()
try:
while self.pop():
pass
except IndexError:
pass
@@ -1,11 +1,11 @@
from Products.MailHost.interfaces import IMailHost
from ftw.testing.pages import Mailing
from ftw.testing.mailing import Mailing
from ftw.testing.testing import PAGE_OBJECT_FUNCTIONAL
from unittest2 import TestCase
from zope.component import getUtility


class TestMailingPageObject(TestCase):
class TestMailing(TestCase):

layer = PAGE_OBJECT_FUNCTIONAL

Expand Down

0 comments on commit 2aba1bd

Please sign in to comment.