diff options
| author | Nicolas Galler <ngl@odoo.com> | 2020-10-19 12:12:09 +0000 |
|---|---|---|
| committer | Nicolas Galler <ngl@odoo.com> | 2020-10-21 09:26:59 +0000 |
| commit | 43718d0694d8d0c7b53e3511cb8523a645ebddb1 (patch) | |
| tree | ae87793caa532d392bad5df25177cbbc03feb85f /addons | |
| parent | f31bdc0ae16698306a776332f3aa4aaa1cade764 (diff) | |
[FIX] product_email_template: use superuser account to send email in su mode
Behavior prior to the fix:
The email template associated with a product on an invoice placed via
the web portal is sent with no email_from, resulting in a failure. The
reason is that before 13.0 .sudo() before sending a mail would send it
as superuser user (which was the intention in this case), but since 13.0
for the same intention we need .with_user(SUPERUSER_ID).
Behavior after the fix:
When sending the product email, if we are in SU mode, we'll switch to
the super user account, emulating the pre-13.0 behavior.
Similar fix to b12bcfbb1b44cc72cdf73506c540c387f00a37e9
opw-2346415
closes odoo/odoo#60409
X-original-commit: 7749fec536bd1533f0da0a2bc0df084e4e80ed4d
Signed-off-by: Nicolas Galler <nicocrm@users.noreply.github.com>
Signed-off-by: Nicolas Martinelli (nim) <nim@odoo.com>
Diffstat (limited to 'addons')
| -rw-r--r-- | addons/product_email_template/models/account_move.py | 5 | ||||
| -rw-r--r-- | addons/product_email_template/tests/__init__.py | 3 | ||||
| -rw-r--r-- | addons/product_email_template/tests/test_account_move.py | 70 |
3 files changed, 77 insertions, 1 deletions
diff --git a/addons/product_email_template/models/account_move.py b/addons/product_email_template/models/account_move.py index 4f65d13da07..07039372614 100644 --- a/addons/product_email_template/models/account_move.py +++ b/addons/product_email_template/models/account_move.py @@ -1,12 +1,15 @@ # -*- coding: utf-8 -*- -from odoo import api, models +from odoo import api, models, SUPERUSER_ID class AccountMove(models.Model): _inherit = 'account.move' def invoice_validate_send_email(self): + if self.env.su: + # sending mail in sudo was meant for it being sent from superuser + self = self.with_user(SUPERUSER_ID) for invoice in self.filtered(lambda x: x.move_type == 'out_invoice'): # send template only on customer invoice # subscribe the partner to the invoice diff --git a/addons/product_email_template/tests/__init__.py b/addons/product_email_template/tests/__init__.py new file mode 100644 index 00000000000..e2899d2c553 --- /dev/null +++ b/addons/product_email_template/tests/__init__.py @@ -0,0 +1,3 @@ +# -*- coding: utf-8 -*- + +from . import test_account_move diff --git a/addons/product_email_template/tests/test_account_move.py b/addons/product_email_template/tests/test_account_move.py new file mode 100644 index 00000000000..4ed7833f5a3 --- /dev/null +++ b/addons/product_email_template/tests/test_account_move.py @@ -0,0 +1,70 @@ +# -*- coding: utf-8 -*- +from odoo.addons.account.tests.common import AccountTestInvoicingCommon +from odoo.tests import tagged + +@tagged('post_install', '-at_install') +class TestAccountMove(AccountTestInvoicingCommon): + def setUp(self): + super().setUp() + Template = self.env['mail.template'] + self.template = Template.create({ + 'name': 'Product Template', + 'subject': 'YOUR PRODUCT', + 'model_id': self.env['ir.model']._get_id('product.template') + }) + self.customer = self.env['res.partner'].create({ + 'name': 'James Bond', + 'email': 'james.bond@yopmail.com' + }) + self.product_a.email_template_id = self.template.id + + def test_send_product_template_email_on_invoice_post(self): + id_max = self.env['mail.message'].search([], order='id desc', limit=1) + if id_max: + id_max = id_max[0].id + else: + id_max = 0 + invoice = self.env['account.move'].create({ + 'move_type': 'out_invoice', + 'partner_id': self.customer.id, + 'invoice_line_ids': [(0, 0, { + 'name': 'Walter PPK', + 'quantity': 1, + 'price_unit': 123, + 'product_id': self.product_a.id + })] + }) + invoice.action_post() + message_sent = self.env['mail.message'].search([('id', '>', id_max), ('subject', '=', 'YOUR PRODUCT')]) + self.assertEqual(len(message_sent), 1, 'Should send 1 message for product') + self.assertTrue(message_sent[0].email_from, 'Should have from email address') + + def test_send_as_system_when_sudo(self): + """ + Test scenario of a product ordered through the portal. + """ + id_max = self.env['mail.message'].search([], order='id desc', limit=1) + if id_max: + id_max = id_max[0].id + else: + id_max = 0 + invoice = self.env['account.move'].create({ + 'move_type': 'out_invoice', + 'partner_id': self.customer.id, + 'invoice_line_ids': [(0, 0, { + 'name': 'Walter PPK', + 'quantity': 1, + 'price_unit': 123, + 'product_id': self.product_a.id + })] + }) + pub_user = self.env['res.users'].create({ + 'login': 'test_public_user', + 'name': 'test_public_user', + 'email': False, + 'groups_id': [(6, 0, [self.env.ref('base.group_public').id])] + }) + invoice.with_user(pub_user).sudo().action_post() + message_sent = self.env['mail.message'].search([('id', '>', id_max), ('subject', '=', 'YOUR PRODUCT')]) + self.assertEqual(len(message_sent), 1, 'Should send 1 message for product') + self.assertTrue(message_sent[0].email_from, 'Should have from email address') |
