Files
odoo_source/addons/test_mass_mailing/tests/test_mailing_server.py
T
Pierre-Yves Dufays ac67e6a8df [IMP] base,{fetch}mail,l10n_it_edi,{test}mass_mailing:prevent using arch server
Prevent archiving in-use mail servers by displaying an error message that
lists where it is still used, allowing to easily identify what need to be
updated before being able to archive the mail server.

Additionally,
- prevent the use of archived server as a fall-safe
- when duplicating a mailing with an archived mail server, replace mail
server by the default one

Detailed explanation:
1. A check has been added that raise an exception when trying to connect to the
smtp server or send an email when the server is archived.
With that solution,
- testing the connection of an archived server displays an error telling that
an archived server cannot be used.
- if a mail is still sent with an archived server, mail are in error :
"Connection failed (outgoing mail server problem)"
This fail-safe ensures that no mail will be sent through an archived mail server
and that the user will get some feedback about it.

The same fail-safe for the incoming mail server has been added.

Notes:
- the connection will outlive the archiving of a mail server still allowing
to send email through the archived server until the connection is closed. But
connection are not kept for long so this shouldn't be a problem.
- it cannot be tested because the connect method return immediately in test
mode.

2. When a mail server is archived, an user error is raised if it is in-use.
The implementation relies on each module to override the method
"_active_usages_compute" in "ir_mail_server" to complete the list with
user-friendly message describing the active elements that could send mail
through the mail server. This has been implemented for:
- l10n_it_edi: server used to send e-invoice
- mail: optional server configured for template
- mass_mailing:
-- default mail server
-- active server configured for mailing

Mail server are referenced in other elements but are not active anymore, it is
just for temporary or history purpose. Those references doesn’t prevent the
archiving of the mail server:
- mail_message
- wizard survey_invite and compose_message
- res_config_settings

Task-2821516

closes odoo/odoo#91240

Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com>
2022-09-01 15:32:44 +02:00

171 lines
8.0 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo.addons.test_mass_mailing.tests.common import TestMassMailCommon
from odoo.exceptions import UserError
from odoo.tests import tagged
from odoo.tests.common import users
from odoo.tools import mute_logger
@tagged('mass_mailing')
class TestMassMailingServer(TestMassMailCommon):
@classmethod
def setUpClass(cls):
super(TestMassMailingServer, cls).setUpClass()
cls._init_mail_servers()
cls.recipients = cls._create_mailing_test_records(model='mailing.test.optout', count=8)
def test_mass_mailing_server_archived_usage_protection(self):
""" Test the protection against using archived server:
- servers used cannot be archived
- mailing clone of a mailing with an archived server gets the default one instead
"""
servers = self.env['ir.mail_server'].create([{
'name': 'Server 1',
'smtp_host': 'archive-test1.smtp.local',
}, {
'name': 'Server 2',
'smtp_host': 'archive-test2.smtp.local',
}])
self.env['ir.config_parameter'].set_param('mass_mailing.mail_server_id', servers[0].id)
mailing = self.env['mailing.mailing'].create({
'subject': 'Mailing',
'body_html': 'Body for <t t-out="object.name" />',
'email_from': 'specific_user@test.com',
'mailing_model_id': self.env['ir.model']._get('mailing.test.optout').id,
})
mailing_clone = mailing.copy()
self.assertEqual(mailing_clone.mail_server_id.id, servers[0].id,
'The clone of a mailing inherits from the server of the copied mailing')
with self.assertRaises(UserError, msg='Servers still used as default and for 2 mailings'):
servers.action_archive()
self.assertTrue(all(server.active for server in servers), 'All servers must be active')
self.env['ir.config_parameter'].set_param('mass_mailing.mail_server_id', False)
with self.assertRaises(UserError, msg='Servers still used for 2 mailings'):
servers.action_archive()
self.assertTrue(all(server.active for server in servers), 'All servers must be active')
with self.mock_smtplib_connection():
mailing.action_send_mail()
with self.assertRaises(UserError, msg='Servers still used for 1 mailings'):
servers.action_archive()
self.assertTrue(all(server.active for server in servers), 'All servers must be active')
with self.mock_smtplib_connection():
mailing_clone.action_send_mail()
servers.action_archive() # Servers no more used -> no error
self.assertFalse(servers.filtered('active'), 'All servers must be archived')
self.assertFalse(mailing.copy().mail_server_id,
'The clone of a mailing with an archived server gets the default one (none here)')
servers[1].action_unarchive()
self.env['ir.config_parameter'].set_param('mass_mailing.mail_server_id', servers[1].id)
mailing_clone = mailing.copy()
self.assertEqual(mailing_clone.mail_server_id.id, servers[1].id,
'The clone of a mailing with an archived server gets the default one')
mailing_clone.action_archive()
with self.assertRaises(UserError, msg='Servers still used as default'):
servers.action_archive()
self.assertTrue(servers[1].active)
self.env['ir.config_parameter'].set_param('mass_mailing.mail_server_id', False)
servers.action_archive() # Servers no more used -> no error
self.assertFalse(servers.filtered('active'), 'All servers must be archived')
@users('user_marketing')
@mute_logger('odoo.addons.mail.models.mail_mail', 'odoo.models.unlink', 'odoo.addons.mass_mailing.models.mailing')
def test_mass_mailing_server_batch(self):
"""Test that the right mail server is chosen to send the mailing.
Test also the envelop and the SMTP headers.
"""
# Test sending mailing in batch
mailings = self.env['mailing.mailing'].create([{
'subject': 'Mailing',
'body_html': 'Body for <t t-out="object.name" />',
'email_from': 'specific_user@test.com',
'mailing_model_id': self.env['ir.model']._get('mailing.test.optout').id,
}, {
'subject': 'Mailing',
'body_html': 'Body for <t t-out="object.name" />',
'email_from': 'unknown_name@test.com',
'mailing_model_id': self.env['ir.model']._get('mailing.test.optout').id,
}])
with self.mock_smtplib_connection():
mailings.action_send_mail()
self.assertEqual(self.find_mail_server_mocked.call_count, 2, 'Must be called only once per mail from')
self.assert_email_sent_smtp(
smtp_from='specific_user@test.com',
message_from='specific_user@test.com',
from_filter=self.server_user.from_filter,
emails_count=8,
)
self.assert_email_sent_smtp(
# Must use the bounce address here because the mail server
# is configured for the entire domain "test.com"
smtp_from=lambda x: 'bounce' in x,
message_from='unknown_name@test.com',
from_filter=self.server_domain.from_filter,
emails_count=8,
)
@users('user_marketing')
@mute_logger('odoo.addons.mail.models.mail_mail', 'odoo.models.unlink', 'odoo.addons.mass_mailing.models.mailing')
def test_mass_mailing_server_default(self):
# We do not have a mail server for this address email, so fall back to the
# "notifications@domain" email.
mailings = self.env['mailing.mailing'].create([{
'subject': 'Mailing',
'body_html': 'Body for <t t-out="object.name" />',
'email_from': '"Testing" <unknow_email@unknow_domain.com>',
'mailing_model_id': self.env['ir.model']._get('mailing.test.optout').id,
}])
with self.mock_smtplib_connection():
mailings.action_send_mail()
self.assertEqual(self.find_mail_server_mocked.call_count, 1)
self.assert_email_sent_smtp(
smtp_from='notifications@test.com',
message_from='"Testing" <notifications@test.com>',
from_filter=self.server_notification.from_filter,
emails_count=8,
)
self.assertEqual(self.find_mail_server_mocked.call_count, 1, 'Must be called only once')
@users('user_marketing')
@mute_logger('odoo.addons.mail.models.mail_mail', 'odoo.models.unlink', 'odoo.addons.mass_mailing.models.mailing')
def test_mass_mailing_server_forced(self):
# We force a mail server on one mailing
mailings = self.env['mailing.mailing'].create([{
'subject': 'Mailing',
'body_html': 'Body for <t t-out="object.name" />',
'email_from': self.server_user.from_filter,
'mailing_model_id': self.env['ir.model']._get('mailing.test.optout').id,
}, {
'subject': 'Mailing',
'body_html': 'Body for <t t-out="object.name" />',
'email_from': 'unknow_email@unknow_domain.com',
'mailing_model_id': self.env['ir.model']._get('mailing.test.optout').id,
'mail_server_id': self.server_notification.id,
}])
with self.mock_smtplib_connection():
mailings.action_send_mail()
self.assertEqual(self.find_mail_server_mocked.call_count, 1, 'Must not be called when mail server is forced')
self.assert_email_sent_smtp(
smtp_from='specific_user@test.com',
message_from='specific_user@test.com',
from_filter=self.server_user.from_filter,
emails_count=8,
)
self.assert_email_sent_smtp(
smtp_from='unknow_email@unknow_domain.com',
message_from='unknow_email@unknow_domain.com',
from_filter=self.server_notification.from_filter,
emails_count=8,
)