fixup! [MIG] mail_outbound_static: Migration to 15.0

This commit is contained in:
Maksym Yankin 2022-01-27 15:08:49 +02:00
parent b7bfb6d15f
commit 44e34aa1c0

View File

@ -3,28 +3,27 @@
import logging import logging
import os import os
import threading
from email import message_from_string from email import message_from_string
from mock import MagicMock
import odoo.tools as tools import odoo.tools as tools
from odoo.exceptions import ValidationError from odoo.exceptions import ValidationError
from odoo.tests.common import TransactionCase from odoo.tests.common import TransactionCase
from odoo.addons.base.tests.common import MockSmtplibCase
_logger = logging.getLogger(__name__) _logger = logging.getLogger(__name__)
class TestIrMailServer(TransactionCase): class TestIrMailServer(TransactionCase, MockSmtplibCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
super().setUpClass() super().setUpClass()
cls.email_from = "derp@example.com" cls.email_from = "derp@example.com"
cls.email_from_another = "another@example.com" cls.email_from_another = "another@example.com"
cls.Model = cls.env["ir.mail_server"] cls.IrMailServer = cls.env["ir.mail_server"]
cls.parameter_model = cls.env["ir.config_parameter"] cls.parameter_model = cls.env["ir.config_parameter"]
cls._delete_mail_servers() cls._delete_mail_servers()
cls.Model.create( cls.IrMailServer.create(
{ {
"name": "localhost", "name": "localhost",
"smtp_host": "localhost", "smtp_host": "localhost",
@ -40,14 +39,14 @@ class TestIrMailServer(TransactionCase):
@classmethod @classmethod
def _delete_mail_servers(cls): def _delete_mail_servers(cls):
"""Delete all available mail servers""" """Delete all available mail servers"""
all_mail_servers = cls.Model.search([]) all_mail_servers = cls.IrMailServer.search([])
if all_mail_servers: if all_mail_servers:
all_mail_servers.unlink() all_mail_servers.unlink()
def _init_mail_server_domain_whilelist_based(self): def _init_mail_server_domain_whilelist_based(self):
self._delete_mail_servers() self._delete_mail_servers()
self.assertFalse(self.Model.search([])) self.assertFalse(self.IrMailServer.search([]))
self.mail_server_domainone = self.Model.create( self.mail_server_domainone = self.IrMailServer.create(
{ {
"name": "sandbox domainone", "name": "sandbox domainone",
"smtp_host": "localhost", "smtp_host": "localhost",
@ -55,7 +54,7 @@ class TestIrMailServer(TransactionCase):
"domain_whitelist": "domainone.com", "domain_whitelist": "domainone.com",
} }
) )
self.mail_server_domaintwo = self.Model.create( self.mail_server_domaintwo = self.IrMailServer.create(
{ {
"name": "sandbox domaintwo", "name": "sandbox domaintwo",
"smtp_host": "localhost", "smtp_host": "localhost",
@ -63,7 +62,7 @@ class TestIrMailServer(TransactionCase):
"domain_whitelist": "domaintwo.com", "domain_whitelist": "domaintwo.com",
} }
) )
self.mail_server_domainthree = self.Model.create( self.mail_server_domainthree = self.IrMailServer.create(
{ {
"name": "sandbox domainthree", "name": "sandbox domainthree",
"smtp_host": "localhost", "smtp_host": "localhost",
@ -76,27 +75,47 @@ class TestIrMailServer(TransactionCase):
_logger.warn(reason) _logger.warn(reason)
self.skipTest(reason) self.skipTest(reason)
def _send_mail(self, message=None, mail_server_id=None, smtp_server=None): def _send_mail(
if message is None: self,
message = self.message message,
connect = MagicMock() mail_server_id=None,
thread = threading.currentThread() smtp_server=None,
thread.testing = False smtp_port=None,
try: smtp_user=None,
self.Model._patch_method("connect", connect) smtp_password=None,
try: smtp_encryption=None,
self.Model.send_email(message, mail_server_id, smtp_server) smtp_ssl_certificate=None,
finally: smtp_ssl_private_key=None,
self.Model._revert_method("connect") smtp_debug=False,
finally: smtp_session=None,
thread.testing = True ):
send_from, send_to, message_string = connect().sendmail.call_args[0] smtp = smtp_session
return message_from_string(message_string) 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
def test_send_email_injects_from_no_canonical(self): def test_send_email_injects_from_no_canonical(self):
"""It should inject the FROM header correctly when no canonical name.""" """It should inject the FROM header correctly when no canonical name."""
self.message.replace_header("From", "test@example.com") self.message.replace_header("From", "test@example.com")
message = self._send_mail() # A mail server is configured for the email
with self.mock_smtplib_connection():
message = self._send_mail(self.message)
self.assertEqual(message["From"], self.email_from) self.assertEqual(message["From"], self.email_from)
def test_send_email_injects_from_with_canonical(self): def test_send_email_injects_from_with_canonical(self):
@ -114,8 +133,12 @@ class TestIrMailServer(TransactionCase):
# Remove mail.bounce.alias to test Return-Path # Remove mail.bounce.alias to test Return-Path
bounce_parameter.unlink() bounce_parameter.unlink()
# Also check passing mail_server_id # Also check passing mail_server_id
mail_server_id = self.Model.sudo().search([], order="sequence", limit=1)[0].id mail_server_id = (
message = self._send_mail(mail_server_id=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)
self.assertEqual(message["From"], '"{}" <{}>'.format(user, self.email_from)) self.assertEqual(message["From"], '"{}" <{}>'.format(user, self.email_from))
self.assertEqual( self.assertEqual(
message["Return-Path"], '"{}" <{}>'.format(user, self.email_from) message["Return-Path"], '"{}" <{}>'.format(user, self.email_from)
@ -128,11 +151,13 @@ class TestIrMailServer(TransactionCase):
expected_mail_server = self.mail_server_domainone expected_mail_server = self.mail_server_domainone
self.message.replace_header("From", email_from) self.message.replace_header("From", email_from)
message = self._send_mail() # A mail server is configured for the email
with self.mock_smtplib_connection():
message = self._send_mail(self.message)
self.assertEqual(message["From"], email_from) self.assertEqual(message["From"], email_from)
used_mail_server = self.Model._get_mail_sever(domain) used_mail_server = self.IrMailServer._get_mail_sever(domain)
used_mail_server = self.Model.browse(used_mail_server) used_mail_server = self.IrMailServer.browse(used_mail_server)
self.assertEqual( self.assertEqual(
used_mail_server, used_mail_server,
expected_mail_server, expected_mail_server,
@ -147,11 +172,13 @@ class TestIrMailServer(TransactionCase):
expected_mail_server = self.mail_server_domaintwo expected_mail_server = self.mail_server_domaintwo
self.message.replace_header("From", email_from) self.message.replace_header("From", email_from)
message = self._send_mail() # A mail server is configured for the email
with self.mock_smtplib_connection():
message = self._send_mail(self.message)
self.assertEqual(message["From"], email_from) self.assertEqual(message["From"], email_from)
used_mail_server = self.Model._get_mail_sever(domain) used_mail_server = self.IrMailServer._get_mail_sever(domain)
used_mail_server = self.Model.browse(used_mail_server) used_mail_server = self.IrMailServer.browse(used_mail_server)
self.assertEqual( self.assertEqual(
used_mail_server, used_mail_server,
expected_mail_server, expected_mail_server,
@ -166,13 +193,15 @@ class TestIrMailServer(TransactionCase):
expected_mail_server = self.mail_server_domainone expected_mail_server = self.mail_server_domainone
self.message.replace_header("From", email_from) self.message.replace_header("From", email_from)
message = self._send_mail() # A mail server is configured for the email
with self.mock_smtplib_connection():
message = self._send_mail(self.message)
self.assertEqual( self.assertEqual(
message["From"], "Mitchell Admin <%s>" % expected_mail_server.smtp_from message["From"], "Mitchell Admin <%s>" % expected_mail_server.smtp_from
) )
used_mail_server = self.Model._get_mail_sever(domain) used_mail_server = self.IrMailServer._get_mail_sever(domain)
used_mail_server = self.Model.browse(used_mail_server) used_mail_server = self.IrMailServer.browse(used_mail_server)
self.assertEqual( self.assertEqual(
used_mail_server, used_mail_server,
expected_mail_server, expected_mail_server,
@ -186,7 +215,7 @@ class TestIrMailServer(TransactionCase):
email_from = "Mitchell Admin <admin@%s>" % domain email_from = "Mitchell Admin <admin@%s>" % domain
self._delete_mail_servers() self._delete_mail_servers()
self.assertFalse(self.Model.search([])) self.assertFalse(self.IrMailServer.search([]))
# Find config values # Find config values
config_smtp_from = tools.config.get("smtp_from") config_smtp_from = tools.config.get("smtp_from")
config_smtp_domain_whitelist = tools.config.get("smtp_domain_whitelist") config_smtp_domain_whitelist = tools.config.get("smtp_domain_whitelist")
@ -197,11 +226,13 @@ class TestIrMailServer(TransactionCase):
) )
self.message.replace_header("From", email_from) self.message.replace_header("From", email_from)
message = self._send_mail() # A mail server is configured for the email
with self.mock_smtplib_connection():
message = self._send_mail(self.message)
self.assertEqual(message["From"], "Mitchell Admin <%s>" % config_smtp_from) self.assertEqual(message["From"], "Mitchell Admin <%s>" % config_smtp_from)
used_mail_server = self.Model._get_mail_sever("example.com") used_mail_server = self.IrMailServer._get_mail_sever("example.com")
used_mail_server = self.Model.browse(used_mail_server) used_mail_server = self.IrMailServer.browse(used_mail_server)
self.assertFalse( self.assertFalse(
used_mail_server, "using this mail server %s" % (used_mail_server.name) used_mail_server, "using this mail server %s" % (used_mail_server.name)
) )
@ -223,13 +254,15 @@ class TestIrMailServer(TransactionCase):
email_from = "Mitchell Admin <admin@%s>" % domain email_from = "Mitchell Admin <admin@%s>" % domain
self._delete_mail_servers() self._delete_mail_servers()
self.assertFalse(self.Model.search([])) self.assertFalse(self.IrMailServer.search([]))
self.message.replace_header("From", email_from) self.message.replace_header("From", email_from)
message = self._send_mail() # A mail server is configured for the email
with self.mock_smtplib_connection():
message = self._send_mail(self.message)
self.assertEqual(message["From"], email_from) self.assertEqual(message["From"], email_from)
used_mail_server = self.Model._get_mail_sever(domain) used_mail_server = self.IrMailServer._get_mail_sever(domain)
used_mail_server = self.Model.browse(used_mail_server) used_mail_server = self.IrMailServer.browse(used_mail_server)
self.assertFalse(used_mail_server) self.assertFalse(used_mail_server)
def test_06_from_outgoing_server_no_name_from(self): def test_06_from_outgoing_server_no_name_from(self):
@ -239,11 +272,13 @@ class TestIrMailServer(TransactionCase):
expected_mail_server = self.mail_server_domainone expected_mail_server = self.mail_server_domainone
self.message.replace_header("From", email_from) self.message.replace_header("From", email_from)
message = self._send_mail() # A mail server is configured for the email
with self.mock_smtplib_connection():
message = self._send_mail(self.message)
self.assertEqual(message["From"], expected_mail_server.smtp_from) self.assertEqual(message["From"], expected_mail_server.smtp_from)
used_mail_server = self.Model._get_mail_sever(domain) used_mail_server = self.IrMailServer._get_mail_sever(domain)
used_mail_server = self.Model.browse(used_mail_server) used_mail_server = self.IrMailServer.browse(used_mail_server)
self.assertEqual( self.assertEqual(
used_mail_server, used_mail_server,
expected_mail_server, expected_mail_server,
@ -258,11 +293,13 @@ class TestIrMailServer(TransactionCase):
expected_mail_server = self.mail_server_domainthree expected_mail_server = self.mail_server_domainthree
self.message.replace_header("From", email_from) self.message.replace_header("From", email_from)
message = self._send_mail() # A mail server is configured for the email
with self.mock_smtplib_connection():
message = self._send_mail(self.message)
self.assertEqual(message["From"], email_from) self.assertEqual(message["From"], email_from)
used_mail_server = self.Model._get_mail_sever(domain) used_mail_server = self.IrMailServer._get_mail_sever(domain)
used_mail_server = self.Model.browse(used_mail_server) used_mail_server = self.IrMailServer.browse(used_mail_server)
self.assertEqual( self.assertEqual(
used_mail_server, used_mail_server,
expected_mail_server, expected_mail_server,
@ -277,11 +314,13 @@ class TestIrMailServer(TransactionCase):
expected_mail_server = self.mail_server_domainthree expected_mail_server = self.mail_server_domainthree
self.message.replace_header("From", email_from) self.message.replace_header("From", email_from)
message = self._send_mail() # A mail server is configured for the email
with self.mock_smtplib_connection():
message = self._send_mail(self.message)
self.assertEqual(message["From"], email_from) self.assertEqual(message["From"], email_from)
used_mail_server = self.Model._get_mail_sever(domain) used_mail_server = self.IrMailServer._get_mail_sever(domain)
used_mail_server = self.Model.browse(used_mail_server) used_mail_server = self.IrMailServer.browse(used_mail_server)
self.assertEqual( self.assertEqual(
used_mail_server, used_mail_server,
expected_mail_server, expected_mail_server,