Initial commit: Mail packages

This commit is contained in:
Ernad Husremovic 2025-08-29 15:20:51 +02:00
commit 4e53507711
1948 changed files with 751201 additions and 0 deletions

View file

@ -0,0 +1,7 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from . import common
from . import test_mailing_internals
from . import test_mailing_retry
from . import test_mailing_sms_ab_testing

View file

@ -0,0 +1,163 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
import random
import re
import werkzeug
from odoo import tools
from odoo.addons.link_tracker.tests.common import MockLinkTracker
from odoo.addons.mass_mailing.tests.common import MassMailCommon
from odoo.addons.sms.tests.common import SMSCase, SMSCommon
class MassSMSCase(SMSCase, MockLinkTracker):
# ------------------------------------------------------------
# ASSERTS
# ------------------------------------------------------------
def assertSMSStatistics(self, recipients_info, mailing, records, check_sms=True):
""" Deprecated, remove in 14.4 """
return self.assertSMSTraces(recipients_info, mailing, records, check_sms=check_sms)
def assertSMSTraces(self, recipients_info, mailing, records,
check_sms=True, sent_unlink=False,
sms_links_info=None):
""" Check content of traces. Traces are fetched based on a given mailing
and records. Their content is compared to recipients_info structure that
holds expected information. Links content may be checked, notably to
assert shortening or unsubscribe links. Sms.sms records may optionally
be checked.
:param recipients_info: list[{
# TRACE
'partner': res.partner record (may be empty),
'number': number used for notification (may be empty, computed based on partner),
'trace_status': outgoing / sent / cancel / bounce / error / opened (sent by default),
'record: linked record,
# SMS.SMS
'content': optional: if set, check content of sent SMS;
'failure_type': error code linked to sms failure (see ``error_code``
field on ``sms.sms`` model);
},
{ ... }];
:param mailing: a mailing.mailing record from which traces have been
generated;
:param records: records given to mailing that generated traces. It is
used notably to find traces using their IDs;
:param check_sms: if set, check sms.sms records that should be linked to traces;
:param sent_unlink: it True, sent sms.sms are deleted and we check gateway
output result instead of actual sms.sms records;
:param sms_links_info: if given, should follow order of ``recipients_info``
and give details about links. See ``assertLinkShortenedHtml`` helper for
more details about content to give;
]
"""
# map trace state to sms state
state_mapping = {
'sent': 'sent',
'outgoing': 'outgoing',
'error': 'error',
'cancel': 'canceled',
'bounce': 'error',
}
traces = self.env['mailing.trace'].search([
('mass_mailing_id', 'in', mailing.ids),
('res_id', 'in', records.ids)
])
self.assertTrue(all(s.model == records._name for s in traces))
# self.assertTrue(all(s.utm_campaign_id == mailing.campaign_id for s in traces))
self.assertEqual(set(s.res_id for s in traces), set(records.ids))
# check each trace
if not sms_links_info:
sms_links_info = [None] * len(recipients_info)
for recipient_info, link_info, record in zip(recipients_info, sms_links_info, records):
partner = recipient_info.get('partner', self.env['res.partner'])
number = recipient_info.get('number')
status = recipient_info.get('trace_status', 'outgoing')
content = recipient_info.get('content', None)
if number is None and partner:
number = partner._sms_get_recipients_info()[partner.id]['sanitized']
trace = traces.filtered(
lambda t: t.sms_number == number and t.trace_status == status and (t.res_id == record.id if record else True)
)
self.assertTrue(len(trace) == 1,
'SMS: found %s notification for number %s, (status: %s) (1 expected)' % (len(trace), number, status))
self.assertTrue(bool(trace.sms_sms_id_int))
if check_sms:
if status == 'sent':
if sent_unlink:
self.assertSMSIapSent([number], content=content)
else:
self.assertSMS(partner, number, 'sent', content=content)
elif status in state_mapping:
sms_state = state_mapping[status]
failure_type = recipient_info['failure_type'] if status in ('error', 'cancel', 'bounce') else None
self.assertSMS(partner, number, sms_state, failure_type=failure_type, content=content)
else:
raise NotImplementedError()
if link_info:
# shortened links are directly included in sms.sms record as well as
# in sent sms (not like mails who are post-processed)
sms_sent = self._find_sms_sent(partner, number)
sms_sms = self._find_sms_sms(partner, number, state_mapping[status])
for (url, is_shortened, add_link_params) in link_info:
if url == 'unsubscribe':
url = '%s/sms/%d/%s' % (mailing.get_base_url(), mailing.id, trace.sms_code)
link_params = {'utm_medium': 'SMS', 'utm_source': mailing.name}
if add_link_params:
link_params.update(**add_link_params)
self.assertLinkShortenedText(
sms_sms.body,
(url, is_shortened),
link_params=link_params,
)
self.assertLinkShortenedText(
sms_sent['body'],
(url, is_shortened),
link_params=link_params,
)
# ------------------------------------------------------------
# GATEWAY TOOLS
# ------------------------------------------------------------
def gateway_sms_click(self, mailing, record):
""" Simulate a click on a sent SMS. Usage: giving a partner and/or
a number, find an SMS sent to him, find shortened links in its body
and call add_click to simulate a click. """
trace = mailing.mailing_trace_ids.filtered(lambda t: t.model == record._name and t.res_id == record.id)
sms_sent = self._find_sms_sent(self.env['res.partner'], trace.sms_number)
self.assertTrue(bool(sms_sent))
return self.gateway_sms_sent_click(sms_sent)
def gateway_sms_sent_click(self, sms_sent):
""" When clicking on a link in a SMS we actually don't have any
easy information in body, only body. We currently click on all found
shortened links. """
for url in re.findall(tools.TEXT_URL_REGEX, sms_sent['body']):
if '/r/' in url: # shortened link, like 'http://localhost:8069/r/LBG/s/53'
parsed_url = werkzeug.urls.url_parse(url)
path_items = parsed_url.path.split('/')
code, sms_sms_id = path_items[2], int(path_items[4])
trace_id = self.env['mailing.trace'].sudo().search([('sms_sms_id_int', '=', sms_sms_id)]).id
self.env['link.tracker.click'].sudo().add_click(
code,
ip='100.200.300.%3f' % random.random(),
country_code='BE',
mailing_trace_id=trace_id
)
class MassSMSCommon(SMSCommon, MassSMSCase, MassMailCommon):
@classmethod
def setUpClass(cls):
super(MassSMSCommon, cls).setUpClass()

View file

@ -0,0 +1,92 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from ast import literal_eval
from odoo.addons.mass_mailing_sms.tests.common import MassSMSCommon
from odoo.tests.common import users
class TestMassMailValues(MassSMSCommon):
@classmethod
def setUpClass(cls):
super(TestMassMailValues, cls).setUpClass()
cls._create_mailing_list()
cls.sms_template_partner = cls.env['sms.template'].create({
'name': 'Test Template',
'model_id': cls.env['ir.model']._get('res.partner').id,
'body': 'Dear {{ object.display_name }} this is an SMS.'
})
@users('user_marketing')
def test_mailing_computed_fields(self):
# Create on res.partner, with default values for computed fields
mailing = self.env['mailing.mailing'].create({
'name': 'TestMailing',
'subject': 'Test',
'mailing_type': 'sms',
'body_plaintext': 'Coucou hibou',
'mailing_model_id': self.env['ir.model']._get('res.partner').id,
})
self.assertEqual(mailing.user_id, self.user_marketing)
self.assertEqual(mailing.body_plaintext, 'Coucou hibou')
self.assertEqual(mailing.medium_id, self.env.ref('mass_mailing_sms.utm_medium_sms'))
self.assertEqual(mailing.mailing_model_name, 'res.partner')
self.assertEqual(mailing.mailing_model_real, 'res.partner')
# default for partner: remove blacklisted
self.assertEqual(literal_eval(mailing.mailing_domain), [('phone_sanitized_blacklisted', '=', False)])
# update template -> update body
mailing.write({'sms_template_id': self.sms_template_partner.id})
self.assertEqual(mailing.body_plaintext, self.sms_template_partner.body)
# update domain
mailing.write({
'mailing_domain': [('email', 'ilike', 'test.example.com')]
})
self.assertEqual(literal_eval(mailing.mailing_domain), [('email', 'ilike', 'test.example.com')])
# reset mailing model -> reset domain; set reply_to -> keep it
mailing.write({
'mailing_model_id': self.env['ir.model']._get('mailing.list').id,
'reply_to': self.email_reply_to,
})
self.assertEqual(mailing.mailing_model_name, 'mailing.list')
self.assertEqual(mailing.mailing_model_real, 'mailing.contact')
# default for mailing list: depends upon contact_list_ids
self.assertEqual(literal_eval(mailing.mailing_domain), [('list_ids', 'in', [])])
mailing.write({
'contact_list_ids': [(4, self.mailing_list_1.id), (4, self.mailing_list_2.id)]
})
self.assertEqual(literal_eval(mailing.mailing_domain), [('list_ids', 'in', (self.mailing_list_1 | self.mailing_list_2).ids)])
@users('user_marketing')
def test_mailing_get_sms_link_replacements_placeholders(self):
"""Test the extra chars counts for estimating total SMS characters length.
* links are transformed into "[base_url]/r/[link_tracker_code]/s/[sms_id]".
* opt-out link is formatted as "\nSTOP SMS : [base_url]/sms/[mailing_id]/[trace_code]".
See `get_sms_link_replacements_placeholders` for safety characters.
"""
mailing = self.env['mailing.mailing'].create({
'body_plaintext': 'Coucou hibou',
'mailing_model_id': self.env['ir.model']._get('res.partner').id,
'mailing_type': 'sms',
'name': 'TestMailing',
'subject': 'Test',
})
base_url = self.env['mailing.mailing'].get_base_url()
link_trackers = bool(self.env['link.tracker'].search([], limit=1)) # depends on demo
expected = {
'link': f'{base_url}/r/xxx{"x" if link_trackers else ""}/s/xxxxx',
'unsubscribe': f"\nSTOP SMS : {base_url}/sms/{'x' * len(str(mailing.id))}/{'x' * self.env['mailing.trace'].CODE_SIZE}",
}
self.assertDictEqual(mailing.get_sms_link_replacements_placeholders(), expected)
no_mailing = self.env['mailing.mailing']
self.assertDictEqual(no_mailing.get_sms_link_replacements_placeholders(), expected)
new_mailing = self.env['mailing.mailing'].new()
self.assertDictEqual(new_mailing.get_sms_link_replacements_placeholders(), expected)

View file

@ -0,0 +1,41 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo.addons.mass_mailing_sms.tests.common import MassSMSCommon
from odoo.addons.base.tests.test_ir_cron import CronMixinCase
from odoo.tests.common import users
from unittest.mock import patch
class TestMailingRetrySMS(MassSMSCommon, CronMixinCase):
@classmethod
def setUpClass(cls):
super(TestMailingRetrySMS, cls).setUpClass()
cls._create_mailing_list()
@users('user_marketing')
def test_sms_retry_immediate_trigger(self):
mailing = self.env['mailing.mailing'].create({
'name': 'TestMailing',
'subject': 'Test',
'mailing_type': 'sms',
'body_plaintext': 'Coucou hibou',
'mailing_model_id': self.env['ir.model']._get('res.partner').id,
'contact_list_ids': [(4, self.mailing_list_1.id)],
})
mailing.action_send_sms()
# force the SMS sending to fail to test our retry mechanism
def patched_sms_sms_send(sms_records, unlink_failed=False, unlink_sent=True, raise_exception=False):
sms_records.write({'state': 'error', 'failure_type':'sms_credit'})
with patch('odoo.addons.sms.models.sms_sms.SmsSms._send', patched_sms_sms_send):
self.env.ref('sms.ir_cron_sms_scheduler_action').sudo().method_direct_trigger()
with self.capture_triggers('mass_mailing.ir_cron_mass_mailing_queue') as captured_triggers:
mailing.action_retry_failed()
self.assertEqual(len(captured_triggers.records), 1, "Should have created an additional trigger immediately")
captured_trigger = captured_triggers.records[0]
self.assertEqual(captured_trigger.cron_id, self.env.ref('mass_mailing.ir_cron_mass_mailing_queue'))

View file

@ -0,0 +1,35 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from datetime import datetime
from odoo.addons.mass_mailing_sms.tests.common import MassSMSCommon
from odoo.addons.mass_mailing.tests.test_mailing_ab_testing import TestMailingABTestingCommon
from odoo.tests import tagged
@tagged('post_install', '-at_install')
class TestMailingSMSABTesting(MassSMSCommon, TestMailingABTestingCommon):
def setUp(self):
super().setUp()
self.ab_testing_mailing_sms_1 = self.env['mailing.mailing'].create({
'subject': 'A/B Testing SMS V1',
'contact_list_ids': self.mailing_list.ids,
'ab_testing_enabled': True,
'ab_testing_pc': 10,
'ab_testing_schedule_datetime': datetime.now(),
'mailing_type': 'sms'
})
self.ab_testing_mailing_sms_2 = self.ab_testing_mailing_sms_1.copy({
'subject': 'A/B Testing SMS V2',
'ab_testing_pc': 20,
})
def test_mailing_sms_ab_testing_compare(self):
# compare version feature should returns all mailings of the same
# campaign having a/b testing enabled and of mailing_type 'sms'.
compare_version = self.ab_testing_mailing_sms_1.action_compare_versions()
self.assertEqual(
self.env['mailing.mailing'].search(compare_version.get('domain')),
self.ab_testing_mailing_sms_1 + self.ab_testing_mailing_sms_2
)