2019-05-24 12:42:58 +02:00
|
|
|
# Copyright 2017 LasLabs Inc.
|
|
|
|
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).
|
|
|
|
|
2020-08-25 18:59:39 +02:00
|
|
|
import logging
|
2019-05-24 12:42:58 +02:00
|
|
|
import os
|
2020-06-30 16:21:11 +02:00
|
|
|
from email import message_from_string
|
2019-05-24 12:42:58 +02:00
|
|
|
|
2020-08-25 18:59:39 +02:00
|
|
|
import odoo.tools as tools
|
2020-09-04 22:45:56 +02:00
|
|
|
from odoo.exceptions import ValidationError
|
2019-05-24 12:42:58 +02:00
|
|
|
from odoo.tests.common import TransactionCase
|
|
|
|
|
2022-01-27 14:08:49 +01:00
|
|
|
from odoo.addons.base.tests.common import MockSmtplibCase
|
|
|
|
|
2020-08-25 18:59:39 +02:00
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
2019-05-24 12:42:58 +02:00
|
|
|
|
2022-01-27 14:08:49 +01:00
|
|
|
class TestIrMailServer(TransactionCase, MockSmtplibCase):
|
2022-01-26 09:22:37 +01:00
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
super().setUpClass()
|
|
|
|
cls.email_from = "derp@example.com"
|
|
|
|
cls.email_from_another = "another@example.com"
|
2022-01-27 14:08:49 +01:00
|
|
|
cls.IrMailServer = cls.env["ir.mail_server"]
|
2022-01-26 09:22:37 +01:00
|
|
|
cls.parameter_model = cls.env["ir.config_parameter"]
|
|
|
|
cls._delete_mail_servers()
|
2022-01-27 14:08:49 +01:00
|
|
|
cls.IrMailServer.create(
|
2020-06-30 16:21:11 +02:00
|
|
|
{
|
|
|
|
"name": "localhost",
|
|
|
|
"smtp_host": "localhost",
|
2022-01-26 09:22:37 +01:00
|
|
|
"smtp_from": cls.email_from,
|
2020-06-30 16:21:11 +02:00
|
|
|
}
|
|
|
|
)
|
2019-05-24 12:42:58 +02:00
|
|
|
message_file = os.path.join(
|
2020-06-30 16:21:11 +02:00
|
|
|
os.path.dirname(os.path.realpath(__file__)), "test.msg"
|
2019-05-24 12:42:58 +02:00
|
|
|
)
|
2020-06-30 16:21:11 +02:00
|
|
|
with open(message_file, "r") as fh:
|
2022-01-26 09:22:37 +01:00
|
|
|
cls.message = message_from_string(fh.read())
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def _delete_mail_servers(cls):
|
|
|
|
"""Delete all available mail servers"""
|
2022-01-27 14:08:49 +01:00
|
|
|
all_mail_servers = cls.IrMailServer.search([])
|
2022-01-26 09:22:37 +01:00
|
|
|
if all_mail_servers:
|
|
|
|
all_mail_servers.unlink()
|
2019-05-24 12:42:58 +02:00
|
|
|
|
2020-08-25 18:59:39 +02:00
|
|
|
def _init_mail_server_domain_whilelist_based(self):
|
|
|
|
self._delete_mail_servers()
|
2022-01-27 14:08:49 +01:00
|
|
|
self.assertFalse(self.IrMailServer.search([]))
|
|
|
|
self.mail_server_domainone = self.IrMailServer.create(
|
2020-08-25 18:59:39 +02:00
|
|
|
{
|
|
|
|
"name": "sandbox domainone",
|
|
|
|
"smtp_host": "localhost",
|
|
|
|
"smtp_from": "notifications@domainone.com",
|
|
|
|
"domain_whitelist": "domainone.com",
|
|
|
|
}
|
|
|
|
)
|
2022-01-27 14:08:49 +01:00
|
|
|
self.mail_server_domaintwo = self.IrMailServer.create(
|
2020-08-25 18:59:39 +02:00
|
|
|
{
|
|
|
|
"name": "sandbox domaintwo",
|
|
|
|
"smtp_host": "localhost",
|
|
|
|
"smtp_from": "hola@domaintwo.com",
|
|
|
|
"domain_whitelist": "domaintwo.com",
|
|
|
|
}
|
|
|
|
)
|
2022-01-27 14:08:49 +01:00
|
|
|
self.mail_server_domainthree = self.IrMailServer.create(
|
2020-08-25 18:59:39 +02:00
|
|
|
{
|
|
|
|
"name": "sandbox domainthree",
|
|
|
|
"smtp_host": "localhost",
|
|
|
|
"smtp_from": "notifications@domainthree.com",
|
|
|
|
"domain_whitelist": "domainthree.com,domainmulti.com",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
def _skip_test(self, reason):
|
|
|
|
_logger.warn(reason)
|
|
|
|
self.skipTest(reason)
|
|
|
|
|
2022-01-27 14:08:49 +01:00
|
|
|
def _send_mail(
|
|
|
|
self,
|
|
|
|
message,
|
|
|
|
mail_server_id=None,
|
|
|
|
smtp_server=None,
|
|
|
|
smtp_port=None,
|
|
|
|
smtp_user=None,
|
|
|
|
smtp_password=None,
|
|
|
|
smtp_encryption=None,
|
|
|
|
smtp_ssl_certificate=None,
|
|
|
|
smtp_ssl_private_key=None,
|
|
|
|
smtp_debug=False,
|
|
|
|
smtp_session=None,
|
|
|
|
):
|
|
|
|
smtp = smtp_session
|
|
|
|
if not smtp:
|
|
|
|
smtp = self.IrMailServer.connect(
|
|
|
|
smtp_server,
|
|
|
|
smtp_port,
|
|
|
|
smtp_user,
|
|
|
|
smtp_password,
|
|
|
|
smtp_encryption,
|
|
|
|
smtp_from=message["From"],
|
|
|
|
ssl_certificate=smtp_ssl_certificate,
|
|
|
|
ssl_private_key=smtp_ssl_private_key,
|
|
|
|
smtp_debug=smtp_debug,
|
|
|
|
mail_server_id=mail_server_id,
|
|
|
|
)
|
|
|
|
|
|
|
|
send_from, send_to, message_string = self.IrMailServer._prepare_email_message(
|
|
|
|
message, smtp
|
|
|
|
)
|
|
|
|
self.IrMailServer.send_email(message)
|
|
|
|
return message_string
|
2019-05-24 12:42:58 +02:00
|
|
|
|
|
|
|
def test_send_email_injects_from_no_canonical(self):
|
2020-12-07 04:54:55 +01:00
|
|
|
"""It should inject the FROM header correctly when no canonical name."""
|
2020-06-30 16:21:11 +02:00
|
|
|
self.message.replace_header("From", "test@example.com")
|
2022-01-27 14:08:49 +01:00
|
|
|
# A mail server is configured for the email
|
|
|
|
with self.mock_smtplib_connection():
|
|
|
|
message = self._send_mail(self.message)
|
2020-06-30 16:21:11 +02:00
|
|
|
self.assertEqual(message["From"], self.email_from)
|
2019-05-24 12:42:58 +02:00
|
|
|
|
|
|
|
def test_send_email_injects_from_with_canonical(self):
|
|
|
|
"""It should inject the FROM header correctly with a canonical name.
|
|
|
|
|
|
|
|
Note that there is an extra `<` in the canonical name to test for
|
|
|
|
proper handling in the split.
|
|
|
|
"""
|
2020-06-30 16:21:11 +02:00
|
|
|
user = "Test < User"
|
|
|
|
self.message.replace_header("From", "%s <test@example.com>" % user)
|
|
|
|
bounce_parameter = self.parameter_model.search(
|
|
|
|
[("key", "=", "mail.bounce.alias")]
|
|
|
|
)
|
2019-05-24 14:40:45 +02:00
|
|
|
if bounce_parameter:
|
|
|
|
# Remove mail.bounce.alias to test Return-Path
|
|
|
|
bounce_parameter.unlink()
|
|
|
|
# Also check passing mail_server_id
|
2022-01-27 14:08:49 +01:00
|
|
|
mail_server_id = (
|
|
|
|
self.IrMailServer.sudo().search([], order="sequence", limit=1)[0].id
|
|
|
|
)
|
|
|
|
# A mail server is configured for the email
|
|
|
|
with self.mock_smtplib_connection():
|
|
|
|
message = self._send_mail(self.message, mail_server_id=mail_server_id)
|
2020-08-25 18:59:39 +02:00
|
|
|
self.assertEqual(message["From"], '"{}" <{}>'.format(user, self.email_from))
|
|
|
|
self.assertEqual(
|
|
|
|
message["Return-Path"], '"{}" <{}>'.format(user, self.email_from)
|
|
|
|
)
|
|
|
|
|
2020-09-04 22:45:56 +02:00
|
|
|
def test_01_from_outgoing_server_domainone(self):
|
2020-08-25 18:59:39 +02:00
|
|
|
self._init_mail_server_domain_whilelist_based()
|
|
|
|
domain = "domainone.com"
|
|
|
|
email_from = "Mitchell Admin <admin@%s>" % domain
|
|
|
|
expected_mail_server = self.mail_server_domainone
|
|
|
|
|
|
|
|
self.message.replace_header("From", email_from)
|
2022-01-27 14:08:49 +01:00
|
|
|
# A mail server is configured for the email
|
|
|
|
with self.mock_smtplib_connection():
|
|
|
|
message = self._send_mail(self.message)
|
2020-08-25 18:59:39 +02:00
|
|
|
self.assertEqual(message["From"], email_from)
|
|
|
|
|
2022-01-27 14:08:49 +01:00
|
|
|
used_mail_server = self.IrMailServer._get_mail_sever(domain)
|
|
|
|
used_mail_server = self.IrMailServer.browse(used_mail_server)
|
2020-08-25 18:59:39 +02:00
|
|
|
self.assertEqual(
|
|
|
|
used_mail_server,
|
|
|
|
expected_mail_server,
|
|
|
|
"It using %s but we expect to use %s"
|
|
|
|
% (used_mail_server.name, expected_mail_server.name),
|
|
|
|
)
|
|
|
|
|
2020-09-04 22:45:56 +02:00
|
|
|
def test_02_from_outgoing_server_domaintwo(self):
|
2020-08-25 18:59:39 +02:00
|
|
|
self._init_mail_server_domain_whilelist_based()
|
|
|
|
domain = "domaintwo.com"
|
|
|
|
email_from = "Mitchell Admin <admin@%s>" % domain
|
|
|
|
expected_mail_server = self.mail_server_domaintwo
|
|
|
|
|
|
|
|
self.message.replace_header("From", email_from)
|
2022-01-27 14:08:49 +01:00
|
|
|
# A mail server is configured for the email
|
|
|
|
with self.mock_smtplib_connection():
|
|
|
|
message = self._send_mail(self.message)
|
2020-08-25 18:59:39 +02:00
|
|
|
self.assertEqual(message["From"], email_from)
|
|
|
|
|
2022-01-27 14:08:49 +01:00
|
|
|
used_mail_server = self.IrMailServer._get_mail_sever(domain)
|
|
|
|
used_mail_server = self.IrMailServer.browse(used_mail_server)
|
2020-08-25 18:59:39 +02:00
|
|
|
self.assertEqual(
|
|
|
|
used_mail_server,
|
|
|
|
expected_mail_server,
|
|
|
|
"It using %s but we expect to use %s"
|
|
|
|
% (used_mail_server.name, expected_mail_server.name),
|
|
|
|
)
|
|
|
|
|
2020-09-04 22:45:56 +02:00
|
|
|
def test_03_from_outgoing_server_another(self):
|
2020-08-25 18:59:39 +02:00
|
|
|
self._init_mail_server_domain_whilelist_based()
|
|
|
|
domain = "example.com"
|
|
|
|
email_from = "Mitchell Admin <admin@%s>" % domain
|
|
|
|
expected_mail_server = self.mail_server_domainone
|
|
|
|
|
|
|
|
self.message.replace_header("From", email_from)
|
2022-01-27 14:08:49 +01:00
|
|
|
# A mail server is configured for the email
|
|
|
|
with self.mock_smtplib_connection():
|
|
|
|
message = self._send_mail(self.message)
|
2020-08-25 18:59:39 +02:00
|
|
|
self.assertEqual(
|
|
|
|
message["From"], "Mitchell Admin <%s>" % expected_mail_server.smtp_from
|
|
|
|
)
|
|
|
|
|
2022-01-27 14:08:49 +01:00
|
|
|
used_mail_server = self.IrMailServer._get_mail_sever(domain)
|
|
|
|
used_mail_server = self.IrMailServer.browse(used_mail_server)
|
2020-08-25 18:59:39 +02:00
|
|
|
self.assertEqual(
|
|
|
|
used_mail_server,
|
|
|
|
expected_mail_server,
|
|
|
|
"It using %s but we expect to use %s"
|
|
|
|
% (used_mail_server.name, expected_mail_server.name),
|
|
|
|
)
|
|
|
|
|
2020-09-04 22:45:56 +02:00
|
|
|
def test_04_from_outgoing_server_none_use_config(self):
|
2020-08-25 18:59:39 +02:00
|
|
|
self._init_mail_server_domain_whilelist_based()
|
|
|
|
domain = "example.com"
|
|
|
|
email_from = "Mitchell Admin <admin@%s>" % domain
|
|
|
|
|
|
|
|
self._delete_mail_servers()
|
2022-01-27 14:08:49 +01:00
|
|
|
self.assertFalse(self.IrMailServer.search([]))
|
2020-08-25 18:59:39 +02:00
|
|
|
# Find config values
|
|
|
|
config_smtp_from = tools.config.get("smtp_from")
|
|
|
|
config_smtp_domain_whitelist = tools.config.get("smtp_domain_whitelist")
|
|
|
|
if not config_smtp_from or not config_smtp_domain_whitelist:
|
|
|
|
self._skip_test(
|
|
|
|
"Cannot test transactions because there is not either smtp_from"
|
|
|
|
" or smtp_domain_whitelist."
|
|
|
|
)
|
|
|
|
|
|
|
|
self.message.replace_header("From", email_from)
|
2022-01-27 14:08:49 +01:00
|
|
|
# A mail server is configured for the email
|
|
|
|
with self.mock_smtplib_connection():
|
|
|
|
message = self._send_mail(self.message)
|
2020-08-25 18:59:39 +02:00
|
|
|
self.assertEqual(message["From"], "Mitchell Admin <%s>" % config_smtp_from)
|
|
|
|
|
2022-01-27 14:08:49 +01:00
|
|
|
used_mail_server = self.IrMailServer._get_mail_sever("example.com")
|
|
|
|
used_mail_server = self.IrMailServer.browse(used_mail_server)
|
2020-08-25 18:59:39 +02:00
|
|
|
self.assertFalse(
|
|
|
|
used_mail_server, "using this mail server %s" % (used_mail_server.name)
|
|
|
|
)
|
|
|
|
|
2020-09-04 22:45:56 +02:00
|
|
|
def test_05_from_outgoing_server_none_same_domain(self):
|
2020-08-25 18:59:39 +02:00
|
|
|
self._init_mail_server_domain_whilelist_based()
|
|
|
|
|
|
|
|
# Find config values
|
|
|
|
config_smtp_from = tools.config.get("smtp_from")
|
|
|
|
config_smtp_domain_whitelist = domain = tools.config.get(
|
|
|
|
"smtp_domain_whitelist"
|
|
|
|
)
|
|
|
|
if not config_smtp_from or not config_smtp_domain_whitelist:
|
|
|
|
self._skip_test(
|
|
|
|
"Cannot test transactions because there is not either smtp_from"
|
|
|
|
" or smtp_domain_whitelist."
|
|
|
|
)
|
|
|
|
|
|
|
|
email_from = "Mitchell Admin <admin@%s>" % domain
|
|
|
|
|
|
|
|
self._delete_mail_servers()
|
2022-01-27 14:08:49 +01:00
|
|
|
self.assertFalse(self.IrMailServer.search([]))
|
2020-08-25 18:59:39 +02:00
|
|
|
self.message.replace_header("From", email_from)
|
2022-01-27 14:08:49 +01:00
|
|
|
# A mail server is configured for the email
|
|
|
|
with self.mock_smtplib_connection():
|
|
|
|
message = self._send_mail(self.message)
|
2020-08-25 18:59:39 +02:00
|
|
|
self.assertEqual(message["From"], email_from)
|
|
|
|
|
2022-01-27 14:08:49 +01:00
|
|
|
used_mail_server = self.IrMailServer._get_mail_sever(domain)
|
|
|
|
used_mail_server = self.IrMailServer.browse(used_mail_server)
|
2020-08-25 18:59:39 +02:00
|
|
|
self.assertFalse(used_mail_server)
|
|
|
|
|
2020-09-04 22:45:56 +02:00
|
|
|
def test_06_from_outgoing_server_no_name_from(self):
|
2020-08-25 18:59:39 +02:00
|
|
|
self._init_mail_server_domain_whilelist_based()
|
|
|
|
domain = "example.com"
|
|
|
|
email_from = "test@%s" % domain
|
|
|
|
expected_mail_server = self.mail_server_domainone
|
|
|
|
|
|
|
|
self.message.replace_header("From", email_from)
|
2022-01-27 14:08:49 +01:00
|
|
|
# A mail server is configured for the email
|
|
|
|
with self.mock_smtplib_connection():
|
|
|
|
message = self._send_mail(self.message)
|
2020-08-25 18:59:39 +02:00
|
|
|
self.assertEqual(message["From"], expected_mail_server.smtp_from)
|
|
|
|
|
2022-01-27 14:08:49 +01:00
|
|
|
used_mail_server = self.IrMailServer._get_mail_sever(domain)
|
|
|
|
used_mail_server = self.IrMailServer.browse(used_mail_server)
|
2020-08-25 18:59:39 +02:00
|
|
|
self.assertEqual(
|
|
|
|
used_mail_server,
|
|
|
|
expected_mail_server,
|
|
|
|
"It using %s but we expect to use %s"
|
|
|
|
% (used_mail_server.name, expected_mail_server.name),
|
|
|
|
)
|
|
|
|
|
2020-09-04 22:45:56 +02:00
|
|
|
def test_07_from_outgoing_server_multidomain_1(self):
|
2020-08-25 18:59:39 +02:00
|
|
|
self._init_mail_server_domain_whilelist_based()
|
|
|
|
domain = "domainthree.com"
|
|
|
|
email_from = "Mitchell Admin <admin@%s>" % domain
|
|
|
|
expected_mail_server = self.mail_server_domainthree
|
|
|
|
|
|
|
|
self.message.replace_header("From", email_from)
|
2022-01-27 14:08:49 +01:00
|
|
|
# A mail server is configured for the email
|
|
|
|
with self.mock_smtplib_connection():
|
|
|
|
message = self._send_mail(self.message)
|
2020-08-25 18:59:39 +02:00
|
|
|
self.assertEqual(message["From"], email_from)
|
|
|
|
|
2022-01-27 14:08:49 +01:00
|
|
|
used_mail_server = self.IrMailServer._get_mail_sever(domain)
|
|
|
|
used_mail_server = self.IrMailServer.browse(used_mail_server)
|
2020-08-25 18:59:39 +02:00
|
|
|
self.assertEqual(
|
|
|
|
used_mail_server,
|
|
|
|
expected_mail_server,
|
|
|
|
"It using %s but we expect to use %s"
|
|
|
|
% (used_mail_server.name, expected_mail_server.name),
|
|
|
|
)
|
|
|
|
|
2020-09-04 22:45:56 +02:00
|
|
|
def test_08_from_outgoing_server_multidomain_3(self):
|
2020-08-25 18:59:39 +02:00
|
|
|
self._init_mail_server_domain_whilelist_based()
|
|
|
|
domain = "domainmulti.com"
|
|
|
|
email_from = "test@%s" % domain
|
|
|
|
expected_mail_server = self.mail_server_domainthree
|
|
|
|
|
|
|
|
self.message.replace_header("From", email_from)
|
2022-01-27 14:08:49 +01:00
|
|
|
# A mail server is configured for the email
|
|
|
|
with self.mock_smtplib_connection():
|
|
|
|
message = self._send_mail(self.message)
|
2020-08-25 18:59:39 +02:00
|
|
|
self.assertEqual(message["From"], email_from)
|
|
|
|
|
2022-01-27 14:08:49 +01:00
|
|
|
used_mail_server = self.IrMailServer._get_mail_sever(domain)
|
|
|
|
used_mail_server = self.IrMailServer.browse(used_mail_server)
|
2019-05-24 12:42:58 +02:00
|
|
|
self.assertEqual(
|
2020-08-25 18:59:39 +02:00
|
|
|
used_mail_server,
|
|
|
|
expected_mail_server,
|
|
|
|
"It using %s but we expect to use %s"
|
|
|
|
% (used_mail_server.name, expected_mail_server.name),
|
2019-05-24 14:40:45 +02:00
|
|
|
)
|
2020-09-04 22:45:56 +02:00
|
|
|
|
|
|
|
def test_09_not_valid_domain_whitelist(self):
|
|
|
|
self._init_mail_server_domain_whilelist_based()
|
|
|
|
mail_server = self.mail_server_domainone
|
|
|
|
mail_server.domain_whitelist = "example.com"
|
|
|
|
error_msg = (
|
|
|
|
"%s is not a valid domain. Please define a list of valid"
|
|
|
|
" domains separated by comma"
|
|
|
|
)
|
|
|
|
|
|
|
|
with self.assertRaisesRegex(ValidationError, error_msg % "asdasd"):
|
|
|
|
mail_server.domain_whitelist = "asdasd"
|
|
|
|
|
|
|
|
with self.assertRaisesRegex(ValidationError, error_msg % "asdasd"):
|
|
|
|
mail_server.domain_whitelist = "example.com, asdasd"
|
|
|
|
|
|
|
|
with self.assertRaisesRegex(ValidationError, error_msg % "invalid"):
|
|
|
|
mail_server.domain_whitelist = "example.com; invalid"
|
|
|
|
|
|
|
|
with self.assertRaisesRegex(ValidationError, error_msg % ";"):
|
|
|
|
mail_server.domain_whitelist = ";"
|
|
|
|
|
|
|
|
with self.assertRaisesRegex(ValidationError, error_msg % "."):
|
|
|
|
mail_server.domain_whitelist = "hola.com,."
|
|
|
|
|
|
|
|
def test_10_not_valid_smtp_from(self):
|
|
|
|
self._init_mail_server_domain_whilelist_based()
|
|
|
|
mail_server = self.mail_server_domainone
|
|
|
|
error_msg = "Not a valid Email From"
|
|
|
|
|
|
|
|
with self.assertRaisesRegex(ValidationError, error_msg):
|
|
|
|
mail_server.smtp_from = "asdasd"
|
|
|
|
|
|
|
|
with self.assertRaisesRegex(ValidationError, error_msg):
|
|
|
|
mail_server.smtp_from = "example.com"
|
|
|
|
|
|
|
|
with self.assertRaisesRegex(ValidationError, error_msg):
|
|
|
|
mail_server.smtp_from = "."
|
|
|
|
|
|
|
|
mail_server.smtp_from = "notifications@test.com"
|