diff options
| author | Julien Castiaux <juc@odoo.com> | 2019-08-20 13:09:55 +0000 |
|---|---|---|
| committer | Julien Castiaux <juc@odoo.com> | 2019-09-17 11:32:54 +0000 |
| commit | dbb6cfdbf803d2ab86ba11450b5928abf5ab658b (patch) | |
| tree | 830220c1e66d96736e097219949b724b9db0744e /addons/test_mail/tests | |
| parent | 1b057dd76b19db00eb5b63dc02f87cdc13cc07cf (diff) | |
[REF] mail: remove deprecated email formataddr
[PEP 594] is going to depreciate the legacy `email.message.Message` API
and its related modules. Among them the `email.utils.formataddr`
function, this function takes a `(name, email)` pair and returns a
string value suitable for From, To and Cc headers.
The stdlib function is capable of handling several character encoding
and two binary-to-ascii encoding. Odoo only uses `utf-8` which is
compatible with the base64 b2a encoding. The re-implementation of that
function has been simplified to only support base64-ed utf-8 and ascii.
[PEP 594]: https://www.python.org/dev/peps/pep-0594/\#email-lagacy-api
Diffstat (limited to 'addons/test_mail/tests')
| -rw-r--r-- | addons/test_mail/tests/common.py | 4 | ||||
| -rw-r--r-- | addons/test_mail/tests/test_mail_channel.py | 4 | ||||
| -rw-r--r-- | addons/test_mail/tests/test_mail_gateway.py | 29 | ||||
| -rw-r--r-- | addons/test_mail/tests/test_mail_message.py | 3 | ||||
| -rw-r--r-- | addons/test_mail/tests/test_message_post.py | 3 | ||||
| -rw-r--r-- | addons/test_mail/tests/test_message_track.py | 4 | ||||
| -rw-r--r-- | addons/test_mail/tests/test_performance.py | 4 |
7 files changed, 23 insertions, 28 deletions
diff --git a/addons/test_mail/tests/common.py b/addons/test_mail/tests/common.py index 17d8594a7d9..d73a091b7b2 100644 --- a/addons/test_mail/tests/common.py +++ b/addons/test_mail/tests/common.py @@ -4,12 +4,12 @@ import json import time from contextlib import contextmanager -from email.utils import formataddr from functools import partial from odoo import api from odoo.addons.bus.models.bus import json_dump from odoo.tests import common, tagged, new_test_user +from odoo.tools import formataddr mail_new_test_user = partial(new_test_user, context={'mail_create_nolog': True, 'mail_create_nosubscribe': True, 'mail_notrack': True, 'no_reset_password': True}) @@ -327,7 +327,7 @@ class MockEmails(common.SingleTransactionCase): self._mails[:] = [] def format(self, template, to='groups@example.com, other@gmail.com', subject='Frogs', - extra='', email_from='Sylvie Lelitre <test.sylvie.lelitre@agrolait.com>', + extra='', email_from='"Sylvie Lelitre" <test.sylvie.lelitre@agrolait.com>', cc='', msg_id='<1198923581.41972151344608186760.JavaMail@agrolait.com>'): return template.format(to=to, subject=subject, cc=cc, extra=extra, email_from=email_from, msg_id=msg_id) diff --git a/addons/test_mail/tests/test_mail_channel.py b/addons/test_mail/tests/test_mail_channel.py index f1b482028c9..74ad29a6d3e 100644 --- a/addons/test_mail/tests/test_mail_channel.py +++ b/addons/test_mail/tests/test_mail_channel.py @@ -1,11 +1,9 @@ # -*- coding: utf-8 -*- -from email.utils import formataddr - from odoo.tests import tagged from odoo.addons.test_mail.tests import common from odoo.addons.test_mail.tests.common import mail_new_test_user from odoo.exceptions import AccessError, except_orm, ValidationError, UserError -from odoo.tools import mute_logger +from odoo.tools import mute_logger, formataddr class TestChannelAccessRights(common.BaseFunctionalTest, common.MockEmails): diff --git a/addons/test_mail/tests/test_mail_gateway.py b/addons/test_mail/tests/test_mail_gateway.py index ff7b3580559..79fb1cf5c3c 100644 --- a/addons/test_mail/tests/test_mail_gateway.py +++ b/addons/test_mail/tests/test_mail_gateway.py @@ -5,7 +5,6 @@ import email import email.policy import socket -from email.utils import formataddr from unittest.mock import DEFAULT from unittest.mock import patch @@ -16,7 +15,11 @@ from odoo.addons.test_mail.models.test_mail_models import MailTestGateway from odoo.addons.test_mail.tests.common import BaseFunctionalTest, MockEmails from odoo.addons.test_mail.tests.common import mail_new_test_user from odoo.tests import tagged +<<<<<<< HEAD from odoo.tools import email_split_and_format, mute_logger, pycompat +======= +from odoo.tools import mute_logger, pycompat, formataddr +>>>>>>> [REF] mail: remove deprecated email formataddr def from_string(text): return email.message_from_string(pycompat.to_text(text), policy=email.policy.SMTP) @@ -27,12 +30,12 @@ class TestEmailParsing(BaseFunctionalTest, MockEmails): def test_message_parse_body(self): # test pure plaintext - plaintext = self.format(test_mail_data.MAIL_TEMPLATE_PLAINTEXT, email_from='Sylvie Lelitre <test.sylvie.lelitre@agrolait.com>') + plaintext = self.format(test_mail_data.MAIL_TEMPLATE_PLAINTEXT, email_from='"Sylvie Lelitre" <test.sylvie.lelitre@agrolait.com>') res = self.env['mail.thread'].message_parse(from_string(plaintext)) self.assertIn('Please call me as soon as possible this afternoon!', res['body']) # test multipart / text and html -> html has priority - multipart = self.format(MAIL_TEMPLATE, email_from='Sylvie Lelitre <test.sylvie.lelitre@agrolait.com>') + multipart = self.format(MAIL_TEMPLATE, email_from='"Sylvie Lelitre" <test.sylvie.lelitre@agrolait.com>') res = self.env['mail.thread'].message_parse(from_string(multipart)) self.assertIn('<p>Please call me as soon as possible this afternoon!</p>', res['body']) @@ -59,12 +62,12 @@ class TestEmailParsing(BaseFunctionalTest, MockEmails): def test_message_parse_eml(self): # Test that the parsing of mail with embedded emails as eml(msg) which generates empty attachments, can be processed. - mail = self.format(test_mail_data.MAIL_EML_ATTACHMENT, email_from='Sylvie Lelitre <test.sylvie.lelitre@agrolait.com>', to='generic@test.com') + mail = self.format(test_mail_data.MAIL_EML_ATTACHMENT, email_from='"Sylvie Lelitre" <test.sylvie.lelitre@agrolait.com>', to='generic@test.com') self.env['mail.thread'].message_parse(from_string(mail)) def test_message_parse_plaintext(self): """ Incoming email in plaintext should be stored as html """ - mail = self.format(test_mail_data.MAIL_TEMPLATE_PLAINTEXT, email_from='Sylvie Lelitre <test.sylvie.lelitre@agrolait.com>', to='generic@test.com') + mail = self.format(test_mail_data.MAIL_TEMPLATE_PLAINTEXT, email_from='"Sylvie Lelitre" <test.sylvie.lelitre@agrolait.com>', to='generic@test.com') res = self.env['mail.thread'].message_parse(from_string(mail)) self.assertIn('<pre>\nPlease call me as soon as possible this afternoon!\n\n--\nSylvie\n</pre>', res['body']) @@ -88,7 +91,7 @@ class TestMailgateway(BaseFunctionalTest, MockEmails): def setUpClass(cls): super(TestMailgateway, cls).setUpClass() cls.test_model = cls.env['ir.model']._get('mail.test.gateway') - cls.email_from = 'Sylvie Lelitre <test.sylvie.lelitre@agrolait.com>' + cls.email_from = '"Sylvie Lelitre" <test.sylvie.lelitre@agrolait.com>' cls.test_record = cls.env['mail.test.gateway'].with_context(cls._test_context).create({ 'name': 'Test', @@ -190,7 +193,7 @@ class TestMailgateway(BaseFunctionalTest, MockEmails): self.assertEqual(len(self._mails), 0, 'No notification / bounce should be sent') # Email recognized if partner has a formatted email - self.partner_1.write({'email': 'Valid Lelitre <%s>' % self.partner_1.email}) + self.partner_1.write({'email': '"Valid Lelitre" <%s>' % self.partner_1.email}) record = self.format_and_process(MAIL_TEMPLATE, self.partner_1.email, 'groups@test.com', subject='Test2') self.assertEqual(record.message_ids[0].author_id, self.partner_1, @@ -269,7 +272,7 @@ class TestMailgateway(BaseFunctionalTest, MockEmails): # Test bounce email self.assertEqual(self._mails[0].get('subject'), 'Re: Should Bounce') self.assertEqual(self._mails[0].get('email_to')[0], 'whatever-2a840@postmaster.twitter.com') - self.assertEqual(self._mails[0].get('email_from'), formataddr(('MAILER-DAEMON', 'bounce.test@test.com'))) + self.assertEqual(self._mails[0].get('email_from'), 'MAILER-DAEMON <bounce.test@test.com>') @mute_logger('odoo.addons.mail.models.mail_thread', 'odoo.models.unlink', 'odoo.addons.mail.models.mail_mail') def test_message_process_alias_followers_bounce(self): @@ -287,7 +290,7 @@ class TestMailgateway(BaseFunctionalTest, MockEmails): 'message_process: incoming email on Followers alias should send a bounce email') self.assertEqual(self._mails[0].get('subject'), 'Re: Should Bounce') self.assertEqual(self._mails[0].get('email_to')[0], 'whatever-2a840@postmaster.twitter.com') - self.assertEqual(self._mails[0].get('email_from'), formataddr(('MAILER-DAEMON', 'bounce.test@test.com'))) + self.assertEqual(self._mails[0].get('email_from'), 'MAILER-DAEMON <bounce.test@test.com>') # Test: partner on followers alias -> bounce self._init_mock_build_email() @@ -297,7 +300,7 @@ class TestMailgateway(BaseFunctionalTest, MockEmails): 'message_process: incoming email on Followers alias should send a bounce email') self.assertEqual(self._mails[0].get('subject'), 'Re: Should Bounce') self.assertEqual(self._mails[0].get('email_to')[0], 'whatever-2a840@postmaster.twitter.com') - self.assertEqual(self._mails[0].get('email_from'), formataddr(('MAILER-DAEMON', 'bounce.test@test.com'))) + self.assertEqual(self._mails[0].get('email_from'), 'MAILER-DAEMON <bounce.test@test.com>') @mute_logger('odoo.addons.mail.models.mail_thread', 'odoo.models') def test_message_process_alias_partner(self): @@ -421,7 +424,7 @@ class TestMailgateway(BaseFunctionalTest, MockEmails): # Test bounce email self.assertEqual(self._mails[0].get('subject'), 'Re: Should Bounce') self.assertEqual(self._mails[0].get('email_to')[0], 'whatever-2a840@postmaster.twitter.com') - self.assertEqual(self._mails[0].get('email_from'), formataddr(('MAILER-DAEMON', 'bounce.test@test.com'))) + self.assertEqual(self._mails[0].get('email_from'), 'MAILER-DAEMON <bounce.test@test.com>') @mute_logger('odoo.addons.mail.models.mail_thread') def test_message_process_bounce_alias(self): @@ -782,7 +785,7 @@ class TestMailThreadCC(BaseFunctionalTest, MockEmails): self.format_and_process(MAIL_TEMPLATE, self.email_from, 'cc_record@test.com', cc='cc2 <cc2@example.com>, cc3@example.com', target_model='mail.test.cc') cc = email_split_and_format(record.email_cc) - self.assertEqual(sorted(cc), ['cc1 <cc1@example.com>', 'cc2@example.com', 'cc3@example.com'], 'new cc should have been added on record (unique)') + self.assertEqual(sorted(cc), ['"cc1" <cc1@example.com>', 'cc2@example.com', 'cc3@example.com'], 'new cc should have been added on record (unique)') @mute_logger('odoo.addons.mail.models.mail_thread') def test_message_cc_update_no_old(self): @@ -792,4 +795,4 @@ class TestMailThreadCC(BaseFunctionalTest, MockEmails): self.format_and_process(MAIL_TEMPLATE, self.email_from, 'cc_record@test.com', cc='cc2 <cc2@example.com>, cc3@example.com', target_model='mail.test.cc') cc = email_split_and_format(record.email_cc) - self.assertEqual(sorted(cc), ['cc2 <cc2@example.com>', 'cc3@example.com'], 'new cc should have been added on record (unique)') + self.assertEqual(sorted(cc), ['"cc2" <cc2@example.com>', 'cc3@example.com'], 'new cc should have been added on record (unique)') diff --git a/addons/test_mail/tests/test_mail_message.py b/addons/test_mail/tests/test_mail_message.py index 6d3290a8a95..be4b5e7887d 100644 --- a/addons/test_mail/tests/test_mail_message.py +++ b/addons/test_mail/tests/test_mail_message.py @@ -1,12 +1,11 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. import base64 -from email.utils import formataddr from odoo.addons.test_mail.tests import common from odoo.addons.test_mail.tests.common import mail_new_test_user from odoo.exceptions import AccessError, except_orm -from odoo.tools import mute_logger +from odoo.tools import mute_logger, formataddr from odoo.tests import tagged diff --git a/addons/test_mail/tests/test_message_post.py b/addons/test_mail/tests/test_message_post.py index 2eecfb98b9e..4b4fa16493e 100644 --- a/addons/test_mail/tests/test_message_post.py +++ b/addons/test_mail/tests/test_message_post.py @@ -3,14 +3,13 @@ import base64 -from email.utils import formataddr from unittest.mock import patch from odoo.addons.test_mail.tests.common import BaseFunctionalTest, MockEmails, TestRecipients from odoo.addons.test_mail.data.test_mail_data import MAIL_TEMPLATE_PLAINTEXT from odoo.addons.test_mail.models.test_mail_models import MailTestSimple from odoo.exceptions import AccessError -from odoo.tools import mute_logger +from odoo.tools import mute_logger, formataddr class TestMessagePost(BaseFunctionalTest, TestRecipients, MockEmails): diff --git a/addons/test_mail/tests/test_message_track.py b/addons/test_mail/tests/test_message_track.py index 2771e7b8d45..c4482be9e9d 100644 --- a/addons/test_mail/tests/test_message_track.py +++ b/addons/test_mail/tests/test_message_track.py @@ -1,9 +1,7 @@ # -*- coding: utf-8 -*- - -from email.utils import formataddr from unittest.mock import patch - from odoo.addons.test_mail.tests import common +from odoo.tools import formataddr class TestTracking(common.BaseFunctionalTest, common.MockEmails): diff --git a/addons/test_mail/tests/test_performance.py b/addons/test_mail/tests/test_performance.py index 919d6ac9dd7..2d70c1b727c 100644 --- a/addons/test_mail/tests/test_performance.py +++ b/addons/test_mail/tests/test_performance.py @@ -2,11 +2,9 @@ # Part of Odoo. See LICENSE file for full copyright and licensing details. import base64 -from email.utils import formataddr - from odoo.tests.common import TransactionCase, users, warmup from odoo.tests import tagged -from odoo.tools import mute_logger +from odoo.tools import mute_logger, formataddr @tagged('mail_performance') |
