File: test_multicompany.py

package info (click to toggle)
odoo 18.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 878,716 kB
  • sloc: javascript: 927,937; python: 685,670; xml: 388,524; sh: 1,033; sql: 415; makefile: 26
file content (491 lines) | stat: -rw-r--r-- 26,429 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from contextlib import contextmanager
from lxml import etree

from odoo.tests import Form, TransactionCase
from odoo.exceptions import AccessError, UserError

class TestMultiCompanyCommon(TransactionCase):

    @classmethod
    def setUpMultiCompany(cls):

        # create companies
        cls.company_a = cls.env['res.company'].create({
            'name': 'Company A'
        })
        cls.company_b = cls.env['res.company'].create({
            'name': 'Company B'
        })

        # shared customers
        cls.partner_1 = cls.env['res.partner'].create({
            'name': 'Valid Lelitre',
            'email': 'valid.lelitre@agrolait.com',
            'company_id': False,
        })
        cls.partner_2 = cls.env['res.partner'].create({
            'name': 'Valid Poilvache',
            'email': 'valid.other@gmail.com',
            'company_id': False,
        })

        # users to use through the various tests
        user_group_employee = cls.env.ref('base.group_user')
        Users = cls.env['res.users'].with_context({'no_reset_password': True})

        cls.user_employee_company_a = Users.create({
            'name': 'Employee Company A',
            'login': 'employee-a',
            'email': 'employee@companya.com',
            'company_id': cls.company_a.id,
            'company_ids': [(6, 0, [cls.company_a.id])],
            'groups_id': [(6, 0, [user_group_employee.id])]
        })
        cls.user_manager_company_a = Users.create({
            'name': 'Manager Company A',
            'login': 'manager-a',
            'email': 'manager@companya.com',
            'company_id': cls.company_a.id,
            'company_ids': [(6, 0, [cls.company_a.id])],
            'groups_id': [(6, 0, [user_group_employee.id])]
        })
        cls.user_employee_company_b = Users.create({
            'name': 'Employee Company B',
            'login': 'employee-b',
            'email': 'employee@companyb.com',
            'company_id': cls.company_b.id,
            'company_ids': [(6, 0, [cls.company_b.id])],
            'groups_id': [(6, 0, [user_group_employee.id])]
        })
        cls.user_manager_company_b = Users.create({
            'name': 'Manager Company B',
            'login': 'manager-b',
            'email': 'manager@companyb.com',
            'company_id': cls.company_b.id,
            'company_ids': [(6, 0, [cls.company_b.id])],
            'groups_id': [(6, 0, [user_group_employee.id])]
        })

    @contextmanager
    def sudo(self, login):
        old_uid = self.uid
        try:
            user = self.env['res.users'].sudo().search([('login', '=', login)])
            # switch user
            self.uid = user.id
            self.env = self.env(user=self.uid)
            yield
        finally:
            # back
            self.uid = old_uid
            self.env = self.env(user=self.uid)

    @contextmanager
    def allow_companies(self, company_ids):
        """ The current user will be allowed in each given companies (like he can sees all of them in the company switcher and they are all checked) """
        old_allow_company_ids = self.env.user.company_ids.ids
        current_user = self.env.user
        try:
            current_user.write({'company_ids': company_ids})
            context = dict(self.env.context, allowed_company_ids=company_ids)
            self.env = self.env(user=current_user, context=context)
            yield
        finally:
            # back
            current_user.write({'company_ids': old_allow_company_ids})
            context = dict(self.env.context, allowed_company_ids=old_allow_company_ids)
            self.env = self.env(user=current_user, context=context)

    @contextmanager
    def switch_company(self, company):
        """ Change the company in which the current user is logged """
        old_companies = self.env.context.get('allowed_company_ids', [])
        try:
            # switch company in context
            new_companies = list(old_companies)
            if company.id not in new_companies:
                new_companies = [company.id] + new_companies
            else:
                new_companies.insert(0, new_companies.pop(new_companies.index(company.id)))
            context = dict(self.env.context, allowed_company_ids=new_companies)
            self.env = self.env(context=context)
            yield
        finally:
            # back
            context = dict(self.env.context, allowed_company_ids=old_companies)
            self.env = self.env(context=context)

class TestMultiCompanyProject(TestMultiCompanyCommon):

    @classmethod
    def setUpClass(cls):
        super(TestMultiCompanyProject, cls).setUpClass()

        cls.setUpMultiCompany()

        user_group_project_user = cls.env.ref('project.group_project_user')
        user_group_project_manager = cls.env.ref('project.group_project_manager')

        # setup users
        cls.user_employee_company_a.write({
            'groups_id': [(4, user_group_project_user.id)]
        })
        cls.user_manager_company_a.write({
            'groups_id': [(4, user_group_project_manager.id)]
        })
        cls.user_employee_company_b.write({
            'groups_id': [(4, user_group_project_user.id)]
        })
        cls.user_manager_company_b.write({
            'groups_id': [(4, user_group_project_manager.id)]
        })

        # create project in both companies
        cls.Project = cls.env['project.project'].with_context({'mail_create_nolog': True, 'tracking_disable': True})
        cls.project_company_a = cls.Project.create({
            'name': 'Project Company A',
            'alias_name': 'project+companya',
            'partner_id': cls.partner_1.id,
            'company_id': cls.company_a.id,
            'type_ids': [
                (0, 0, {
                    'name': 'New',
                    'sequence': 1,
                }),
                (0, 0, {
                    'name': 'Won',
                    'sequence': 10,
                })
            ]
        })
        cls.project_company_b = cls.Project.create({
            'name': 'Project Company B',
            'alias_name': 'project+companyb',
            'partner_id': cls.partner_1.id,
            'company_id': cls.company_b.id,
            'type_ids': [
                (0, 0, {
                    'name': 'New',
                    'sequence': 1,
                }),
                (0, 0, {
                    'name': 'Won',
                    'sequence': 10,
                })
            ]
        })
        # already-existing tasks in company A and B
        Task = cls.env['project.task'].with_context({'mail_create_nolog': True, 'tracking_disable': True})
        cls.task_1 = Task.create({
            'name': 'Task 1 in Project A',
            'user_ids': cls.user_employee_company_a,
            'project_id': cls.project_company_a.id
        })
        cls.task_2 = Task.create({
            'name': 'Task 2 in Project B',
            'user_ids': cls.user_employee_company_b,
            'project_id': cls.project_company_b.id
        })

    def test_create_project(self):
        """ Check project creation in multiple companies """
        with self.sudo('manager-a'):
            project = self.env['project.project'].with_context({'tracking_disable': True}).create({
                'name': 'Project Company A',
                'partner_id': self.partner_1.id,
            })
            self.assertFalse(project.company_id, "A newly created project should have a company set to False by default")

            with self.switch_company(self.company_b):
                with self.assertRaises(AccessError, msg="Manager can not create project in a company in which he is not allowed"):
                    project = self.env['project.project'].with_context({'tracking_disable': True}).create({
                        'name': 'Project Company B',
                        'partner_id': self.partner_1.id,
                        'company_id': self.company_b.id
                    })

                # when allowed in other company, can create a project in another company (different from the one in which you are logged)
                with self.allow_companies([self.company_a.id, self.company_b.id]):
                    project = self.env['project.project'].with_context({'tracking_disable': True}).create({
                        'name': 'Project Company B',
                        'partner_id': self.partner_1.id,
                        'company_id': self.company_b.id
                    })

    def test_generate_analytic_account(self):
        """ Check the analytic account generation, company propagation """
        with self.sudo('manager-b'):
            with self.allow_companies([self.company_a.id, self.company_b.id]):
                self.project_company_a._create_analytic_account()

                self.assertEqual(self.project_company_a.company_id, self.project_company_a.account_id.company_id, "The analytic account created from a project should be in the same company.")

        project_no_company = self.Project.create({'name': 'Project no company'})
        #ensures that all the existing plan have a company_id
        project_no_company._create_analytic_account()
        self.assertFalse(project_no_company.account_id.company_id, "The analytic account created from a project without company_id should have its company_id field set to False.")

        project_no_company_2 = self.Project.create({'name': 'Project no company 2'})
        project_no_company_2._create_analytic_account()
        self.assertNotEqual(project_no_company_2.account_id, project_no_company.account_id, "The analytic account created should be different from the account created for the 1st project.")
        self.assertEqual(project_no_company_2.account_id.plan_id, project_no_company.account_id.plan_id, "No new analytic should have been created.")

    def test_analytic_account_company_consistency(self):
        """
            This test ensures that the following invariant is kept:
            If the company of an analytic account is set, all of its project must have the same company.
            If the company of an analytic account is not set, its project can either have a company set, or none.
        """
        project_no_company = self.Project.create({'name': 'Project no company'})
        project_no_company._create_analytic_account()
        account_no_company = project_no_company.account_id
        self.project_company_a._create_analytic_account()
        account_a = self.project_company_a.account_id

        # Set the account of the project to a new account without company_id
        self.project_company_a.account_id = account_no_company
        self.assertEqual(self.project_company_a.account_id, account_no_company, "The new account should be set on the project.")
        self.assertFalse(account_no_company.company_id, "The company of the account should not have been updated.")
        self.project_company_a.account_id = account_a

        # Set the account of the project to a new account with a company_id
        project_no_company.account_id = account_a
        self.assertEqual(project_no_company.company_id, self.company_a, "The company of the project should have been updated to the company of its new account.")
        self.assertEqual(project_no_company.account_id, account_a, "The account of the project should have been updated.")
        project_no_company.account_id = account_no_company
        project_no_company.company_id = False

        # Neither the project nor its account have a company_id
        # set the company of the project
        project_no_company.company_id = self.company_a
        self.assertEqual(project_no_company.company_id, self.company_a, "The company of the project should have been updated.")
        self.assertFalse(account_no_company.company_id, "The company of the account should not have been updated for the company of the project was False before its update.")
        project_no_company.company_id = False
        # set the company of the account
        account_no_company.company_id = self.company_a
        self.assertEqual(project_no_company.company_id, self.company_a, "The company of the project should have been updated to the company of its new account.")
        self.assertEqual(account_no_company.company_id, self.company_a, "The company of the account should have been updated.")

        # The project and its account have the same company (company A)
        # set the company of the project to False
        self.project_company_a.company_id = False
        self.assertFalse(self.project_company_a.company_id, "The company of the project should have been updated.")
        self.assertFalse(account_a.company_id, "The company of the account should be set to False, as it only has one project linked to it and the company of the project was set from company A to False")
        account_a.company_id = self.company_a
        # set the company of the project to company B
        self.project_company_a.company_id = self.company_b
        self.assertEqual(self.project_company_a.company_id, self.company_b, "The company of the project should have been updated.")
        self.assertEqual(account_a.company_id, self.company_b, "The company of the account should have been updated, for its company was the same as the one of its project and the company of the project was set before the update.")
        # set the company of the account to company A
        account_a.company_id = self.company_a
        self.assertEqual(self.project_company_a.company_id, self.company_a, "The company of the project should have been updated to the company of its account.")
        self.assertEqual(account_a.company_id, self.company_a, "The company of the account should have been updated.")
        # set the company of the account to False
        account_a.company_id = False
        self.assertEqual(self.project_company_a.company_id, self.company_a, "The company of the project should not have been updated for the company of its account has been set to False.")
        self.assertFalse(account_a.company_id, "The company of the account should have been updated.")

        # The project has a company_id set, but not its account
        # set the company of the account to company B (!= project.company_id)
        account_a.company_id = self.company_b
        self.assertEqual(self.project_company_a.company_id, self.company_b, "The company of the project should have been updated to the company of its account even if the new company set on the account is a different one than the one the project.")
        self.assertEqual(account_a.company_id, self.company_b, "The company of the account should have been updated.")
        account_a.company_id = False
        self.project_company_a.company_id = self.company_a
        # set the company of the account to company A (== project.company_id)
        account_a.company_id = self.company_a
        self.assertEqual(self.project_company_a.company_id, self.company_a, "The company of the project should have been updated to the company of its account.")
        self.assertEqual(account_a.company_id, self.company_a, "The company of the account should have been updated.")
        account_a.company_id = False
        # set the company of the project to company B
        self.project_company_a.company_id = self.company_b
        self.assertEqual(self.project_company_a.company_id, self.company_b, "The company of the project should have been updated.")
        self.assertFalse(account_a.company_id, "The company of the account should not have been updated for it is was set to False.")
        # set the company of the project to False
        self.project_company_a.company_id = False
        self.assertFalse(self.project_company_a.company_id, "The company of the project should have been updated.")
        self.assertFalse(account_a.company_id, "The company of the account should not have been updated for it was set to False.")

        # creates an AAL for the account_a
        account_a.company_id = self.company_b
        aal = self.env['account.analytic.line'].create({
            'name': 'other revenues line',
            'account_id': account_a.id,
            'company_id': self.company_b.id,
            'amount': 100,
        })
        with self.assertRaises(UserError):
            self.project_company_a.company_id = self.company_a
        self.assertEqual(self.project_company_a.company_id, self.company_b, "The account of the project contains AAL, its company can not be updated.")
        aal.unlink()

        project_no_company.account_id = account_a
        self.assertEqual(project_no_company.company_id, account_a.company_id)
        with self.assertRaises(UserError):
            self.project_company_a.company_id = self.company_a
        self.assertEqual(self.project_company_a.company_id, self.company_b, "The account of the project is linked to more than one project, its company can not be updated.")

    def test_create_task(self):
        with self.sudo('employee-a'):
            # create task, set project; the onchange will set the correct company
            with Form(self.env['project.task'].with_context({'tracking_disable': True})) as task_form:
                task_form.name = 'Test Task in company A'
                task_form.project_id = self.project_company_a
            task = task_form.save()

            self.assertEqual(task.company_id, self.project_company_a.company_id, "The company of the task should be the one from its project.")

    def test_update_company_id(self):
        """ this test ensures that:
        - All the tasks of a project with a company set have the same company as their project. Updating the task set the task to a private state.
        Updating the company the project update all the tasks even if the company of the project is set to False.
        - The tasks of a project without company can have any company set. Updating a task does not update the company of its project. Updating the project update
        all the tasks even if these tasks had a company set.
        """
        project = self.Project.create({'name': 'Project'})
        task = self.env['project.task'].create({
            'name': 'task no company',
            'project_id': project.id,
        })
        self.assertFalse(task.company_id, "Creating a task in a project without company set its company_id to False.")

        with self.debug_mode():
            task_form = Form(task)
            task_form.company_id = self.company_a
            task = task_form.save()
        self.assertFalse(project.company_id, "Setting a new company on a task should not update the company of its project.")
        self.assertEqual(task.company_id, self.company_a, "The company of the task should have been updated.")

        project.company_id = self.company_b
        self.assertEqual(project.company_id, self.company_b, "The company of the project should have been updated.")
        self.assertEqual(task.company_id, self.company_b, "The company of the task should have been updated.")


        with self.debug_mode():
            task_form = Form(task)
            task_form.company_id = self.company_a
            task = task_form.save()
        self.assertEqual(task.company_id, self.company_a, "The company of the task should have been updated.")
        self.assertFalse(task.project_id, "The task should now be a private task.")
        self.assertEqual(project.company_id, self.company_b, "the company of the project should not have been updated.")

        task_1, task_2, task_3 = self.env['project.task'].create([{
            'name': 'task 1',
            'project_id': project.id,
        }, {
            'name': 'task 2',
            'project_id': project.id,
        }, {
            'name': 'task 3',
            'project_id': project.id,
        }])
        project.company_id = False
        for task in project.task_ids:
            self.assertFalse(task.company_id, "The tasks should not have a company_id set.")
        task_1.company_id = self.company_a
        task_2.company_id = self.company_b
        self.assertEqual(task_1.company_id, self.company_a, "The company of task_1 should have been set to company A.")
        self.assertEqual(task_2.company_id, self.company_b, "The company of task_2 should have been set to company B.")
        self.assertFalse(task_3.company_id, "The company of the task_3 should not have been updated.")
        self.assertFalse(project.company_id, "The company of the project should not have been updated.")
        company_c = self.env['res.company'].create({'name': 'company C'})
        project.company_id = company_c
        for task in project.tasks:
            self.assertEqual(task.company_id, company_c, "The company of the tasks should have been updated to company C.")

    def test_move_task(self):
        with self.sudo('employee-a'):
            with self.allow_companies([self.company_a.id, self.company_b.id]):
                with Form(self.task_1) as task_form:
                    task_form.project_id = self.project_company_b
                task = task_form.save()

                self.assertEqual(task.company_id, self.company_b, "The company of the task should be the one from its project.")

                with Form(self.task_1) as task_form:
                    task_form.project_id = self.project_company_a
                task = task_form.save()

                self.assertEqual(task.company_id, self.company_a, "Moving a task should change its company.")

    def test_create_subtask(self):
        # 1) Create a subtask and check that every field is correctly set on the subtask
        with (
            Form(self.task_1) as task_1_form,
            task_1_form.child_ids.new() as subtask_line,
        ):
            self.assertEqual(subtask_line.project_id, self.task_1.project_id, "The task's project should already be set on the subtask.")
            subtask_line.name = 'Test Subtask'
        subtask = self.task_1.child_ids[0]
        self.assertTrue(subtask.show_display_in_project, "The subtask's field 'display in project' should be visible.")
        self.assertFalse(subtask.display_in_project, "The subtask's field 'display in project' should be unchecked.")
        self.assertEqual(subtask.company_id, self.task_1.company_id, "The company of the subtask should be the one from its project.")

        # 2) Change the project of the parent task and check that the subtask follows it
        with Form(self.task_1) as task_1_form:
            task_1_form.project_id = self.project_company_b
        self.assertEqual(subtask.project_id, self.task_1.project_id, "The task's project should already be set on the subtask.")
        self.assertTrue(subtask.show_display_in_project, "The subtask's field 'display in project' should be visible.")
        self.assertFalse(subtask.display_in_project, "The subtask's field 'display in project' should be unchecked.")
        self.assertEqual(subtask.company_id, self.project_company_b.company_id, "The company of the subtask should be the one from its project.")
        task_1_form.project_id = self.project_company_a

        # 3) Change the parent of the subtask and check that every field is correctly set on it
        # For `parent_id` to  be visible in the view, you need
        # 1. The debug mode
        # <field name="parent_id" groups="base.group_no_one"/>
        view = self.env.ref('project.view_task_form2').sudo()
        tree = etree.fromstring(view.arch)
        for node in tree.xpath('//field[@name="parent_id"][@invisible]'):
            node.attrib.pop('invisible')
        view.arch = etree.tostring(tree)
        with (
            self.debug_mode(),
            Form(subtask) as subtask_form
        ):
            subtask_form.parent_id = self.task_2
            self.assertEqual(subtask_form.project_id, self.task_2.project_id, "The task's project should already be set on the subtask.")
            self.assertTrue(subtask.show_display_in_project, "The subtask's field 'display in project' should be visible.")
            self.assertFalse(subtask.display_in_project, "The subtask's field 'display in project' should be unchecked.")
        self.assertEqual(subtask.company_id, self.task_2.company_id, "The company of the subtask should be the one from its new project, set from its parent.")

        # 4) Change the project of the subtask and check some fields
        with (
            self.debug_mode(),
            Form(subtask) as subtask_form
        ):
            subtask.project_id = self.project_company_a
            self.assertFalse(subtask.show_display_in_project, "The subtask's field 'display in project' shouldn't be visible.")
            self.assertTrue(subtask.display_in_project, "The subtask's field 'display in project' should be checked.")
        self.assertEqual(subtask.company_id, self.project_company_a.company_id, "The company of the subtask should be the one from its project, and not from its parent.")


    def test_cross_subtask_project(self):

        # For `parent_id` to  be visible in the view, you need
        # 1. The debug mode
        # <field name="parent_id" groups="base.group_no_one"/>
        view = self.env.ref('project.view_task_form2').sudo()
        tree = etree.fromstring(view.arch)
        for node in tree.xpath('//field[@name="parent_id"][@invisible]'):
            node.attrib.pop('invisible')
        view.arch = etree.tostring(tree)

        with self.sudo('employee-a'):
            with self.allow_companies([self.company_a.id, self.company_b.id]):
                with self.debug_mode():
                    with Form(self.env['project.task'].with_context({'tracking_disable': True})) as task_form:
                        task_form.name = 'Test Subtask in company B'
                        task_form.project_id = self.task_1.project_id
                        task_form.parent_id = self.task_1
                    task = task_form.save()

                self.assertEqual(self.task_1.child_ids.ids, [task.id])

        with self.sudo('employee-a'):
            with self.assertRaises(AccessError):
                with Form(task) as task_form:
                    task_form.name = "Testing changing name in a company I can not read/write"