Skip to content

Commit 0434b30

Browse files
committed
Add testing for beautifulsoup4 as html -> text converter.
1 parent ed3ae08 commit 0434b30

File tree

1 file changed

+90
-41
lines changed

1 file changed

+90
-41
lines changed

test/test_mailgw.py

Lines changed: 90 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@
2727
skip_pgp = mark_class(pytest.mark.skip(
2828
reason="Skipping PGP tests: 'gpg' not installed"))
2929

30+
try:
31+
import bs4
32+
skip_beautifulsoup = lambda func, *args, **kwargs: func
33+
except ImportError:
34+
from .pytest_patcher import mark_class
35+
skip_beautifulsoup = mark_class(pytest.mark.skip(
36+
reason="Skipping beautifulsoup tests: 'bs4' not installed"))
37+
3038

3139
from roundup.anypy.email_ import message_from_bytes
3240
from roundup.anypy.strings import b2s, u2s, s2b
@@ -236,6 +244,10 @@ def _allowAnonymousSubmit(self):
236244

237245
def _create_mailgw(self, message, args=()):
238246
class MailGW(self.instance.MailGW):
247+
"""call _handle_message as handle_message
248+
the real handle_message reopens the database, and destroys
249+
the db that we supply as part of the test.
250+
"""
239251
def handle_message(self, message):
240252
return self._handle_message(message)
241253
handler = MailGW(self.instance, args)
@@ -297,7 +309,11 @@ def testEmptyMessage(self):
297309

298310
class MailgwTestCase(MailgwTestAbstractBase, StringFragmentCmpHelper, unittest.TestCase):
299311

300-
def testTextHtmlMessage(self):
312+
@skip_beautifulsoup
313+
def testTextHtmlMessageBeautifulSoup(self):
314+
self.testTextHtmlMessage(converter='beautifulsoup')
315+
316+
def testTextHtmlMessage(self, converter='dehtml'):
301317
html_message='''Content-Type: text/html;
302318
charset="iso-8859-1"
303319
From: Chef <[email protected]>
@@ -353,17 +369,20 @@ def testTextHtmlMessage(self):
353369
</div>
354370
</body>
355371
'''
356-
text_fragments = ['Roundup\n Home\nDownload\nDocs\nRoundup Features\nInstalling Roundup\nUpgrading to newer versions of Roundup\nRoundup FAQ\nUser Guide\nCustomising Roundup\nAdministration Guide\nPrerequisites\n\nRoundup requires Python 2.6 or newer (but not Python 3) with a functioning\nanydbm module. Download the latest version from http://www.python.org/.\nIt is highly recommended that users install the latest patch version\nof python as these contain many fixes to serious bugs.\n\nSome variants of Linux will need an additional ', ('python dev', u2s(u'\u201cpython dev\u201d')), ' package\ninstalled for Roundup installation to work. Debian and derivatives, are\nknown to require this.\n\nIf you', (u2s(u'\u2019'), ''), 're on windows, you will either need to be using the ActiveState python\ndistribution (at http://www.activestate.com/Products/ActivePython/), or you', (u2s(u'\u2019'), ''), 'll\nhave to install the win32all package separately (get it from\nhttp://starship.python.net/crew/mhammond/win32/).']
372+
text_fragments = {}
373+
text_fragments['dehtml'] = ['Roundup\n Home\nDownload\nDocs\nRoundup Features\nInstalling Roundup\nUpgrading to newer versions of Roundup\nRoundup FAQ\nUser Guide\nCustomising Roundup\nAdministration Guide\nPrerequisites\n\nRoundup requires Python 2.6 or newer (but not Python 3) with a functioning\nanydbm module. Download the latest version from http://www.python.org/.\nIt is highly recommended that users install the latest patch version\nof python as these contain many fixes to serious bugs.\n\nSome variants of Linux will need an additional ', ('python dev', u2s(u'\u201cpython dev\u201d')), ' package\ninstalled for Roundup installation to work. Debian and derivatives, are\nknown to require this.\n\nIf you', (u2s(u'\u2019'), ''), 're on windows, you will either need to be using the ActiveState python\ndistribution (at http://www.activestate.com/Products/ActivePython/), or you', (u2s(u'\u2019'), ''), 'll\nhave to install the win32all package separately (get it from\nhttp://starship.python.net/crew/mhammond/win32/).']
374+
text_fragments['beautifulsoup'] = ['Roundup\nHome\nDownload\nDocs\nRoundup Features\nInstalling Roundup\nUpgrading to newer versions of Roundup\nRoundup FAQ\nUser Guide\nCustomising Roundup\nAdministration Guide\nPrerequisites\nRoundup requires Python 2.6 or newer (but not Python 3) with a functioning\nanydbm module. Download the latest version from\nhttp://www.python.org/\n.\nIt is highly recommended that users install the latest patch version\nof python as these contain many fixes to serious bugs.\nSome variants of Linux will need an additional ', ('python dev', u2s(u'\u201cpython dev\u201d')), ' package\ninstalled for Roundup installation to work. Debian and derivatives, are\nknown to require this.\nIf you', (u2s(u'\u2019'), "'"), 're on windows, you will either need to be using the ActiveState python\ndistribution (at\nhttp://www.activestate.com/Products/ActivePython/\n), or you’ll\nhave to install the win32all package separately (get it from\nhttp://starship.python.net/crew/mhammond/win32/\n).']
357375

358-
self.db.config.MAILGW_CONVERT_HTMLTOTEXT = "dehtml"
376+
self.db.config.MAILGW_CONVERT_HTMLTOTEXT = converter
359377
nodeid = self._handle_mail(html_message)
360378
assert not os.path.exists(SENDMAILDEBUG)
361379
msgid = self.db.issue.get(nodeid, 'messages')[0]
362380
self.compareStringFragments(self.db.msg.get(msgid, 'content'),
363-
text_fragments)
381+
text_fragments[converter])
364382

365-
self.db.config.MAILGW_CONVERT_HTMLTOTEXT = "none"
366-
self.assertRaises(MailUsageError, self._handle_mail, html_message)
383+
if converter == 'dehtml':
384+
self.db.config.MAILGW_CONVERT_HTMLTOTEXT = "none"
385+
self.assertRaises(MailUsageError, self._handle_mail, html_message)
367386

368387
def testMessageWithFromInIt(self):
369388
nodeid = self._handle_mail('''Content-Type: text/plain;
@@ -1381,16 +1400,15 @@ def testMultipartRFC822(self):
13811400
--001485f339f8f361fb049188dbba--
13821401
'''%html_doc
13831402

1384-
def disabletestMultipartTextifyHTMLBeautifulSoup(self):
1385-
self.maxDiff = None
1386-
self.MultipartTextifyHTML(converter="beautifulsoup")
1387-
1388-
def testMultipartTextifyHTMLDehtml(self):
1389-
self.MultipartTextifyHTML(converter="dehtml")
1403+
@skip_beautifulsoup
1404+
def testMultipartTextifyHTMLBeautifulSoup(self):
1405+
self.testMultipartTextifyHTML(converter="beautifulsoup")
13901406

1391-
def MultipartTextifyHTML(self, converter='dehtml'):
1407+
def testMultipartTextifyHTML(self, converter='dehtml'):
13921408
text_fragments = {}
13931409
text_fragments['dehtml'] = ['Roundup\n Home\nDownload\nDocs\nRoundup Features\nInstalling Roundup\nUpgrading to newer versions of Roundup\nRoundup FAQ\nUser Guide\nCustomising Roundup\nAdministration Guide\nPrerequisites\n\nRoundup requires Python 2.5 or newer (but not Python 3) with a functioning\nanydbm module. Download the latest version from http://www.python.org/.\nIt is highly recommended that users install the latest patch version\nof python as these contain many fixes to serious bugs.\n\nSome variants of Linux will need an additional ', ('python dev', u2s(u'\u201cpython dev\u201d')), ' package\ninstalled for Roundup installation to work. Debian and derivatives, are\nknown to require this.\n\nIf you', (u2s(u'\u2019'), ''), 're on windows, you will either need to be using the ActiveState python\ndistribution (at http://www.activestate.com/Products/ActivePython/), or you', (u2s(u'\u2019'), ''), 'll\nhave to install the win32all package separately (get it from\nhttp://starship.python.net/crew/mhammond/win32/).\n\numlaut'] + [b2s(b" \xc3\xa4\xc3\xb6\xc3\xbc\xc3\x84\xc3\x96\xc3\x9c\xc3\x9f")]
1410+
text_fragments['beautifulsoup'] = ['Roundup\nHome\nDownload\nDocs\nRoundup Features\nInstalling Roundup\nUpgrading to newer versions of Roundup\nRoundup FAQ\nUser Guide\nCustomising Roundup\nAdministration Guide\nPrerequisites\nRoundup requires Python 2.5 or newer (but not Python 3) with a functioning\nanydbm module. Download the latest version from\nhttp://www.python.org/\n.\nIt is highly recommended that users install the latest patch version\nof python as these contain many fixes to serious bugs.\nSome variants of Linux will need an additional ', ('python dev', u2s(u'\u201cpython dev\u201d')), ' package\ninstalled for Roundup installation to work. Debian and derivatives, are\nknown to require this.\nIf you', (u2s(u'\u2019'), "'"), 're on windows, you will either need to be using the ActiveState python\ndistribution (at\nhttp://www.activestate.com/Products/ActivePython/\n), or you’ll\nhave to install the win32all package separately (get it from\nhttp://starship.python.net/crew/mhammond/win32/\n).\numlaut'] + [b2s(b" \xc3\xa4\xc3\xb6\xc3\xbc\xc3\x84\xc3\x96\xc3\x9c\xc3\x9f")]
1411+
13941412

13951413
# \xc3\xa4\xc3\xb6\xc3\xbc\xc3\x84\xc3\x96\xc3\x9c\xc3\x9f
13961414
# append above with leading space to end of mycontent. It is the
@@ -1404,7 +1422,6 @@ def MultipartTextifyHTML(self, converter='dehtml'):
14041422
msg = self.db.msg.getnode(messages[-1])
14051423

14061424
print(msg.content)
1407-
print(text_fragments[converter][0])
14081425
# html converted to utf-8 text
14091426
self.compareStringFragments(msg.content,
14101427
text_fragments[converter])
@@ -1418,6 +1435,63 @@ def MultipartTextifyHTML(self, converter='dehtml'):
14181435
content = { 0: "75,23,16,18\n",
14191436
1: self.html_doc.replace(" =E4=F6=FC=C4=D6=DC=DF",
14201437
b2s(b" \xc3\xa4\xc3\xb6\xc3\xbc\xc3\x84\xc3\x96\xc3\x9c\xc3\x9f")) }
1438+
email_body = {}
1439+
email_body['dehtml'] = '''Roundup
1440+
Home
1441+
Download
1442+
Docs
1443+
Roundup Features
1444+
Installing Roundup
1445+
Upgrading to newer versions of Roundup
1446+
Roundup FAQ
1447+
User Guide
1448+
Customising Roundup
1449+
Administration Guide
1450+
Prerequisites
1451+
1452+
Roundup requires Python 2.5 or newer (but not Python 3) with a functioning
1453+
anydbm module. Download the latest version from http://www.python.org/.
1454+
It is highly recommended that users install the latest patch version
1455+
of python as these contain many fixes to serious bugs.
1456+
1457+
Some variants of Linux will need an additional python dev package
1458+
installed for Roundup installation to work. Debian and derivatives, are
1459+
known to require this.
1460+
1461+
If youre on windows, you will either need to be using the ActiveState python
1462+
distribution (at http://www.activestate.com/Products/ActivePython/), or youll
1463+
have to install the win32all package separately (get it from
1464+
http://starship.python.net/crew/mhammond/win32/).
1465+
'''
1466+
email_body['beautifulsoup']='''Roundup
1467+
Home
1468+
Download
1469+
Docs
1470+
Roundup Features
1471+
Installing Roundup
1472+
Upgrading to newer versions of Roundup
1473+
Roundup FAQ
1474+
User Guide
1475+
Customising Roundup
1476+
Administration Guide
1477+
Prerequisites
1478+
Roundup requires Python 2.5 or newer (but not Python 3) with a functioning
1479+
anydbm module. Download the latest version from
1480+
http://www.python.org/
1481+
.
1482+
It is highly recommended that users install the latest patch version
1483+
of python as these contain many fixes to serious bugs.
1484+
Some variants of Linux will need an additional python dev package
1485+
installed for Roundup installation to work. Debian and derivatives, are
1486+
known to require this.
1487+
If youre on windows, you will either need to be using the ActiveState python
1488+
distribution (at
1489+
http://www.activestate.com/Products/ActivePython/
1490+
), or youll
1491+
have to install the win32all package separately (get it from
1492+
http://starship.python.net/crew/mhammond/win32/
1493+
).'''
1494+
14211495
for n, id in enumerate (msg.files):
14221496
f = self.db.file.getnode (id)
14231497
self.assertEqual(f.name, name)
@@ -1458,33 +1532,8 @@ def MultipartTextifyHTML(self, converter='dehtml'):
14581532
14591533
Contrary, Mary <[email protected]> added the comment:
14601534
1461-
Roundup
1462-
Home
1463-
Download
1464-
Docs
1465-
Roundup Features
1466-
Installing Roundup
1467-
Upgrading to newer versions of Roundup
1468-
Roundup FAQ
1469-
User Guide
1470-
Customising Roundup
1471-
Administration Guide
1472-
Prerequisites
1473-
1474-
Roundup requires Python 2.5 or newer (but not Python 3) with a functioning
1475-
anydbm module. Download the latest version from http://www.python.org/.
1476-
It is highly recommended that users install the latest patch version
1477-
of python as these contain many fixes to serious bugs.
1478-
1479-
Some variants of Linux will need an additional python dev package
1480-
installed for Roundup installation to work. Debian and derivatives, are
1481-
known to require this.
1482-
1483-
If youre on windows, you will either need to be using the ActiveState python
1484-
distribution (at http://www.activestate.com/Products/ActivePython/), or youll
1485-
have to install the win32all package separately (get it from
1486-
http://starship.python.net/crew/mhammond/win32/).
1487-
1535+
''' + email_body[converter] +
1536+
'''
14881537
umlaut =C3=A4=C3=B6=C3=BC=C3=84=C3=96=C3=9C=C3=9F
14891538
14901539
----------

0 commit comments

Comments
 (0)