Debian Codegraph @

addons/sale_management/tests/test_sale_order_template.py

raw symbols SCIP
1 # Part of Odoo. See LICENSE file for full copyright and licensing details.
2
3 from odoo.exceptions import UserError, ValidationError
4 from odoo.fields import Command
5 from odoo.tests import tagged
6
7 from odoo.addons.sale_management.tests.common import SaleManagementCommon
8
9
10 @tagged('-at_install', 'post_install')
11 class TestSaleOrderTemplate(SaleManagementCommon):
12
13 @classmethod
14 def setUpClass(cls):
15 super().setUpClass()
16
17 cls.branch_company, cls.other_company = cls.env['res.company'].create([
18 {
19 'name': "Branch company",
20 'parent_id': cls.company.id,
21 },
22 {'name': "Other Company"},
23 ])
24 (
25 cls.parent_company_product,
26 cls.branch_company_product,
27 cls.other_company_product,
28 ) = cls.env['product.product'].create([
29 {
30 'name': 'Parent company product',
31 'company_id': cls.company.id,
32 },
33 {
34 'name': 'Branch company product',
35 'company_id': cls.branch_company.id,
36 },
37 {
38 'name': 'Other company product',
39 'company_id': cls.other_company.id,
40 },
41 ])
42
43 def test_no_restricted_product_on_shared_template(self):
44 self.empty_order_template.company_id = False
45
46 with self.assertRaises(UserError):
47 self.empty_order_template.sale_order_template_line_ids = [
48 Command.create({
49 'product_id': self.parent_company_product.id,
50 }),
51 ]
52
53 def test_template_cannot_use_unrelated_company_products(self):
54 # Access to products of other companies
55 with self.assertRaises(UserError):
56 self.empty_order_template.sale_order_template_line_ids = [
57 Command.create({
58 'product_id': self.other_company_product.id,
59 }),
60 ]
61
62 def test_parent_template_cannot_use_branch_company_products(self):
63 with self.assertRaises(UserError):
64 self.empty_order_template.sale_order_template_line_ids = [
65 Command.create({
66 'product_id': self.branch_company_product.id,
67 }),
68 ]
69
70 def test_branch_template_can_use_parent_company_products(self):
71 self.assertFalse(self.product.company_id)
72 self.empty_order_template.company_id = self.branch_company.id
73
74 self.empty_order_template.write({
75 'sale_order_template_line_ids': [
76 Command.create({
77 'product_id': self.branch_company_product.id,
78 }),
79 Command.create({
80 'product_id': self.parent_company_product.id,
81 }),
82 Command.create({ # Shared product
83 'product_id': self.product.id,
84 }),
85 ],
86 })
87
88 def test_company_changes_on_template(self):
89 """Test `_check_company_id` constraint.
90
91 Since most multi-company issues are already catched by the automated `check_company` logic
92 (see other tests), we have to trigger issues the other way (through the template field) to
93 test the constraint.
94 """
95 self.empty_order_template.write({
96 'company_id': self.company.id,
97 'sale_order_template_line_ids': [
98 Command.create({
99 'product_id': self.parent_company_product.id,
100 })
101 ],
102 })
103
104 # Branch company is allowed to use parent company products
105 self.empty_order_template.company_id = self.branch_company.id
106
107 # Cannot share template if contains restricted products
108 with self.assertRaises(ValidationError):
109 self.empty_order_template.company_id = False
110
111 # Template cannot hold products from other companies
112 with self.assertRaises(ValidationError):
113 self.empty_order_template.company_id = self.other_company.id