|
| 1 | +# Copyright (C) 2019 Open Source Integrators |
| 2 | +# Copyright (C) 2019 Serpent Consulting Services |
| 3 | +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). |
| 4 | +from odoo.tests import common |
| 5 | + |
| 6 | + |
| 7 | +class TestProject(common.TransactionCase): |
| 8 | + |
| 9 | + def setUp(self): |
| 10 | + super(TestProject, self).setUp() |
| 11 | + self.project_obj = self.env['project.project'] |
| 12 | + self.task_obj = self.env['project.task'] |
| 13 | + self.res_users_model = self.env['res.users'] |
| 14 | + |
| 15 | + self.partner_1 = self.env['res.partner'].create({ |
| 16 | + 'name': 'SERPENTCS ', |
| 17 | + |
| 18 | + |
| 19 | + # Groups |
| 20 | + self.grp_mngr =\ |
| 21 | + self.env.ref('project.group_project_manager') |
| 22 | + self.grp_user = self.env.ref('project.group_project_user') |
| 23 | + # Company |
| 24 | + self.company = self.env.ref('base.main_company') |
| 25 | + # Main Operating Unit |
| 26 | + self.main_OU = self.env.ref('operating_unit.main_operating_unit') |
| 27 | + # B2C Operating Unit |
| 28 | + self.b2c_OU = self.env.ref('operating_unit.b2c_operating_unit') |
| 29 | + # Create User 1 with Main OU |
| 30 | + self.user1 = self._create_user('user_1', [self.grp_mngr, |
| 31 | + self.grp_user], |
| 32 | + self.company, [self.main_OU]) |
| 33 | + # Create User 2 with B2C OU |
| 34 | + self.user2 = self._create_user('user_2', [self.grp_mngr, |
| 35 | + self.grp_user], |
| 36 | + self.company, [self.b2c_OU]) |
| 37 | + |
| 38 | + self.project1 = self._create_project(self.user1, self.main_OU) |
| 39 | + self.project2 = self._create_project(self.user2, self.b2c_OU) |
| 40 | + self.task1 = self._create_task(self.user1, self.project1) |
| 41 | + self.task2 = self._create_task(self.user2, self.project2) |
| 42 | + |
| 43 | + def _create_user(self, login, groups, company, operating_units): |
| 44 | + """ Create a user. """ |
| 45 | + group_ids = [group.id for group in groups] |
| 46 | + user = self.res_users_model.create({ |
| 47 | + 'name': login, |
| 48 | + 'login': login, |
| 49 | + 'password': 'demo', |
| 50 | + |
| 51 | + 'company_id': company.id, |
| 52 | + 'company_ids': [(4, company.id)], |
| 53 | + 'operating_unit_ids': [(4, ou.id) for ou in operating_units], |
| 54 | + 'groups_id': [(6, 0, group_ids)] |
| 55 | + }) |
| 56 | + return user |
| 57 | + |
| 58 | + def _create_project(self, uid, operating_unit): |
| 59 | + project = self.project_obj.sudo(uid).create({ |
| 60 | + 'name': 'Test Project', |
| 61 | + 'operating_unit_id': operating_unit.id, |
| 62 | + 'privacy_visibility': 'employees', |
| 63 | + 'partner_id': self.partner_1.id |
| 64 | + }) |
| 65 | + return project |
| 66 | + |
| 67 | + def _create_task(self, uid, project): |
| 68 | + task = self.task_obj.sudo(uid).create({ |
| 69 | + 'name': 'Test Task', |
| 70 | + 'user_id': uid.id, |
| 71 | + 'project_id': project.id |
| 72 | + }) |
| 73 | + return task |
| 74 | + |
| 75 | + def test_project(self): |
| 76 | + # User 2 is only assigned to B2C Operating Unit, and cannot |
| 77 | + # access Project for Main Operating Unit. |
| 78 | + projects = self.project_obj.sudo(self.user2.id).search( |
| 79 | + [('id', '=', self.project2.id), |
| 80 | + ('operating_unit_id', '=', self.main_OU.id)]) |
| 81 | + self.assertEqual(projects.ids, [], 'User 2 should not have access to ' |
| 82 | + '%s' % self.main_OU.name) |
| 83 | + self.assertEqual(self.project1.operating_unit_id.id, self.main_OU.id) |
| 84 | + |
| 85 | + def test_project_task(self): |
| 86 | + # User 2 is only assigned to B2C Operating Unit, and cannot |
| 87 | + # access Task for Main Operating Unit. |
| 88 | + tasks = self.task_obj.sudo(self.user2.id).search( |
| 89 | + [('id', '=', self.task2.id), |
| 90 | + ('operating_unit_id', '=', self.main_OU.id)]) |
| 91 | + self.assertEqual(tasks.ids, [], 'User 2 should not have access to ' |
| 92 | + '%s' % self.main_OU.name) |
| 93 | + self.assertEqual(self.task1.operating_unit_id.id, |
| 94 | + self.project1.operating_unit_id.id) |
0 commit comments