Files
odoo_source/addons/sales_team/tests/test_sales_team_internals.py
T
Thibault Delavallée 297bee205f [REF] sales_team, crm: support mono/multi team membership mode
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

Purpose of this commit is to allow to use either mono salesteam mode, either
multi salesteams mode. Basic CRM use mono membership: a salesperson belongs
to a single sales team. It is however possible to choose to work using multiple
memberships. This is configured through a configuration parameter available in
CRM configuration settings.

Mono salesteam behavior
  * a given user can be member of a single sales team at a time;
  * when moving an user from team to team, its existing memberships are
    archived to keep history of what has been done within this team;
  * display an alert when creating a new team member that would archive
    existing memberships;
  * displayed members on team form view are directly res.users records to
    avoid displaying unnecessary layer when using a simple CRM;

Multi salesteam behavior
  * a given user can be member of several sales team at a time;
  * unicity contraint (user_id, crm_team_id) is still enforced;
  * displayed members on team form view are crm.team.member records as
    advanced configuration can be done directly at that level;

In all cases when adding a new member check for archived membership before
creating it. If an archived version of the member exists it is unlinked in
order to avoid breaking the unicity constraint.

Archived memberships can be managed in its dedicated menu for advanced sales
team use.

SPECIFICATIONS: SETTINGS VIEW

Move Multi Teams checkbox below Recurring Revenues / Leads checkboxes. On its
right alias configuration should appear if Leads is checked.

PLS take third configuration line. On its right future commits will gradually
add automatic assignment configuration.

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#996
2021-01-21 10:59:24 +00:00

96 lines
3.6 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import exceptions
from odoo.addons.mail.tests.common import mail_new_test_user
from odoo.addons.sales_team.tests.common import TestSalesMC
from odoo.tests.common import users, TransactionCase
from odoo.tools import mute_logger
class TestCornerCases(TransactionCase):
def setUp(self):
super(TestCornerCases, self).setUp()
self.user_sales_leads = mail_new_test_user(
self.env, login='user_sales_leads',
name='Laetitia Sales Leads', email='crm_leads@test.example.com',
company_id=self.env.user.company_id.id,
notification_type='inbox',
groups='sales_team.group_sale_salesman_all_leads,base.group_partner_manager',
)
self.sales_team_1 = self.env['crm.team'].create({
'name': 'Test Sales Team',
'sequence': 5,
'company_id': False,
'user_id': self.env.user.id,
})
def test_unicity(self):
""" Archived memberships should be removed when detecting duplicates.
Creating duplicates should raise unicity constraint.
Note: redoing the data set to avoid clashing with SavepointCase as
we expect a db-level assert """
sales_team_1_m1 = self.env['crm.team.member'].create({
'user_id': self.user_sales_leads.id,
'crm_team_id': self.sales_team_1.id,
})
sales_team_1_m1.write({'active': False})
sales_team_1_m1.flush()
sales_team_1_m2 = self.env['crm.team.member'].create({
'user_id': self.user_sales_leads.id,
'crm_team_id': self.sales_team_1.id,
})
found = self.env['crm.team.member'].search([
('user_id', '=', self.user_sales_leads.id),
('crm_team_id', '=', self.sales_team_1.id),
])
self.assertEqual(found, sales_team_1_m2)
with self.assertRaises(exceptions.UserError), mute_logger('odoo.sql_db'):
self.env['crm.team.member'].create({
'user_id': self.user_sales_leads.id,
'crm_team_id': self.sales_team_1.id,
})
def test_unicity_multicreate(self):
""" Test constraint works with creating duplicates in the same create
method. """
with self.assertRaises(exceptions.UserError), mute_logger('odoo.sql_db'):
self.env['crm.team.member'].create([
{'user_id': self.user_sales_leads.id, 'crm_team_id': self.sales_team_1.id},
{'user_id': self.user_sales_leads.id, 'crm_team_id': self.sales_team_1.id}
])
class TestSecurity(TestSalesMC):
@users('user_sales_leads')
def test_team_access(self):
sales_team = self.sales_team_1.with_user(self.env.user)
sales_team.read(['name'])
for member in sales_team.member_ids:
member.read(['name'])
with self.assertRaises(exceptions.AccessError):
sales_team.write({'name': 'Trolling'})
for membership in sales_team.crm_team_member_ids:
membership.read(['name'])
with self.assertRaises(exceptions.AccessError):
membership.write({'active': False})
with self.assertRaises(exceptions.AccessError):
sales_team.write({'member_ids': [(5, 0)]})
@users('user_sales_leads')
def test_team_multi_company(self):
self.sales_team_1.with_user(self.env.user).read(['name'])
with self.assertRaises(exceptions.AccessError):
self.team_c2.with_user(self.env.user).read(['name'])