809bc13103427dfb1f461e8afbffcda650b0dc3e
[odoo/odoo.git] / addons / portal / tests / test_portal.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Business Applications
5 #    Copyright (c) 2012-TODAY OpenERP S.A. <http://openerp.com>
6 #
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.
11 #
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.
16 #
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/>.
19 #
20 ##############################################################################
21
22 from openerp.addons.mail.tests import test_mail
23 from openerp.tools import append_content_to_html
24
25
26 class test_portal(test_mail.TestMailMockups):
27
28     def setUp(self):
29         super(test_portal, self).setUp()
30         self.ir_model = self.registry('ir.model')
31         self.mail_group = self.registry('mail.group')
32         self.mail_mail = self.registry('mail.mail')
33         self.res_users = self.registry('res.users')
34         self.res_partner = self.registry('res.partner')
35
36         # create a 'pigs' group that will be used through the various tests
37         self.group_pigs_id = self.mail_group.create(self.cr, self.uid,
38             {'name': 'Pigs', 'description': 'Fans of Pigs, unite !'})
39
40     def test_00_mail_invite(self):
41         cr, uid = self.cr, self.uid
42         user_admin = self.res_users.browse(cr, uid, uid)
43         self.mail_invite = self.registry('mail.wizard.invite')
44         base_url = self.registry('ir.config_parameter').get_param(cr, uid, 'web.base.url', default='')
45         portal_ref = self.registry('ir.model.data').get_object_reference(cr, uid, 'portal', 'portal_group')
46         portal_id = portal_ref and portal_ref[1] or False
47
48         # 0 - Admin
49         p_a_id = user_admin.partner_id.id
50         # 1 - Bert Tartopoils, with email, should receive emails for comments and emails
51         p_b_id = self.res_partner.create(cr, uid, {'name': 'Bert Tartopoils', 'email': 'b@b'})
52
53         # ----------------------------------------
54         # CASE1: generated URL
55         # ----------------------------------------
56
57         url = self.mail_mail._generate_signin_url(cr, uid, p_b_id, portal_id, 1234)
58         self.assertEqual(url,  base_url + '/login?action=signin&partner_id=%s&group=%s&key=%s' % (p_b_id, portal_id, 1234),
59             'generated signin URL incorrect')
60
61         # ----------------------------------------
62         # CASE2: invite Bert
63         # ----------------------------------------
64
65         _sent_email_subject = 'Invitation to follow Pigs'
66         _sent_email_body = append_content_to_html('<div>You have been invited to follow Pigs.</div>', url)
67
68         # Do: create a mail_wizard_invite, validate it
69         self._init_mock_build_email()
70         mail_invite_id = self.mail_invite.create(cr, uid, {'partner_ids': [(4, p_b_id)]}, {'default_res_model': 'mail.group', 'default_res_id': self.group_pigs_id})
71         self.mail_invite.add_followers(cr, uid, [mail_invite_id])
72         group_pigs = self.mail_group.browse(cr, uid, self.group_pigs_id)
73
74         # Test: Pigs followers should contain Admin and Bert
75         follower_ids = [follower.id for follower in group_pigs.message_follower_ids]
76         self.assertEqual(set(follower_ids), set([p_a_id, p_b_id]), 'Pigs followers after invite is incorrect')
77         # Test: sent email subject, body
78         self.assertEqual(len(self._build_email_kwargs_list), 1, 'sent email number incorrect, should be only for Bert')
79         for sent_email in self._build_email_kwargs_list:
80             self.assertEqual(sent_email.get('subject'), _sent_email_subject, 'sent email subject incorrect')
81             self.assertEqual(sent_email.get('body'), _sent_email_body, 'sent email body incorrect')