Skip to content

Commit

Permalink
Problem: Products.MailHost 4.10 breaks tests under Python 3
Browse files Browse the repository at this point in the history
Solution: Take care of line separators in tests
  • Loading branch information
gotcha committed Jun 1, 2021
1 parent 213bd58 commit d6b6a94
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
7 changes: 4 additions & 3 deletions src/collective/easyform/tests/serverside_field.rst
Expand Up @@ -44,8 +44,9 @@ thank you page::

Test for 'Subject' in the mail body::

>>> msgtext = portal.MailHost.msgtext[portal.MailHost.msgtext.index(b'\n\n'):]
>>> body = b'\n\n'.join(portal.MailHost.msgtext.split(b'\n\n')[1:])
>>> TWOLINESEP = LINESEP + LINESEP
>>> msgtext = portal.MailHost.msgtext[portal.MailHost.msgtext.index(TWOLINESEP):]
>>> body = TWOLINESEP.join(portal.MailHost.msgtext.split(TWOLINESEP)[1:])
>>> b'Subject' in body
False

Expand All @@ -72,7 +73,7 @@ TODO: Since getform('xy').mech_form isn't available anymore we have to move this
# <sent mail from ...to ['mdummy@address.com']>

# >>> portal = layer['portal']
# >>> body = '\n\n'.join(portal.MailHost.msgtext.split('\n\n')[1:])
# >>> body = TWOLINESEP.join(portal.MailHost.msgtext.split(TWOLINESEP)[1:])
# >>> 'Subject' in body
# True

Expand Down
7 changes: 7 additions & 0 deletions src/collective/easyform/tests/testDocTests.py
Expand Up @@ -16,6 +16,12 @@
except ImportError:
from plone.testing.z2 import Browser

try:
from email import message_from_bytes
LINESEP = b'\r\n'
except ImportError:
# Python 2
LINESEP = b'\n'

optionflags = (
doctest.REPORT_ONLY_FIRST_FAILURE
Expand Down Expand Up @@ -69,6 +75,7 @@ def test_suite():
f,
optionflags=optionflags,
globs={
"LINESEP": LINESEP,
"get_browser": get_browser,
"get_image_path": get_image_path,
},
Expand Down
30 changes: 14 additions & 16 deletions src/collective/easyform/tests/testMailer.py
Expand Up @@ -21,9 +21,11 @@
try:
# Python 3
from email import message_from_bytes
LINESEP = b'\r\n'
except ImportError:
# Python 2
from email import message_from_string as message_from_bytes
LINESEP = b'\n'


class TestFunctions(base.EasyFormTestCase):
Expand All @@ -36,7 +38,8 @@ def dummy_send(self, mfrom, mto, messageText, immediate=False):
# It is text instead of bytes.
messageText = messageText.encode("utf-8")
self.messageText = messageText
self.messageBody = b"\n\n".join(messageText.split(b"\n\n")[1:])
TWOLINESEP = LINESEP + LINESEP
self.messageBody = TWOLINESEP.join(messageText.split(TWOLINESEP)[1:])

def afterSetUp(self):
super(TestFunctions, self).afterSetUp()
Expand Down Expand Up @@ -190,6 +193,7 @@ def test_TemplateReplacement(self):
request = self.LoadRequestForm(**data)
self.messageText = b""
mailer.onSuccess(data, request)

self.assertIn(b"Hello test subject,", self.messageBody)
self.assertIn(b"Thanks, test subject!", self.messageBody)
self.assertIn(b"Eat my footer, test subject.", self.messageBody)
Expand Down Expand Up @@ -366,31 +370,25 @@ def test_selectiveFieldMailing(self):
# make sure all fields are sent unless otherwise specified
self.messageText = b""
mailer.onSuccess(fields, request)
self.assertTrue(
b"te=\nst subject" in self.messageBody
and b"test@test.org" in self.messageBody
and b"test comments" in self.messageBody
)
self.assertIn(b"te=" + LINESEP + b"st subject", self.messageBody)
self.assertIn(b"test@test.org", self.messageBody)
self.assertIn(b"test comments", self.messageBody)

# setting some show fields shouldn't change that
mailer.showFields = ("topic", "comments")
self.messageText = b""
mailer.onSuccess(fields, request)
self.assertTrue(
b"te=\nst subject" in self.messageBody
and b"test@test.org" in self.messageBody
and b"test comments" in self.messageBody
)
self.assertIn(b"te=" + LINESEP + b"st subject", self.messageBody)
self.assertIn(b"test@test.org", self.messageBody)
self.assertIn(b"test comments", self.messageBody)

# until we turn off the showAll flag
mailer.showAll = False
self.messageText = b""
mailer.onSuccess(fields, request)
self.assertTrue(
b"te=\nst subject" in self.messageBody
and b"test@test.org" not in self.messageBody
and b"test comments" in self.messageBody
)
self.assertIn(b"te=" + LINESEP + b"st subject", self.messageBody)
self.assertNotIn(b"test@test.org", self.messageBody)
self.assertIn(b"test comments", self.messageBody)

# check includeEmpties
mailer.includeEmpties = False
Expand Down

0 comments on commit d6b6a94

Please sign in to comment.