# -*- coding: utf-8 -*- ############################################################################## # # OpenERP, Open Source Business Applications # Copyright (c) 2012-TODAY OpenERP S.A. # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as # published by the Free Software Foundation, either version 3 of the # License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . # ############################################################################## from openerp.tests import common MAIL_TEMPLATE = """Return-Path: To: {to} Received: by mail1.openerp.com (Postfix, from userid 10002) id 5DF9ABFB2A; Fri, 10 Aug 2012 16:16:39 +0200 (CEST) From: Sylvie Lelitre Subject: {subject} MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_4200734_24778174.1344608186754" Date: Fri, 10 Aug 2012 14:16:26 +0000 Message-ID: <1198923581.41972151344608186760.JavaMail@agrolait.com> {extra} ------=_Part_4200734_24778174.1344608186754 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Please call me as soon as possible this afternoon! -- Sylvie ------=_Part_4200734_24778174.1344608186754 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable =20 =20 =20

Please call me as soon as possible this afternoon!

--
Sylvie

------=_Part_4200734_24778174.1344608186754-- """ class test_mail(common.TransactionCase): def setUp(self): super(test_mail, self).setUp() self.ir_model = self.registry('ir.model') self.mail_alias = self.registry('mail.alias') self.mail_thread = self.registry('mail.thread') self.mail_group = self.registry('mail.group') self.res_users = self.registry('res.users') # groups@.. will cause the creation of new mail groups self.mail_group_model_id = self.ir_model.search(self.cr, self.uid, [('model','=', 'mail.group')])[0] self.mail_alias.create(self.cr, self.uid, {'alias_name': 'groups', 'alias_model_id': self.mail_group_model_id}) # tech@... will append new messages to the 'tech' group self.group_tech_id = self.mail_group.create(self.cr, self.uid, {'name': 'tech'}) def test_message_process(self): # Incoming mail creates a new mail_group "frogs" self.assertEqual(self.mail_group.search(self.cr, self.uid, [('name','=','frogs')]), []) mail_frogs = MAIL_TEMPLATE.format(to='groups@example.com, other@gmail.com', subject='frogs', extra='') self.mail_thread.message_process(self.cr, self.uid, None, mail_frogs) frog_groups = self.mail_group.search(self.cr, self.uid, [('name','=','frogs')]) self.assertTrue(len(frog_groups) == 1) # Previously-created group can be emailed now - it should have an implicit alias group+frogs@... frog_group = self.mail_group.browse(self.cr, self.uid, frog_groups[0]) group_messages = frog_group.message_ids self.assertTrue(len(group_messages) == 1, 'New group should only have the original message') mail_frog_news = MAIL_TEMPLATE.format(to='Friendly Frogs ', subject='news', extra='') self.mail_thread.message_process(self.cr, self.uid, None, mail_frog_news) frog_group.refresh() self.assertTrue(len(frog_group.message_ids) == 2, 'Group should contain 2 messages now') # Even with a wrong destination, a reply should end up in the correct thread mail_reply = MAIL_TEMPLATE.format(to='erroneous@example.com>', subject='Re: news', extra='In-Reply-To: <12321321-openerp-%d-mail.group@example.com>\n'%frog_group.id) self.mail_thread.message_process(self.cr, self.uid, None, mail_reply) frog_group.refresh() self.assertTrue(len(frog_group.message_ids) == 3, 'Group should contain 3 messages now') # No model passed and no matching alias must raise mail_spam = MAIL_TEMPLATE.format(to='noone@example.com', subject='spam', extra='') self.assertRaises(Exception, self.mail_thread.message_process, self.cr, self.uid, None, mail_spam)