Files
odoo_source/addons/sales_team/tests/test_sales_team.py
T
Thibault Delavallée 6bacc1cf9b [REF] sales_team: improve multi company management
GLOBAL PURPOSE

Ability to have salesmen belonging to several sales team is a core requirement
of CRM. It is therefore moved from website_crm_score to crm along with cleaning
and behavior improvement. Automatic lead assignment is also moved and cleaned.

SPECIFICATIONS: COMPANY-BASED DOMAINS

Improve support of multi company. Sales team can either be linked to a given
company, either be cross companies (company_id = False). Currently it is easy
to have issues while trying to add users from a companyA to a team belonging
to companyB.

When using mono membership mode
  * members are res.users records;
  * a domain is added on users to belong either to the team company, either
    to all available teams if team has no company;

When using multi membership mode
  * members are crm.team.member records;
  * a domain is added on user_id field of membership records so that they
    belong either to the team company, either to all available teams if team
    has no company;

Those domains are implemented using a computed field it is not directly
translatable to a domain. Domain is added directly on field at model level
and include the "avoid shared users" domain leaf.

SPECIFCIATIONS: CHECK_COMPANY

``check_company`` attribute is set on team member allowing to automatically
detect badly configured memberships. Check company is activated on user_id
and team_id fields of crm.team.member model to ensure they match.

LINKS

Task ID-2086889 (main task)
Task ID-2357969 (scoring migration task)
Community PR odoo/odoo#48422
Enterprise PR odoo/enterprise#499
Upgrade PR odoo/upgrade#9966
2021-01-21 11:00:09 +00:00

130 lines
5.4 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import exceptions
from odoo.addons.sales_team.tests.common import TestSalesCommon, TestSalesMC
from odoo.tests.common import users
class TestDefaultTeam(TestSalesCommon):
"""Tests to check if correct default team is found."""
@classmethod
def setUpClass(cls):
"""Set up data for default team tests."""
super(TestDefaultTeam, cls).setUpClass()
cls.company_2 = cls.env['res.company'].create({
'name': 'New Test Company',
'email': 'company.2@test.example.com',
'country_id': cls.env.ref('base.fr'),
})
cls.team_c2 = cls.env['crm.team'].create({
'name': 'C2 Team1',
'sequence': 1,
'company_id': cls.company_2.id,
})
cls.team_sequence = cls.env['crm.team'].create({
'name': 'Team LowSequence',
'sequence': 0,
'company_id': False,
})
cls.team_responsible = cls.env['crm.team'].create({
'name': 'Team 3',
'user_id': cls.user_sales_manager.id,
'sequence': 3,
'company_id': cls.company_main.id
})
def test_default_team_member(self):
with self.with_user('user_sales_leads'):
team = self.env['crm.team']._get_default_team_id()
self.assertEqual(team, self.sales_team_1)
# responsible with lower sequence better than member with higher sequence
self.team_responsible.user_id = self.user_sales_leads.id
with self.with_user('user_sales_leads'):
team = self.env['crm.team']._get_default_team_id()
self.assertEqual(team, self.team_responsible)
def test_default_team_fallback(self):
""" Test fallback: domain, order """
self.sales_team_1.member_ids = [(5,)]
self.sales_team_1.flush()
with self.with_user('user_sales_leads'):
team = self.env['crm.team']._get_default_team_id()
self.assertEqual(team, self.team_sequence)
# next one is team_responsible with sequence = 3 (team_c2 is in another company)
self.team_sequence.active = False
with self.with_user('user_sales_leads'):
team = self.env['crm.team']._get_default_team_id()
self.assertEqual(team, self.team_responsible)
self.user_sales_leads.write({
'company_ids': [(4, self.company_2.id)],
'company_id': self.company_2.id,
})
# multi company: switch company
self.user_sales_leads.write({'company_id': self.company_2.id})
with self.with_user('user_sales_leads'):
team = self.env['crm.team']._get_default_team_id()
self.assertEqual(team, self.team_c2)
class TestMultiCompany(TestSalesMC):
"""Tests to check multi company management with sales team and their
members. """
@users('user_sales_manager')
def test_team_members(self):
""" Test update of team users involving company check """
team_c2 = self.env['crm.team'].browse(self.team_c2.id)
team_c2.write({'name': 'Manager Update'})
self.assertEqual(team_c2.member_ids, self.env['res.users'])
# can add someone from same company
self.env.user.write({'company_id': self.company_2.id})
team_c2.write({'member_ids': [(4, self.env.user.id)]})
self.assertEqual(team_c2.member_ids, self.env.user)
# cannot add someone from another company
with self.assertRaises(exceptions.UserError):
team_c2.write({'member_ids': [(4, self.user_sales_salesman.id)]})
# reset members, change company
team_c2.write({'member_ids': [(5, 0)], 'company_id': self.company_main.id})
self.assertEqual(team_c2.member_ids, self.env['res.users'])
team_c2.write({'member_ids': [(4, self.user_sales_salesman.id)]})
self.assertEqual(team_c2.member_ids, self.user_sales_salesman)
# cannot change company as it breaks memberships mc check
with self.assertRaises(exceptions.UserError):
team_c2.write({'company_id': self.company_2.id})
@users('user_sales_manager')
def test_team_memberships(self):
""" Test update of team member involving company check """
team_c2 = self.env['crm.team'].browse(self.team_c2.id)
team_c2.write({'name': 'Manager Update'})
self.assertEqual(team_c2.member_ids, self.env['res.users'])
# can add someone from same company
self.env.user.write({'company_id': self.company_2.id})
team_c2.write({'crm_team_member_ids': [(0, 0, {'user_id': self.env.user.id})]})
self.assertEqual(team_c2.member_ids, self.env.user)
# cannot add someone from another company
with self.assertRaises(exceptions.UserError):
team_c2.write({'crm_team_member_ids': [(0, 0, {'user_id': self.user_sales_salesman.id})]})
# reset members, change company
team_c2.write({'member_ids': [(5, 0)], 'company_id': self.company_main.id})
self.assertEqual(team_c2.member_ids, self.env['res.users'])
team_c2.write({'crm_team_member_ids': [(0, 0, {'user_id': self.user_sales_salesman.id})]})
self.assertEqual(team_c2.member_ids, self.user_sales_salesman)
# cannot change company as it breaks memberships mc check
with self.assertRaises(exceptions.UserError):
team_c2.write({'company_id': self.company_2.id})