2012-08-14 08:04:21 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
##############################################################################
|
|
|
|
#
|
|
|
|
# OpenERP, Open Source Business Applications
|
|
|
|
# Copyright (c) 2012-TODAY OpenERP S.A. <http://openerp.com>
|
|
|
|
#
|
|
|
|
# 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 <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
##############################################################################
|
|
|
|
|
|
|
|
from openerp.tests import common
|
|
|
|
|
|
|
|
MAIL_TEMPLATE = """Return-Path: <whatever-2a840@postmaster.twitter.com>
|
|
|
|
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 <sylvie.lelitre@agrolait.com>
|
|
|
|
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
|
|
|
|
|
|
|
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
|
|
|
<html>
|
|
|
|
<head>=20
|
|
|
|
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Dutf-8" />
|
|
|
|
</head>=20
|
|
|
|
<body style=3D"margin: 0; padding: 0; background: #ffffff;-webkit-text-size-adjust: 100%;">=20
|
|
|
|
|
|
|
|
<p>Please call me as soon as possible this afternoon!</p>
|
|
|
|
|
|
|
|
<p>--<br/>
|
|
|
|
Sylvie
|
|
|
|
<p>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
------=_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')
|
2012-08-23 18:06:48 +00:00
|
|
|
self.mail_mail = self.registry('mail.mail')
|
|
|
|
self.mail_message = self.registry('mail.message')
|
2012-08-23 12:25:37 +00:00
|
|
|
self.mail_notification = self.registry('mail.notification')
|
|
|
|
self.mail_followers = self.registry('mail.followers')
|
2012-08-14 08:04:21 +00:00
|
|
|
self.res_users = self.registry('res.users')
|
2012-08-23 12:25:37 +00:00
|
|
|
self.res_partner = self.registry('res.partner')
|
2012-08-14 08:04:21 +00:00
|
|
|
|
|
|
|
# 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})
|
|
|
|
|
2012-08-23 12:25:37 +00:00
|
|
|
# create a 'pigs' group that will be used through the various tests
|
|
|
|
self.group_pigs_id = self.mail_group.create(self.cr, self.uid, {'name': 'pigs'})
|
2012-08-14 08:04:21 +00:00
|
|
|
|
2012-08-23 12:25:37 +00:00
|
|
|
def test_00_message_process(self):
|
2012-08-22 13:30:28 +00:00
|
|
|
cr, uid = self.cr, self.uid
|
2012-08-14 08:04:21 +00:00
|
|
|
# Incoming mail creates a new mail_group "frogs"
|
2012-08-22 13:30:28 +00:00
|
|
|
self.assertEqual(self.mail_group.search(cr, uid, [('name','=','frogs')]), [])
|
2012-08-14 08:04:21 +00:00
|
|
|
mail_frogs = MAIL_TEMPLATE.format(to='groups@example.com, other@gmail.com', subject='frogs', extra='')
|
2012-08-22 13:30:28 +00:00
|
|
|
self.mail_thread.message_process(cr, uid, None, mail_frogs)
|
|
|
|
frog_groups = self.mail_group.search(cr, uid, [('name','=','frogs')])
|
2012-08-14 08:04:21 +00:00
|
|
|
self.assertTrue(len(frog_groups) == 1)
|
|
|
|
|
|
|
|
# Previously-created group can be emailed now - it should have an implicit alias group+frogs@...
|
2012-08-22 13:30:28 +00:00
|
|
|
frog_group = self.mail_group.browse(cr, uid, frog_groups[0])
|
2012-08-14 08:04:21 +00:00
|
|
|
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 <group+frogs@example.com>', subject='news', extra='')
|
2012-08-22 13:30:28 +00:00
|
|
|
self.mail_thread.message_process(cr, uid, None, mail_frog_news)
|
2012-08-14 08:04:21 +00:00
|
|
|
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)
|
2012-08-22 13:30:28 +00:00
|
|
|
self.mail_thread.message_process(cr, uid, None, mail_reply)
|
2012-08-14 08:04:21 +00:00
|
|
|
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,
|
2012-08-22 13:30:28 +00:00
|
|
|
cr, uid, None, mail_spam)
|
2012-08-23 12:25:37 +00:00
|
|
|
|
|
|
|
def test_01_many2many_reference_field(self):
|
|
|
|
""" Tests designed for the many2many_reference field (follower_ids).
|
2012-08-23 13:11:44 +00:00
|
|
|
We will test to perform writes using the many2many commands 0, 3, 4,
|
2012-08-23 12:25:37 +00:00
|
|
|
5 and 6. """
|
|
|
|
cr, uid = self.cr, self.uid
|
|
|
|
user_admin = self.res_users.browse(cr, uid, uid)
|
|
|
|
group_pigs = self.mail_group.browse(cr, uid, self.group_pigs_id)
|
|
|
|
|
2012-08-23 18:06:48 +00:00
|
|
|
# Create partner Bert Poilu
|
2012-08-23 12:25:37 +00:00
|
|
|
partner_bert_id = self.res_partner.create(cr, uid, {'name': 'Bert Poilu'})
|
|
|
|
|
|
|
|
# Create 'disturbing' values in mail.followers: same res_id, other res_model; same res_model, other res_id
|
|
|
|
group_dummy_id = self.mail_group.create(cr, uid,
|
|
|
|
{'name': 'Dummy group'})
|
|
|
|
self.mail_followers.create(cr, uid,
|
|
|
|
{'res_model': 'mail.thread', 'res_id': self.group_pigs_id, 'partner_id': partner_bert_id})
|
|
|
|
self.mail_followers.create(cr, uid,
|
|
|
|
{'res_model': 'mail.group', 'res_id': group_dummy_id, 'partner_id': partner_bert_id})
|
|
|
|
|
|
|
|
# Pigs just created: should be only Admin as follower
|
|
|
|
follower_ids = [follower.id for follower in group_pigs.message_follower_ids]
|
|
|
|
self.assertTrue(len(follower_ids) == 1,
|
|
|
|
'Newly created group should have only 1 follower')
|
|
|
|
self.assertTrue(user_admin.partner_id.id in follower_ids,
|
|
|
|
'Admin should be the only Pigs group follower')
|
|
|
|
|
|
|
|
# Subscribe Bert through a '4' command
|
|
|
|
group_pigs.write({'message_follower_ids': [(4, partner_bert_id)]})
|
|
|
|
group_pigs.refresh()
|
|
|
|
follower_ids = [follower.id for follower in group_pigs.message_follower_ids]
|
|
|
|
self.assertTrue(len(follower_ids) == 2,
|
|
|
|
'Pigs group should have 2 followers after linking Bert')
|
|
|
|
self.assertTrue(all(id in follower_ids for id in [partner_bert_id, user_admin.partner_id.id]),
|
|
|
|
'Bert and Admin should be the 2 Pigs group followers')
|
|
|
|
|
|
|
|
# Unsubscribe Bert through a '3' command
|
|
|
|
group_pigs.write({'message_follower_ids': [(3, partner_bert_id)]})
|
|
|
|
group_pigs.refresh()
|
|
|
|
follower_ids = [follower.id for follower in group_pigs.message_follower_ids]
|
|
|
|
self.assertTrue(len(follower_ids) == 1,
|
|
|
|
'Pigs group should have 1 follower after unlinking Bert')
|
|
|
|
self.assertTrue(all(id in follower_ids for id in [user_admin.partner_id.id]),
|
|
|
|
'Admin should be the only Pigs group follower')
|
|
|
|
|
|
|
|
# Set followers through a '6' command
|
|
|
|
group_pigs.write({'message_follower_ids': [(6, 0, [partner_bert_id])]})
|
|
|
|
group_pigs.refresh()
|
|
|
|
follower_ids = [follower.id for follower in group_pigs.message_follower_ids]
|
|
|
|
self.assertTrue(len(follower_ids) == 1,
|
|
|
|
'Pigs group should have 1 follower after replacing followers')
|
|
|
|
self.assertTrue(follower_ids == [partner_bert_id],
|
|
|
|
'Bert should be the only Pigs group follower')
|
|
|
|
|
|
|
|
# Add a follower created on the fly through a '0' command
|
|
|
|
group_pigs.write({'message_follower_ids': [(0, 0, {'name': 'Patrick Fiori'})]})
|
|
|
|
partner_patrick_id = self.res_partner.search(cr, uid, [('name', '=', 'Patrick Fiori')])[0]
|
|
|
|
group_pigs.refresh()
|
|
|
|
follower_ids = [follower.id for follower in group_pigs.message_follower_ids]
|
|
|
|
self.assertTrue(len(follower_ids) == 2,
|
|
|
|
'Pigs group should have 2 followers after linking to new partner')
|
|
|
|
self.assertTrue(all(id in follower_ids for id in [partner_bert_id, partner_patrick_id]),
|
|
|
|
'Bert and Patrick should be Pigs group followers')
|
|
|
|
|
|
|
|
# Finally, unlink through a '5' command
|
|
|
|
group_pigs.write({'message_follower_ids': [(5, 0)]})
|
|
|
|
group_pigs.refresh()
|
|
|
|
follower_ids = [follower.id for follower in group_pigs.message_follower_ids]
|
|
|
|
self.assertTrue(len(follower_ids) == 0,
|
|
|
|
'Pigs group should not have followers anymore')
|
|
|
|
|
|
|
|
# Test dummy data has not been altered
|
|
|
|
fol_obj_ids = self.mail_followers.search(cr, uid, [('res_model', '=', 'mail.thread'), ('res_id', '=', self.group_pigs_id)])
|
|
|
|
follower_ids = [follower.partner_id.id for follower in self.mail_followers.browse(cr, uid, fol_obj_ids)]
|
|
|
|
self.assertTrue(len(follower_ids) == 1,
|
|
|
|
'mail.thread dummy data should have 1 follower')
|
|
|
|
self.assertTrue(follower_ids[0] == partner_bert_id,
|
|
|
|
'Bert should be the follower of dummy mail.thread data')
|
|
|
|
fol_obj_ids = self.mail_followers.search(cr, uid, [('res_model', '=', 'mail.group'), ('res_id', '=', group_dummy_id)])
|
|
|
|
follower_ids = [follower.partner_id.id for follower in self.mail_followers.browse(cr, uid, fol_obj_ids)]
|
|
|
|
self.assertTrue(len(follower_ids) == 2,
|
|
|
|
'mail.group dummy data should have 2 followers')
|
|
|
|
self.assertTrue(all(id in follower_ids for id in [partner_bert_id, user_admin.partner_id.id]),
|
|
|
|
'Bert and Admin should be the followers of dummy mail.group data')
|
|
|
|
|
|
|
|
def test_02_message_followers(self):
|
|
|
|
""" Tests designed for the subscriber API. """
|
|
|
|
cr, uid = self.cr, self.uid
|
|
|
|
user_admin = self.res_users.browse(cr, uid, uid)
|
|
|
|
group_pigs = self.mail_group.browse(cr, uid, self.group_pigs_id)
|
|
|
|
|
|
|
|
# Create user Raoul
|
|
|
|
user_raoul_id = self.res_users.create(cr, uid, {'name': 'Raoul Grosbedon', 'login': 'raoul'})
|
|
|
|
user_raoul = self.res_users.browse(cr, uid, user_raoul_id)
|
|
|
|
|
|
|
|
# Subscribe Raoul twice (niak niak) through message_subscribe_users
|
|
|
|
group_pigs.message_subscribe_users([user_raoul_id, user_raoul_id])
|
|
|
|
group_pigs.refresh()
|
|
|
|
follower_ids = [follower.id for follower in group_pigs.message_follower_ids]
|
|
|
|
self.assertTrue(len(follower_ids) == 2,
|
|
|
|
'Pigs group should have 2 followers after having subscribed Raoul. Subscribing twice '\
|
|
|
|
'the same user should create only one follower.')
|
|
|
|
self.assertTrue(all(id in follower_ids for id in [user_raoul.partner_id.id, user_admin.partner_id.id]),
|
|
|
|
'Admin and Raoul should be the 2 Pigs group followers')
|
|
|
|
|
|
|
|
# Unsubscribe Raoul twice through message_unsubscribe_users
|
|
|
|
group_pigs.message_unsubscribe_users([user_raoul_id, user_raoul_id])
|
|
|
|
group_pigs.refresh()
|
|
|
|
follower_ids = [follower.id for follower in group_pigs.message_follower_ids]
|
|
|
|
self.assertTrue(len(follower_ids) == 1,
|
|
|
|
'Pigs group should have 1 follower after unsubscribing Raoul')
|
|
|
|
self.assertTrue(all(id in follower_ids for id in [user_admin.partner_id.id]),
|
|
|
|
'Admin the only Pigs group followers')
|
2012-08-23 18:06:48 +00:00
|
|
|
|
|
|
|
def test_10_mail_composer(self):
|
|
|
|
""" Tests designed for the mail.compose.message wizard. """
|
|
|
|
cr, uid = self.cr, self.uid
|
|
|
|
mail_compose = self.registry('mail.compose.message')
|
|
|
|
user_admin = self.res_users.browse(cr, uid, uid)
|
|
|
|
group_pigs = self.mail_group.browse(cr, uid, self.group_pigs_id)
|
|
|
|
# print 'pigs message_ids: %s' % group_pigs.message_ids
|
|
|
|
# print 'pigs follower_ids: %s' % group_pigs.message_follower_ids
|
|
|
|
|
|
|
|
# Create partner Bert Poilu
|
|
|
|
partner_bert_id = self.res_partner.create(cr, uid, {'name': 'Bert Poilu', 'email': 'bert@poil.poil'})
|
|
|
|
partner_raoul_id = self.res_partner.create(cr, uid, {'name': 'Raoul Grosbedon'})
|
|
|
|
|
|
|
|
# Create a new comment on group_pigs
|
|
|
|
compose_id = mail_compose.create(cr, uid,
|
|
|
|
{'subject': 'Pigs', 'body_text': 'Pigs rules', 'partner_ids': [(4, partner_bert_id), (4, partner_raoul_id)]},
|
|
|
|
{'mail.compose.message': 'comment', 'default_model': 'mail.group', 'default_res_id': self.group_pigs_id})
|
|
|
|
# print 'wizard id: %s' % compose_id
|
|
|
|
compose = mail_compose.browse(cr, uid, compose_id)
|
|
|
|
# print 'wizard partner_ids: %s' % compose.partner_ids
|
|
|
|
self.assertTrue(compose.model == 'mail.group' and compose.res_id == self.group_pigs_id)
|
|
|
|
|
|
|
|
# Send the comment
|
|
|
|
mail_compose.send_mail(cr, uid, [compose_id])
|
|
|
|
|
|
|
|
group_pigs.refresh()
|
|
|
|
# print 'pigs message_ids: %s' % group_pigs.message_ids[0]
|
|
|
|
new_message = group_pigs.message_ids[0]
|
|
|
|
new_partner_ids = [partner.id for partner in new_message.partner_ids]
|
|
|
|
|
|
|
|
# print 'pigs new_message subject: %s' % new_message.subject
|
|
|
|
# print 'pigs new_message body: %s' % new_message.body
|
|
|
|
# print 'pigs new_message partner_ids: %s' % new_message.partner_ids
|
|
|
|
|
|
|
|
notif_ids = self.mail_notification.search(cr, uid, [('message_id', '=', new_message.id)])
|
|
|
|
# print notif_ids
|
|
|
|
|
|
|
|
# Message partners = notified people = writer + partner_ids
|
|
|
|
self.assertTrue(len(new_partner_ids) == 3, 'There should be 3 partners linked to the newly posted comment.')
|
|
|
|
self.assertTrue(len(notif_ids) == 3, 'There should be only 3 entries in mail_notification')
|
|
|
|
self.assertTrue(all(id in [user_admin.partner_id.id, partner_bert_id, partner_raoul_id] for id in new_partner_ids),
|
|
|
|
'Admin, Bert and Roaul should be the 3 partners of the newly created message')
|
|
|
|
|