[FIX] base: be defensive when computing parts of 'from_filter'

From filter could be ill-defined, like ' ' or ','. This commit just make
some code more defensive against those values.

Task-3453347 (Mail: Move Mail ICP from Base to Mail)
Prepares Task-36879 (Mail: Support MultiCompany Aliases)

Part-of: odoo/odoo#130750
This commit is contained in:
Thibault Delavallée
2023-08-22 20:58:53 +02:00
parent 79677134d7
commit 29f7e6d894
2 changed files with 7 additions and 5 deletions
+2 -4
View File
@@ -202,8 +202,7 @@ class IrMailServer(models.Model):
def _get_test_email_from(self):
self.ensure_one()
if self.from_filter:
from_filter_parts = [part.strip() for part in self.from_filter.split(",")]
if from_filter_parts := [part.strip() for part in (self.from_filter or '').split(",") if part.strip()]:
if mail_from := next((email for email in from_filter_parts if "@" in email), None):
# All emails will be sent from the same address
return mail_from
@@ -797,8 +796,7 @@ class IrMailServer(models.Model):
normalized_mail_from = email_normalize(email_from)
normalized_domain = email_domain_extract(normalized_mail_from)
for email_filter in from_filter.split(','):
email_filter = email_filter.strip()
for email_filter in [part.strip() for part in (from_filter or '').split(',') if part.strip()]:
if '@' in email_filter and email_normalize(email_filter) == normalized_mail_from:
return True
if '@' not in email_filter and email_domain_normalize(email_filter) == normalized_domain:
@@ -182,7 +182,9 @@ class TestIrMailServer(TransactionCase, MockSmtplibCase):
('notifications@example.com', 'example.com'),
# default relies on "odoo"
(False, 'example.com'),
# fallback on user email
# fallback on user email if no from_filter
('example_2.com', ' '),
('example_2.com', ','),
('example_2.com', False),
(False, False),
], [
@@ -194,6 +196,8 @@ class TestIrMailServer(TransactionCase, MockSmtplibCase):
'odoo@example.com',
self.env.user.email,
self.env.user.email,
self.env.user.email,
self.env.user.email,
],
):
with self.subTest(default_from=default_from, from_filter=from_filter):