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
|
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import tests
from odoo.addons.hr_holidays.tests.common import TestHrHolidaysCommon
from odoo.exceptions import AccessError, UserError
@tests.tagged('access_rights', 'post_install', '-at_install')
class TestAllocationRights(TestHrHolidaysCommon):
def setUp(self):
super().setUp()
self.rd_dept.manager_id = False
self.hr_dept.manager_id = False
self.employee_emp.parent_id = False
self.employee_emp.leave_manager_id = False
self.lt_validation_hr = self.env['hr.leave.type'].create({
'name': 'Validation = HR',
'allocation_validation_type': 'hr',
'allocation_type': 'fixed_allocation',
'validity_start': False,
})
self.lt_validation_manager = self.env['hr.leave.type'].create({
'name': 'Validation = manager',
'allocation_validation_type': 'manager',
'allocation_type': 'fixed_allocation',
'validity_start': False,
})
self.lt_validation_both = self.env['hr.leave.type'].create({
'name': 'Validation = both',
'allocation_validation_type': 'both',
'allocation_type': 'fixed_allocation',
'validity_start': False,
})
def request_allocation(self, user, values={}):
values = dict(values, **{
'name': 'Allocation',
'number_of_days': 1,
})
return self.env['hr.leave.allocation'].with_user(user).create(values)
class TestAccessRightsSimpleUser(TestAllocationRights):
def test_simple_user_request_allocation(self):
""" A simple user can request an allocation but not approve it """
values = {
'employee_id': self.employee_emp.id,
'holiday_status_id': self.lt_validation_hr.id,
}
allocation = self.request_allocation(self.user_employee.id, values)
with self.assertRaises(UserError):
allocation.action_approve()
def test_simple_user_request_fixed_allocation(self):
""" A simple user cannot request an allocation if set by HR """
self.lt_validation_hr.allocation_type = 'fixed'
values = {
'employee_id': self.employee_emp.id,
'holiday_status_id': self.lt_validation_hr.id,
}
with self.assertRaises(AccessError):
self.request_allocation(self.user_employee.id, values)
def test_simple_user_reset_to_draft(self):
""" A simple user can reset to draft only his own allocation """
values = {
'employee_id': self.employee_emp.id,
'holiday_status_id': self.lt_validation_hr.id,
}
allocation = self.request_allocation(self.user_employee.id, values)
self.assertEqual(allocation.state, 'confirm')
allocation.action_draft()
self.assertEqual(allocation.state, 'draft', "It should be reset to draft state")
class TestAccessRightsEmployeeManager(TestAllocationRights):
def setUp(self):
super().setUp()
self.managed_employee = self.env['hr.employee'].create({
'name': 'Jolly Jumper',
'leave_manager_id': self.user_employee.id,
})
def test_manager_request_allocation_other(self):
""" A manager cannot request and approve an allocation for employees he doesn't manage """
values = {
'employee_id': self.employee_hruser.id,
'holiday_status_id': self.lt_validation_manager.id,
}
with self.assertRaises(AccessError):
self.request_allocation(self.user_employee.id, values) # user is not the employee's manager
def test_manager_approve_request_allocation(self):
""" A manager can request and approve an allocation for managed employees """
values = {
'employee_id': self.managed_employee.id,
'holiday_status_id': self.lt_validation_manager.id,
}
allocation = self.request_allocation(self.user_employee.id, values)
allocation.action_approve()
self.assertEqual(allocation.state, 'validate', "The allocation should be validated")
def test_manager_refuse_request_allocation(self):
""" A manager can request and refuse an allocation for managed employees """
values = {
'employee_id': self.managed_employee.id,
'holiday_status_id': self.lt_validation_manager.id,
}
allocation = self.request_allocation(self.user_employee.id, values)
allocation.action_refuse()
self.assertEqual(allocation.state, 'refuse', "The allocation should be validated")
def test_manager_batch_allocation(self):
""" A manager cannot create batch allocation """
values = {
'holiday_status_id': self.lt_validation_manager.id,
'holiday_type': 'company',
'mode_company_id': self.user_employee.company_id.id,
}
with self.assertRaises(AccessError):
self.request_allocation(self.user_employee.id, values)
def test_manager_approve_own(self):
""" A manager cannot approve his own allocation """
values = {
'employee_id': self.user_employee.employee_id.id,
'holiday_status_id': self.lt_validation_manager.id,
}
allocation = self.request_allocation(self.user_employee.id, values)
with self.assertRaises(UserError):
allocation.action_approve()
def test_manager_only_first_approval(self):
""" A manager can only do the first approval """
values = {
'employee_id': self.managed_employee.id,
'holiday_status_id': self.lt_validation_both.id,
}
allocation = self.request_allocation(self.user_employee.id, values)
allocation.action_approve()
with self.assertRaises(UserError):
allocation.action_validate()
class TestAccessRightsHolidayUser(TestAllocationRights):
def test_holiday_user_request_allocation(self):
""" A holiday user can request and approve an allocation for any employee """
values = {
'employee_id': self.employee_emp.id,
'holiday_status_id': self.lt_validation_hr.id,
}
allocation = self.request_allocation(self.user_hruser.id, values)
allocation.action_approve()
self.assertEqual(allocation.state, 'validate', "It should have been validated")
def test_holiday_user_request_fixed_allocation(self):
""" A holiday user can request and approve an allocation if set by HR """
self.lt_validation_hr.allocation_type = 'fixed'
values = {
'employee_id': self.employee_emp.id,
'holiday_status_id': self.lt_validation_hr.id,
}
allocation = self.request_allocation(self.user_hruser.id, values)
allocation.action_approve()
self.assertEqual(allocation.state, 'validate', "It should have been validated")
def test_holiday_user_both_second_approval(self):
""" A holiday user can only do the second approval when double validation """
values = {
'employee_id': self.employee_emp.id,
'holiday_status_id': self.lt_validation_both.id,
}
allocation = self.request_allocation(self.user_hruser.id, values)
with self.assertRaises(UserError):
allocation.action_approve()
allocation.sudo().action_approve() # First approval by someone else
allocation.action_validate()
self.assertEqual(allocation.state, 'validate', "It should have been validated")
def test_holiday_user_batch_allocation(self):
""" A holiday user cannot create a batch allocation """
values = {
'holiday_status_id': self.lt_validation_hr.id,
'holiday_type': 'company',
'mode_company_id': self.user_employee.company_id.id,
}
with self.assertRaises(AccessError):
self.request_allocation(self.user_hruser.id, values)
def test_holiday_user_cannot_approve_own(self):
""" A holiday user cannot approve his own allocation """
values = {
'employee_id': self.employee_hruser.id,
'holiday_status_id': self.lt_validation_hr.id,
}
allocation = self.request_allocation(self.user_hruser.id, values)
with self.assertRaises(UserError):
allocation.action_approve()
class TestAccessRightsHolidayManager(TestAllocationRights):
def test_holiday_manager_can_approve_own(self):
""" A holiday manager can approve his own allocation """
values = {
'employee_id': self.employee_hrmanager.id,
'holiday_status_id': self.lt_validation_hr.id,
}
allocation = self.request_allocation(self.user_hrmanager.id, values)
allocation.action_approve()
self.assertEqual(allocation.state, 'validate', "It should have been validated")
def test_holiday_manager_both_validation(self):
""" A holiday manager can perform both validation """
values = {
'employee_id': self.employee_emp.id,
'holiday_status_id': self.lt_validation_both.id,
}
allocation = self.request_allocation(self.user_hrmanager.id, values)
allocation.action_approve()
self.assertEqual(allocation.state, 'validate1', "It should have been validated one time")
allocation.action_validate()
self.assertEqual(allocation.state, 'validate', "It should have been completely validated")
def test_holiday_manager_refuse_validated(self):
""" A holiday manager can refuse a validated allocation """
values = {
'employee_id': self.employee_emp.id,
'holiday_status_id': self.lt_validation_hr.id,
}
allocation = self.request_allocation(self.user_hrmanager.id, values)
allocation.action_approve()
self.assertEqual(allocation.state, 'validate', "It should have been validated")
allocation.action_refuse()
self.assertEqual(allocation.state, 'refuse', "It should have been refused")
|