diff options
| author | Thibault Francois <tfr@openerp.com> | 2020-01-23 15:39:57 +0000 |
|---|---|---|
| committer | Thibault Francois <tfr@openerp.com> | 2020-02-14 10:40:57 +0000 |
| commit | 8c1c7f26876eac8a431ef12fe373298fd01ce241 (patch) | |
| tree | ad12a749705e5c8b912b0cd577b96935835c2b41 /addons/test_mail | |
| parent | 7639f7fbcb7578ca77509c656737747904b1b927 (diff) | |
[FIX] mail: Don't create duplicate mail.notification
When more than one user are linked to the same res.partner and this user is assigned to a task or a sale.order
as many email notifications will be created as there are users linked to the same res.partner.
This won't send more than 1 email, but multiple mail.notification
records will be created.
This causes duplicate entries in the mail.notification table, and can
crash when an old unique constraint is still present in that table (from
earlier Odoo versions)
To prevent this, we deduplicate the notifications and order by notification
type to get 'email' first in case the user have different notification
type ('email', 'inbox').
How to reproduce:
- Create a first user test1
- Create a second user test2
- Merge the contact test1 and test2
- Create a customer customer
- Set the user test1 as salesman
- Create a new sale.order with customer as partner
Add a test to reproduce the pathological scenario
Solution
add distinct on partner.id and order by partner.id,
users.notification_type
closes odoo/odoo#45386
X-original-commit: 00f36c60e4790c8f5341cbc4f2c67e22e6d68f87
Signed-off-by: Olivier Dony (odo) <odo@openerp.com>
Diffstat (limited to 'addons/test_mail')
| -rw-r--r-- | addons/test_mail/tests/test_mail_followers.py | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/addons/test_mail/tests/test_mail_followers.py b/addons/test_mail/tests/test_mail_followers.py index f3b1957d6c6..1e1597334d5 100644 --- a/addons/test_mail/tests/test_mail_followers.py +++ b/addons/test_mail/tests/test_mail_followers.py @@ -3,6 +3,7 @@ from psycopg2 import IntegrityError +from odoo.tests import tagged from odoo.addons.test_mail.tests.common import TestMailCommon from odoo.tools.misc import mute_logger @@ -205,3 +206,38 @@ class AdvancedFollowersTest(TestMailCommon): self.assertEqual( sub1.message_follower_ids.filtered(lambda fol: fol.partner_id == self.user_employee.partner_id).subtype_ids, all_defaults) + + +@tagged('post_install', '-at_install') +class DuplicateNotificationTest(TestMailCommon): + def test_no_duplicate_notification(self): + """ + Check that we only create one mail.notification per partner + + Post install because we need the registery to be ready to send notification + """ + #Simulate case of 2 users that got their partner merged + common_partner = self.env['res.partner'].create({"name": "demo1", "email": "demo1@test.com"}) + user_1 = self.env['res.users'].create({'login': 'demo1', 'partner_id': common_partner.id, 'notification_type': 'email'}) + user_2 = self.env['res.users'].create({'login': 'demo2', 'partner_id': common_partner.id, 'notification_type': 'inbox'}) + + #Trigger auto subscribe notification + test = self.env['mail.test.track'].create({"name": "Test Track", "user_id": user_2.id}) + mail_message = self.env['mail.message'].search([ + ('res_id', '=', test.id), + ('model', '=', 'mail.test.track'), + ('message_type', '=', 'user_notification') + ]) + notif = self.env['mail.notification'].search([ + ('mail_message_id', '=', mail_message.id), + ('res_partner_id', '=', common_partner.id) + ]) + self.assertEqual(len(notif), 1) + self.assertEqual(notif.notification_type, 'email') + + subtype = self.env.ref('mail.mt_comment') + res = self.env['mail.followers']._get_recipient_data(test, 'comment', subtype.id, pids=common_partner.ids) + partner_notif = [r for r in res if r[0] == common_partner.id] + self.assertEqual(len(partner_notif), 1) + self.assertEqual(partner_notif[0][5], 'email') + |
