# -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. from odoo.addons.mail.tests.common import MailCommon from odoo.exceptions import AccessError from odoo.tests import Form, HttpCase, tagged, users from odoo.tools import mute_logger @tagged('mail_composer') class TestMailComposer(MailCommon): @classmethod def setUpClass(cls): super(TestMailComposer, cls).setUpClass() 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.test_record = cls.env['res.partner'].with_context(cls._test_context).create({ 'name': 'Test', }) cls.body_html = """
Here! A link for you! Make good use of it.
Hello
' self.assertEqual( form.partner_ids.ids, partner_classic.ids, 'Default populates the field' ) saved_form = form.save() self.assertEqual( saved_form.partner_ids, partner_classic, 'Default value is kept at save' ) with self.mock_mail_gateway(): saved_form._action_send_mail() message = self.test_record.message_ids[0] self.assertEqual(message.body, 'Hello
') self.assertEqual(message.partner_ids, partner_classic) self.assertEqual(message.subject, f'{test_record.name}') @mute_logger('odoo.addons.mail.models.mail_mail') @users('employee') def test_composer_default_recipients_private(self): """ Test usage of a private partner in composer, as default value """ partner_private = self.partner_private.with_env(self.env) partner_classic = self.partner_classic.with_env(self.env) test_record = self.test_record.with_env(self.env) form = Form(self.env['mail.compose.message'].with_context({ 'default_partner_ids': (partner_private + partner_classic).ids, 'default_model': test_record._name, 'default_res_ids': test_record.ids, })) form.body = 'Hello
' self.assertEqual( sorted(form.partner_ids.ids), sorted((partner_private + partner_classic).ids), 'Default populates the field' ) saved_form = form.save() self.assertEqual( saved_form.partner_ids, partner_private + partner_classic, 'Default value is kept at save' ) with self.mock_mail_gateway(): saved_form._action_send_mail() message = self.test_record.message_ids[0] self.assertEqual(message.body, 'Hello
') self.assertEqual(message.partner_ids, partner_private + partner_classic) self.assertEqual(message.subject, f'{test_record.name}') @mute_logger('odoo.addons.base.models.ir_rule', 'odoo.addons.mail.models.mail_mail') @users('employee') def test_composer_default_recipients_private_norights(self): """ Test usage of a private partner in composer when not having the rights to see them, as default value """ self.user_employee.write({'company_ids': [ (3, self.other_company.id), ]}) with self.assertRaises(AccessError): _name = self.partner_private.with_env(self.env).name partner_classic = self.partner_classic.with_env(self.env) test_record = self.test_record.with_env(self.env) with self.assertRaises(AccessError): _form = Form(self.env['mail.compose.message'].with_context({ 'default_partner_ids': (self.partner_private + partner_classic).ids, 'default_model': test_record._name, 'default_res_ids': test_record.ids, })) @mute_logger('odoo.addons.mail.models.mail_mail') @users('employee') def test_composer_template_recipients_private(self): """ Test usage of a private partner in composer, coming from template value """ email_to_new = 'new.customer@test.example.com' self.mail_template.write({ 'email_to': f'{self.partner_private_2.email_formatted}, {email_to_new}', 'partner_to': f'{self.partner_private.id},{self.partner_classic.id}', }) template = self.mail_template.with_env(self.env) partner_private = self.partner_private.with_env(self.env) partner_private_2 = self.partner_private_2.with_env(self.env) partner_classic = self.partner_classic.with_env(self.env) test_record = self.test_record.with_env(self.env) form = Form(self.env['mail.compose.message'].with_context({ 'default_model': test_record._name, 'default_res_ids': test_record.ids, 'default_template_id': template.id, })) # transformation from email_to into partner_ids: find or create existing_partner = self.env['res.partner'].search( [('email_normalized', '=', self.partner_private_2.email_normalized)] ) self.assertEqual(existing_partner, partner_private_2, 'Should find existing private contact') new_partner = self.env['res.partner'].search( [('email_normalized', '=', email_to_new)] ) self.assertEqual(new_partner.type, 'contact', 'Should create a new contact') self.assertEqual( sorted(form.partner_ids.ids), sorted((partner_private + partner_classic + partner_private_2 + new_partner).ids), 'Template populates the field with both email_to and partner_to' ) saved_form = form.save() self.assertEqual( saved_form.partner_ids, partner_private + partner_classic + partner_private_2 + new_partner, 'Template value is kept at save' ) with self.mock_mail_gateway(): saved_form._action_send_mail() message = self.test_record.message_ids[0] self.assertIn('