Debian Codegraph @

addons/hr_timesheet/tests/test_portal_timesheet.py

raw
1 # -*- coding: utf-8 -*-
2
3 from odoo import Command
4 from odoo.tests import tagged
5
6 from odoo.addons.project.tests.test_project_sharing import TestProjectSharingCommon
7
8 @tagged('post_install', '-at_install')
9 class TestPortalTimesheet(TestProjectSharingCommon):
10
11 def test_ensure_fields_view_get_access(self):
12 """ Ensure that the method _fields_view_get is accessible without
13 raising an error for all portal users
14 """
15 # A portal collaborator is added to a project to enable the rule analytic.account.analytic.line.timesheet.portal.user
16 self.project_portal.write({
17 'collaborator_ids': [
18 Command.create({'partner_id': self.user_portal.partner_id.id}),
19 ],
20 })
21 for view in ['form', 'list']:
22 # Ensure that uom.uom records are not present in cache
23 self.env.invalidate_all()
24 # Should not raise any access error
25 self.env['account.analytic.line'].with_user(self.user_portal).get_view(view_type=view)
26
27 def test_action_view_subtask_timesheet(self):
28 """ Ensure that the action view_subtask_timesheet is accessible without
29 raising an error for all portal users
30 """
31 # A portal collaborator is added to a project to enable the rule analytic.account.analytic.line.timesheet.portal.user
32 self.project_portal.write({
33 'collaborator_ids': [
34 Command.create({'partner_id': self.user_portal.partner_id.id}),
35 ],
36 })
37 action = self.task_portal.action_view_subtask_timesheet()
38 tree_view_id = form_view_id = kanban_view_id = False
39 for view_id, view_type in action['views']:
40 if view_type == 'list':
41 tree_view_id = view_id
42 elif view_type == 'form':
43 form_view_id = view_id
44 elif view_type == 'kanban':
45 kanban_view_id = view_id
46
47 action = self.task_portal.with_user(self.user_portal).action_view_subtask_timesheet()
48 portal_tree_view_id = self.env['ir.model.data']._xmlid_to_res_id('hr_timesheet.hr_timesheet_line_portal_tree')
49 portal_form_view_id = self.env['ir.model.data']._xmlid_to_res_id('hr_timesheet.timesheet_view_form_portal_user')
50 portal_kanban_view_id = self.env['ir.model.data']._xmlid_to_res_id('hr_timesheet.view_kanban_account_analytic_line_portal_user')
51 if portal_tree_view_id and portal_form_view_id and portal_kanban_view_id:
52 # no need to check that if the views are not installed or already removed
53 for view_id, view_type in action['views']:
54 if view_type == 'list':
55 self.assertEqual(view_id, portal_tree_view_id)
56 elif view_type == 'form':
57 self.assertEqual(view_id, portal_form_view_id)
58 elif view_type == 'kanban':
59 self.assertEqual(view_id, portal_kanban_view_id)
60
61 self.env['ir.ui.view'].browse([portal_tree_view_id, portal_form_view_id, portal_kanban_view_id]).unlink()
62
63 action = self.task_portal.with_user(self.user_portal).action_view_subtask_timesheet()
64 for view_id, view_type in action['views']:
65 if view_type == 'list':
66 self.assertEqual(view_id, tree_view_id)
67 elif view_type == 'form':
68 self.assertEqual(view_id, form_view_id)
69 elif view_type == 'kanban':
70 self.assertEqual(view_id, kanban_view_id)
71
72 def test_timesheet_visibility_portal(self):
73 """
74 Steps:
75 1. Retrieve the domain that determines timesheet visibility for the portal user.
76 2. Create an employee linked to the project user.
77 3. Create a timesheet entry associated with a specific project and task.
78 4. Assign the portal user as the partner on the task.
79 5. Search for timesheets using the retrieved domain.
80 6. Verify that the created timesheet is visible to the portal user.
81 7. Remove the portal user as the partner of the task.
82 8. Search for timesheets again using the same domain.
83 9. Verify that the timesheet is no longer visible to the portal user.
84 10. Assign the portal user as the partner of the project.
85 11. Search for timesheets again using the same domain.
86 12. Verify that the timesheet is now visible to the portal user.
87 """
88 AnalyticLineModel = self.env['account.analytic.line']
89 timesheet_domain = AnalyticLineModel.with_user(self.user_portal)._timesheet_get_portal_domain()
90
91 employee = self.env['hr.employee'].create({
92 'name': 'Project User Employee',
93 'user_id': self.user_projectuser.id,
94 })
95
96 timesheet_entry = AnalyticLineModel.create({
97 'name': 'Timesheet',
98 'project_id': self.project_cows.id,
99 'task_id': self.task_cow.id,
100 'employee_id': employee.id,
101 })
102
103 self.task_cow.write({'partner_id': self.user_portal.partner_id.id})
104 timesheets = AnalyticLineModel.search(timesheet_domain)
105 self.assertIn(timesheet_entry.id, timesheets.ids, "Portal user should see the timesheet when set as the partner on the task.")
106
107 self.task_cow.write({'partner_id': False})
108 timesheets = AnalyticLineModel.search(timesheet_domain)
109 self.assertNotIn(timesheet_entry.id, timesheets.ids, "Portal user should not see the timesheet when not assigned as the task's partner.")
110
111 self.project_cows.write({'partner_id': self.user_portal.partner_id.id})
112 timesheets = AnalyticLineModel.search(timesheet_domain)
113 self.assertIn(timesheet_entry.id, timesheets.ids, "Portal user should see the timesheet when set as the project’s partner.")