diff options
| author | dht-odoo <dht@odoo.com> | 2021-03-19 10:43:00 +0000 |
|---|---|---|
| committer | Thibault Delavallée <tde@odoo.com> | 2021-04-13 12:14:29 +0000 |
| commit | e5880130c1534217c5bae905af8e5a040f40a819 (patch) | |
| tree | ab6c18d0161efa419cd885d3fcac109f169e76e9 /addons/test_mail | |
| parent | a2985f32afeeca992bfd46c792549239935f2128 (diff) | |
[FIX] {test_}mail: properly handle deletion of mail notif from composer
Before this commit:
When someone sends mail with help of mail_composer(mail.compose.message)
without having a mail template set, mail is not deleted after being sent
(and causes unwanted/unexpected burden on DB). This bug was introduced
with commit[1], where auto deletion was disabled if template is not set
on the composer.
After this commit:
If there is no mail template set on the composer, or if the template is
set and 'auto_delete' is enabled on that template, the mail sent from
composer will be deleted after being sent successfully.
A context key is added to allow controlling this behavior. It is notably use
to check query counters differences.
commit[1] - https://github.com/odoo/odoo/commit/8a026e26c6ae25e3fdb5369a7fa3343d70fab782#diff-898a3d5c08a567c1a7b82c026a213d796e850e376cbdb1c9e7936409f440d37aR257
Task id: 2484915
PR odoo/odoo#68870
Original Pr #68403
X-original-commit: 05ad6c477f7da2a495e5baf5b4629323b9d11fa0
Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com>
Diffstat (limited to 'addons/test_mail')
| -rw-r--r-- | addons/test_mail/tests/test_mail_composer.py | 42 | ||||
| -rw-r--r-- | addons/test_mail/tests/test_performance.py | 21 |
2 files changed, 63 insertions, 0 deletions
diff --git a/addons/test_mail/tests/test_mail_composer.py b/addons/test_mail/tests/test_mail_composer.py index 0547caca98d..6c60ea3d293 100644 --- a/addons/test_mail/tests/test_mail_composer.py +++ b/addons/test_mail/tests/test_mail_composer.py @@ -448,6 +448,48 @@ class TestComposerResultsComment(TestMailComposer): @users('employee') @mute_logger('odoo.tests', 'odoo.addons.mail.models.mail_mail') + def test_mail_composer_notifications_delete(self): + """ Notifications are correctly deleted once sent """ + composer = self.env['mail.compose.message'].with_context( + self._get_web_context(self.test_record) + ).create({ + 'body': '<p>Test Body</p>', + 'partner_ids': [(4, self.partner_1.id), (4, self.partner_2.id)] + }) + with self.mock_mail_gateway(mail_unlink_sent=True): + composer.send_mail() + + # notifications + message = self.test_record.message_ids[0] + self.assertEqual(message.notified_partner_ids, self.partner_employee_2 + self.partner_1 + self.partner_2) + + # global outgoing + self.assertEqual(len(self._new_mails), 2, 'Should have created 2 mail.mail (1 for users, 1 for customers)') + self.assertEqual(len(self._mails), 3, 'Should have sent an email each recipient') + self.assertEqual(self._new_mails.exists(), self.env['mail.mail'], 'Should have deleted mail.mail records') + + # ensure ``mail_auto_delete`` context key allow to override this behavior + composer = self.env['mail.compose.message'].with_context( + self._get_web_context(self.test_record), + mail_auto_delete=False, + ).create({ + 'body': '<p>Test Body</p>', + 'partner_ids': [(4, self.partner_1.id), (4, self.partner_2.id)] + }) + with self.mock_mail_gateway(mail_unlink_sent=True): + composer.send_mail() + + # notifications + message = self.test_record.message_ids[0] + self.assertEqual(message.notified_partner_ids, self.partner_employee_2 + self.partner_1 + self.partner_2) + + # global outgoing + self.assertEqual(len(self._new_mails), 2, 'Should have created 2 mail.mail (1 for users, 1 for customers)') + self.assertEqual(len(self._mails), 3, 'Should have sent an email each recipient') + self.assertEqual(len(self._new_mails.exists()), 2, 'Should not have deleted mail.mail records') + + @users('employee') + @mute_logger('odoo.tests', 'odoo.addons.mail.models.mail_mail') def test_mail_composer_recipients(self): """ Test partner_ids given to composer are given to the final message. """ composer = self.env['mail.compose.message'].with_context( diff --git a/addons/test_mail/tests/test_performance.py b/addons/test_mail/tests/test_performance.py index fa64f9d39f2..b0fad48da95 100644 --- a/addons/test_mail/tests/test_performance.py +++ b/addons/test_mail/tests/test_performance.py @@ -270,6 +270,27 @@ class TestMailAPIPerformance(BaseMailPerformance): 'partner_ids': [(4, customer_id)], }) + with self.assertQueryCount(__system__=32, emp=39): + composer.send_mail() + + @users('__system__', 'emp') + @warmup + @mute_logger('odoo.tests', 'odoo.addons.mail.models.mail_mail') + def test_mail_composer_nodelete(self): + self._create_test_records() + test_record = self.env['mail.test.ticket'].browse(self.test_record_full.id) + customer_id = self.customer.id + with self.assertQueryCount(__system__=2, emp=2): + composer = self.env['mail.compose.message'].with_context({ + 'default_composition_mode': 'comment', + 'default_model': test_record._name, + 'default_res_id': test_record.id, + 'mail_auto_delete': False, + }).create({ + 'body': '<p>Test Body</p>', + 'partner_ids': [(4, customer_id)], + }) + with self.assertQueryCount(__system__=25, emp=32): composer.send_mail() |
