diff options
| -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') |
