2012-08-30 15:51:54 +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-09-05 15:56:26 +00:00
import base64
2013-08-27 13:30:58 +00:00
from openerp . addons . mail . tests . common import TestMail
2013-12-20 14:46:52 +00:00
from openerp . tools import mute_logger
2012-08-30 15:51:54 +00:00
2013-08-27 13:30:58 +00:00
class test_message_compose ( TestMail ) :
2012-08-30 15:51:54 +00:00
def setUp ( self ) :
super ( test_message_compose , self ) . setUp ( )
2012-09-05 15:56:26 +00:00
# create a 'pigs' and 'bird' groups that will be used through the various tests
self . group_bird_id = self . mail_group . create ( self . cr , self . uid ,
{ ' name ' : ' Bird ' , ' description ' : ' I am angry ! ' } )
2012-08-30 15:51:54 +00:00
def test_00_message_compose_wizard ( self ) :
2012-11-13 13:10:44 +00:00
""" Tests designed for the mail.compose.message wizard updated by email_template. """
2012-08-30 15:51:54 +00:00
cr , uid = self . cr , self . uid
2012-11-13 13:10:44 +00:00
mail_compose = self . registry ( ' mail.compose.message ' )
2012-08-31 17:19:00 +00:00
self . res_users . write ( cr , uid , [ uid ] , { ' signature ' : ' Admin ' , ' email ' : ' a@a.a ' } )
2012-08-30 15:51:54 +00:00
user_admin = self . res_users . browse ( cr , uid , uid )
2012-09-05 15:56:26 +00:00
p_a_id = user_admin . partner_id . id
2012-08-30 15:51:54 +00:00
group_pigs = self . mail_group . browse ( cr , uid , self . group_pigs_id )
2012-09-05 15:56:26 +00:00
group_bird = self . mail_group . browse ( cr , uid , self . group_bird_id )
2012-08-30 15:51:54 +00:00
2012-09-05 15:56:26 +00:00
# Mail data
_subject1 = ' Pigs '
_subject2 = ' Bird '
2013-02-14 11:12:30 +00:00
_body_html1 = ' Fans of Pigs, unite ! '
_body_html2 = ' I am angry ! '
2012-09-05 15:56:26 +00:00
_attachments = [
2013-03-07 11:06:57 +00:00
{ ' name ' : ' First ' , ' datas_fname ' : ' first.txt ' , ' datas ' : base64 . b64encode ( ' My first attachment ' ) , ' res_model ' : ' res.partner ' , ' res_id ' : self . partner_admin_id } ,
{ ' name ' : ' Second ' , ' datas_fname ' : ' second.txt ' , ' datas ' : base64 . b64encode ( ' My second attachment ' ) , ' res_model ' : ' res.partner ' , ' res_id ' : self . partner_admin_id } ,
2012-09-05 15:56:26 +00:00
]
_attachments_test = [ ( ' first.txt ' , ' My first attachment ' ) , ( ' second.txt ' , ' My second attachment ' ) ]
# Create template on mail.group, with attachments
2012-08-31 17:19:00 +00:00
group_model_id = self . registry ( ' ir.model ' ) . search ( cr , uid , [ ( ' model ' , ' = ' , ' mail.group ' ) ] ) [ 0 ]
2012-11-13 13:10:44 +00:00
email_template = self . registry ( ' email.template ' )
email_template_id = email_template . create ( cr , uid , {
' model_id ' : group_model_id ,
' name ' : ' Pigs Template ' ,
' subject ' : ' $ {object.name} ' ,
' body_html ' : ' $ {object.description} ' ,
2014-04-24 08:26:22 +00:00
' user_signature ' : False ,
2012-09-05 15:56:26 +00:00
' attachment_ids ' : [ ( 0 , 0 , _attachments [ 0 ] ) , ( 0 , 0 , _attachments [ 1 ] ) ] ,
2014-01-14 13:46:27 +00:00
' email_to ' : ' b@b.b, c@c.c ' ,
2012-11-13 13:10:44 +00:00
' email_cc ' : ' d@d.d '
} )
2012-08-30 15:51:54 +00:00
2012-09-05 15:56:26 +00:00
# ----------------------------------------
# CASE1: comment and save as template
# ----------------------------------------
2012-08-30 15:51:54 +00:00
2012-09-05 15:56:26 +00:00
# 1. Comment on pigs
2012-11-13 13:10:44 +00:00
compose_id = mail_compose . create ( cr , uid ,
2014-03-17 11:53:15 +00:00
{ ' subject ' : ' Forget me subject ' , ' body ' : ' <p>Dummy body</p> ' } ,
2012-11-13 13:10:44 +00:00
{ ' default_composition_mode ' : ' comment ' ,
' default_model ' : ' mail.group ' ,
2012-09-05 15:56:26 +00:00
' default_res_id ' : self . group_pigs_id ,
' active_ids ' : [ self . group_pigs_id , self . group_bird_id ] } )
2012-11-13 13:10:44 +00:00
compose = mail_compose . browse ( cr , uid , compose_id )
2012-09-05 15:56:26 +00:00
# 2. Save current composition form as a template
2012-11-13 13:10:44 +00:00
mail_compose . save_as_template ( cr , uid , [ compose_id ] , context = { ' default_model ' : ' mail.group ' } )
# Test: email_template subject, body_html, model
last_template_id = email_template . search ( cr , uid , [ ( ' model ' , ' = ' , ' mail.group ' ) , ( ' subject ' , ' = ' , ' Forget me subject ' ) ] , limit = 1 ) [ 0 ]
self . assertTrue ( last_template_id , ' email_template not found for model mail.group, subject Forget me subject ' )
last_template = email_template . browse ( cr , uid , last_template_id )
self . assertEqual ( last_template . body_html , ' <p>Dummy body</p> ' , ' email_template incorrect body_html ' )
2012-09-05 15:56:26 +00:00
# ----------------------------------------
# CASE2: comment with template, save as template
# ----------------------------------------
# 1. Comment on pigs
2012-11-16 17:02:59 +00:00
context = {
' default_composition_mode ' : ' comment ' ,
' default_model ' : ' mail.group ' ,
' default_res_id ' : self . group_pigs_id ,
' default_use_template ' : False ,
' default_template_id ' : email_template_id ,
' active_ids ' : [ self . group_pigs_id , self . group_bird_id ]
}
2014-03-17 11:53:15 +00:00
compose_id = mail_compose . create ( cr , uid , { ' subject ' : ' Forget me subject ' , ' body ' : ' Dummy body ' } , context )
2012-11-16 17:02:59 +00:00
compose = mail_compose . browse ( cr , uid , compose_id , context )
onchange_res = compose . onchange_template_id ( email_template_id , ' comment ' , ' mail.group ' , self . group_pigs_id ) [ ' value ' ]
onchange_res [ ' partner_ids ' ] = [ ( 4 , partner_id ) for partner_id in onchange_res . pop ( ' partner_ids ' , [ ] ) ]
onchange_res [ ' attachment_ids ' ] = [ ( 4 , attachment_id ) for attachment_id in onchange_res . pop ( ' attachment_ids ' , [ ] ) ]
compose . write ( onchange_res )
2012-09-03 18:11:11 +00:00
compose . refresh ( )
2012-11-16 17:02:59 +00:00
2012-09-03 18:11:11 +00:00
message_pids = [ partner . id for partner in compose . partner_ids ]
partner_ids = self . res_partner . search ( cr , uid , [ ( ' email ' , ' in ' , [ ' b@b.b ' , ' c@c.c ' , ' d@d.d ' ] ) ] )
2012-11-15 13:56:34 +00:00
# Test: mail.compose.message: subject, body, partner_ids
2012-09-03 18:11:11 +00:00
self . assertEqual ( compose . subject , _subject1 , ' mail.compose.message subject incorrect ' )
2013-02-14 08:35:16 +00:00
self . assertIn ( _body_html1 , compose . body , ' mail.compose.message body incorrect ' )
2012-09-03 18:11:11 +00:00
self . assertEqual ( set ( message_pids ) , set ( partner_ids ) , ' mail.compose.message partner_ids incorrect ' )
2013-03-07 11:06:57 +00:00
# Test: mail.compose.message: attachments (owner has not been modified)
2012-09-05 15:56:26 +00:00
for attach in compose . attachment_ids :
2013-03-07 11:06:57 +00:00
self . assertEqual ( attach . res_model , ' res.partner ' , ' mail.compose.message attachment res_model through templat was overriden ' )
self . assertEqual ( attach . res_id , self . partner_admin_id , ' mail.compose.message attachment res_id incorrect ' )
2013-03-01 14:48:27 +00:00
self . assertIn ( ( attach . datas_fname , base64 . b64decode ( attach . datas ) ) , _attachments_test ,
' mail.message attachment name / data incorrect ' )
# Test: mail.message: attachments
mail_compose . send_mail ( cr , uid , [ compose_id ] )
group_pigs . refresh ( )
message_pigs = group_pigs . message_ids [ 0 ]
for attach in message_pigs . attachment_ids :
self . assertEqual ( attach . res_model , ' mail.group ' , ' mail.compose.message attachment res_model through templat was overriden ' )
self . assertEqual ( attach . res_id , self . group_pigs_id , ' mail.compose.message attachment res_id incorrect ' )
2013-02-28 14:55:13 +00:00
self . assertIn ( ( attach . datas_fname , base64 . b64decode ( attach . datas ) ) , _attachments_test ,
2012-09-05 15:56:26 +00:00
' mail.message attachment name / data incorrect ' )
# ----------------------------------------
# CASE3: mass_mail with template
# ----------------------------------------
# 1. Mass_mail on pigs and bird, with a default_partner_ids set to check he is correctly added
2012-11-16 17:02:59 +00:00
context = {
' default_composition_mode ' : ' mass_mail ' ,
2013-02-25 16:48:57 +00:00
' default_notify ' : True ,
2012-11-16 17:02:59 +00:00
' default_model ' : ' mail.group ' ,
' default_res_id ' : self . group_pigs_id ,
' default_template_id ' : email_template_id ,
' default_partner_ids ' : [ p_a_id ] ,
' active_ids ' : [ self . group_pigs_id , self . group_bird_id ]
}
2014-03-17 11:53:15 +00:00
compose_id = mail_compose . create ( cr , uid , { ' subject ' : ' Forget me subject ' , ' body ' : ' Dummy body ' } , context )
2012-11-16 17:02:59 +00:00
compose = mail_compose . browse ( cr , uid , compose_id , context )
onchange_res = compose . onchange_template_id ( email_template_id , ' mass_mail ' , ' mail.group ' , self . group_pigs_id ) [ ' value ' ]
onchange_res [ ' partner_ids ' ] = [ ( 4 , partner_id ) for partner_id in onchange_res . pop ( ' partner_ids ' , [ ] ) ]
onchange_res [ ' attachment_ids ' ] = [ ( 4 , attachment_id ) for attachment_id in onchange_res . pop ( ' attachment_ids ' , [ ] ) ]
compose . write ( onchange_res )
2012-08-30 15:51:54 +00:00
compose . refresh ( )
2012-11-16 17:02:59 +00:00
2012-09-05 15:56:26 +00:00
message_pids = [ partner . id for partner in compose . partner_ids ]
partner_ids = [ p_a_id ]
self . assertEqual ( compose . subject , ' $ {object.name} ' , ' mail.compose.message subject incorrect ' )
2014-04-24 08:26:22 +00:00
self . assertEqual ( compose . body , ' <p>$ {object.description} </p> ' , ' mail.compose.message body incorrect ' ) # todo: check signature
2012-09-05 15:56:26 +00:00
self . assertEqual ( set ( message_pids ) , set ( partner_ids ) , ' mail.compose.message partner_ids incorrect ' )
2012-08-30 15:51:54 +00:00
2012-11-16 17:02:59 +00:00
# 2. Post the comment, get created message
2012-11-13 13:10:44 +00:00
mail_compose . send_mail ( cr , uid , [ compose_id ] , { ' default_res_id ' : - 1 , ' active_ids ' : [ self . group_pigs_id , self . group_bird_id ] } )
2012-08-31 17:19:00 +00:00
group_pigs . refresh ( )
group_bird . refresh ( )
message_pigs = group_pigs . message_ids [ 0 ]
message_bird = group_bird . message_ids [ 0 ]
# Test: subject, body
self . assertEqual ( message_pigs . subject , _subject1 , ' mail.message subject on Pigs incorrect ' )
self . assertEqual ( message_bird . subject , _subject2 , ' mail.message subject on Bird incorrect ' )
2013-02-14 08:35:16 +00:00
self . assertIn ( _body_html1 , message_pigs . body , ' mail.message body on Pigs incorrect ' )
self . assertIn ( _body_html2 , message_bird . body , ' mail.message body on Bird incorrect ' )
2012-09-05 15:56:26 +00:00
# Test: partner_ids: p_a_id (default) + 3 newly created partners
2014-03-17 11:53:15 +00:00
# message_pigs_pids = [partner.id for partner in message_pigs.notified_partner_ids]
# message_bird_pids = [partner.id for partner in message_bird.notified_partner_ids]
# partner_ids = self.res_partner.search(cr, uid, [('email', 'in', ['b@b.b', 'c@c.c', 'd@d.d'])])
# partner_ids.append(p_a_id)
# self.assertEqual(set(message_pigs_pids), set(partner_ids), 'mail.message on pigs incorrect number of notified_partner_ids')
# self.assertEqual(set(message_bird_pids), set(partner_ids), 'mail.message on bird notified_partner_ids incorrect')
2012-11-07 10:27:45 +00:00
2012-11-07 11:39:25 +00:00
# ----------------------------------------
2013-02-25 10:19:18 +00:00
# CASE4: test newly introduced partner_to field
2012-11-07 11:39:25 +00:00
# ----------------------------------------
2012-11-07 10:27:45 +00:00
2012-11-13 13:10:44 +00:00
# get already-created partners back
p_b_id = self . res_partner . search ( cr , uid , [ ( ' email ' , ' = ' , ' b@b.b ' ) ] ) [ 0 ]
p_c_id = self . res_partner . search ( cr , uid , [ ( ' email ' , ' = ' , ' c@c.c ' ) ] ) [ 0 ]
p_d_id = self . res_partner . search ( cr , uid , [ ( ' email ' , ' = ' , ' d@d.d ' ) ] ) [ 0 ]
2013-02-25 10:19:18 +00:00
# modify template: use partner_to, use template and email address in email_to to test all features together
2012-11-07 10:27:45 +00:00
user_model_id = self . registry ( ' ir.model ' ) . search ( cr , uid , [ ( ' model ' , ' = ' , ' res.users ' ) ] ) [ 0 ]
2012-11-13 13:10:44 +00:00
email_template . write ( cr , uid , [ email_template_id ] , {
2012-11-07 10:27:45 +00:00
' model_id ' : user_model_id ,
' body_html ' : ' $ {object.login} ' ,
2014-01-14 13:46:27 +00:00
' email_to ' : ' $ {object.email} , c@c ' ,
2013-02-25 10:19:18 +00:00
' partner_to ' : ' %i , %i ' % ( p_b_id , p_c_id ) ,
2012-11-13 13:10:44 +00:00
' email_cc ' : ' d@d ' ,
2012-11-07 10:27:45 +00:00
} )
# patner by email + partner by id (no double)
2012-11-13 13:10:44 +00:00
send_to = [ p_a_id , p_b_id , p_c_id , p_d_id ]
2012-11-07 10:27:45 +00:00
# Generate messsage with default email and partner on template
2012-11-13 13:10:44 +00:00
mail_value = mail_compose . generate_email_for_composer ( cr , uid , email_template_id , uid )
self . assertEqual ( set ( mail_value [ ' partner_ids ' ] ) , set ( send_to ) , ' mail.message partner_ids list created by template is incorrect ' )
2013-12-20 14:46:52 +00:00
@mute_logger ( ' openerp.osv.orm ' , ' openerp.osv.orm ' )
def test_10_email_templating ( self ) :
""" Tests designed for the mail.compose.message wizard updated by email_template. """
cr , uid , context = self . cr , self . uid , { }
# create the email.template on mail.group model
group_model_id = self . registry ( ' ir.model ' ) . search ( cr , uid , [ ( ' model ' , ' = ' , ' mail.group ' ) ] ) [ 0 ]
email_template = self . registry ( ' email.template ' )
email_template_id = email_template . create ( cr , uid , {
' model_id ' : group_model_id ,
' name ' : ' Pigs Template ' ,
' email_from ' : ' Raoul Grosbedon <raoul@example.com> ' ,
' subject ' : ' $ {object.name} ' ,
' body_html ' : ' $ {object.description} ' ,
' user_signature ' : True ,
2014-01-14 18:06:52 +00:00
' email_to ' : ' b@b.b, c@c.c ' ,
2013-12-20 14:46:52 +00:00
' email_cc ' : ' d@d.d ' ,
2013-12-24 11:24:07 +00:00
' partner_to ' : ' $ {user.partner_id.id} , %s , %s ,-1 ' % ( self . user_raoul . partner_id . id , self . user_bert . partner_id . id )
2013-12-20 14:46:52 +00:00
} )
# not force send: email_recipients is not taken into account
msg_id = email_template . send_mail ( cr , uid , email_template_id , self . group_pigs_id , context = context )
mail = self . mail_mail . browse ( cr , uid , msg_id , context = context )
self . assertEqual ( mail . subject , ' Pigs ' , ' email_template: send_mail: wrong subject ' )
2014-01-14 18:06:52 +00:00
self . assertEqual ( mail . email_to , ' b@b.b, c@c.c ' , ' email_template: send_mail: wrong email_to ' )
2013-12-20 14:46:52 +00:00
self . assertEqual ( mail . email_cc , ' d@d.d ' , ' email_template: send_mail: wrong email_cc ' )
2013-12-24 11:24:07 +00:00
self . assertEqual (
set ( [ partner . id for partner in mail . recipient_ids ] ) ,
set ( ( self . partner_admin_id , self . user_raoul . partner_id . id , self . user_bert . partner_id . id ) ) ,
' email_template: send_mail: wrong management of partner_to ' )
2013-12-20 14:46:52 +00:00
# force send: take email_recipients into account
email_template . send_mail ( cr , uid , email_template_id , self . group_pigs_id , force_send = True , context = context )
sent_emails = self . _build_email_kwargs_list
2013-12-24 11:24:07 +00:00
email_to_lst = [
2014-03-25 10:39:55 +00:00
[ ' b@b.b ' , ' c@c.c ' ] , [ ' Administrator <admin@yourcompany.example.com> ' ] ,
[ ' Raoul Grosbedon <raoul@raoul.fr> ' ] , [ ' Bert Tartignole <bert@bert.fr> ' ] ]
2013-12-24 11:24:07 +00:00
self . assertEqual ( len ( sent_emails ) , 4 , ' email_template: send_mail: 3 valid email recipients + email_to -> should send 4 emails ' )
2013-12-20 14:46:52 +00:00
for email in sent_emails :
2013-12-24 11:24:07 +00:00
self . assertIn ( email [ ' email_to ' ] , email_to_lst , ' email_template: send_mail: wrong email_recipients ' )