diff --git a/addons/mass_mailing/models/mail_thread.py b/addons/mass_mailing/models/mail_thread.py index ef1aceee749..1f442675922 100644 --- a/addons/mass_mailing/models/mail_thread.py +++ b/addons/mass_mailing/models/mail_thread.py @@ -57,7 +57,7 @@ class MailThread(models.AbstractModel): if bounced_email: three_months_ago = fields.Datetime.to_string(datetime.datetime.now() - datetime.timedelta(weeks=13)) stats = self.env['mailing.trace'].search(['&', ('bounced', '>', three_months_ago), ('email', '=ilike', bounced_email)]).mapped('bounced') - if len(stats) >= BLACKLIST_MAX_BOUNCED_LIMIT and (not bounced_partner or bounced_partner.message_bounce >= BLACKLIST_MAX_BOUNCED_LIMIT): + if len(stats) >= BLACKLIST_MAX_BOUNCED_LIMIT and (not bounced_partner or any(p.message_bounce >= BLACKLIST_MAX_BOUNCED_LIMIT for p in bounced_partner)): if max(stats) > min(stats) + datetime.timedelta(weeks=1): blacklist_rec = self.env['mail.blacklist'].sudo()._add(bounced_email) blacklist_rec._message_log( diff --git a/addons/test_mass_mailing/tests/test_blacklist_behavior.py b/addons/test_mass_mailing/tests/test_blacklist_behavior.py index a031e9784c7..a5a77c7d437 100644 --- a/addons/test_mass_mailing/tests/test_blacklist_behavior.py +++ b/addons/test_mass_mailing/tests/test_blacklist_behavior.py @@ -5,21 +5,52 @@ import datetime from odoo.addons.test_mass_mailing.tests import common from odoo.tests.common import users +from odoo.addons.mass_mailing.models.mail_thread import BLACKLIST_MAX_BOUNCED_LIMIT class TestAutoBlacklist(common.TestMassMailCommon): + @classmethod + def setUpClass(cls): + super(TestAutoBlacklist, cls).setUpClass() + cls.target_rec = cls._create_mailing_test_records()[0] + cls.mailing_bl.write({'mailing_domain': [('id', 'in', cls.target_rec.ids)]}) + @users('user_marketing') def test_mailing_bounce_w_auto_bl(self): + self._test_mailing_bounce_w_auto_bl(None) + + @users('user_marketing') + def test_mailing_bounce_w_auto_bl_partner(self): + bounced_partner = self.env['res.partner'].sudo().create({ + 'name': 'Bounced Partner', + 'email': self.target_rec.email_from, + 'message_bounce': BLACKLIST_MAX_BOUNCED_LIMIT, + }) + self._test_mailing_bounce_w_auto_bl({'bounced_partner': bounced_partner}) + + @users('user_marketing') + def test_mailing_bounce_w_auto_bl_partner_duplicates(self): + bounced_partners = self.env['res.partner'].sudo().create({ + 'name': 'Bounced Partner1', + 'email': self.target_rec.email_from, + 'message_bounce': BLACKLIST_MAX_BOUNCED_LIMIT, + }) | self.env['res.partner'].sudo().create({ + 'name': 'Bounced Partner2', + 'email': self.target_rec.email_from, + 'message_bounce': BLACKLIST_MAX_BOUNCED_LIMIT, + }) + self._test_mailing_bounce_w_auto_bl({'bounced_partner': bounced_partners}) + + def _test_mailing_bounce_w_auto_bl(self, bounce_base_values): mailing = self.env['mailing.mailing'].browse(self.mailing_bl.ids) - target = self._create_mailing_test_records()[0] - mailing.write({'mailing_domain': [('id', 'in', target.ids)]}) + target = self.env['mailing.test.blacklist'].browse(self.target_rec.ids) # create bounced history of 4 statistics for idx in range(4): new_mailing = mailing.copy() self._create_bounce_trace(new_mailing, target, dt=datetime.datetime.now() - datetime.timedelta(weeks=idx+2)) - self.gateway_mail_bounce(new_mailing, target) + self.gateway_mail_bounce(new_mailing, target, bounce_base_values) # mass mail record: ok, not blacklisted yet mailing.action_put_in_queue() @@ -33,7 +64,7 @@ class TestAutoBlacklist(common.TestMassMailCommon): ) # call bounced - self.gateway_mail_bounce(mailing, target) + self.gateway_mail_bounce(mailing, target, bounce_base_values) # check blacklist blacklist_record = self.env['mail.blacklist'].sudo().search([('email', '=', target.email_normalized)])