Skip to content

Commit be878f5

Browse files
[IMP] base: test body alternative done in build_email
Purpose of this commit is to add some tests about body alternative done in build_email when none is given. It uses a library we are about to remove and testing it is therefore necessary. Task-2702034 X-original-commit: 92b102c Part-of: odoo#82486
1 parent f22d6be commit be878f5

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

odoo/addons/base/tests/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from . import test_ir_cron
2121
from . import test_ir_http
2222
from . import test_ir_filters
23+
from . import test_ir_mail_server
2324
from . import test_ir_model
2425
from . import test_ir_sequence
2526
from . import test_ir_sequence_date_range
@@ -51,4 +52,3 @@
5152
from . import test_cloc
5253
from . import test_profiler
5354
from . import test_pdf
54-
from . import test_ir_mail_server

odoo/addons/base/tests/test_ir_mail_server.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@
33

44
from unittest.mock import patch
55

6+
from odoo import tools
7+
from odoo.addons.base.tests import test_mail_examples
68
from odoo.addons.base.tests.common import MockSmtplibCase
9+
from odoo.tests import tagged
710
from odoo.tests.common import TransactionCase
811
from odoo.tools import mute_logger
912

13+
14+
@tagged('mail_server')
1015
class TestIrMailServer(TransactionCase, MockSmtplibCase):
1116

1217
def setUp(self):
@@ -48,6 +53,41 @@ def test_match_from_filter(self):
4853
for email, from_filter in tests:
4954
self.assertFalse(match_from_filter(email, from_filter))
5055

56+
def test_mail_body(self):
57+
bodies = [
58+
'content',
59+
'<p>content</p>',
60+
'<head><meta content="text/html; charset=utf-8" http-equiv="Content-Type"></head><body><p>content</p></body>',
61+
test_mail_examples.MISC_HTML_SOURCE,
62+
test_mail_examples.QUOTE_THUNDERBIRD_HTML,
63+
]
64+
expected_list = [
65+
'content',
66+
'content',
67+
'content',
68+
"test1\n\n**test2**\n\n_test3_\n\n_test4_\n\n~~test5~~\n\ntest6\n\n * test7\n * test8\n\n 1. test9\n 2. test10\n\n> test11\n\n> > test12\n>>\n\n>> \n>\n\n[google](http://google.com) [test link](javascript:alert\('malicious code'\))",
69+
'On 01/05/2016 10:24 AM, Raoul Poilvache wrote: \n\n> **_Test reply. The suite._** \n>\n>\n> \n>\n>\n> \-- \n>\n>\n> Raoul Poilvache\n\nTop cool !!! \n \n\n \n \n -- \n Raoul Poilvache\n ',
70+
]
71+
for body, expected in zip(bodies, expected_list):
72+
message = self.env['ir.mail_server'].build_email(
73+
'john.doe@from.example.com',
74+
'destinataire@to.example.com',
75+
body=body,
76+
subject='Subject',
77+
subtype='html',
78+
)
79+
body_alternative = False
80+
for part in message.walk():
81+
if part.get_content_maintype() == 'multipart':
82+
continue # skip container
83+
if part.get_content_type() == 'text/plain':
84+
if not part.get_payload():
85+
continue
86+
body_alternative = tools.ustr(part.get_content())
87+
# remove ending new lines as it just adds noise
88+
body_alternative = body_alternative.strip('\n')
89+
self.assertEqual(body_alternative, expected)
90+
5191
@mute_logger('odoo.models.unlink')
5292
def test_mail_server_priorities(self):
5393
"""Test if we choose the right mail server to send an email.

0 commit comments

Comments
 (0)