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
|
2012-11-14 11:55:04 +00:00
|
|
|
from openerp.addons.mail.tests import test_mail_mockup
|
2012-08-30 15:51:54 +00:00
|
|
|
|
|
|
|
|
2012-11-14 11:55:04 +00:00
|
|
|
class test_message_compose(test_mail_mockup.TestMailMockups):
|
2012-08-30 15:51:54 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
super(test_message_compose, self).setUp()
|
|
|
|
self.mail_group = self.registry('mail.group')
|
|
|
|
self.mail_mail = self.registry('mail.mail')
|
|
|
|
self.mail_message = self.registry('mail.message')
|
|
|
|
self.res_users = self.registry('res.users')
|
|
|
|
self.res_partner = self.registry('res.partner')
|
|
|
|
|
2012-09-05 15:56:26 +00:00
|
|
|
# create a 'pigs' and 'bird' groups that will be used through the various tests
|
2012-08-30 15:51:54 +00:00
|
|
|
self.group_pigs_id = self.mail_group.create(self.cr, self.uid,
|
|
|
|
{'name': 'Pigs', 'description': 'Fans of Pigs, unite !'})
|
2012-09-05 15:56:26 +00:00
|
|
|
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'
|
2012-11-14 13:34:38 +00:00
|
|
|
_body_html1 = 'Fans of Pigs, unite !\n<p>Admin</p>\n'
|
|
|
|
_body_html2 = 'I am angry !\n<p>Admin</p>\n'
|
2012-09-05 15:56:26 +00:00
|
|
|
_attachments = [
|
|
|
|
{'name': 'First', 'datas_fname': 'first.txt', 'datas': base64.b64encode('My first attachment')},
|
|
|
|
{'name': 'Second', 'datas_fname': 'second.txt', 'datas': base64.b64encode('My second attachment')}
|
|
|
|
]
|
|
|
|
_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}',
|
|
|
|
'user_signature': True,
|
2012-09-05 15:56:26 +00:00
|
|
|
'attachment_ids': [(0, 0, _attachments[0]), (0, 0, _attachments[1])],
|
2012-11-13 13:10:44 +00:00
|
|
|
'email_to': 'b@b.b c@c.c',
|
|
|
|
'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,
|
2012-09-05 15:56:26 +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]
|
|
|
|
}
|
|
|
|
compose_id = mail_compose.create(cr, uid, {'subject': 'Forget me subject', 'body': 'Dummy body'}, context)
|
|
|
|
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')
|
2012-09-05 15:56:26 +00:00
|
|
|
self.assertEqual(compose.body, _body_html1, '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')
|
2012-09-05 15:56:26 +00:00
|
|
|
# Test: mail.compose.message: attachments
|
|
|
|
# Test: mail.message: attachments
|
|
|
|
for attach in compose.attachment_ids:
|
|
|
|
self.assertEqual(attach.res_model, 'mail.group', 'mail.message attachment res_model incorrect')
|
|
|
|
self.assertEqual(attach.res_id, self.group_pigs_id, 'mail.message attachment res_id incorrect')
|
|
|
|
self.assertIn((attach.name, base64.b64decode(attach.datas)), _attachments_test,
|
|
|
|
'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',
|
|
|
|
'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]
|
|
|
|
}
|
|
|
|
compose_id = mail_compose.create(cr, uid, {'subject': 'Forget me subject', 'body': 'Dummy body'}, context)
|
|
|
|
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')
|
|
|
|
self.assertEqual(compose.body, '${object.description}', 'mail.compose.message body incorrect')
|
|
|
|
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')
|
2012-09-05 15:56:26 +00:00
|
|
|
self.assertEqual(message_pigs.body, _body_html1, 'mail.message body on Pigs incorrect')
|
|
|
|
self.assertEqual(message_bird.body, _body_html2, 'mail.message body on Bird incorrect')
|
|
|
|
# Test: partner_ids: p_a_id (default) + 3 newly created partners
|
2012-10-25 11:30:48 +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]
|
2012-10-01 21:16:05 +00:00
|
|
|
partner_ids = self.res_partner.search(cr, uid, [('email', 'in', ['b@b.b', 'c@c.c', 'd@d.d'])])
|
2012-10-25 11:30:48 +00:00
|
|
|
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
|
|
|
# ----------------------------------------
|
2012-11-13 13:10:44 +00:00
|
|
|
# CASE4: test newly introduced email_recipients 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]
|
|
|
|
# modify template: use email_recipients, 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}',
|
|
|
|
'email_to': '${object.email} c@c',
|
2012-11-07 11:39:25 +00:00
|
|
|
'email_recipients': '%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')
|