|
3 | 3 |
|
4 | 4 | from unittest.mock import patch |
5 | 5 |
|
| 6 | +from odoo import tools |
| 7 | +from odoo.addons.base.tests import test_mail_examples |
6 | 8 | from odoo.addons.base.tests.common import MockSmtplibCase |
| 9 | +from odoo.tests import tagged |
7 | 10 | from odoo.tests.common import TransactionCase |
8 | 11 | from odoo.tools import mute_logger |
9 | 12 |
|
| 13 | + |
| 14 | +@tagged('mail_server') |
10 | 15 | class TestIrMailServer(TransactionCase, MockSmtplibCase): |
11 | 16 |
|
12 | 17 | def setUp(self): |
@@ -48,6 +53,41 @@ def test_match_from_filter(self): |
48 | 53 | for email, from_filter in tests: |
49 | 54 | self.assertFalse(match_from_filter(email, from_filter)) |
50 | 55 |
|
| 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 | + |
51 | 91 | @mute_logger('odoo.models.unlink') |
52 | 92 | def test_mail_server_priorities(self): |
53 | 93 | """Test if we choose the right mail server to send an email. |
|
0 commit comments