summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOdoo's Mergebot <robodoo@odoo.com>2020-10-20 13:41:41 +0200
committerGitHub <noreply@github.com>2020-10-20 13:41:41 +0200
commitf997e673b7c326797e00dd70d4b2e5684be8c45b (patch)
tree28ab25ada914033d82f7b4190e1ae91cf97214cf
parent7e54d9aa6b6a955200e7002b143848de0c3a093e (diff)
parent7370d573ae91cf442d045aec2b49332061f6c0ee (diff)
[FIX] mass_mailing_sms: try to render jinja content when using test wizards
# Purpose The wizard to test a mailing does not compute the jinja part. It does not detect syntax errors that would make cron crash # Specifications If there is at least one record in the mailing model then render template completely. Thow an error if rendering cannot be done. The downside is that we cannot test the jinja part if there is not yet any record in the mailing's model. Do it for mass_mailing and mass_mailing_sms. Add tests to ensure error detection in test wizard effectively works. Task ID-2312442 PR #55696 Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com>
-rw-r--r--addons/mass_mailing/wizard/mailing_mailing_test.py22
-rw-r--r--addons/mass_mailing_sms/wizard/mailing_sms_test.py9
-rw-r--r--addons/test_mail_full/tests/test_mass_sms.py30
-rw-r--r--addons/test_mass_mailing/tests/test_mailing_internals.py41
4 files changed, 96 insertions, 6 deletions
diff --git a/addons/mass_mailing/wizard/mailing_mailing_test.py b/addons/mass_mailing/wizard/mailing_mailing_test.py
index 2ffba12ebf5..e3c1970afae 100644
--- a/addons/mass_mailing/wizard/mailing_mailing_test.py
+++ b/addons/mass_mailing/wizard/mailing_mailing_test.py
@@ -22,16 +22,28 @@ class TestMassMailing(models.TransientModel):
mailing = self.mass_mailing_id
test_emails = tools.email_split(self.email_to)
mass_mail_layout = self.env.ref('mass_mailing.mass_mailing_mail_layout')
+
+ record = self.env[mailing.mailing_model_real].search([], limit=1)
+ body = mailing._prepend_preview(mailing.body_html, mailing.preview)
+ subject = mailing.subject
+
+ # If there is atleast 1 record for the model used in this mailing, then we use this one to render the template
+ # Downside: Jinja syntax is only tested when there is atleast one record of the mailing's model
+ if record:
+ # Returns a proper error if there is a syntax error with jinja
+ body = self.env['mail.render.mixin']._render_template(body, mailing.mailing_model_real, record.ids, post_process=True)[record.id]
+ subject = self.env['mail.render.mixin']._render_template(subject, mailing.mailing_model_real, record.ids)[record.id]
+
+ # Convert links in absolute URLs before the application of the shortener
+ body = self.env['mail.render.mixin']._replace_local_links(body)
+ body = tools.html_sanitize(body, sanitize_attributes=True, sanitize_style=True)
+
for test_mail in test_emails:
- # Convert links in absolute URLs before the application of the shortener
- body = mailing._prepend_preview(mailing.body_html, mailing.preview)
- body = self.env['mail.render.mixin']._replace_local_links(body)
- body = tools.html_sanitize(body, sanitize_attributes=True, sanitize_style=True)
mail_values = {
'email_from': mailing.email_from,
'reply_to': mailing.reply_to,
'email_to': test_mail,
- 'subject': mailing.subject,
+ 'subject': subject,
'body_html': mass_mail_layout._render({'body': body}, engine='ir.qweb', minimal_qcontext=True),
'notification': True,
'mailing_id': mailing.id,
diff --git a/addons/mass_mailing_sms/wizard/mailing_sms_test.py b/addons/mass_mailing_sms/wizard/mailing_sms_test.py
index 198304dc2c6..1ea9f8718eb 100644
--- a/addons/mass_mailing_sms/wizard/mailing_sms_test.py
+++ b/addons/mass_mailing_sms/wizard/mailing_sms_test.py
@@ -24,9 +24,16 @@ class MassSMSTest(models.TransientModel):
invalid_numbers = [number for number, info in sanitize_res.items() if info['code']]
if invalid_numbers:
raise exceptions.UserError(_('Following numbers are not correctly encoded: %s, example : "+32 495 85 85 77, +33 545 55 55 55"', repr(invalid_numbers)))
+
+ record = self.env[self.mailing_id.mailing_model_real].search([], limit=1)
+ body = self.mailing_id.body_plaintext
+ if record:
+ # Returns a proper error if there is a syntax error with jinja
+ body = self.env['mail.render.mixin']._render_template(body, self.mailing_id.mailing_model_real, record.ids)[record.id]
+
self.env['sms.api']._send_sms_batch([{
'res_id': 0,
'number': number,
- 'content': self.mailing_id.body_plaintext,
+ 'content': body,
} for number in sanitized_numbers])
return True
diff --git a/addons/test_mail_full/tests/test_mass_sms.py b/addons/test_mail_full/tests/test_mass_sms.py
index 2bc1760cc60..a3e3d35e14a 100644
--- a/addons/test_mail_full/tests/test_mass_sms.py
+++ b/addons/test_mail_full/tests/test_mass_sms.py
@@ -5,6 +5,7 @@
from odoo.addons.phone_validation.tools import phone_validation
from odoo.addons.test_mail_full.tests.common import TestMailFullCommon
from odoo.tests import tagged
+from odoo.tools import mute_logger
@tagged('mass_mailing')
@@ -147,3 +148,32 @@ class TestMassSMS(TestMailFullCommon):
[{'partner': record.customer_id, 'number': self.records_numbers[i+5], 'content': 'Dear %s this is a mass SMS.' % record.display_name} for i, record in enumerate(self.records[5:])],
self.mailing, self.records[5:], check_sms=True
)
+
+ @mute_logger('odoo.addons.mail.models.mail_render_mixin')
+ def test_mass_sms_test_button(self):
+ mailing = self.env['mailing.mailing'].create({
+ 'name': 'TestButton',
+ 'subject': 'Subject ${object.name}',
+ 'preview': 'Preview ${object.name}',
+ 'state': 'draft',
+ 'mailing_type': 'sms',
+ 'body_plaintext': 'Hello ${object.name}',
+ 'mailing_model_id': self.env['ir.model']._get('res.partner').id,
+ })
+ mailing_test = self.env['mailing.sms.test'].with_user(self.user_marketing).create({
+ 'numbers': '+32456001122',
+ 'mailing_id': mailing.id,
+ })
+
+ with self.with_user('user_marketing'):
+ with self.mockSMSGateway():
+ mailing_test.action_send_sms()
+
+ # Test if bad jinja in the body raises an error
+ mailing.write({
+ 'body_plaintext': 'Hello ${object.name_id.id}',
+ })
+
+ with self.with_user('user_marketing'):
+ with self.mock_mail_gateway(), self.assertRaises(Exception):
+ mailing_test.action_send_sms()
diff --git a/addons/test_mass_mailing/tests/test_mailing_internals.py b/addons/test_mass_mailing/tests/test_mailing_internals.py
index 48eab1853bb..5d284ef6d09 100644
--- a/addons/test_mass_mailing/tests/test_mailing_internals.py
+++ b/addons/test_mass_mailing/tests/test_mailing_internals.py
@@ -4,6 +4,7 @@
from odoo.addons.test_mass_mailing.tests.common import TestMassMailCommon
from odoo.addons.test_mass_mailing.data.mail_test_data import MAIL_TEMPLATE
from odoo.tests.common import users
+from odoo.tools import mute_logger
class TestMailingInternals(TestMassMailCommon):
@@ -22,6 +23,46 @@ class TestMailingInternals(TestMassMailCommon):
'alias_contact': 'everyone'
})
+ @mute_logger('odoo.addons.mail.models.mail_render_mixin')
+ def test_mailing_test_button(self):
+ mailing = self.env['mailing.mailing'].create({
+ 'name': 'TestButton',
+ 'subject': 'Subject ${object.name}',
+ 'preview': 'Preview ${object.name}',
+ 'state': 'draft',
+ 'mailing_type': 'mail',
+ 'body_html': '<p>Hello ${object.name}</p>',
+ 'mailing_model_id': self.env['ir.model']._get('res.partner').id,
+ })
+ mailing_test = self.env['mailing.mailing.test'].create({
+ 'email_to': 'test@test.com',
+ 'mass_mailing_id': mailing.id,
+ })
+
+ with self.mock_mail_gateway():
+ mailing_test.send_mail_test()
+
+ # Test if bad jinja in the subject raises an error
+ mailing.write({'subject': 'Subject ${object.name_id.id}'})
+ with self.mock_mail_gateway(), self.assertRaises(Exception):
+ mailing_test.send_mail_test()
+
+ # Test if bad jinja in the body raises an error
+ mailing.write({
+ 'subject': 'Subject ${object.name}',
+ 'body_html': '<p>Hello ${object.name_id.id}</p>',
+ })
+ with self.mock_mail_gateway(), self.assertRaises(Exception):
+ mailing_test.send_mail_test()
+
+ # Test if bad jinja in the preview raises an error
+ mailing.write({
+ 'body_html': '<p>Hello ${object.name}</p>',
+ 'preview': 'Preview ${object.name_id.id}',
+ })
+ with self.mock_mail_gateway(), self.assertRaises(Exception):
+ mailing_test.send_mail_test()
+
def test_mailing_trace_update(self):
customers = self.env['res.partner']
for x in range(0, 3):