[MERGE] [FIX] mail: fixed wrong parsing of multipart/mixed emails. Alternative mode...
authorThibault Delavallée <tde@openerp.com>
Fri, 23 Aug 2013 12:37:16 +0000 (14:37 +0200)
committerThibault Delavallée <tde@openerp.com>
Fri, 23 Aug 2013 12:37:16 +0000 (14:37 +0200)
Added tests.

Also added some notification_email_send=none in mail demo data to avoid sending emails in demo/data/update.

bzr revid: tde@openerp.com-20130823123716-anqlxg1gk6c19kdc

addons/mail/data/mail_data.xml
addons/mail/mail_thread.py
addons/mail/tests/test_mail_features.py
addons/mail/tests/test_mail_gateway.py
addons/portal/portal_demo.xml

index 02cfd62..8af808e 100644 (file)
             <field name="args">()</field>
         </record>
 
+        <!-- Admin should not receivve emails at creation -->
+        <record id="base.partner_root" model="res.partner">
+            <field name="notification_email_send">none</field>
+        </record>
+
         <!-- Discussion subtype for messaging / Chatter -->
         <record id="mt_comment" model="mail.message.subtype">
             <field name="name">Discussions</field>
index ff8c5cd..b6754c2 100644 (file)
@@ -748,8 +748,10 @@ class mail_thread(osv.AbstractModel):
                 # text/plain -> <pre/>
                 body = tools.append_content_to_html(u'', body, preserve=True)
         else:
-            alternative = (message.get_content_type() == 'multipart/alternative')
+            alternative = False
             for part in message.walk():
+                if part.get_content_type() == 'multipart/alternative':
+                    alternative = True
                 if part.get_content_maintype() == 'multipart':
                     continue  # skip container
                 filename = part.get_filename()  # None if normal part
index e1bef83..ef6e54d 100644 (file)
@@ -234,7 +234,7 @@ class test_mail(TestMailBase):
         # Data creation
         # --------------------------------------------------
         # 0 - Update existing users-partners
-        self.res_users.write(cr, uid, [uid], {'email': 'a@a'})
+        self.res_users.write(cr, uid, [uid], {'email': 'a@a', 'notification_email_send': 'comment'})
         self.res_users.write(cr, uid, [self.user_raoul_id], {'email': 'r@r'})
         # 1 - Bert Tartopoils, with email, should receive emails for comments and emails
         p_b_id = self.res_partner.create(cr, uid, {'name': 'Bert Tartopoils', 'email': 'b@b'})
index e4269d5..8b10afe 100644 (file)
@@ -80,6 +80,67 @@ Please call me as soon as possible this afternoon!
 Sylvie
 """
 
+MAIL_MULTIPART_MIXED = """Return-Path: <ignasse.carambar@gmail.com>
+X-Original-To: raoul@grosbedon.fr
+Delivered-To: raoul@grosbedon.fr
+Received: by mail1.grosbedon.com (Postfix, from userid 10002)
+    id E8166BFACA; Fri, 23 Aug 2013 13:18:01 +0200 (CEST)
+X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail1.grosbedon.com
+X-Spam-Level: 
+X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,FREEMAIL_FROM,
+    HTML_MESSAGE,RCVD_IN_DNSWL_LOW autolearn=unavailable version=3.3.1
+Received: from mail-ie0-f173.google.com (mail-ie0-f173.google.com [209.85.223.173])
+    by mail1.grosbedon.com (Postfix) with ESMTPS id 9BBD7BFAAA
+    for <raoul@openerp.fr>; Fri, 23 Aug 2013 13:17:55 +0200 (CEST)
+Received: by mail-ie0-f173.google.com with SMTP id qd12so575130ieb.4
+        for <raoul@grosbedon.fr>; Fri, 23 Aug 2013 04:17:54 -0700 (PDT)
+DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
+        d=gmail.com; s=20120113;
+        h=mime-version:date:message-id:subject:from:to:content-type;
+        bh=dMNHV52EC7GAa7+9a9tqwT9joy9z+1950J/3A6/M/hU=;
+        b=DGuv0VjegdSrEe36ADC8XZ9Inrb3Iu+3/52Bm+caltddXFH9yewTr0JkCRQaJgMwG9
+         qXTQgP8qu/VFEbCh6scu5ZgU1hknzlNCYr3LT+Ih7dAZVUEHUJdwjzUU1LFV95G2RaCd
+         /Lwff6CibuUvrA+0CBO7IRKW0Sn5j0mukYu8dbaKsm6ou6HqS8Nuj85fcXJfHSHp6Y9u
+         dmE8jBh3fHCHF/nAvU+8aBNSIzl1FGfiBYb2jCoapIuVFitKR4q5cuoodpkH9XqqtOdH
+         DG+YjEyi8L7uvdOfN16eMr7hfUkQei1yQgvGu9/5kXoHg9+Gx6VsZIycn4zoaXTV3Nhn
+         nu4g==
+MIME-Version: 1.0
+X-Received: by 10.50.124.65 with SMTP id mg1mr1144467igb.43.1377256674216;
+ Fri, 23 Aug 2013 04:17:54 -0700 (PDT)
+Received: by 10.43.99.71 with HTTP; Fri, 23 Aug 2013 04:17:54 -0700 (PDT)
+Date: Fri, 23 Aug 2013 13:17:54 +0200
+Message-ID: <CAP76m_V4BY2F7DWHzwfjteyhW8L2LJswVshtmtVym+LUJ=rASQ@mail.gmail.com>
+Subject: Test mail multipart/mixed
+From: =?ISO-8859-1?Q?Raoul Grosbedon=E9e?= <ignasse.carambar@gmail.com>
+To: Followers of ASUSTeK-Joseph-Walters <raoul@grosbedon.fr>
+Content-Type: multipart/mixed; boundary=089e01536c4ed4d17204e49b8e96
+
+--089e01536c4ed4d17204e49b8e96
+Content-Type: multipart/alternative; boundary=089e01536c4ed4d16d04e49b8e94
+
+--089e01536c4ed4d16d04e49b8e94
+Content-Type: text/plain; charset=ISO-8859-1
+
+Should create a multipart/mixed: from gmail, *bold*, with attachment.
+
+-- 
+Marcel Boitempoils.
+
+--089e01536c4ed4d16d04e49b8e94
+Content-Type: text/html; charset=ISO-8859-1
+
+<div dir="ltr">Should create a multipart/mixed: from gmail, <b>bold</b>, with attachment.<br clear="all"><div><br></div>-- <br>Marcel Boitempoils.</div>
+
+--089e01536c4ed4d16d04e49b8e94--
+--089e01536c4ed4d17204e49b8e96
+Content-Type: text/plain; charset=US-ASCII; name="test.txt"
+Content-Disposition: attachment; filename="test.txt"
+Content-Transfer-Encoding: base64
+X-Attachment-Id: f_hkpb27k00
+
+dGVzdAo=
+--089e01536c4ed4d17204e49b8e96--"""
+
 
 class TestMailgateway(TestMailBase):
 
@@ -123,6 +184,24 @@ class TestMailgateway(TestMailBase):
         self.assertEqual(partner_info['partner_id'], p_b_id,
                         'mail_thread: message_find_partner_from_emails wrong partner found')
 
+    def test_09_message_parse(self):
+        """ Testing incoming emails parsing """
+        cr, uid = self.cr, self.uid
+
+        res = self.mail_thread.message_parse(cr, uid, MAIL_TEMPLATE_PLAINTEXT)
+        self.assertIn('Please call me as soon as possible this afternoon!', res.get('body', ''),
+                      'message_parse: missing text in text/plain body after parsing')
+
+        res = self.mail_thread.message_parse(cr, uid, MAIL_TEMPLATE)
+        self.assertIn('<p>Please call me as soon as possible this afternoon!</p>', res.get('body', ''),
+                      'message_parse: missing html in multipart/alternative body after parsing')
+
+        res = self.mail_thread.message_parse(cr, uid, MAIL_MULTIPART_MIXED)
+        self.assertNotIn('Should create a multipart/mixed: from gmail, *bold*, with attachment', res.get('body', ''),
+                         'message_parse: text version should not be in body after parsing multipart/mixed')
+        self.assertIn('<div dir="ltr">Should create a multipart/mixed: from gmail, <b>bold</b>, with attachment.<br clear="all"><div><br></div>', res.get('body', ''),
+                      'message_parse: html version should be in body after parsing multipart/mixed')
+
     def test_10_message_process(self):
         """ Testing incoming emails processing. """
         cr, uid, user_raoul = self.cr, self.uid, self.user_raoul
@@ -362,7 +441,7 @@ class TestMailgateway(TestMailBase):
         frog_group = self.mail_group.browse(cr, uid, frog_groups[0])
         msg = frog_group.message_ids[0]
         # Test: plain text content should be wrapped and stored as html
-        self.assertEqual(msg.body, '<pre>\nPlease call me as soon as possible this afternoon!\n\n--\nSylvie\n</pre>',
+        self.assertIn('<pre>\nPlease call me as soon as possible this afternoon!\n\n--\nSylvie\n</pre>', msg.body,
                             'message_process: plaintext incoming email incorrectly parsed')
 
     def test_20_thread_parent_resolution(self):
index c05189f..361e427 100644 (file)
@@ -8,6 +8,7 @@
             <field name="email">demo@portal.example.com</field>
             <field name="supplier" eval="False"/>
             <field name="customer" eval="True"/>
+            <field name="notification_email_send">none</field>
         </record>
         <record id="demo_user0" model="res.users">
             <field name="partner_id" ref="partner_demo_portal"/>