[IMP] sale order line invisible type
[odoo/odoo.git] / addons / mail / tests / test_mail.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Business Applications
5 #    Copyright (c) 2012-TODAY OpenERP S.A. <http://openerp.com>
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22 from openerp.tests import common
23
24 MAIL_TEMPLATE = """Return-Path: <whatever-2a840@postmaster.twitter.com>
25 To: {to}
26 Received: by mail1.openerp.com (Postfix, from userid 10002)
27     id 5DF9ABFB2A; Fri, 10 Aug 2012 16:16:39 +0200 (CEST)
28 From: Sylvie Lelitre <sylvie.lelitre@agrolait.com>
29 Subject: {subject}
30 MIME-Version: 1.0
31 Content-Type: multipart/alternative; 
32     boundary="----=_Part_4200734_24778174.1344608186754"
33 Date: Fri, 10 Aug 2012 14:16:26 +0000
34 Message-ID: <1198923581.41972151344608186760.JavaMail@agrolait.com>
35 {extra}
36 ------=_Part_4200734_24778174.1344608186754
37 Content-Type: text/plain; charset=utf-8
38 Content-Transfer-Encoding: quoted-printable
39
40 Please call me as soon as possible this afternoon!
41
42 --
43 Sylvie
44 ------=_Part_4200734_24778174.1344608186754
45 Content-Type: text/html; charset=utf-8
46 Content-Transfer-Encoding: quoted-printable
47
48 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
49 <html>
50  <head>=20
51   <meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Dutf-8" />
52  </head>=20
53  <body style=3D"margin: 0; padding: 0; background: #ffffff;-webkit-text-size-adjust: 100%;">=20
54   
55   <p>Please call me as soon as possible this afternoon!</p>
56   
57   <p>--<br/>
58      Sylvie
59   <p>
60  </body>
61 </html>
62 ------=_Part_4200734_24778174.1344608186754--
63 """
64
65
66 class test_mail(common.TransactionCase):
67
68     def setUp(self):
69         super(test_mail, self).setUp()
70         self.ir_model = self.registry('ir.model')
71         self.mail_alias = self.registry('mail.alias')
72         self.mail_thread = self.registry('mail.thread')
73         self.mail_group = self.registry('mail.group')
74         self.res_users = self.registry('res.users')
75
76         # groups@.. will cause the creation of new mail groups
77         self.mail_group_model_id = self.ir_model.search(self.cr, self.uid, [('model','=', 'mail.group')])[0]
78         self.mail_alias.create(self.cr, self.uid, {'alias_name': 'groups',
79                                                    'alias_model_id': self.mail_group_model_id})
80         
81         # tech@... will append new messages to the 'tech' group 
82         self.group_tech_id = self.mail_group.create(self.cr, self.uid, {'name': 'tech'})
83
84     def test_message_process(self):
85         # Incoming mail creates a new mail_group "frogs"
86         self.assertEqual(self.mail_group.search(self.cr, self.uid, [('name','=','frogs')]), [])
87         mail_frogs = MAIL_TEMPLATE.format(to='groups@example.com, other@gmail.com', subject='frogs', extra='')
88         self.mail_thread.message_process(self.cr, self.uid, None, mail_frogs)
89         frog_groups = self.mail_group.search(self.cr, self.uid, [('name','=','frogs')])
90         self.assertTrue(len(frog_groups) == 1)
91
92         # Previously-created group can be emailed now - it should have an implicit alias group+frogs@...
93         frog_group = self.mail_group.browse(self.cr, self.uid, frog_groups[0])
94         group_messages = frog_group.message_ids
95         self.assertTrue(len(group_messages) == 1, 'New group should only have the original message')
96         mail_frog_news = MAIL_TEMPLATE.format(to='Friendly Frogs <group+frogs@example.com>', subject='news', extra='')
97         self.mail_thread.message_process(self.cr, self.uid, None, mail_frog_news)
98         frog_group.refresh()
99         self.assertTrue(len(frog_group.message_ids) == 2, 'Group should contain 2 messages now')
100
101         # Even with a wrong destination, a reply should end up in the correct thread
102         mail_reply = MAIL_TEMPLATE.format(to='erroneous@example.com>', subject='Re: news',
103                                           extra='In-Reply-To: <12321321-openerp-%d-mail.group@example.com>\n'%frog_group.id)
104         self.mail_thread.message_process(self.cr, self.uid, None, mail_reply)
105         frog_group.refresh()
106         self.assertTrue(len(frog_group.message_ids) == 3, 'Group should contain 3 messages now')
107         
108         # No model passed and no matching alias must raise
109         mail_spam = MAIL_TEMPLATE.format(to='noone@example.com', subject='spam', extra='')
110         self.assertRaises(Exception,
111                           self.mail_thread.message_process,
112                           self.cr, self.uid, None, mail_spam)