[IMP] crm: add tests for current MC behavior

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 <tde@odoo.com>
Co-authored-by: Thibault François <tfr@odoo.com>
This commit is contained in:
Thibault François
2021-10-27 06:39:50 +00:00
committed by Thibault Delavallée
co-authored by Thibault François
parent 99b1c30c6a
commit d3f35714f6
4 changed files with 194 additions and 4 deletions
-3
View File
@@ -5,6 +5,3 @@ from . import controllers
from . import models
from . import report
from . import wizard
from odoo import api, SUPERUSER_ID
+3 -1
View File
@@ -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
+54
View File
@@ -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):
@@ -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)