Launchpad automatic translations update.
[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_mockup
23 from openerp.osv.orm import except_orm
24 from openerp.tools.misc import mute_logger
25
26
27 class test_portal(test_mail_mockup.TestMailMockups):
28
29     def setUp(self):
30         super(test_portal, self).setUp()
31         cr, uid = self.cr, self.uid
32         self.ir_model = self.registry('ir.model')
33         self.mail_group = self.registry('mail.group')
34         self.mail_mail = self.registry('mail.mail')
35         self.mail_message = self.registry('mail.message')
36         self.mail_invite = self.registry('mail.wizard.invite')
37         self.res_users = self.registry('res.users')
38         self.res_partner = self.registry('res.partner')
39
40         # create a 'pigs' group that will be used through the various tests
41         self.group_pigs_id = self.mail_group.create(self.cr, self.uid,
42             {'name': 'Pigs', 'description': 'Fans of Pigs, unite !'})
43
44         # Find Portal group
45         group_portal = self.registry('ir.model.data').get_object(cr, uid, 'portal', 'group_portal')
46         self.group_portal_id = group_portal.id
47
48         # Create Chell (portal user)
49         self.user_chell_id = self.res_users.create(cr, uid, {'name': 'Chell Gladys', 'login': 'chell', 'groups_id': [(6, 0, [self.group_portal_id])]})
50         user_chell = self.res_users.browse(cr, uid, self.user_chell_id)
51         self.partner_chell_id = user_chell.partner_id.id
52
53         # Set an email address for the user running the tests, used as Sender for outgoing mails
54         self.res_users.write(cr, uid, uid, {'email': 'test@localhost'})
55
56     @mute_logger('openerp.addons.base.ir.ir_model')
57     def test_00_access_rights(self):
58         """ Test basic mail_message and mail_group access rights for portal users. """
59         cr, uid = self.cr, self.uid
60
61         # Prepare group: Pigs (portal)
62         self.mail_group.message_post(cr, uid, self.group_pigs_id, body='Message')
63         self.mail_group.write(cr, uid, [self.group_pigs_id], {'name': 'Jobs', 'public': 'groups', 'group_public_id': self.group_portal_id})
64
65         # ----------------------------------------
66         # CASE1: Chell will use the Chatter
67         # ----------------------------------------
68
69         # Do: Chell reads Pigs messages, ok because restricted to portal group
70         message_ids = self.mail_group.read(cr, self.user_chell_id, self.group_pigs_id, ['message_ids'])['message_ids']
71         self.mail_message.read(cr, self.user_chell_id, message_ids)
72
73         # Do: Chell posts a message on Pigs, crash because can not write on group or is not in the followers
74         with self.assertRaises(except_orm):
75             self.mail_group.message_post(cr, self.user_chell_id, self.group_pigs_id, body='Message')
76
77         # Do: Chell is added to Pigs followers
78         self.mail_group.message_subscribe(cr, uid, [self.group_pigs_id], [self.partner_chell_id])
79
80         # Test: Chell posts a message on Pigs, ok because in the followers
81         self.mail_group.message_post(cr, self.user_chell_id, self.group_pigs_id, body='Message')
82
83     def test_50_mail_invite(self):
84         cr, uid = self.cr, self.uid
85         user_admin = self.res_users.browse(cr, uid, uid)
86         base_url = self.registry('ir.config_parameter').get_param(cr, uid, 'web.base.url', default='')
87         # 0 - Admin
88         partner_admin_id = user_admin.partner_id.id
89         # 1 - Bert Tartopoils, with email, should receive emails for comments and emails
90         partner_bert_id = self.res_partner.create(cr, uid, {'name': 'Bert Tartopoils', 'email': 'b@b'})
91
92         # ----------------------------------------
93         # CASE: invite Bert to follow Pigs
94         # ----------------------------------------
95
96         # Do: create a mail_wizard_invite, validate it
97         self._init_mock_build_email()
98         context = {'default_res_model': 'mail.group', 'default_res_id': self.group_pigs_id}
99         mail_invite_id = self.mail_invite.create(cr, uid, {'partner_ids': [(4, partner_bert_id)]}, context)
100         self.mail_invite.add_followers(cr, uid, [mail_invite_id])
101
102         # Test: Pigs followers should contain Admin and Bert
103         group_pigs = self.mail_group.browse(cr, uid, self.group_pigs_id)
104         follower_ids = [follower.id for follower in group_pigs.message_follower_ids]
105         self.assertEqual(set(follower_ids), set([partner_admin_id, partner_bert_id]), 'Pigs followers after invite is incorrect')
106
107         # Test: partner must have been prepared for signup
108         partner_bert = self.res_partner.browse(cr, uid, partner_bert_id)
109         self.assertTrue(partner_bert.signup_valid, 'partner has not been prepared for signup')
110         self.assertTrue(base_url in partner_bert.signup_url, 'signup url is incorrect')
111         self.assertTrue(cr.dbname in partner_bert.signup_url, 'signup url is incorrect')
112         self.assertTrue(partner_bert.signup_token in partner_bert.signup_url, 'signup url is incorrect')
113
114         # Test: (pretend to) send email and check subject, body
115         self.assertEqual(len(self._build_email_kwargs_list), 1, 'sent email number incorrect, should be only for Bert')
116         for sent_email in self._build_email_kwargs_list:
117             self.assertEqual(sent_email.get('subject'), 'Invitation to follow Pigs',
118                              'subject of invitation email is incorrect')
119             self.assertTrue('You have been invited to follow Pigs' in sent_email.get('body'),
120                             'body of invitation email is incorrect')
121             self.assertTrue(partner_bert.signup_url in sent_email.get('body'),
122                             'body of invitation email does not contain signup url')