1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Business Applications
5 # Copyright (c) 2012-TODAY OpenERP S.A. <http://openerp.com>
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as
9 # published by the Free Software Foundation, either version 3 of the
10 # License, or (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ##############################################################################
23 from openerp.addons.mail.tests.test_mail_base import TestMailBase
26 class test_message_compose(TestMailBase):
29 super(test_message_compose, self).setUp()
31 # create a 'pigs' and 'bird' groups that will be used through the various tests
32 self.group_bird_id = self.mail_group.create(self.cr, self.uid,
33 {'name': 'Bird', 'description': 'I am angry !'})
35 def test_00_message_compose_wizard(self):
36 """ Tests designed for the mail.compose.message wizard updated by email_template. """
37 cr, uid = self.cr, self.uid
38 mail_compose = self.registry('mail.compose.message')
39 self.res_users.write(cr, uid, [uid], {'signature': 'Admin', 'email': 'a@a.a'})
40 user_admin = self.res_users.browse(cr, uid, uid)
41 p_a_id = user_admin.partner_id.id
42 group_pigs = self.mail_group.browse(cr, uid, self.group_pigs_id)
43 group_bird = self.mail_group.browse(cr, uid, self.group_bird_id)
48 _body_html1 = '<div><p>Fans of Pigs, unite !\n</p><p>Admin</p></div>'
49 _body_html2 = '<div><p>I am angry !\n</p><p>Admin</p></div>'
51 {'name': 'First', 'datas_fname': 'first.txt', 'datas': base64.b64encode('My first attachment')},
52 {'name': 'Second', 'datas_fname': 'second.txt', 'datas': base64.b64encode('My second attachment')}
54 _attachments_test = [('first.txt', 'My first attachment'), ('second.txt', 'My second attachment')]
56 # Create template on mail.group, with attachments
57 group_model_id = self.registry('ir.model').search(cr, uid, [('model', '=', 'mail.group')])[0]
58 email_template = self.registry('email.template')
59 email_template_id = email_template.create(cr, uid, {
60 'model_id': group_model_id,
61 'name': 'Pigs Template',
62 'subject': '${object.name}',
63 'body_html': '${object.description}',
64 'user_signature': True,
65 'attachment_ids': [(0, 0, _attachments[0]), (0, 0, _attachments[1])],
66 'email_to': 'b@b.b c@c.c',
70 # ----------------------------------------
71 # CASE1: comment and save as template
72 # ----------------------------------------
75 compose_id = mail_compose.create(cr, uid,
76 {'subject': 'Forget me subject', 'body': '<p>Dummy body</p>'},
77 {'default_composition_mode': 'comment',
78 'default_model': 'mail.group',
79 'default_res_id': self.group_pigs_id,
80 'active_ids': [self.group_pigs_id, self.group_bird_id]})
81 compose = mail_compose.browse(cr, uid, compose_id)
83 # 2. Save current composition form as a template
84 mail_compose.save_as_template(cr, uid, [compose_id], context={'default_model': 'mail.group'})
85 # Test: email_template subject, body_html, model
86 last_template_id = email_template.search(cr, uid, [('model', '=', 'mail.group'), ('subject', '=', 'Forget me subject')], limit=1)[0]
87 self.assertTrue(last_template_id, 'email_template not found for model mail.group, subject Forget me subject')
88 last_template = email_template.browse(cr, uid, last_template_id)
89 self.assertEqual(last_template.body_html, '<p>Dummy body</p>', 'email_template incorrect body_html')
91 # ----------------------------------------
92 # CASE2: comment with template, save as template
93 # ----------------------------------------
97 'default_composition_mode': 'comment',
98 'default_model': 'mail.group',
99 'default_res_id': self.group_pigs_id,
100 'default_use_template': False,
101 'default_template_id': email_template_id,
102 'active_ids': [self.group_pigs_id, self.group_bird_id]
104 compose_id = mail_compose.create(cr, uid, {'subject': 'Forget me subject', 'body': 'Dummy body'}, context)
105 compose = mail_compose.browse(cr, uid, compose_id, context)
106 onchange_res = compose.onchange_template_id(email_template_id, 'comment', 'mail.group', self.group_pigs_id)['value']
107 onchange_res['partner_ids'] = [(4, partner_id) for partner_id in onchange_res.pop('partner_ids', [])]
108 onchange_res['attachment_ids'] = [(4, attachment_id) for attachment_id in onchange_res.pop('attachment_ids', [])]
109 compose.write(onchange_res)
112 message_pids = [partner.id for partner in compose.partner_ids]
113 partner_ids = self.res_partner.search(cr, uid, [('email', 'in', ['b@b.b', 'c@c.c', 'd@d.d'])])
114 # Test: mail.compose.message: subject, body, partner_ids
115 self.assertEqual(compose.subject, _subject1, 'mail.compose.message subject incorrect')
116 self.assertEqual(compose.body, _body_html1, 'mail.compose.message body incorrect')
117 self.assertEqual(set(message_pids), set(partner_ids), 'mail.compose.message partner_ids incorrect')
118 # Test: mail.compose.message: attachments
119 # Test: mail.message: attachments
120 for attach in compose.attachment_ids:
121 self.assertEqual(attach.res_model, 'mail.group', 'mail.message attachment res_model incorrect')
122 self.assertEqual(attach.res_id, self.group_pigs_id, 'mail.message attachment res_id incorrect')
123 self.assertIn((attach.name, base64.b64decode(attach.datas)), _attachments_test,
124 'mail.message attachment name / data incorrect')
126 # ----------------------------------------
127 # CASE3: mass_mail with template
128 # ----------------------------------------
130 # 1. Mass_mail on pigs and bird, with a default_partner_ids set to check he is correctly added
132 'default_composition_mode': 'mass_mail',
133 'default_model': 'mail.group',
134 'default_res_id': self.group_pigs_id,
135 'default_template_id': email_template_id,
136 'default_partner_ids': [p_a_id],
137 'active_ids': [self.group_pigs_id, self.group_bird_id]
139 compose_id = mail_compose.create(cr, uid, {'subject': 'Forget me subject', 'body': 'Dummy body'}, context)
140 compose = mail_compose.browse(cr, uid, compose_id, context)
141 onchange_res = compose.onchange_template_id(email_template_id, 'mass_mail', 'mail.group', self.group_pigs_id)['value']
142 onchange_res['partner_ids'] = [(4, partner_id) for partner_id in onchange_res.pop('partner_ids', [])]
143 onchange_res['attachment_ids'] = [(4, attachment_id) for attachment_id in onchange_res.pop('attachment_ids', [])]
144 compose.write(onchange_res)
147 message_pids = [partner.id for partner in compose.partner_ids]
148 partner_ids = [p_a_id]
149 self.assertEqual(compose.subject, '${object.name}', 'mail.compose.message subject incorrect')
150 self.assertEqual(compose.body, '<p>${object.description}</p>', 'mail.compose.message body incorrect')
151 self.assertEqual(set(message_pids), set(partner_ids), 'mail.compose.message partner_ids incorrect')
153 # 2. Post the comment, get created message
154 mail_compose.send_mail(cr, uid, [compose_id], {'default_res_id': -1, 'active_ids': [self.group_pigs_id, self.group_bird_id]})
157 message_pigs = group_pigs.message_ids[0]
158 message_bird = group_bird.message_ids[0]
159 # Test: subject, body
160 self.assertEqual(message_pigs.subject, _subject1, 'mail.message subject on Pigs incorrect')
161 self.assertEqual(message_bird.subject, _subject2, 'mail.message subject on Bird incorrect')
162 # self.assertEqual(message_pigs.body, _body_html1, 'mail.message body on Pigs incorrect')
163 # self.assertEqual(message_bird.body, _body_html2, 'mail.message body on Bird incorrect')
164 # Test: partner_ids: p_a_id (default) + 3 newly created partners
165 message_pigs_pids = [partner.id for partner in message_pigs.notified_partner_ids]
166 message_bird_pids = [partner.id for partner in message_bird.notified_partner_ids]
167 partner_ids = self.res_partner.search(cr, uid, [('email', 'in', ['b@b.b', 'c@c.c', 'd@d.d'])])
168 partner_ids.append(p_a_id)
169 self.assertEqual(set(message_pigs_pids), set(partner_ids), 'mail.message on pigs incorrect number of notified_partner_ids')
170 self.assertEqual(set(message_bird_pids), set(partner_ids), 'mail.message on bird notified_partner_ids incorrect')
172 # ----------------------------------------
173 # CASE4: test newly introduced email_recipients field
174 # ----------------------------------------
176 # get already-created partners back
177 p_b_id = self.res_partner.search(cr, uid, [('email', '=', 'b@b.b')])[0]
178 p_c_id = self.res_partner.search(cr, uid, [('email', '=', 'c@c.c')])[0]
179 p_d_id = self.res_partner.search(cr, uid, [('email', '=', 'd@d.d')])[0]
180 # modify template: use email_recipients, use template and email address in email_to to test all features together
181 user_model_id = self.registry('ir.model').search(cr, uid, [('model', '=', 'res.users')])[0]
182 email_template.write(cr, uid, [email_template_id], {
183 'model_id': user_model_id,
184 'body_html': '${object.login}',
185 'email_to': '${object.email} c@c',
186 'email_recipients': '%i,%i' % (p_b_id, p_c_id),
189 # patner by email + partner by id (no double)
190 send_to = [p_a_id, p_b_id, p_c_id, p_d_id]
191 # Generate messsage with default email and partner on template
192 mail_value = mail_compose.generate_email_for_composer(cr, uid, email_template_id, uid)
193 self.assertEqual(set(mail_value['partner_ids']), set(send_to), 'mail.message partner_ids list created by template is incorrect')