# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from markupsafe import Markup
from odoo.addons.mail.tests.common import MailCommon
from odoo.exceptions import AccessError, UserError
from odoo.modules.module import get_module_resource
from odoo.tests import Form, tagged, users
from odoo.tools import convert_file
@tagged('mail_template')
class TestMailTemplate(MailCommon):
@classmethod
def setUpClass(cls):
super(TestMailTemplate, cls).setUpClass()
# Enable the Jinja rendering restriction
cls.env['ir.config_parameter'].set_param('mail.restrict.template.rendering', True)
cls.user_employee.groups_id -= cls.env.ref('mail.group_mail_template_editor')
cls.mail_template = cls.env['mail.template'].create({
'name': 'Test template',
'subject': '{{ 1 + 5 }}',
'body_html': '
foo
'}) employee_template.with_user(self.user_employee).body_html = 'bar
' employee_template = self.env['mail.template'].with_user(self.user_employee).create({'email_to': 'foo@bar.com'}) employee_template.with_user(self.user_employee).email_to = 'bar@foo.com' # Standard employee cannot create and edit templates with dynamic qweb with self.assertRaises(AccessError): self.env['mail.template'].with_user(self.user_employee).create({'body_html': ''}) # Standard employee cannot edit templates from another user, non-dynamic and dynamic with self.assertRaises(AccessError): mail_template.with_user(self.user_employee).body_html = 'foo
' with self.assertRaises(AccessError): mail_template.with_user(self.user_employee).body_html = '' # Standard employee can edit his own templates if not dynamic employee_template.with_user(self.user_employee).body_html = 'foo
' # Standard employee cannot create and edit templates with dynamic inline fields with self.assertRaises(AccessError): self.env['mail.template'].with_user(self.user_employee).create({'email_to': '{{ object.partner_id.email }}'}) # Standard employee cannot edit his own templates if dynamic with self.assertRaises(AccessError): employee_template.with_user(self.user_employee).body_html = '' with self.assertRaises(AccessError): employee_template.with_user(self.user_employee).email_to = '{{ object.partner_id.email }}' def test_mail_template_acl_translation(self): ''' Test that a user that doenn't have the group_mail_template_editor cannot create / edit translation with dynamic code if he cannot write dynamic code on the related record itself. ''' self.env.ref('base.lang_fr').sudo().active = True employee_template = self.env['mail.template'].with_user(self.user_employee).create({ 'model_id': self.env.ref('base.model_res_partner').id, 'subject': 'The subject', 'body_html': 'foo
', }) ### check qweb dynamic # write on translation for template without dynamic code is allowed employee_template.with_context(lang='fr_FR').body_html = 'non-qweb' # cannot write dynamic code on mail_template translation for employee without the group mail_template_editor. with self.assertRaises(AccessError): employee_template.with_context(lang='fr_FR').body_html = 'foo
' ### check qweb inline dynamic # write on translation for template without dynamic code is allowed employee_template.with_context(lang='fr_FR').subject = 'non-qweb' # cannot write dynamic code on mail_template translation for employee without the group mail_template_editor. with self.assertRaises(AccessError): employee_template.with_context(lang='fr_FR').subject = '{{ object.foo }}' employee_template.with_context(lang='fr_FR').sudo().subject = '{{ object.foo }}' def test_server_archived_usage_protection(self): """ Test the protection against using archived server (servers used cannot be archived) """ IrMailServer = self.env['ir.mail_server'] server = IrMailServer.create({ 'name': 'Server', 'smtp_host': 'archive-test.smtp.local', }) self.mail_template.mail_server_id = server.id with self.assertRaises(UserError, msg='Server cannot be archived because it is used'): server.action_archive() self.assertTrue(server.active) self.mail_template.mail_server_id = IrMailServer server.action_archive() # No more usage -> can be archived self.assertFalse(server.active) @tagged('mail_template') class TestMailTemplateReset(MailCommon): def _load(self, module, *args): convert_file(self.cr, module='mail', filename=get_module_resource(module, *args), idref={}, mode='init', noupdate=False, kind='test') def test_mail_template_reset(self): self._load('mail', 'tests', 'test_mail_template.xml') mail_template = self.env.ref('mail.mail_template_test').with_context(lang=self.env.user.lang) mail_template.write({ 'body_html': '