From 461a1ef460175314ecef2e3bc1fc9ca20b068f12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Wed, 28 Jul 2021 09:00:43 +0000 Subject: [PATCH] [IMP] mail: add tests for email finding tools Purpose is to better understand the use of email_re regex (find emails in a text) and email_split_tuples (find name and email in a string used in email headers or as email_from / email_to / email_cc). Task ID-2377974 Community PR odoo/odoo#61467 --- addons/mail/tests/test_mail_tools.py | 110 +++++++++++++++++++++++++++ odoo/tools/mail.py | 4 +- 2 files changed, 113 insertions(+), 1 deletion(-) diff --git a/addons/mail/tests/test_mail_tools.py b/addons/mail/tests/test_mail_tools.py index 931936ed353..f66a586a93e 100644 --- a/addons/mail/tests/test_mail_tools.py +++ b/addons/mail/tests/test_mail_tools.py @@ -3,6 +3,7 @@ from odoo.addons.mail.tests.common import MailCommon from odoo.tests import tagged, users +from odoo import tools @tagged('mail_tools') @@ -21,6 +22,30 @@ class TestMailTools(MailCommon): 'phone': '0456334455', }) + cls.sources = [ + # single email + 'alfred.astaire@test.example.com', + ' alfred.astaire@test.example.com ', + 'Fredo The Great ', + '"Fredo The Great" ', + 'Fredo "The Great" ', + # multiple emails + 'alfred.astaire@test.example.com, evelyne.gargouillis@test.example.com', + 'Fredo The Great , Evelyne The Goat ', + '"Fredo The Great" , evelyne.gargouillis@test.example.com', + '"Fredo The Great" , ', + # text containing email + 'Hello alfred.astaire@test.example.com how are you ?', + '

Hello alfred.astaire@test.example.com

', + # text containing emails + 'Hello "Fredo" , evelyne.gargouillis@test.example.com', + 'Hello "Fredo" and evelyne.gargouillis@test.example.com', + # falsy + '

Hello Fredo

', + 'j\'adore écrire des @gmail.com ou "@gmail.com" a bit randomly', + '', + ] + @users('employee') def test_find_partner_from_emails(self): Partner = self.env['res.partner'] @@ -81,3 +106,88 @@ class TestMailTools(MailCommon): # test with wildcard "_" found = Partner._mail_find_partner_from_emails(['alfred_astaire@test.example.com']) self.assertEqual(found, [self.env['res.partner']]) + + @users('employee') + def test_tools_email_re(self): + expected = [ + # single email + ['alfred.astaire@test.example.com'], + ['alfred.astaire@test.example.com'], + ['alfred.astaire@test.example.com'], + ['alfred.astaire@test.example.com'], + ['alfred.astaire@test.example.com'], + # multiple emails + ['alfred.astaire@test.example.com', 'evelyne.gargouillis@test.example.com'], + ['alfred.astaire@test.example.com', 'evelyne.gargouillis@test.example.com'], + ['alfred.astaire@test.example.com', 'evelyne.gargouillis@test.example.com'], + ['alfred.astaire@test.example.com', 'evelyne.gargouillis@test.example.com'], + # text containing email + ['alfred.astaire@test.example.com'], + ['alfred.astaire@test.example.com'], + # text containing emails + ['alfred.astaire@test.example.com', 'evelyne.gargouillis@test.example.com'], + ['alfred.astaire@test.example.com', 'evelyne.gargouillis@test.example.com'], + # falsy + [], [], [], + ] + + for src, exp in zip(self.sources, expected): + res = tools.email_re.findall(src) + self.assertEqual( + res, exp, + 'Seems email_re is broken with %s (expected %r, received %r)' % (src, exp, res) + ) + + @users('employee') + def test_tools_email_split_tuples(self): + expected = [ + # single email + [('', 'alfred.astaire@test.example.com')], + [('', 'alfred.astaire@test.example.com')], + [('Fredo The Great', 'alfred.astaire@test.example.com')], + [('Fredo The Great', 'alfred.astaire@test.example.com')], + [('Fredo The Great', 'alfred.astaire@test.example.com')], + # multiple emails + [('', 'alfred.astaire@test.example.com'), ('', 'evelyne.gargouillis@test.example.com')], + [('Fredo The Great', 'alfred.astaire@test.example.com'), ('Evelyne The Goat', 'evelyne.gargouillis@test.example.com')], + [('Fredo The Great', 'alfred.astaire@test.example.com'), ('', 'evelyne.gargouillis@test.example.com')], + [('Fredo The Great', 'alfred.astaire@test.example.com'), ('', 'evelyne.gargouillis@test.example.com')], + # text containing email -> probably not designed for that + [('', 'Hello alfred.astaire@test.example.comhowareyou?')], + [('', 'Hello alfred.astaire@test.example.com')], + # text containing emails -> probably not designed for that + [('Hello Fredo', 'alfred.astaire@test.example.com'), ('', 'evelyne.gargouillis@test.example.com')], + [('Hello Fredo', 'alfred.astaire@test.example.com'), ('', 'and evelyne.gargouillis@test.example.com')], + # falsy -> probably not designed for that + [], [('', "j'adore écrire des@gmail.comou"), ('', '@gmail.com')], [], + ] + + for src, exp in zip(self.sources, expected): + res = tools.email_split_tuples(src) + self.assertEqual( + res, exp, + 'Seems email_split_tuples is broken with %s (expected %r, received %r)' % (src, exp, res) + ) + + @users('employee') + def test_tools_single_email_re(self): + expected = [ + # single email + ['alfred.astaire@test.example.com'], + [], [], [], [], # formatting issue for single email re + # multiple emails -> couic + [], [], [], [], + # text containing email -> couic + [], [], + # text containing emails -> couic + [], [], + # falsy + [], [], [], + ] + + for src, exp in zip(self.sources, expected): + res = tools.single_email_re.findall(src) + self.assertEqual( + res, exp, + 'Seems single_email_re is broken with %s (expected %r, received %r)' % (src, exp, res) + ) diff --git a/odoo/tools/mail.py b/odoo/tools/mail.py index 11b29e0ecec..b3a03d31e9f 100644 --- a/odoo/tools/mail.py +++ b/odoo/tools/mail.py @@ -471,7 +471,9 @@ def generate_tracking_message_id(res_id): return "<%s.%.15f-openerp-%s@%s>" % (rndstr, time.time(), res_id, socket.gethostname()) def email_split_tuples(text): - """ Return a list of (name, email) address tuples found in ``text``""" + """ Return a list of (name, email) address tuples found in ``text`` . Note + that text should be an email header or a stringified email list as it may + give broader results than expected on actual text. """ if not text: return [] return [(addr[0], addr[1]) for addr in getaddresses([text])