Skip to content

Commit de72889

Browse files
committed
Codecov showed text/html followed by text/plain not tested. Fixed bug
discovered. The html part was double attached. Removed an unneeded attachment call. Added some more test cases. Reworked test code to make selecting html filter tool easier.
1 parent 2973b88 commit de72889

File tree

2 files changed

+86
-7
lines changed

2 files changed

+86
-7
lines changed

roundup/mailgw.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,8 +425,8 @@ def extract_content(self, parent_type=None, ignore_alternatives=False,
425425
attachments.append(part.text_as_attachment())
426426
elif html_part_found:
427427
# text/plain part found after html
428-
# save html as attachment
429-
attachments.append(cpart.as_attachment())
428+
# text/html already stored as attachment,
429+
# so just use the text as the content.
430430
content = new_content
431431
cpart = part
432432
else:

test/test_multipart.py

Lines changed: 84 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,15 @@ def testMultipart(self):
160160
p = m.getpart()
161161
self.assert_(p is None)
162162

163-
def TestExtraction(self, spec, expected):
164-
from roundup.dehtml import dehtml
163+
def TestExtraction(self, spec, expected, convert_html_with=False):
164+
if convert_html_with:
165+
from roundup.dehtml import dehtml
166+
html2text=dehtml(convert_html_with).html2text
167+
else:
168+
html2text=None
165169

166170
self.assertEqual(ExampleMessage(spec).extract_content(
167-
html2text=dehtml('dhtml').html2text), expected)
171+
html2text=html2text), expected)
168172

169173
def testTextPlain(self):
170174
self.TestExtraction('text/plain', ('foo\n', [], False))
@@ -186,14 +190,27 @@ def testMultipartMixed(self):
186190
[('foo.pdf', 'application/pdf', 'foo\n')], False))
187191

188192
def testMultipartMixedHtml(self):
193+
# test with html conversion enabled
189194
self.TestExtraction("""
190195
multipart/mixed
191196
text/html
192197
application/pdf""",
193198
('bar\n',
194199
[('bar.html', 'text/html',
195200
'<html><body>bar</body></html>\n'),
196-
('foo.pdf', 'application/pdf', 'foo\n')], False))
201+
('foo.pdf', 'application/pdf', 'foo\n')], False),
202+
convert_html_with='dehtml')
203+
204+
# test with html conversion disabled
205+
self.TestExtraction("""
206+
multipart/mixed
207+
text/html
208+
application/pdf""",
209+
(None,
210+
[('bar.html', 'text/html',
211+
'<html><body>bar</body></html>\n'),
212+
('foo.pdf', 'application/pdf', 'foo\n')], False),
213+
convert_html_with=False)
197214

198215
def testMultipartAlternative(self):
199216
self.TestExtraction("""
@@ -210,7 +227,69 @@ def testMultipartAlternativeHtml(self):
210227
('bar\n',
211228
[('bar.html', 'text/html',
212229
'<html><body>bar</body></html>\n'),
213-
('foo.pdf', 'application/pdf', 'foo\n')], False))
230+
('foo.pdf', 'application/pdf', 'foo\n')], False),
231+
convert_html_with='dehtml')
232+
233+
self.TestExtraction("""
234+
multipart/alternative
235+
text/html
236+
application/pdf""",
237+
(None,
238+
[('bar.html', 'text/html',
239+
'<html><body>bar</body></html>\n'),
240+
('foo.pdf', 'application/pdf', 'foo\n')], False),
241+
convert_html_with=False)
242+
243+
def testMultipartAlternativeHtmlText(self):
244+
# text should take priority over html when html is first
245+
self.TestExtraction("""
246+
multipart/alternative
247+
text/html
248+
text/plain
249+
application/pdf""",
250+
('foo\n',
251+
[('bar.html', 'text/html',
252+
'<html><body>bar</body></html>\n'),
253+
('foo.pdf', 'application/pdf', 'foo\n')], False),
254+
convert_html_with='dehtml')
255+
256+
# text should take priority over html when text is first
257+
self.TestExtraction("""
258+
multipart/alternative
259+
text/plain
260+
text/html
261+
application/pdf""",
262+
('foo\n',
263+
[('bar.html', 'text/html',
264+
'<html><body>bar</body></html>\n'),
265+
('foo.pdf', 'application/pdf', 'foo\n')], False),
266+
convert_html_with='dehtml')
267+
268+
# text should take priority over html when text is second and
269+
# html is disabled
270+
self.TestExtraction("""
271+
multipart/alternative
272+
text/html
273+
text/plain
274+
application/pdf""",
275+
('foo\n',
276+
[('bar.html', 'text/html',
277+
'<html><body>bar</body></html>\n'),
278+
('foo.pdf', 'application/pdf', 'foo\n')], False),
279+
convert_html_with=False)
280+
281+
# text should take priority over html when text is first and
282+
# html is disabled
283+
self.TestExtraction("""
284+
multipart/alternative
285+
text/plain
286+
text/html
287+
application/pdf""",
288+
('foo\n',
289+
[('bar.html', 'text/html',
290+
'<html><body>bar</body></html>\n'),
291+
('foo.pdf', 'application/pdf', 'foo\n')], False),
292+
convert_html_with=False)
214293

215294
def testDeepMultipartAlternative(self):
216295
self.TestExtraction("""

0 commit comments

Comments
 (0)