From ea4c4a22c41ec780818ec03ff6801791bda46065 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Thu, 18 Aug 2016 13:12:35 +0200 Subject: [PATCH] [IMP] mail: clean test about forwarded emails and parent_id Previous version of the test was actually not correct. Mailgateway was not considering references as in-reply-to when trying to find answers that have to be considered as a new thread instead of an answer to an existing thread. Purpose of the test is actually to be sure that answers considered as a new thread have no parent_id set. Otherwise you may have threads linked to messages belonging to other records, leading to access right issues. Moreover if this email begins a new discussion thread it makes no sense to have a parent message set. A new test is written to better simulate the test case. It sets a message_id containing reply_to which is the way odoo uses to indicated that answers to a message have to be considered as new threads. This is used notably in mass mailing for example. --- addons/mail/tests/test_mail_gateway.py | 57 ++++++++++++++++++-------- 1 file changed, 39 insertions(+), 18 deletions(-) diff --git a/addons/mail/tests/test_mail_gateway.py b/addons/mail/tests/test_mail_gateway.py index 147d0ed6c81..f5492dc22e9 100644 --- a/addons/mail/tests/test_mail_gateway.py +++ b/addons/mail/tests/test_mail_gateway.py @@ -512,7 +512,7 @@ class TestMailgateway(TestMail): @mute_logger('openerp.addons.mail.models.mail_thread', 'openerp.models') def test_message_process_in_reply_to(self): - """ Incoming email using in-rely-to should go into the right destination event with a wrong destination """ + """ Incoming email using in-rely-to should go into the right destination even with a wrong destination """ self.format_and_process( MAIL_TEMPLATE, email_from='valid.other@gmail.com', msg_id='<1198923581.41972151344608186800.JavaMail.diff1@agrolait.com>', @@ -524,7 +524,7 @@ class TestMailgateway(TestMail): @mute_logger('openerp.addons.mail.models.mail_thread', 'openerp.models') def test_message_process_references(self): - """ Incoming email using in-rely-to should go into the right destination event with a wrong destination """ + """ Incoming email using references should go into the right destination even with a wrong destination """ self.format_and_process( MAIL_TEMPLATE, to='erroneous@example.com', extra='References: <2233@a.com>\r\n\t<3edss_dsa@b.com> %s' % self.fake_email.message_id, @@ -700,21 +700,42 @@ class TestMailgateway(TestMail): self.assertEqual(msg.needaction_partner_ids, self.user_employee.partner_id | self.env.user.partner_id, 'message_post: private discussion: incorrect notified recipients when replying') - # Do bert forward it to an alias - msg = self.env['mail.message'].browse(msg1.id) + @mute_logger('openerp.addons.mail.models.mail_thread', 'openerp.models', 'openerp.addons.mail.models.mail_mail') + def test_forward_parent_id(self): + msg = self.group_pigs.sudo(self.user_employee).message_post(no_auto_thread=True, subtype='mail.mt_comment') + self.assertNotIn(msg.model, msg.message_id) + self.assertNotIn('-%d-' % msg.res_id, msg.message_id) + self.assertIn('reply_to', msg.message_id) + # forward it to a new thread AND an existing thread - for i, to in enumerate(['groups', 'public']): - fw_msg_id = '' % (i,) - fw_message = MAIL_TEMPLATE.format(to='%s@example.com' % (to,), - cc='', - subject='FW: Re: 1', - email_from='b.t@example.com', - extra='References: %s' % msg.message_id, - msg_id=fw_msg_id) - self.env['mail.thread'].message_process(None, fw_message) + fw_msg_id = '' + fw_message = MAIL_TEMPLATE.format(to='groups@example.com', + cc='', + subject='FW: Re: 1', + email_from='b.t@example.com', + extra='In-Reply-To: %s' % msg.message_id, + msg_id=fw_msg_id) + self.env['mail.thread'].message_process(None, fw_message) + msg_fw = self.env['mail.message'].search([('message_id', '=', fw_msg_id)]) + self.assertEqual(len(msg_fw), 1) + channel = self.env['mail.channel'].search([('name', "=", msg_fw.subject)]) + self.assertEqual(len(channel), 1) + self.assertEqual(msg_fw.model, 'mail.channel') + self.assertFalse(msg_fw.parent_id) + self.assertTrue(msg_fw.res_id == channel.id) - msg_fw = self.env['mail.message'].search([('message_id', '=', fw_msg_id)]) - self.assertEqual(len(msg_fw), 1) - - self.assertEqual(msg_fw.model, 'mail.channel') - self.assertFalse(msg_fw.parent_id) + fw_msg_id = '' + fw_message = MAIL_TEMPLATE.format(to='public@example.com', + cc='', + subject='FW: Re: 2', + email_from='b.t@example.com', + extra='In-Reply-To: %s' % msg.message_id, + msg_id=fw_msg_id) + self.env['mail.thread'].message_process(None, fw_message) + msg_fw = self.env['mail.message'].search([('message_id', '=', fw_msg_id)]) + self.assertEqual(len(msg_fw), 1) + channel = self.env['mail.channel'].search([('name', "=", msg_fw.subject)]) + self.assertEqual(len(channel), 0) + self.assertEqual(msg_fw.model, 'mail.channel') + self.assertFalse(msg_fw.parent_id) + self.assertTrue(msg_fw.res_id == self.group_public.id)