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/>.
#
##############################################################################
2012-08-24 16:11:17 +00:00
import tools
2012-08-14 08:04:21 +00:00
2012-08-31 12:56:05 +00:00
from openerp . tests import common
2012-08-14 08:04:21 +00:00
MAIL_TEMPLATE = """ Return-Path: <whatever-2a840@postmaster.twitter.com>
To : { to }
Received : by mail1 . openerp . com ( Postfix , from userid 10002 )
id 5 DF9ABFB2A ; 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 = 3 D " Content-Type " content = 3 D " text/html; charset=3Dutf-8 " / >
< / head > = 20
< body style = 3 D " 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 ) :
2012-08-30 08:51:16 +00:00
def _mock_smtp_gateway ( self , * args , * * kwargs ) :
return True
def _mock_build_email ( self , * args , * * kwargs ) :
self . _build_email_args = args
self . _build_email_kwargs = kwargs
return self . build_email_real ( * args , * * kwargs )
2012-08-14 08:04:21 +00:00
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
2012-08-24 16:11:17 +00:00
# Install mock SMTP gateway
2012-08-30 08:51:16 +00:00
self . build_email_real = self . registry ( ' ir.mail_server ' ) . build_email
self . registry ( ' ir.mail_server ' ) . build_email = self . _mock_build_email
self . registry ( ' ir.mail_server ' ) . send_email = self . _mock_smtp_gateway
2012-08-24 16:11:17 +00:00
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
2012-08-30 14:35:49 +00:00
self . group_pigs_id = self . mail_group . create ( self . cr , self . uid ,
{ ' name ' : ' Pigs ' , ' description ' : ' Fans of Pigs, unite ! ' } )
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
2012-08-27 16:22:37 +00:00
def test_10_many2many_reference_field ( self ) :
2012-08-23 12:25:37 +00:00
""" 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
2012-08-30 12:21:12 +00:00
follower_ids = set ( [ follower . id for follower in group_pigs . message_follower_ids ] )
self . assertEqual ( follower_ids , set ( [ user_admin . partner_id . id ] ) , ' Admin should be the only Pigs fan ' )
2012-08-23 12:25:37 +00:00
# Subscribe Bert through a '4' command
group_pigs . write ( { ' message_follower_ids ' : [ ( 4 , partner_bert_id ) ] } )
group_pigs . refresh ( )
2012-08-30 12:21:12 +00:00
follower_ids = set ( [ follower . id for follower in group_pigs . message_follower_ids ] )
self . assertEqual ( follower_ids , set ( [ partner_bert_id , user_admin . partner_id . id ] ) , ' Bert and Admin should be the only Pigs fans ' )
2012-08-23 12:25:37 +00:00
# Unsubscribe Bert through a '3' command
group_pigs . write ( { ' message_follower_ids ' : [ ( 3 , partner_bert_id ) ] } )
group_pigs . refresh ( )
2012-08-30 12:21:12 +00:00
follower_ids = set ( [ follower . id for follower in group_pigs . message_follower_ids ] )
self . assertEqual ( follower_ids , set ( [ user_admin . partner_id . id ] ) , ' Admin should be the only Pigs fan ' )
2012-08-23 12:25:37 +00:00
# Set followers through a '6' command
group_pigs . write ( { ' message_follower_ids ' : [ ( 6 , 0 , [ partner_bert_id ] ) ] } )
group_pigs . refresh ( )
2012-08-30 12:21:12 +00:00
follower_ids = set ( [ follower . id for follower in group_pigs . message_follower_ids ] )
self . assertEqual ( follower_ids , set ( [ partner_bert_id ] ) , ' Bert should be the only Pigs fan ' )
2012-08-23 12:25:37 +00:00
# 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 ( )
2012-08-30 12:21:12 +00:00
follower_ids = set ( [ follower . id for follower in group_pigs . message_follower_ids ] )
self . assertEqual ( follower_ids , set ( [ partner_bert_id , partner_patrick_id ] ) , ' Bert and Patrick should be the only Pigs fans ' )
2012-08-23 12:25:37 +00:00
# Finally, unlink through a '5' command
group_pigs . write ( { ' message_follower_ids ' : [ ( 5 , 0 ) ] } )
group_pigs . refresh ( )
2012-08-30 12:21:12 +00:00
follower_ids = set ( [ follower . id for follower in group_pigs . message_follower_ids ] )
self . assertFalse ( follower_ids , ' Pigs group should not have fans anymore ' )
2012-08-23 12:25:37 +00:00
# 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 ) ] )
2012-08-30 12:21:12 +00:00
follower_ids = set ( [ follower . partner_id . id for follower in self . mail_followers . browse ( cr , uid , fol_obj_ids ) ] )
self . assertEqual ( follower_ids , set ( [ partner_bert_id ] ) , ' Bert should be the follower of dummy mail.thread data ' )
2012-08-23 12:25:37 +00:00
fol_obj_ids = self . mail_followers . search ( cr , uid , [ ( ' res_model ' , ' = ' , ' mail.group ' ) , ( ' res_id ' , ' = ' , group_dummy_id ) ] )
2012-08-30 12:21:12 +00:00
follower_ids = set ( [ follower . partner_id . id for follower in self . mail_followers . browse ( cr , uid , fol_obj_ids ) ] )
self . assertEqual ( follower_ids , set ( [ partner_bert_id , user_admin . partner_id . id ] ) , ' Bert and Admin should be the followers of dummy mail.group data ' )
2012-08-23 12:25:37 +00:00
2012-08-27 16:22:37 +00:00
def test_11_message_followers ( self ) :
2012-08-23 12:25:37 +00:00
""" 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 )
2012-08-31 11:27:21 +00:00
# Subscribe Raoul three times (niak niak) through message_subscribe_users
2012-08-23 12:25:37 +00:00
group_pigs . message_subscribe_users ( [ user_raoul_id , user_raoul_id ] )
2012-08-31 11:27:21 +00:00
group_pigs . message_subscribe_users ( [ user_raoul_id ] )
2012-08-23 12:25:37 +00:00
group_pigs . refresh ( )
2012-08-31 11:27:21 +00:00
follower_ids = [ follower . id for follower in group_pigs . message_follower_ids ]
self . assertEqual ( len ( follower_ids ) , 2 , ' There should be 2 Pigs fans ' )
self . assertEqual ( set ( follower_ids ) , set ( [ user_raoul . partner_id . id , user_admin . partner_id . id ] ) , ' Admin and Raoul should be the only 2 Pigs fans ' )
2012-08-23 12:25:37 +00:00
# Unsubscribe Raoul twice through message_unsubscribe_users
group_pigs . message_unsubscribe_users ( [ user_raoul_id , user_raoul_id ] )
group_pigs . refresh ( )
2012-08-31 11:27:21 +00:00
follower_ids = [ follower . id for follower in group_pigs . message_follower_ids ]
self . assertEqual ( follower_ids , [ user_admin . partner_id . id ] , ' Admin must be the only Pigs fan ' )
2012-08-23 18:06:48 +00:00
2012-08-30 08:51:16 +00:00
def test_20_message_post ( self ) :
""" Tests designed for message_post. """
cr , uid = self . cr , self . uid
self . res_users . write ( cr , uid , [ uid ] , { ' signature ' : ' Admin ' , ' email ' : ' a@a ' } )
user_admin = self . res_users . browse ( cr , uid , uid )
group_pigs = self . mail_group . browse ( cr , uid , self . group_pigs_id )
# 0 - Admin
p_a_id = user_admin . partner_id . id
# 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 ' } )
# 2 - Carine Poilvache, with email, should never receive emails
p_c_id = self . res_partner . create ( cr , uid , { ' name ' : ' Carine Poilvache ' , ' email ' : ' c@c ' , ' notification_email_send ' : ' email ' } )
# 3 - Dédé Grosbedon, without email, to test email verification; should receive emails for every message
p_d_id = self . res_partner . create ( cr , uid , { ' name ' : ' Dédé Grosbedon ' , ' notification_email_send ' : ' all ' } )
# Subscribe #1, #2
group_pigs . message_subscribe ( [ p_b_id , p_c_id ] )
# Mail data
_subject = ' Pigs '
_mail_subject = ' %s posted on %s ' % ( user_admin . name , group_pigs . name )
_body1 = ' Pigs rules '
2012-08-30 12:21:12 +00:00
_mail_body1 = ' Pigs rules \n <pre>Admin</pre> \n '
2012-08-30 08:51:16 +00:00
_mail_bodyalt1 = ' Pigs rules \n Admin '
_body2 = ' <html>Pigs rules</html> '
2012-08-30 12:21:12 +00:00
_mail_body2 = ' <html>Pigs rules \n <pre>Admin</pre> \n </html> '
_mail_bodyalt2 = ' Pigs rules \n Admin \n '
2012-08-31 12:56:05 +00:00
_attachments = [ ( ' First ' , ' My first attachment ' ) , ( ' Second ' , ' My second attachment ' ) ]
2012-08-30 08:51:16 +00:00
2012-08-31 12:56:05 +00:00
# CASE1: post comment, body and subject specified
2012-09-04 13:36:48 +00:00
msg_id = self . mail_group . message_post ( cr , uid , self . group_pigs_id , body = _body1 , subject = _subject , type = ' comment ' )
2012-08-30 08:51:16 +00:00
message = self . mail_message . browse ( cr , uid , msg_id )
2012-08-30 12:21:12 +00:00
sent_email = self . _build_email_kwargs
2012-09-05 15:19:50 +00:00
self . assertFalse ( self . mail_mail . search ( cr , uid , [ ( ' mail_message_id ' , ' = ' , msg_id ) ] ) , ' mail.mail notifications should have been auto-deleted! ' )
2012-08-30 08:51:16 +00:00
# Test: mail_message: subject is _subject, body is _body1 (no formatting done)
2012-08-30 12:21:12 +00:00
self . assertEqual ( message . subject , _subject , ' mail.message subject incorrect ' )
self . assertEqual ( message . body , _body1 , ' mail.message body incorrect ' )
2012-08-31 12:56:05 +00:00
# Test: sent_email: email send by server: correct subject, body; body_alternative
2012-08-30 12:21:12 +00:00
self . assertEqual ( sent_email [ ' subject ' ] , _subject , ' sent_email subject incorrect ' )
self . assertEqual ( sent_email [ ' body ' ] , _mail_body1 , ' sent_email body incorrect ' )
self . assertEqual ( sent_email [ ' body_alternative ' ] , _mail_bodyalt1 , ' sent_email body_alternative is incorrect ' )
2012-08-30 08:51:16 +00:00
# Test: mail_message: partner_ids = group followers
2012-08-30 12:21:12 +00:00
message_pids = set ( [ partner . id for partner in message . partner_ids ] )
test_pids = set ( [ p_a_id , p_b_id , p_c_id ] )
self . assertEqual ( test_pids , message_pids , ' mail.message partners incorrect ' )
2012-08-30 08:51:16 +00:00
# Test: notification linked to this message = group followers = partner_ids
notif_ids = self . mail_notification . search ( cr , uid , [ ( ' message_id ' , ' = ' , message . id ) ] )
2012-08-30 12:21:12 +00:00
notif_pids = set ( [ notif . partner_id . id for notif in self . mail_notification . browse ( cr , uid , notif_ids ) ] )
self . assertEqual ( notif_pids , test_pids , ' mail.message notification partners incorrect ' )
# Test: sent_email: email_to should contain b@b, not c@c (pref email), not a@a (writer)
self . assertEqual ( sent_email [ ' email_to ' ] , [ ' b@b ' ] , ' sent_email email_to is incorrect ' )
2012-08-30 08:51:16 +00:00
2012-08-31 12:56:05 +00:00
# CASE2: post an email with attachments, parent_id, partner_ids
# TESTS: automatic subject, signature in body_html, attachments propagation
2012-09-04 13:36:48 +00:00
msg_id2 = self . mail_group . message_post ( cr , uid , self . group_pigs_id , body = _body2 , type = ' email ' ,
2012-08-31 12:56:05 +00:00
partner_ids = [ ( 6 , 0 , [ p_d_id ] ) ] , parent_id = msg_id , attachments = _attachments )
2012-08-30 08:51:16 +00:00
message = self . mail_message . browse ( cr , uid , msg_id2 )
2012-08-30 12:21:12 +00:00
sent_email = self . _build_email_kwargs
2012-09-05 15:19:50 +00:00
self . assertFalse ( self . mail_mail . search ( cr , uid , [ ( ' mail_message_id ' , ' = ' , msg_id2 ) ] ) , ' mail.mail notifications should have been auto-deleted! ' )
2012-08-30 08:51:16 +00:00
2012-08-31 12:56:05 +00:00
# Test: mail_message: subject is False, body is _body2 (no formatting done), parent_id is msg_id
2012-08-30 12:21:12 +00:00
self . assertEqual ( message . subject , False , ' mail.message subject incorrect ' )
self . assertEqual ( message . body , _body2 , ' mail.message body incorrect ' )
self . assertEqual ( message . parent_id . id , msg_id , ' mail.message parent_id incorrect ' )
2012-08-31 12:56:05 +00:00
# Test: sent_email: email send by server: correct subject, body, body_alternative
2012-08-30 12:21:12 +00:00
self . assertEqual ( sent_email [ ' subject ' ] , _mail_subject , ' sent_email subject incorrect ' )
self . assertEqual ( sent_email [ ' body ' ] , _mail_body2 , ' sent_email body incorrect ' )
self . assertEqual ( sent_email [ ' body_alternative ' ] , _mail_bodyalt2 , ' sent_email body_alternative incorrect ' )
2012-08-30 08:51:16 +00:00
# Test: mail_message: partner_ids = group followers
2012-08-30 12:21:12 +00:00
message_pids = set ( [ partner . id for partner in message . partner_ids ] )
test_pids = set ( [ p_a_id , p_b_id , p_c_id , p_d_id ] )
self . assertEqual ( message_pids , test_pids , ' mail.message partners incorrect ' )
2012-08-31 12:56:05 +00:00
# Test: notifications linked to this message = group followers = partner_ids
2012-08-30 08:51:16 +00:00
notif_ids = self . mail_notification . search ( cr , uid , [ ( ' message_id ' , ' = ' , message . id ) ] )
2012-08-30 12:21:12 +00:00
notif_pids = set ( [ notif . partner_id . id for notif in self . mail_notification . browse ( cr , uid , notif_ids ) ] )
self . assertEqual ( notif_pids , test_pids , ' mail.message notification partners incorrect ' )
# Test: sent_email: email_to should contain b@b, c@c, not a@a (writer)
self . assertEqual ( set ( sent_email [ ' email_to ' ] ) , set ( [ ' b@b ' , ' c@c ' ] ) , ' sent_email email_to incorrect ' )
2012-08-31 12:56:05 +00:00
# Test: attachments
for i in range ( len ( message . attachment_ids ) ) :
self . assertEqual ( message . attachment_ids [ i ] . name , _attachments [ i ] [ 0 ] , ' mail.message attachment name incorrect ' )
self . assertEqual ( message . attachment_ids [ i ] . res_model , ' mail.group ' , ' mail.message attachment res_model incorrect ' )
self . assertEqual ( message . attachment_ids [ i ] . res_id , self . group_pigs_id , ' mail.message attachment res_id incorrect ' )
2012-09-05 15:19:50 +00:00
self . assertEqual ( message . attachment_ids [ i ] . datas . decode ( ' base64 ' ) , _attachments [ i ] [ 1 ] , ' mail.message attachment data incorrect ' )
2012-08-31 12:56:05 +00:00
def test_21_message_compose_wizard ( self ) :
2012-08-30 08:51:16 +00:00
""" Tests designed for the mail.compose.message wizard. """
2012-08-23 18:06:48 +00:00
cr , uid = self . cr , self . uid
mail_compose = self . registry ( ' mail.compose.message ' )
2012-08-30 08:51:16 +00:00
self . res_users . write ( cr , uid , [ uid ] , { ' signature ' : ' Admin ' , ' email ' : ' a@a ' } )
2012-08-23 18:06:48 +00:00
user_admin = self . res_users . browse ( cr , uid , uid )
group_pigs = self . mail_group . browse ( cr , uid , self . group_pigs_id )
2012-08-24 16:11:17 +00:00
# Mail data
_subject = ' Pigs '
2012-08-31 17:15:07 +00:00
_subject_reply = ' Re: Pigs '
2012-08-30 08:51:16 +00:00
_mail_subject = ' %s posted on %s ' % ( user_admin . name , group_pigs . name )
2012-08-24 16:11:17 +00:00
_body_text = ' Pigs rules '
2012-08-30 08:51:16 +00:00
_msg_body1 = ' <pre>Pigs rules</pre> '
_body_html = ' <html>Pigs rules</html> '
_msg_body2 = ' <html>Pigs rules</html> '
2012-08-31 12:56:05 +00:00
_attachments = [
2012-09-05 15:19:50 +00:00
{ ' name ' : ' First ' , ' datas ' : ' My first attachment ' . encode ( ' base64 ' ) } ,
{ ' name ' : ' Second ' , ' datas ' : ' My second attachment ' . encode ( ' base64 ' ) }
2012-08-31 12:56:05 +00:00
]
2012-08-24 16:11:17 +00:00
# Create partners
2012-08-30 08:51:16 +00:00
# 0 - Admin
p_a_id = user_admin . partner_id . id
2012-08-24 16:11:17 +00:00
# 1 - Bert Tartopoils, with email, should receive emails for comments and emails
2012-08-30 08:51:16 +00:00
p_b_id = self . res_partner . create ( cr , uid , { ' name ' : ' Bert Tartopoils ' , ' email ' : ' b@b ' } )
# 2 - Carine Poilvache, with email, should never receive emails
p_c_id = self . res_partner . create ( cr , uid , { ' name ' : ' Carine Poilvache ' , ' email ' : ' c@c ' , ' notification_email_send ' : ' email ' } )
# 3 - Dédé Grosbedon, without email, to test email verification; should receive emails for every message
p_d_id = self . res_partner . create ( cr , uid , { ' name ' : ' Dédé Grosbedon ' , ' notification_email_send ' : ' all ' } )
# Subscribe #1
group_pigs . message_subscribe ( [ p_b_id ] )
2012-08-23 18:06:48 +00:00
2012-08-31 12:56:05 +00:00
# CASE1: comment group_pigs with body_text and subject
2012-08-23 18:06:48 +00:00
compose_id = mail_compose . create ( cr , uid ,
2012-08-30 08:51:16 +00:00
{ ' subject ' : _subject , ' body_text ' : _body_text , ' partner_ids ' : [ ( 4 , p_c_id ) , ( 4 , p_d_id ) ] } ,
2012-09-04 09:28:20 +00:00
{ ' default_composition_mode ' : ' comment ' , ' default_model ' : ' mail.group ' , ' default_res_id ' : self . group_pigs_id } )
2012-08-23 18:06:48 +00:00
compose = mail_compose . browse ( cr , uid , compose_id )
2012-08-31 12:56:05 +00:00
# Test: mail.compose.message: model, res_id
self . assertEqual ( compose . model , ' mail.group ' , ' mail.compose.message incorrect model ' )
self . assertEqual ( compose . res_id , self . group_pigs_id , ' mail.compose.message incorrect res_id ' )
2012-08-23 18:06:48 +00:00
2012-08-24 16:11:17 +00:00
# Post the comment, get created message
2012-08-23 18:06:48 +00:00
mail_compose . send_mail ( cr , uid , [ compose_id ] )
group_pigs . refresh ( )
2012-08-31 12:56:05 +00:00
message = group_pigs . message_ids [ 0 ]
2012-08-30 08:51:16 +00:00
# Test: mail.message: subject, body inside pre
2012-08-31 12:56:05 +00:00
self . assertEqual ( message . subject , False , ' mail.message incorrect subject ' )
self . assertEqual ( message . body , _msg_body1 , ' mail.message incorrect body ' )
# Test: mail.message: partner_ids = entries in mail.notification: group_pigs fans (a, b) + mail.compose.message partner_ids (c, d)
msg_pids = [ partner . id for partner in message . partner_ids ]
2012-08-30 08:51:16 +00:00
test_pids = [ p_a_id , p_b_id , p_c_id , p_d_id ]
2012-08-31 12:56:05 +00:00
notif_ids = self . mail_notification . search ( cr , uid , [ ( ' message_id ' , ' = ' , message . id ) ] )
self . assertEqual ( len ( notif_ids ) , 4 , ' mail.message: too much notifications created ' )
self . assertEqual ( set ( msg_pids ) , set ( test_pids ) , ' mail.message partner_ids incorrect ' )
2012-08-24 16:11:17 +00:00
2012-08-31 17:15:07 +00:00
# CASE2: reply to last comment (update its subject) with attachments
message . write ( { ' subject ' : _subject } )
2012-08-24 16:11:17 +00:00
compose_id = mail_compose . create ( cr , uid ,
2012-08-31 12:56:05 +00:00
{ ' attachment_ids ' : [ ( 0 , 0 , _attachments [ 0 ] ) , ( 0 , 0 , _attachments [ 1 ] ) ] } ,
2012-09-04 09:28:20 +00:00
{ ' default_composition_mode ' : ' reply ' , ' default_model ' : ' mail.thread ' , ' default_res_id ' : self . group_pigs_id , ' default_parent_id ' : message . id } )
2012-08-24 16:11:17 +00:00
compose = mail_compose . browse ( cr , uid , compose_id )
2012-08-30 08:51:16 +00:00
2012-08-31 12:56:05 +00:00
# Test: form view methods
2012-08-31 17:15:07 +00:00
# Test: model, res_id, parent_id, content_subtype
2012-08-31 12:56:05 +00:00
self . assertEqual ( compose . model , ' mail.group ' , ' mail.compose.message incorrect model ' )
self . assertEqual ( compose . res_id , self . group_pigs_id , ' mail.compose.message incorrect res_id ' )
self . assertEqual ( compose . parent_id . id , message . id , ' mail.compose.message incorrect parent_id ' )
2012-08-31 17:15:07 +00:00
self . assertEqual ( compose . content_subtype , ' html ' , ' mail.compose.message incorrect content_subtype ' )
2012-08-27 16:22:37 +00:00
2012-08-31 12:56:05 +00:00
# Post the comment, get created message
mail_compose . send_mail ( cr , uid , [ compose_id ] )
group_pigs . refresh ( )
message = group_pigs . message_ids [ 0 ]
2012-08-31 17:15:07 +00:00
# Test: subject as Re:.., body in html
self . assertEqual ( message . subject , _subject_reply , ' mail.message incorrect subject ' )
self . assertIn ( ' Administrator wrote:<blockquote><pre>Pigs rules</pre></blockquote></div> ' , message . body , ' mail.message body is incorrect ' )
2012-08-31 12:56:05 +00:00
# Test: attachments
for i in range ( len ( message . attachment_ids ) ) :
self . assertEqual ( message . attachment_ids [ i ] . name , _attachments [ i ] [ ' name ' ] , ' mail.message attachment name incorrect ' )
self . assertEqual ( message . attachment_ids [ i ] . res_model , ' mail.group ' , ' mail.message attachment res_model incorrect ' )
self . assertEqual ( message . attachment_ids [ i ] . res_id , self . group_pigs_id , ' mail.message attachment res_id incorrect ' )
2012-09-05 15:19:50 +00:00
self . assertEqual ( message . attachment_ids [ i ] . datas . decode ( ' base64 ' ) , _attachments [ i ] [ ' datas ' ] . decode ( ' base64 ' ) , ' mail.message attachment data incorrect ' )
2012-08-31 12:56:05 +00:00
# CASE3 - Create in mass_mail composition mode that should work with or without email_template installed
2012-08-30 14:35:49 +00:00
compose_id = mail_compose . create ( cr , uid ,
{ ' subject ' : _subject , ' body ' : ' $ {object.description} ' } ,
{ ' default_composition_mode ' : ' mass_mail ' , ' default_model ' : ' mail.group ' , ' default_res_id ' : - 1 ,
' active_ids ' : [ self . group_pigs_id ] } )
compose = mail_compose . browse ( cr , uid , compose_id )
# Post the comment, get created message
mail_compose . send_mail ( cr , uid , [ compose_id ] , { ' default_res_id ' : - 1 , ' active_ids ' : [ self . group_pigs_id ] } )
group_pigs . refresh ( )
2012-08-31 17:15:07 +00:00
message = group_pigs . message_ids [ 0 ]
2012-08-30 14:35:49 +00:00
# Test: last message on Pigs = last created message
test_msg = self . mail_message . browse ( cr , uid , self . mail_message . search ( cr , uid , [ ] , limit = 1 ) ) [ 0 ]
2012-08-31 17:15:07 +00:00
self . assertEqual ( message . id , test_msg . id , ' Pigs did not receive its mass mailing message ' )
2012-08-30 14:35:49 +00:00
# Test: mail.message: subject, body
2012-08-31 17:15:07 +00:00
self . assertEqual ( message . subject , _subject , ' mail.message subject is incorrect ' )
self . assertEqual ( message . body , group_pigs . description , ' mail.message body is incorrect ' )
2012-08-30 14:35:49 +00:00
2012-08-27 16:22:37 +00:00
def test_30_message_read ( self ) :
""" Tests designed for message_read. """
# TDE NOTE: this test is not finished, as the message_read method is not fully specified.
# It wil be updated as soon as we have fixed specs !
cr , uid = self . cr , self . uid
group_pigs = self . mail_group . browse ( cr , uid , self . group_pigs_id )
2012-08-28 14:26:34 +00:00
# Test message_read_tree_flatten that flattens a thread according to a given thread_level
import copy
2012-08-31 14:37:07 +00:00
tree = [ { ' id ' : 1 , ' child_ids ' : [
{ ' id ' : 3 , ' child_ids ' : [ ] } ,
{ ' id ' : 4 , ' child_ids ' : [
{ ' id ' : 5 , ' child_ids ' : [ ] } ,
{ ' id ' : 12 , ' child_ids ' : [ ] } ,
] } ,
{ ' id ' : 8 , ' child_ids ' : [
{ ' id ' : 10 , ' child_ids ' : [ ] } ,
] } ,
2012-08-28 14:26:34 +00:00
] } ,
2012-08-31 14:37:07 +00:00
{ ' id ' : 2 , ' child_ids ' : [
{ ' id ' : 7 , ' child_ids ' : [
{ ' id ' : 9 , ' child_ids ' : [ ] } ,
] } ,
2012-08-28 14:26:34 +00:00
] } ,
2012-08-31 14:37:07 +00:00
{ ' id ' : 6 , ' child_ids ' : [
{ ' id ' : 11 , ' child_ids ' : [ ] } ,
2012-08-28 14:26:34 +00:00
] } ,
2012-08-31 14:37:07 +00:00
]
2012-08-28 14:26:34 +00:00
new_tree = self . mail_message . message_read_tree_flatten ( cr , uid , copy . deepcopy ( tree ) , 0 , 0 )
self . assertTrue ( len ( new_tree ) == 12 , ' Flattening wrongly produced ' )
new_tree = self . mail_message . message_read_tree_flatten ( cr , uid , copy . deepcopy ( tree ) , 0 , 1 )
self . assertTrue ( len ( new_tree ) == 3 and len ( new_tree [ 0 ] [ ' child_ids ' ] ) == 6 and len ( new_tree [ 1 ] [ ' child_ids ' ] ) == 2 and len ( new_tree [ 2 ] [ ' child_ids ' ] ) == 1 ,
' Flattening wrongly produced ' )
new_tree = self . mail_message . message_read_tree_flatten ( cr , uid , copy . deepcopy ( tree ) , 0 , 2 )
self . assertTrue ( len ( new_tree ) == 3 and len ( new_tree [ 0 ] [ ' child_ids ' ] ) == 3 and len ( new_tree [ 0 ] [ ' child_ids ' ] [ 1 ] ) == 2 ,
' Flattening wrongly produced ' )
2012-08-27 16:22:37 +00:00
# Add a few messages to pigs group
msgid1 = group_pigs . message_post ( body = ' My Body ' , subject = ' 1 ' , parent_id = False )
msgid2 = group_pigs . message_post ( body = ' My Body ' , subject = ' 1-1 ' , parent_id = msgid1 )
msgid3 = group_pigs . message_post ( body = ' My Body ' , subject = ' 1-2 ' , parent_id = msgid1 )
msgid4 = group_pigs . message_post ( body = ' My Body ' , subject = ' 2 ' , parent_id = False )
msgid5 = group_pigs . message_post ( body = ' My Body ' , subject = ' 1-1-1 ' , parent_id = msgid2 )
2012-08-28 14:26:34 +00:00
msgid6 = group_pigs . message_post ( body = ' My Body ' , subject = ' 2-1 ' , parent_id = msgid4 )
2012-08-27 16:22:37 +00:00
# First try: read flat
2012-08-28 14:26:34 +00:00
first_try_ids = [ msgid6 , msgid5 , msgid4 , msgid3 , msgid2 , msgid1 ]
tree = self . mail_message . message_read ( cr , uid , ids = False , domain = [ ( ' model ' , ' = ' , ' mail.group ' ) , ( ' res_id ' , ' = ' , self . group_pigs_id ) ] , thread_level = 0 )
self . assertTrue ( all ( elem [ ' id ' ] in first_try_ids for elem in tree ) and len ( tree ) == 6 ,
' Incorrect structure and/or number of childs in purely flat message_read ' )
2012-08-27 16:22:37 +00:00
# Second try: read with thread_level 1
2012-08-28 14:26:34 +00:00
tree = self . mail_message . message_read ( cr , uid , ids = False , domain = [ ( ' model ' , ' = ' , ' mail.group ' ) , ( ' res_id ' , ' = ' , self . group_pigs_id ) ] , thread_level = 1 )
self . assertTrue ( len ( tree ) == 2 and len ( tree [ 1 ] [ ' child_ids ' ] ) == 3 , ' Incorrect number of child in message_read ' )
# Third try: read with thread_level 2
tree = self . mail_message . message_read ( cr , uid , ids = False , domain = [ ( ' model ' , ' = ' , ' mail.group ' ) , ( ' res_id ' , ' = ' , self . group_pigs_id ) ] , thread_level = 2 )
self . assertTrue ( len ( tree ) == 2 and len ( tree [ 1 ] [ ' child_ids ' ] ) == 2 and len ( tree [ 1 ] [ ' child_ids ' ] [ 0 ] [ ' child_ids ' ] ) == 1 , ' Incorrect number of child in message_read ' )
2012-08-28 11:31:28 +00:00
def test_40_needaction ( self ) :
""" Tests for mail.message needaction. """
cr , uid = self . cr , self . uid
group_pigs = self . mail_group . browse ( cr , uid , self . group_pigs_id )
user_admin = self . res_users . browse ( cr , uid , uid )
# Demo values: check unread notification = needaction on mail.message
notif_ids = self . mail_notification . search ( cr , uid , [
( ' partner_id ' , ' = ' , user_admin . partner_id . id ) ,
( ' read ' , ' = ' , False )
] )
na_count = self . mail_message . _needaction_count ( cr , uid , domain = [ ] )
2012-09-05 15:19:50 +00:00
self . assertEqual ( len ( notif_ids ) , na_count , ' unread notifications count does not match needaction count ' )
2012-08-28 11:31:28 +00:00
# Post 4 message on group_pigs
2012-08-28 17:39:01 +00:00
for dummy in range ( 4 ) :
group_pigs . message_post ( body = ' My Body ' )
2012-08-28 11:31:28 +00:00
# Check there are 4 new needaction on mail.message
notif_ids = self . mail_notification . search ( cr , uid , [
( ' partner_id ' , ' = ' , user_admin . partner_id . id ) ,
( ' read ' , ' = ' , False )
] )
na_count = self . mail_message . _needaction_count ( cr , uid , domain = [ ] )
2012-09-05 15:19:50 +00:00
self . assertEqual ( len ( notif_ids ) , na_count , ' unread notifications count does not match needaction count ' )
2012-08-28 11:31:28 +00:00
# Check there are 4 needaction on mail.message with particular domain
na_count = self . mail_message . _needaction_count ( cr , uid , domain = [ ( ' model ' , ' = ' , ' mail.group ' ) , ( ' res_id ' , ' = ' , self . group_pigs_id ) ] )
2012-09-05 15:19:50 +00:00
self . assertEqual ( na_count , 4 , ' posted message count does not match needaction count ' )
2012-08-28 17:39:01 +00:00
def test_50_thread_parent_resolution ( self ) :
""" Verify parent/child relationships are correctly established when processing incoming mails """
cr , uid = self . cr , self . uid
group_pigs = self . mail_group . browse ( cr , uid , self . group_pigs_id )
msg1 = group_pigs . message_post ( body = ' My Body ' , subject = ' 1 ' )
msg2 = group_pigs . message_post ( body = ' My Body ' , subject = ' 2 ' )
msg1 , msg2 = self . mail_message . browse ( cr , uid , [ msg1 , msg2 ] )
self . assertTrue ( msg1 . message_id , " New message should have a proper message_id " )
# Reply to msg1, make sure the reply is properly attached using the various reply identification mechanisms
# 1. In-Reply-To header
reply_msg = MAIL_TEMPLATE . format ( to = ' Pretty Pigs <group+pigs@example.com>, other@gmail.com ' , subject = ' Re: 1 ' ,
extra = ' In-Reply-To: %s ' % msg1 . message_id )
self . mail_thread . message_process ( cr , uid , None , reply_msg )
# 2. References header
reply_msg2 = MAIL_TEMPLATE . format ( to = ' Pretty Pigs <group+pigs@example.com>, other@gmail.com ' , subject = ' Re: Re: 1 ' ,
extra = ' References: <2233@a.com> \r \n \t <3edss_dsa@b.com> %s ' % msg1 . message_id )
self . mail_thread . message_process ( cr , uid , None , reply_msg2 )
# 3. Subject contains [<ID>] + model passed to message+process -> only attached to group, not to mail
reply_msg3 = MAIL_TEMPLATE . format ( to = ' Pretty Pigs <group+pigs@example.com>, other@gmail.com ' ,
extra = ' ' , subject = ' Re: [ %s ] 1 ' % self . group_pigs_id )
self . mail_thread . message_process ( cr , uid , ' mail.group ' , reply_msg3 )
group_pigs . refresh ( )
msg1 . refresh ( )
self . assertEqual ( 5 , len ( group_pigs . message_ids ) , ' group should contain 5 messages ' )
self . assertEqual ( 2 , len ( msg1 . child_ids ) , ' msg1 should have 2 children now ' )