File: test_account_lock_exception.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 (421 lines) | stat: -rw-r--r-- 21,101 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
from datetime import timedelta

from freezegun import freeze_time

from odoo import Command, fields
from odoo.addons.account.models.company import SOFT_LOCK_DATE_FIELDS
from odoo.addons.account.tests.common import AccountTestInvoicingCommon
from odoo.exceptions import UserError
from odoo.tests import new_test_user, tagged


@tagged('post_install', '-at_install')
class TestAccountLockException(AccountTestInvoicingCommon):

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

        cls.fakenow = cls.env.cr.now()
        cls.startClassPatcher(freeze_time(cls.fakenow))

        cls.other_user = new_test_user(
            cls.env,
            name='Other User',
            login='other_user',
            password='password',
            email='other_user@example.com',
            groups_id=cls.get_default_groups().ids,
            company_id=cls.env.company.id,
        )

        cls.company_data_2 = cls.setup_other_company()

        cls.soft_lock_date_info = [
            ('fiscalyear_lock_date', 'out_invoice'),
            ('tax_lock_date', 'out_invoice'),
            ('sale_lock_date', 'out_invoice'),
            ('purchase_lock_date', 'in_invoice'),
        ]

    def test_user_exception_move_edit_multi_user(self):
        """
        Test that an exception for a specific user only works for that user.
        """
        for lock_date_field, move_type in self.soft_lock_date_info:
            with self.subTest(lock_date_field=lock_date_field, move_type=move_type), self.cr.savepoint() as sp:
                move = self.init_invoice(move_type, invoice_date='2016-01-01', post=True, amounts=[1000.0], taxes=self.tax_sale_a)

                # Lock the move
                self.company[lock_date_field] = fields.Date.to_date('2020-01-01')
                with self.assertRaises(UserError), self.cr.savepoint():
                    move.button_draft()

                # Add an exception to make the move editable (for the current user)
                self.env['account.lock_exception'].create({
                    'company_id': self.company.id,
                    'user_id': self.env.user.id,
                    lock_date_field: fields.Date.to_date('2010-01-01'),
                    'end_datetime': self.fakenow + timedelta(hours=24),
                    'reason': 'test_user_exception_move_edit_multi_user',
                })
                move.button_draft()
                move.action_post()

                # Check that the exception does not apply to other users
                with self.assertRaises(UserError), self.cr.savepoint():
                    move.with_user(self.other_user).button_draft()
                sp.close()  # Rollback to ensure all subtests start in the same situation

    def test_global_exception_move_edit_multi_user(self):
        """
        Test that an exception without a specified user works for any user.
        """
        for lock_date_field, move_type in self.soft_lock_date_info:
            with self.subTest(lock_date_field=lock_date_field, move_type=move_type), self.cr.savepoint() as sp:
                move = self.init_invoice(move_type, invoice_date='2016-01-01', post=True, amounts=[1000.0], taxes=self.tax_sale_a)

                # Lock the move
                self.company[lock_date_field] = fields.Date.to_date('2020-01-01')
                with self.assertRaises(UserError), self.cr.savepoint():
                    move.button_draft()

                # Add a global exception to make the move editable for everyone
                self.env['account.lock_exception'].create({
                    'company_id': self.company.id,
                    'user_id': False,
                    lock_date_field: fields.Date.to_date('2010-01-01'),
                    'end_datetime': self.fakenow + timedelta(hours=24),
                    'reason': 'test_global_exception_move_edit_multi_user',
                })

                move.button_draft()
                move.action_post()

                move.with_user(self.other_user).button_draft()

                sp.close()  # Rollback to ensure all subtests start in the same situation

    def test_user_exception_branch(self):
        """
        Test that the locking and exception mechanism works correctly in company hierarchies.
            * A lock in the branch does not lock the parent.
            * A lock in the parent also locks the branch.
            * An exception in the branch does not matter for the lock in the parent.
            * Let both parent and branch be locked.
              To make changes in the locked period in the brranch we need exceptions in both companies.
        """

        root_company = self.company_data['company']
        root_company.write({'child_ids': [Command.create({'name': 'branch'})]})
        self.cr.precommit.run()  # load the CoA
        branch = root_company.child_ids

        for lock_date_field, move_type in self.soft_lock_date_info:
            with self.subTest(lock_date_field=lock_date_field, move_type=move_type), self.cr.savepoint() as sp:
                # Create a move in the branch
                branch_move = self.init_invoice(
                    move_type, invoice_date='2016-01-01', post=True, amounts=[1000.0], taxes=self.tax_sale_a, company=branch,
                )

                # Create a move in the parent company
                root_move = self.init_invoice(
                    move_type, invoice_date='2016-01-01', post=True, amounts=[1000.0], taxes=self.tax_sale_a, company=root_company,
                )

                # Lock the branch
                branch[lock_date_field] = fields.Date.to_date('2020-01-01')

                # The branch_move is locked while the root_move is not
                with self.assertRaises(UserError), self.cr.savepoint():
                    branch_move.button_draft()
                root_move.button_draft()
                root_move.action_post()

                # Add an exception in the branch to make the branch_move editable (for the current user)
                self.env['account.lock_exception'].create({
                    'company_id': branch.id,
                    'user_id': self.env.user.id,
                    lock_date_field: fields.Date.to_date('2010-01-01'),
                    'end_datetime': self.fakenow + timedelta(hours=24),
                    'reason': 'test_user_exception_branch branch exception',
                })
                branch_move.button_draft()
                branch_move.action_post()

                # Lock the parent company
                root_company[lock_date_field] = fields.Date.to_date('2020-01-01')

                # Check that both moves are locked now (the branch exception alone is insufficient)
                for move in [branch_move, root_move]:
                    with self.assertRaises(UserError), self.cr.savepoint():
                        move.button_draft()

                # Add an exception in the parent company to make both moves editable (for the current user)
                self.env['account.lock_exception'].create({
                    'company_id': root_company.id,
                    'user_id': self.env.user.id,
                    lock_date_field: fields.Date.to_date('2010-01-01'),
                    'end_datetime': self.fakenow + timedelta(hours=24),
                    'reason': 'test_user_exception_branch root_company exception',
                })
                for move in [branch_move, root_move]:
                    move.button_draft()
                    move.action_post()

                sp.close()  # Rollback to ensure all subtests start in the same situation

    def test_user_exception_wrong_company(self):
        """
        Test that an exception only works for the specified company.
        """
        for lock_date_field, move_type in self.soft_lock_date_info:
            with self.subTest(lock_date_field=lock_date_field, move_type=move_type), self.cr.savepoint() as sp:
                move = self.init_invoice(move_type, invoice_date='2016-01-01', post=True, amounts=[1000.0], taxes=self.tax_sale_a)
                # Lock the move
                self.company[lock_date_field] = fields.Date.to_date('2020-01-01')
                with self.assertRaises(UserError), self.cr.savepoint():
                    move.button_draft()

                # Add an exception for another company
                self.env['account.lock_exception'].create({
                    'company_id': self.company_data_2['company'].id,
                    'user_id': self.env.user.id,
                    lock_date_field: fields.Date.to_date('2010-01-01'),
                    'end_datetime': self.fakenow + timedelta(hours=24),
                    'reason': 'test_user_exception_move_edit_multi_user',
                })

                # Check that the exception is insufficient
                with self.assertRaises(UserError), self.cr.savepoint():
                    move.button_draft()

                sp.close()  # Rollback to ensure all subtests start in the same situation

    def test_user_exception_insufficient(self):
        """
        Test that the exception only works if the specified lock date is actually before the accounting date.
        """
        for lock_date_field, move_type in self.soft_lock_date_info:
            with self.subTest(lock_date_field=lock_date_field, move_type=move_type), self.cr.savepoint() as sp:
                move = self.init_invoice(move_type, invoice_date='2016-01-01', post=True, amounts=[1000.0], taxes=self.tax_sale_a)

                # Lock the move
                self.company[lock_date_field] = fields.Date.to_date('2020-01-01')
                with self.assertRaises(UserError), self.cr.savepoint():
                    move.button_draft()

                # Add an exception before the lock date but not before the date of the test_invoice
                self.env['account.lock_exception'].create({
                    'company_id': self.company.id,
                    'user_id': self.env.user.id,
                    lock_date_field: fields.Date.to_date('2016-01-01'),
                    'end_datetime': self.fakenow + timedelta(hours=24),
                    'reason': 'test_user_exception_move_edit_multi_user',
                })

                # Check that the exception is insufficient
                with self.assertRaises(UserError), self.cr.savepoint():
                    move.button_draft()

                sp.close()  # Rollback to ensure all subtests start in the same situation

    def test_expired_exception(self):
        """
        Test that the exception does not work if we are past the `end_datetime` of the exception.
        """
        for lock_date_field, move_type in self.soft_lock_date_info:
            with self.subTest(lock_date_field=lock_date_field, move_type=move_type), self.cr.savepoint() as sp:
                move = self.init_invoice(move_type, invoice_date='2016-01-01', post=True, amounts=[1000.0], taxes=self.tax_sale_a)

                # Lock the move
                self.company[lock_date_field] = fields.Date.to_date('2020-01-01')
                with self.assertRaises(UserError), self.cr.savepoint():
                    move.button_draft()

                # Add an expired exception
                self.env['account.lock_exception'].create({
                    'company_id': self.company.id,
                    'user_id': self.env.user.id,
                    lock_date_field: fields.Date.to_date('2010-01-01'),
                    'create_date': self.fakenow - timedelta(hours=24),
                    'end_datetime': self.fakenow - timedelta(milliseconds=1),
                    'reason': 'test_expired_exception',
                })
                with self.assertRaises(UserError), self.cr.savepoint():
                    move.button_draft()

                sp.close()  # Rollback to ensure all subtests start in the same situation

    def test_revoked_exception(self):
        for lock_date_field, move_type in self.soft_lock_date_info:
            with self.subTest(lock_date_field=lock_date_field, move_type=move_type), self.cr.savepoint() as sp:
                move = self.init_invoice(move_type, invoice_date='2016-01-01', post=True, amounts=[1000.0], taxes=self.tax_sale_a)

                # Lock the move
                self.company[lock_date_field] = fields.Date.to_date('2020-01-01')
                with self.assertRaises(UserError), self.cr.savepoint():
                    move.button_draft()

                # Add an exception to make the move editable (for the current user)
                exception = self.env['account.lock_exception'].create({
                    'company_id': self.company.id,
                    'user_id': self.env.user.id,
                    lock_date_field: fields.Date.to_date('2010-01-01'),
                    'end_datetime': self.fakenow + timedelta(hours=24),
                    'reason': 'test_user_exception_move_edit_multi_user',
                })
                move.button_draft()
                move.action_post()

                exception.action_revoke()

                # Check that the exception does not work anymore
                with self.assertRaises(UserError), self.cr.savepoint():
                    move.button_draft()

                sp.close()  # Rollback to ensure all subtests start in the same situation

    def test_user_exception_wrong_field(self):
        for lock_date_field, move_type, exception_lock_date_field in [
            ('fiscalyear_lock_date', 'out_invoice', 'tax_lock_date'),
            ('tax_lock_date', 'out_invoice', 'fiscalyear_lock_date'),
            ('sale_lock_date', 'out_invoice', 'purchase_lock_date'),
            ('purchase_lock_date', 'in_invoice', 'sale_lock_date'),
        ]:
            with self.subTest(lock_date_field=lock_date_field, move_type=move_type), self.cr.savepoint() as sp:
                move = self.init_invoice(move_type, invoice_date='2016-01-01', post=True, amounts=[1000.0], taxes=self.tax_sale_a)
                # Lock the move
                self.company[lock_date_field] = fields.Date.to_date('2020-01-01')
                with self.assertRaises(UserError), self.cr.savepoint():
                    move.button_draft()

                # Add an exception for a different lock date field
                self.env['account.lock_exception'].create({
                    'company_id': self.company_data_2['company'].id,
                    'user_id': self.env.user.id,
                    exception_lock_date_field: fields.Date.to_date('2010-01-01'),
                    'end_datetime': self.fakenow + timedelta(hours=24),
                    'reason': 'test_user_exception_wrong_field',
                })

                # Check that the exception is insufficient
                with self.assertRaises(UserError), self.cr.savepoint():
                    move.button_draft()

                sp.close()  # Rollback to ensure all subtests start in the same situation

    def test_hard_lock_date(self):
        """
        Test that
          * exceptions (for other lock date fields) do not allow bypassing the hard lock date
          * the hard lock date cannot be decreased or removed
        """
        in_move = self.init_invoice('in_invoice', invoice_date='2016-01-01', post=True, amounts=[1000.0], taxes=self.tax_sale_a)
        out_move = self.init_invoice('out_invoice', invoice_date='2016-01-01', post=True, amounts=[1000.0], taxes=self.tax_sale_a)

        self.company.hard_lock_date = fields.Date.to_date('2020-01-01')

        # Check that we cannot remove the hard lock date.
        with self.assertRaises(UserError), self.cr.savepoint():
            self.company.hard_lock_date = False

        # Check that we cannot decrease the hard lock date.
        with self.assertRaises(UserError), self.cr.savepoint():
            self.company.hard_lock_date = fields.Date.to_date('2019-01-01')

        # Create exceptions for all lock date fields except the hard lock date
        self.env['account.lock_exception'].create([
            {
            'company_id': self.company_data_2['company'].id,
            'user_id': self.env.user.id,
            lock_date_field: fields.Date.to_date('2010-01-01'),
            'end_datetime': self.fakenow + timedelta(hours=24),
            'reason': f'test_hard_lock_ignores_exceptions {lock_date_field}',
            }
            for lock_date_field in SOFT_LOCK_DATE_FIELDS
        ])

        # Check that the exceptions are insufficient
        for move in [in_move, out_move]:
            with self.assertRaises(UserError), self.cr.savepoint():
                move.button_draft()

    def test_company_lock_date(self):
        """
        Test the `company_lock_date` field is set corretly on exception creation.
        Test the behavior when a company lock date is changed.
          * Every active exception gets revoked and recreated with the new company lock date
          * Non-active exceptions are not affected
        """
        self.env['account.lock_exception'].search([]).sudo().unlink()
        for lock_date_field, move_type in self.soft_lock_date_info:
            with self.subTest(lock_date_field=lock_date_field, move_type=move_type), self.cr.savepoint() as sp:
                self.company[lock_date_field] = fields.Date.to_date('2020-01-01')

                revoked_exception = self.env['account.lock_exception'].create({
                    'company_id': self.company.id,
                    'user_id': self.env.user.id,
                    lock_date_field: fields.Date.to_date('2010-01-01'),
                    'end_datetime': self.fakenow + timedelta(hours=24),
                    'reason': 'test_exception_recreated_on_lock_date_change revoked',
                })
                revoked_exception.action_revoke()
                active_exception = self.env['account.lock_exception'].create({
                    'company_id': self.company.id,
                    'user_id': self.env.user.id,
                    lock_date_field: fields.Date.to_date('2010-01-01'),
                    'end_datetime': self.fakenow + timedelta(hours=24),
                    'reason': 'test_exception_recreated_on_lock_date_change active',
                })

                # Check that the company lock date field was set correcyly on exception creation
                self.assertEqual(revoked_exception.company_lock_date, fields.Date.to_date('2020-01-01'))
                self.assertEqual(active_exception.company_lock_date, fields.Date.to_date('2020-01-01'))

                # The lock date change should trigger the "recreation" proces
                self.company[lock_date_field] = fields.Date.to_date('2021-01-01')

                self.assertEqual(revoked_exception.company_lock_date, fields.Date.to_date('2020-01-01'))

                self.assertEqual(active_exception.state, 'revoked')

                exceptions = self.env['account.lock_exception'].with_context(active_test=False).search([])
                self.assertEqual(len(exceptions), 3)
                new_exception = exceptions - revoked_exception - active_exception
                # Check that the new exception is a "recreation" of the `active_exception`
                self.assertRecordValues(new_exception, [{
                    'company_id': self.company.id,
                    'user_id': self.env.user.id,
                    lock_date_field: fields.Date.to_date('2010-01-01'),
                    'company_lock_date': fields.Date.to_date('2021-01-01'),
                    'end_datetime': self.env.cr.now() + timedelta(hours=24),
                    'reason': 'test_exception_recreated_on_lock_date_change active',
                }])

                sp.close()  # Rollback to ensure all subtests start in the same situation

    def test_user_exception_remove_lock_date(self):
        """
        Test that an exception removing a lock date (instead of just decreasing it) works.
        """
        for lock_date_field, move_type in self.soft_lock_date_info:
            with self.subTest(lock_date_field=lock_date_field, move_type=move_type), self.cr.savepoint() as sp:
                move = self.init_invoice(move_type, invoice_date='2016-01-01', post=True, amounts=[1000.0], taxes=self.tax_sale_a)

                # Lock the move
                self.company[lock_date_field] = fields.Date.to_date('2020-01-01')
                with self.assertRaises(UserError):
                    move.button_draft()

                # Add an exception removing the lock date
                self.env['account.lock_exception'].create({
                    'company_id': self.company.id,
                    'user_id': self.env.user.id,
                    lock_date_field: False,
                    'end_datetime': self.fakenow + timedelta(hours=24),
                    'reason': 'test_user_exception_move_edit_multi_user',
                })
                move.button_draft()

                sp.close()  # Rollback to ensure all subtests start in the same situation