[IMP] crm: add test for '_message_get_suggested_recipients'

This method is still not really tested, except its override of 'mail.thread.cc'.
So better have a test, especially in CRM that has some specific overrides.
Formatted email and multi-email input are tested, to check their current
support. Next commits will try to improve that, notably avoid formatting
issues.

Task-2612945 (Mail: Defensive email formatting)

X-original-commit: odoo/odoo@ce1fb51352
Part-of: odoo/odoo#134934
This commit is contained in:
Thibault Delavallée
2023-09-11 15:22:09 +00:00
parent 31150660ca
commit 904d074c5c
5 changed files with 207 additions and 133 deletions
+4 -110
View File
@@ -11,7 +11,7 @@ from odoo.addons.crm.tests.common import TestCrmCommon, INCOMING_EMAIL
from odoo.addons.phone_validation.tools.phone_validation import phone_format
from odoo.exceptions import UserError
from odoo.tests.common import Form, tagged, users
from odoo.tools import formataddr, mute_logger
from odoo.tools import mute_logger
@tagged('lead_internals')
@@ -675,114 +675,6 @@ class TestCRMLead(TestCrmCommon):
self.assertEqual(new_lead.partner_id.email, 'unknown.sender@test.example.com')
self.assertEqual(new_lead.partner_id.team_id, self.sales_team_1)
@users('user_sales_manager')
def test_message_get_suggested_recipients(self):
"""This test checks that creating a contact from a lead with an inactive language will ignore the language
while creating a contact from a lead with an active language will take it into account """
ResLang = self.env['res.lang'].sudo().with_context(active_test=False)
# Create a lead with an inactive language -> should ignore the preset language
lang_fr = ResLang.search([('code', '=', 'fr_FR')])
if not lang_fr:
lang_fr = ResLang._create_lang('fr_FR')
# set French language as inactive then try to call "_message_get_suggested_recipients"
# -> lang code should be ignored
lang_fr.active = False
lead1 = self.env['crm.lead'].create({
'name': 'TestLead',
'email_from': self.test_email,
'lang_id': lang_fr.id,
})
data = lead1._message_get_suggested_recipients()[lead1.id]
self.assertEqual(len(data), 1)
suggested_partner_id, suggested_partner_email, suggested_lang, suggested_reason, create_vals = data[0]
self.assertFalse(suggested_partner_id)
self.assertEqual(suggested_partner_email, self.test_email)
self.assertEqual(suggested_lang, None)
self.assertEqual(suggested_reason, 'Customer Email')
self.assertEqual(create_vals['lang'], 'fr_FR')
# Create a lead with an active language -> should keep the preset language for recipients
lang_en = ResLang.search([('code', '=', 'en_US')])
if not lang_en:
lang_en = ResLang._create_lang('en_US')
# set American English language as active then try to call "_message_get_suggested_recipients"
# -> lang code should be kept
lang_en.active = True
lead2 = self.env['crm.lead'].create({
'name': 'TestLead',
'email_from': self.test_email,
'lang_id': lang_en.id,
})
data = lead2._message_get_suggested_recipients()[lead2.id]
self.assertEqual(len(data), 1)
suggested_partner_id, suggested_partner_email, suggested_lang, suggested_reason, create_vals = data[0]
self.assertFalse(suggested_partner_id)
self.assertEqual(suggested_partner_email, self.test_email)
self.assertEqual(suggested_lang, 'en_US')
self.assertEqual(suggested_reason, 'Customer Email')
self.assertEqual(create_vals['lang'], lang_en.code)
@users('user_sales_manager')
def test_message_get_suggested_recipients_auto_creation(self):
"""Check default creates value for auto creation of recipient
(customer)."""
lead_details_for_contact = {
'title': self.env.ref('base.res_partner_title_mister').id,
'street': '3rd Floor, Room 3-C',
'street2': '123 Arlington Avenue',
'zip': '13202',
'city': 'New York',
'country_id': self.env.ref('base.us').id,
'state_id': self.env.ref('base.state_us_39').id,
'website': 'https://www.arlington123.com/3f3c',
'phone': '678-728-0949',
'mobile': '661-606-0781',
'function': 'Delivery Boy',
'team_id': self.sales_team_1.id,
'user_id': self.user_sales_manager.id,
}
for partner_name, name, email in [
(False, 'Test', 'test_default_create@example.com'),
('Delivery Boy company', 'Test With Company', 'default_create_with_partner@example.com'),
('Delivery Boy company', '', 'default_create_with_partner_no_name@example.com'),
]:
formatted_email = formataddr((name, email))
with self.subTest(partner_name=partner_name):
lang = self.env['res.lang'].sudo().search([], limit=1)[0]
description = '<p>Top</p>'
lead1 = self.env['crm.lead'].create({
'name': 'TestLead',
'contact_name': name,
'email_from': formatted_email,
'lang_id': lang.id,
'description': description,
'partner_name': partner_name,
**lead_details_for_contact,
})
data = lead1._message_get_suggested_recipients()[lead1.id]
suggested_partner_id, suggested_partner_email, suggested_lang, suggested_reason, create_vals = data[0]
self.assertFalse(suggested_partner_id)
self.assertEqual(suggested_partner_email, formatted_email)
self.assertEqual(suggested_lang, lang.code)
self.assertEqual(suggested_reason, 'Customer Email')
self.assertEqual(create_vals, lead1._get_customer_information().get(email, {}))
for field, value in lead_details_for_contact.items():
self.assertEqual(create_vals.get(field), value)
expected_name = partner_name if partner_name and not name else name
self.assertEqual(create_vals['name'], expected_name)
self.assertEqual(create_vals['comment'], description) # description -> comment
# Parent company not created even if partner_name is set
self.assertFalse(create_vals.get('parent_id')) # not supported, even if partner_name set
self.assertEqual(create_vals['company_name'], partner_name) # partner_name -> company_name
expected_company_type = 'company' if partner_name and not name else 'person'
self.assertEqual(create_vals.get('company_type', 'person'), expected_company_type)
# Check that the creation of the contact won't fail
partner = self.env['res.partner'].create(create_vals)
partner.unlink()
@users('user_sales_manager')
def test_phone_mobile_search(self):
lead_1 = self.env['crm.lead'].create({
@@ -917,6 +809,8 @@ class TestCRMLead(TestCrmCommon):
self.assertFalse(lead.phone_sanitized)
class TestLeadFormatAddress(FormatAddressCase):
@tagged('lead_internals')
class TestLeadFormTools(FormatAddressCase):
def test_address_view(self):
self.assertAddressView('crm.lead')
+178 -1
View File
@@ -1,10 +1,186 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from .common import TestCrmCommon
from odoo.addons.crm.tests.common import TestCrmCommon
from odoo.tests import tagged, users
from odoo.tools import formataddr, mute_logger
@tagged('mail_thread', 'mail_gateway')
class NewLeadNotification(TestCrmCommon):
""" Test mail features support on lead + specific overrides and support """
@classmethod
def setUpClass(cls):
""" Activate some langs to test lang propagation in various mail flows """
super(NewLeadNotification, cls).setUpClass()
cls.test_email = '"Test Email" <test.email@example.com>'
model_lang = cls.env['res.lang'].sudo().with_context(active_test=False)
# Create a lead with an inactive language -> should ignore the preset language
cls.lang_fr = model_lang.search([('code', '=', 'fr_FR')])
if not cls.lang_fr:
cls.lang_fr = model_lang._create_lang('fr_FR')
# set French language as inactive then try to call "_message_get_suggested_recipients"
# -> lang code should be ignored
cls.lang_fr.active = False
# Create a lead with an active language -> should keep the preset language for recipients
cls.lang_en = model_lang.search([('code', '=', 'en_US')])
if not cls.lang_en:
cls.lang_en = model_lang._create_lang('en_US')
# set American English language as active then try to call "_message_get_suggested_recipients"
# -> lang code should be kept
cls.lang_en.active = True
@users('user_sales_manager')
def test_lead_message_get_suggested_recipients_email(self):
""" Test '_message_get_suggested_recipients' and its override in lead
when dealing with various emails. """
lead_format, lead_multi, lead_from, lead_partner = self.env['crm.lead'].create([
{
'email_from': '"New Customer" <new.customer.format@test.example.com>',
'name': 'Test Suggestion (email_from with format)',
'partner_name': 'Format Name',
'user_id': self.user_sales_leads.id,
}, {
'email_from': 'new.customer.multi.1@test.example.com, new.customer.2@test.example.com',
'name': 'Test Suggestion (email_from multi)',
'partner_name': 'Multi Name',
'user_id': self.user_sales_leads.id,
}, {
'email_from': 'new.customer.simple@test.example.com',
'name': 'Test Suggestion (email_from)',
'partner_name': 'Std Name',
'user_id': self.user_sales_leads.id,
}, {
'name': 'Test Suggestion (partner_id)',
'partner_id': self.contact_1.id,
'user_id': self.user_sales_leads.id,
}
])
for lead, expected in zip(
lead_format + lead_multi + lead_from + lead_partner,
[(False, '"New Customer" <new.customer.format@test.example.com>', None, 'Customer Email',
{'company_name': 'Format Name', 'email': 'new.customer.format@test.example.com',
'name': 'Format Name', 'user_id': self.user_sales_leads.id,
'team_id': self.sales_team_1.id,
}),
(False, 'new.customer.multi.1@test.example.com, new.customer.2@test.example.com', None, 'Customer Email',
{}), # no email_normalized -> no information
(False, 'new.customer.simple@test.example.com', None, 'Customer Email',
{'company_name': 'Std Name', 'email': 'new.customer.simple@test.example.com',
'name': 'Std Name', 'user_id': self.user_sales_leads.id,
'team_id': self.sales_team_1.id,
}),
(self.contact_1.id, '"Philip J Fry" <"Philip, J. Fry" <philip.j.fry@test.example.com>>', self.contact_1.lang, 'Customer',
{}), # creation values only if not partner
]
):
with self.subTest(lead=lead, lead_name=lead.name, email_from=lead.email_from):
res = lead._message_get_suggested_recipients()[lead.id]
self.assertEqual(len(res), 1)
self.assertEqual(res[0][:4], expected[:4])
customer_data = expected[4]
if not customer_data:
self.assertFalse(res[0][4])
else:
for partner_fname in customer_data:
found, expected = res[0][4][partner_fname], customer_data[partner_fname]
self.assertEqual(
found, expected,
f'Lead suggested customer: wrong value for {partner_fname} got {found} instead of {expected}')
@users('user_sales_manager')
def test_lead_message_get_suggested_recipients_langs(self):
"""This test checks that creating a contact from a lead with an inactive
language will ignore the language while creating a contact from a lead
with an active language will take it into account """
leads = self.env['crm.lead'].create([
{
'email_from': self.test_email,
'lang_id': self.lang_fr.id,
'name': 'TestLead',
}, {
'email_from': self.test_email,
'lang_id': self.lang_en.id,
'name': 'TestLead',
}
])
expected_list = [
(False, self.test_email, None, 'Customer Email', {'lang': 'fr_FR'}),
(False, self.test_email, 'en_US', 'Customer Email', {'lang': 'en_US'}),
]
for lead, expected in zip(leads, expected_list):
with self.subTest(lead=lead):
res = lead._message_get_suggested_recipients()[lead.id]
self.assertEqual(len(res), 1)
self.assertEqual(res[0][:4], expected[:4])
for partner_fname in expected[4]:
found = res[0][4][partner_fname]
expected = expected[4][partner_fname]
self.assertEqual(
found, expected,
f'Lead suggested customer: wrong value for {partner_fname} got {found} instead of {expected}')
@users('user_sales_manager')
def test_lead_message_get_suggested_recipients_values_for_create(self):
"""Check default creates value used when creating client from suggested
recipients (customer)."""
lead_details_for_contact = {
'title': self.env.ref('base.res_partner_title_mister').id,
'street': '3rd Floor, Room 3-C',
'street2': '123 Arlington Avenue',
'zip': '13202',
'city': 'New York',
'country_id': self.env.ref('base.us').id,
'state_id': self.env.ref('base.state_us_39').id,
'website': 'https://www.arlington123.com/3f3c',
'phone': '678-728-0949',
'mobile': '661-606-0781',
'function': 'Delivery Boy',
'team_id': self.sales_team_1.id,
'user_id': self.user_sales_manager.id,
}
for partner_name, name, email in [
(False, 'Test', 'test_default_create@example.com'),
('Delivery Boy company', 'Test With Company', 'default_create_with_partner@example.com'),
('Delivery Boy company', '', 'default_create_with_partner_no_name@example.com'),
]:
formatted_email = formataddr((name, email))
with self.subTest(partner_name=partner_name):
lang = self.env['res.lang'].sudo().search([], limit=1)[0]
description = '<p>Top</p>'
lead1 = self.env['crm.lead'].create({
'name': 'TestLead',
'contact_name': name,
'email_from': formatted_email,
'lang_id': lang.id,
'description': description,
'partner_name': partner_name,
**lead_details_for_contact,
})
data = lead1._message_get_suggested_recipients()[lead1.id]
suggested_partner_id, suggested_partner_email, suggested_lang, suggested_reason, create_vals = data[0]
self.assertFalse(suggested_partner_id)
self.assertEqual(suggested_partner_email, formatted_email)
self.assertEqual(suggested_lang, lang.code)
self.assertEqual(suggested_reason, 'Customer Email')
self.assertEqual(create_vals, lead1._get_customer_information().get(email, {}))
for field, value in lead_details_for_contact.items():
self.assertEqual(create_vals.get(field), value)
expected_name = partner_name if partner_name and not name else name
self.assertEqual(create_vals['name'], expected_name)
self.assertEqual(create_vals['comment'], description) # description -> comment
# Parent company not created even if partner_name is set
self.assertFalse(create_vals.get('parent_id')) # not supported, even if partner_name set
self.assertEqual(create_vals['company_name'], partner_name) # partner_name -> company_name
expected_company_type = 'company' if partner_name and not name else 'person'
self.assertEqual(create_vals.get('company_type', 'person'), expected_company_type)
# Check that the creation of the contact won't fail
partner = self.env['res.partner'].create(create_vals)
partner.unlink()
def test_new_lead_notification(self):
""" Test newly create leads like from the website. People and channels
@@ -39,6 +215,7 @@ class NewLeadNotification(TestCrmCommon):
lead_user = lead.with_user(self.user_sales_manager)
self.assertTrue(lead_user.message_needaction)
@mute_logger('odoo.addons.mail.models.mail_thread')
def test_new_lead_from_email_multicompany(self):
company0 = self.env.company
company1 = self.env['res.company'].create({'name': 'new_company'})
+1 -2
View File
@@ -1,8 +1,7 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo.addons.crm.tests.common import TestCrmCommon
from odoo.tests import HttpCase
from odoo.tests.common import tagged, users
from odoo.tests.common import tagged
@tagged('post_install', '-at_install')
@@ -360,25 +360,6 @@ class TestDiscuss(MailCommon, TestRecipients):
[(False, email, None, 'Customer Email', {'mobile': '+33199001015', 'phone': False})]
)
@users("employee")
@mute_logger('odoo.addons.mail.models.mail_mail')
def test_suggested_recipients_mail_cc(self):
""" MailThreadCC mixin adds its own suggested recipients management
coming from CC (carbon copy) management. """
record = self.env['mail.test.cc'].create({
'email_cc': 'cc1@example.com, cc2@example.com, cc3 <cc3@example.com>',
})
suggestions = record._message_get_suggested_recipients()[record.id]
self.assertEqual(
sorted(suggestions),
[
(False, '"cc3" <cc3@example.com>', None, 'CC Email', {}),
(False, 'cc1@example.com', None, 'CC Email', {}),
(False, 'cc2@example.com', None, 'CC Email', {}),
],
'cc should be in suggestions'
)
@users("employee")
def test_unlink_notification_message(self):
channel = self.env['discuss.channel'].create({'name': 'testChannel'})
@@ -4,7 +4,7 @@
from odoo import exceptions, tools
from odoo.addons.mail.tests.common import MailCommon
from odoo.addons.test_mail.tests.common import TestRecipients
from odoo.tests.common import tagged
from odoo.tests.common import tagged, users
from odoo.tools import mute_logger
@@ -59,3 +59,26 @@ class TestMailThread(MailCommon, TestRecipients):
self.assertTrue(new_record.is_blacklisted)
bl_record.unlink()
@tagged('mail_thread', 'mail_thread_cc')
class TestMailThreadCC(MailCommon):
@users("employee")
@mute_logger('odoo.addons.mail.models.mail_mail')
def test_suggested_recipients_mail_cc(self):
""" MailThreadCC mixin adds its own suggested recipients management
coming from CC (carbon copy) management. """
record = self.env['mail.test.cc'].create({
'email_cc': 'cc1@example.com, cc2@example.com, cc3 <cc3@example.com>',
})
suggestions = record._message_get_suggested_recipients()[record.id]
self.assertEqual(
sorted(suggestions),
[
(False, '"cc3" <cc3@example.com>', None, 'CC Email', {}),
(False, 'cc1@example.com', None, 'CC Email', {}),
(False, 'cc2@example.com', None, 'CC Email', {}),
],
'cc should be in suggestions'
)