From d3f35714f68ccebc10c6dfcab28edf84164b3808 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Fran=C3=A7ois?= Date: Wed, 1 Sep 2021 08:39:15 +0000 Subject: [PATCH] [IMP] crm: add tests for current MC behavior MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Purpose of this commit is to highlight current behavior of multi company in lead. Notably a company is set at creation even when no team or user is set, leading to a lot of issues when dealing with lead merge or convert. Task-2520276 X-original-commit: dc8d82bbe032cef2f732fdfb1ded3068edc37371 Part-of: odoo/odoo#78860 Co-authored-by: Thibault Delavallée Co-authored-by: Thibault François --- addons/crm/__init__.py | 3 - addons/crm/tests/__init__.py | 4 +- addons/crm/tests/common.py | 54 +++++++ .../crm/tests/test_crm_lead_multicompany.py | 137 ++++++++++++++++++ 4 files changed, 194 insertions(+), 4 deletions(-) create mode 100644 addons/crm/tests/test_crm_lead_multicompany.py diff --git a/addons/crm/__init__.py b/addons/crm/__init__.py index 853b0b01f31..4e9b019bd7a 100644 --- a/addons/crm/__init__.py +++ b/addons/crm/__init__.py @@ -5,6 +5,3 @@ from . import controllers from . import models from . import report from . import wizard - -from odoo import api, SUPERUSER_ID - diff --git a/addons/crm/tests/__init__.py b/addons/crm/tests/__init__.py index bd1dddd508b..e5d7f8d6aec 100644 --- a/addons/crm/tests/__init__.py +++ b/addons/crm/tests/__init__.py @@ -1,5 +1,7 @@ # -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. +from . import test_crm_activity from . import test_crm_lead from . import test_crm_lead_assignment from . import test_crm_lead_notification @@ -8,8 +10,8 @@ from . import test_crm_lead_convert_mass from . import test_crm_lead_duplicates from . import test_crm_lead_lost from . import test_crm_lead_merge +from . import test_crm_lead_multicompany from . import test_crm_lead_smart_calendar -from . import test_crm_activity from . import test_crm_ui from . import test_crm_pls from . import test_performances diff --git a/addons/crm/tests/common.py b/addons/crm/tests/common.py index c1a1088d0df..71633308398 100644 --- a/addons/crm/tests/common.py +++ b/addons/crm/tests/common.py @@ -60,6 +60,21 @@ class TestCrmCommon(TestSalesCommon, MailCase): super(TestCrmCommon, cls).setUpClass() cls._init_mail_gateway() + # Salesmen organization + # ------------------------------------------------------------ + # Role: M (team member) R (team manager) + # SALESMAN---------------sales_team_1 + # admin------------------M----------- + # user_sales_manager-----R----------- + # user_sales_leads-------M----------- + # user_sales_salesman----/----------- + + # Sales teams organization + # ------------------------------------------------------------ + # SALESTEAM-----------SEQU-----COMPANY + # sales_team_1--------5--------False + # data----------------9999-----?? + cls.sales_team_1.write({ 'alias_name': 'sales.test', 'use_leads': True, @@ -227,6 +242,45 @@ class TestCrmCommon(TestSalesCommon, MailCase): 'res_id': cls.activity_type_1.id, }) + @classmethod + def _activate_multi_company(cls): + cls.company_2 = cls.env['res.company'].create({ + 'country_id': cls.env.ref('base.au').id, + 'currency_id': cls.env.ref('base.AUD').id, + 'email': 'company.2@test.example.com', + 'name': 'New Test Company', + }) + + cls.user_sales_manager_mc = mail_new_test_user( + cls.env, + company_id=cls.company_2.id, + company_ids=[(4, cls.company_main.id), (4, cls.company_2.id)], + email='user.sales.manager.mc@test.example.com', + login='user_sales_manager_mc', + groups='sales_team.group_sale_manager,base.group_partner_manager', + name='Myrddin Sales Manager', + notification_type='inbox', + ) + cls.team_company2 = cls.env['crm.team'].create({ + 'company_id': cls.company_2.id, + 'name': 'C2 Team', + 'sequence': 10, + 'user_id': False, + }) + cls.team_company2_m1 = cls.env['crm.team.member'].create({ + 'crm_team_id': cls.team_company2.id, + 'user_id': cls.user_sales_manager_mc.id, + 'assignment_max': 30, + 'assignment_domain': False, + }) + + cls.team_company1 = cls.env['crm.team'].create({ + 'company_id': cls.company_main.id, + 'name': 'MainCompany Team', + 'sequence': 50, + 'user_id': cls.user_sales_manager.id, + }) + def _create_leads_batch(self, lead_type='lead', count=10, email_dup_count=0, partner_count=0, partner_ids=None, user_ids=None, country_ids=None, probabilities=None): diff --git a/addons/crm/tests/test_crm_lead_multicompany.py b/addons/crm/tests/test_crm_lead_multicompany.py new file mode 100644 index 00000000000..035df831521 --- /dev/null +++ b/addons/crm/tests/test_crm_lead_multicompany.py @@ -0,0 +1,137 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from odoo.addons.crm.tests.common import TestCrmCommon +from odoo.exceptions import AccessError +from odoo.tests import Form, tagged +from odoo.tests.common import users + + +@tagged('multi_company') +class TestCRMLeadMultiCompany(TestCrmCommon): + + @classmethod + def setUpClass(cls): + super(TestCRMLeadMultiCompany, cls).setUpClass() + cls._activate_multi_company() + + def test_initial_data(self): + """ Ensure global data for those tests to avoid unwanted side effects """ + self.assertFalse(self.sales_team_1.company_id) + self.assertEqual(self.user_sales_manager_mc.company_id, self.company_2) + + @users('user_sales_manager_mc') + def test_lead_mc_company_computation(self): + """ Test lead company computation depending on various parameters. Check + the company is set from the team_id or from the env if there is no team. + No responsible, no team, should not limit company. """ + # Lead with falsy values are kept + lead_no_team = self.env['crm.lead'].create({ + 'name': 'L1', + 'team_id': False, + 'user_id': False, + }) + # self.assertFalse(lead_no_team.company_id) FIXME: should not limit company + self.assertEqual(lead_no_team.company_id, self.user_sales_manager_mc.company_id) + self.assertFalse(lead_no_team.team_id) + self.assertFalse(lead_no_team.user_id) + + # Lead with team with company sets company + lead_team_c2 = self.env['crm.lead'].create({ + 'name': 'L2', + 'team_id': self.team_company2.id, + 'user_id': False, + }) + self.assertEqual(lead_team_c2.company_id, self.company_2) + self.assertFalse(lead_team_c2.user_id) + + # Update team wo company: reset lead company also + lead_team_c2.team_id = self.sales_team_1 + # self.assertFalse(lead_team_c2.company_id) FIXME: currently kept + self.assertEqual(lead_team_c2.company_id, self.company_2) + + # Lead with global team has no company + lead_team_no_company = self.env['crm.lead'].create({ + 'name': 'No company', + 'team_id': self.sales_team_1.id, + 'user_id': False, + }) + # self.assertFalse(lead_no_team.company_id) FIXME: should not limit company + self.assertEqual(lead_no_team.company_id, self.user_sales_manager_mc.company_id) + + # Update team w company updates company + lead_team_no_company.team_id = self.team_company2 + self.assertEqual(lead_team_no_company.company_id, self.company_2) + self.assertEqual(lead_team_no_company.team_id, self.team_company2) + + @users('user_sales_manager_mc') + def test_lead_mc_company_form(self): + """ Test lead company computation using form view """ + crm_lead_form = Form(self.env['crm.lead']) + crm_lead_form.name = "Test Lead" + + # default values: current user, its team and therefore its company + self.assertEqual(crm_lead_form.company_id, self.company_2) + self.assertEqual(crm_lead_form.user_id, self.user_sales_manager_mc) + self.assertEqual(crm_lead_form.team_id, self.team_company2) + + # remove user, team only + crm_lead_form.user_id = self.env['res.users'] + self.assertEqual(crm_lead_form.company_id, self.company_2) + self.assertEqual(crm_lead_form.user_id, self.env['res.users']) + self.assertEqual(crm_lead_form.team_id, self.team_company2) + + # remove team, user only + crm_lead_form.user_id = self.user_sales_manager_mc + crm_lead_form.team_id = self.env['crm.team'] + self.assertEqual(crm_lead_form.company_id, self.company_2) + self.assertEqual(crm_lead_form.user_id, self.user_sales_manager_mc) + self.assertEqual(crm_lead_form.team_id, self.env['crm.team']) + + # remove both + crm_lead_form.user_id = self.env['res.users'] + self.assertEqual(crm_lead_form.company_id, self.company_2) + self.assertEqual(crm_lead_form.user_id, self.env['res.users']) + self.assertEqual(crm_lead_form.team_id, self.env['crm.team']) + + lead = crm_lead_form.save() + + # user_sales_manager cannot read it due to MC rules + with self.assertRaises(AccessError): + lead.with_user(self.user_sales_manager).read(['name']) + + @users('user_sales_manager_mc') + def test_lead_mc_company_form_progressives_setup(self): + """ Specific bug reported at Task-2520276. Flow + 0) The sales team have no company set + 1) Create a lead without a user_id and a team_id + 2) Assign a team to the lead + 3) Assign a user_id + + Goal: if no company is set on the sales team the lead at step 2 should + not have any company_id set. Previous behavior + 1) set the company of the env.user + 2) Keep the company of the lead + 3) set the user company if the current company is not one of the allowed company of the user + + Wanted behavior + 1) leave the company empty + 2) set the company of the team even if it's False (so erase the company if the team has no company set) + 3) set the user company if the current company is not one of the allowed company of the user + """ + lead = self.env['crm.lead'].create({ + 'name': 'Test Progressive Setup', + 'user_id': False, + 'team_id': False, + }) + crm_lead_form = Form(lead) + # self.assertEqual(crm_lead_form.company_id, self.env['res.company']) FIXME + self.assertEqual(crm_lead_form.company_id, self.company_2) + + crm_lead_form.team_id = self.sales_team_1 + # self.assertEqual(crm_lead_form.company_id, self.env['res.company']) # FIXME + self.assertEqual(crm_lead_form.company_id, self.company_2) + + crm_lead_form.user_id = self.env.user + # self.assertEqual(crm_lead_form.company_id, self.env['res.company']) # FIXME + self.assertEqual(crm_lead_form.company_id, self.company_2)