Skip to content

Commit bcdbbae

Browse files
lade-odootde-banana-odoo
authored andcommitted
[IMP] crm: add tests for MC computation on lead with partner company
Currently using a customer with a company set on a lead without company crashes, as it keep a void company on the lead. This is not compatible with the company set on the partner itself. OPW-2805181 Task-2888330 Part-of: odoo#88108
1 parent c169c9d commit bcdbbae

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

addons/crm/tests/common.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,13 @@ def _activate_multi_company(cls):
281281
'user_id': cls.user_sales_manager.id,
282282
})
283283

284+
cls.partner_c2 = cls.env['res.partner'].create({
285+
'company_id': cls.company_2.id,
286+
'email': '"Partner C2" <[email protected]>',
287+
'name': 'Customer for C2',
288+
'phone': '+32455001122',
289+
})
290+
284291
def _create_leads_batch(self, lead_type='lead', count=10, email_dup_count=0,
285292
partner_count=0, partner_ids=None, user_ids=None,
286293
country_ids=None, probabilities=None, suffix=''):

addons/crm/tests/test_crm_lead_multicompany.py

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
# Part of Odoo. See LICENSE file for full copyright and licensing details.
33

44
from odoo.addons.crm.tests.common import TestCrmCommon
5-
from odoo.addons.mail.tests.common import mail_new_test_user
6-
from odoo.exceptions import AccessError
5+
from odoo.exceptions import AccessError, UserError
76
from odoo.tests import Form, tagged
87
from odoo.tests.common import users
98

@@ -117,6 +116,38 @@ def test_lead_mc_company_computation_env_user_restrict(self):
117116
self.assertEqual(lead.team_id, self.sales_team_1)
118117
self.assertEqual(lead.user_id, self.user_sales_manager_mc)
119118

119+
120+
@users('user_sales_manager_mc')
121+
def test_lead_mc_company_computation_partner_restrict(self):
122+
""" Check company on partner limits the company on lead. As contacts may
123+
be separated by company, lead with a partner should be limited to that
124+
company. """
125+
partner_c2 = self.partner_c2.with_env(self.env)
126+
self.assertEqual(partner_c2.company_id, self.company_2)
127+
lead = self.env['crm.lead'].create({
128+
'partner_id': partner_c2.id,
129+
'name': 'MC Partner, no company lead',
130+
'user_id': False,
131+
'team_id': False,
132+
})
133+
self.assertEqual(lead.company_id, self.company_2)
134+
135+
partner_main = self.env['res.partner'].create({
136+
'company_id': self.company_main.id,
137+
'email': '[email protected]',
138+
'name': 'Customer for Main',
139+
})
140+
lead.write({'partner_id': partner_main})
141+
self.assertEqual(lead.company_id, self.company_main)
142+
143+
# writing current user on lead would imply putting its team and team's company
144+
# on lead (aka self.company_2), and this clashes with company restriction on
145+
# customer
146+
with self.assertRaises(UserError):
147+
lead.write({
148+
'user_id': self.env.user,
149+
})
150+
120151
@users('user_sales_manager_mc')
121152
def test_lead_mc_company_form(self):
122153
""" Test lead company computation using form view """
@@ -188,3 +219,21 @@ def test_lead_mc_company_form_progressives_setup(self):
188219
crm_lead_form.user_id = self.env.user
189220
# self.assertEqual(crm_lead_form.company_id, self.env['res.company']) # FIXME
190221
self.assertEqual(crm_lead_form.company_id, self.company_2)
222+
223+
@users('user_sales_manager_mc')
224+
def test_lead_mc_company_form_w_partner_id(self):
225+
""" Test lead company computation with partner having a company. """
226+
partner_c2 = self.partner_c2.with_env(self.env)
227+
crm_lead_form = Form(self.env['crm.lead'])
228+
crm_lead_form.name = "Test Lead"
229+
230+
crm_lead_form.user_id = self.user_sales_manager_mc
231+
crm_lead_form.partner_id = partner_c2
232+
self.assertEqual(crm_lead_form.company_id, self.company_2, 'Crm: company comes from sales')
233+
self.assertEqual(crm_lead_form.team_id, self.team_company2, 'Crm: team comes from sales')
234+
235+
# reset sales: should not reset company, as partner constrains it
236+
crm_lead_form.team_id = self.env['crm.team']
237+
crm_lead_form.user_id = self.env['res.users']
238+
# ensuring that company_id is not overwritten when the salesperson becomes empty (w\o any team_id)
239+
self.assertEqual(crm_lead_form.company_id, self.company_2, 'Crm: company comes from partner')

0 commit comments

Comments
 (0)