[IMP] mail_outbound_static: black, isort, prettier

This commit is contained in:
Frédéric Garbely 2020-06-30 16:21:11 +02:00 committed by Maksym Yankin
parent 29dad4da0f
commit d88b08eefc
3 changed files with 55 additions and 63 deletions

View File

@ -2,19 +2,15 @@
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).
{
'name': 'Mail Outbound Static',
'summary': 'Allows you to configure the from header for a mail server.',
'version': '13.0.1.0.0',
'category': 'Discuss',
'website': 'https://github.com/OCA/social',
'author': 'brain-tec AG, LasLabs, Odoo Community Association (OCA)',
'license': 'LGPL-3',
'application': False,
'installable': True,
'depends': [
'base',
],
'data': [
'views/ir_mail_server_view.xml',
],
"name": "Mail Outbound Static",
"summary": "Allows you to configure the from header for a mail server.",
"version": "13.0.1.0.0",
"category": "Discuss",
"website": "https://github.com/OCA/social",
"author": "brain-tec AG, LasLabs, Odoo Community Association (OCA)",
"license": "LGPL-3",
"application": False,
"installable": True,
"depends": ["base"],
"data": ["views/ir_mail_server_view.xml"],
}

View File

@ -6,43 +6,42 @@ from odoo import api, fields, models
class IrMailServer(models.Model):
_inherit = 'ir.mail_server'
_inherit = "ir.mail_server"
smtp_from = fields.Char(
string='Email From',
help='Set this in order to email from a specific address.'
string="Email From", help="Set this in order to email from a specific address."
)
@api.model
def send_email(self, message, mail_server_id=None, smtp_server=None,
*args, **kwargs):
def send_email(
self, message, mail_server_id=None, smtp_server=None, *args, **kwargs
):
# Replicate logic from core to get mail server
mail_server = None
if mail_server_id:
mail_server = self.sudo().browse(mail_server_id)
elif not smtp_server:
mail_server = self.sudo().search([], order='sequence', limit=1)
mail_server = self.sudo().search([], order="sequence", limit=1)
if mail_server and mail_server.smtp_from:
split_from = message['From'].rsplit(' <', 1)
split_from = message["From"].rsplit(" <", 1)
if len(split_from) > 1:
email_from = '%s <%s>' % (
split_from[0], mail_server.smtp_from,
)
email_from = "{} <{}>".format(split_from[0], mail_server.smtp_from)
else:
email_from = mail_server.smtp_from
message.replace_header('From', email_from)
bounce_alias = self.env['ir.config_parameter'].get_param(
"mail.bounce.alias")
message.replace_header("From", email_from)
bounce_alias = self.env["ir.config_parameter"].get_param(
"mail.bounce.alias"
)
if not bounce_alias:
# then, bounce handling is disabled and we want
# Return-Path = From
if 'Return-Path' in message:
message.replace_header('Return-Path', email_from)
if "Return-Path" in message:
message.replace_header("Return-Path", email_from)
else:
message.add_header('Return-Path', email_from)
message.add_header("Return-Path", email_from)
return super(IrMailServer, self).send_email(
message, mail_server_id, smtp_server, *args, **kwargs

View File

@ -3,30 +3,31 @@
import os
import threading
from email import message_from_string
from mock import MagicMock
from email import message_from_string
from odoo.tests.common import TransactionCase
class TestIrMailServer(TransactionCase):
def setUp(self):
super(TestIrMailServer, self).setUp()
self.email_from = 'derp@example.com'
self.email_from_another = 'another@example.com'
self.Model = self.env['ir.mail_server']
self.parameter_model = self.env['ir.config_parameter']
self.Model.create({
'name': 'localhost',
'smtp_host': 'localhost',
'smtp_from': self.email_from,
})
message_file = os.path.join(
os.path.dirname(os.path.realpath(__file__)), 'test.msg',
self.email_from = "derp@example.com"
self.email_from_another = "another@example.com"
self.Model = self.env["ir.mail_server"]
self.parameter_model = self.env["ir.config_parameter"]
self.Model.create(
{
"name": "localhost",
"smtp_host": "localhost",
"smtp_from": self.email_from,
}
)
with open(message_file, 'r') as fh:
message_file = os.path.join(
os.path.dirname(os.path.realpath(__file__)), "test.msg"
)
with open(message_file, "r") as fh:
self.message = message_from_string(fh.read())
def _send_mail(self, message=None, mail_server_id=None, smtp_server=None):
@ -34,24 +35,24 @@ class TestIrMailServer(TransactionCase):
message = self.message
connect = MagicMock()
thread = threading.currentThread()
setattr(thread, 'testing', False)
thread.testing = False
try:
self.Model._patch_method('connect', connect)
self.Model._patch_method("connect", connect)
try:
self.Model.send_email(message, mail_server_id, smtp_server)
finally:
self.Model._revert_method('connect')
self.Model._revert_method("connect")
finally:
setattr(thread, 'testing', True)
thread.testing = True
send_from, send_to, message_string = connect().sendmail.call_args[0]
return message_from_string(message_string)
def test_send_email_injects_from_no_canonical(self):
"""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()
self.assertEqual(message['From'], self.email_from)
self.assertEqual(message["From"], self.email_from)
def test_send_email_injects_from_with_canonical(self):
"""It should inject the FROM header correctly with a canonical name.
@ -59,22 +60,18 @@ class TestIrMailServer(TransactionCase):
Note that there is an extra `<` in the canonical name to test for
proper handling in the split.
"""
user = 'Test < User'
self.message.replace_header('From', '%s <test@example.com>' % user)
bounce_parameter = self.parameter_model.search([
('key', '=', 'mail.bounce.alias')])
user = "Test < User"
self.message.replace_header("From", "%s <test@example.com>" % user)
bounce_parameter = self.parameter_model.search(
[("key", "=", "mail.bounce.alias")]
)
if bounce_parameter:
# Remove mail.bounce.alias to test Return-Path
bounce_parameter.unlink()
# Also check passing mail_server_id
mail_server_id = self.Model.sudo().search(
[], order='sequence', limit=1)[0].id
mail_server_id = self.Model.sudo().search([], order="sequence", limit=1)[0].id
message = self._send_mail(mail_server_id=mail_server_id)
self.assertEqual(message["From"], "{} <{}>".format(user, self.email_from))
self.assertEqual(
message['From'],
'%s <%s>' % (user, self.email_from),
)
self.assertEqual(
message['Return-Path'],
'%s <%s>' % (user, self.email_from),
message["Return-Path"], "{} <{}>".format(user, self.email_from)
)