From 62355b6ad3e2af16efabe1c3ca2fb053a6084bc1 Mon Sep 17 00:00:00 2001 From: Yannick Tivisse Date: Wed, 8 Jul 2020 13:26:27 +0000 Subject: [FIX] test_new_api: Fix bad shared cache usage for stored computed fields Purpose ======= Since the 13.0, a regression has been introduced in hr_timesheet. Considering: - The account.analytic.line model, representing a timesheet entry - The project.task model, representing a task, on which we have the fields: - timesheet_ids: One2many - planned_hours: Computed (sudo), stored, representing the sum of the timesheet amounts - An ir.rule restricting the timesheets CRUD to his own only. A user can only see its own timesheets on a task, but the field "Planned Hours", which is stored-compute_sudo, should take all the timesheet lines into account However, when adding a new line and then recomputing the value, no existing line from another user is binded on self, then the value is erased and saved on the database. Specification ============= This commit introduces a test that illustrates that bad usage of a shared cache. A correct way to fix this could be to avoid using the cache if an ir.rule is pointing to the related model. That would force the recomputation in a real sudoed environment. closes odoo/odoo#55408 Taskid: 2285924 X-original-commit: 50e229fe66b2a82717d4260287e18a875bbd26f5 Signed-off-by: Yannick Tivisse (yti) --- odoo/addons/test_new_api/tests/test_new_fields.py | 36 +++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'odoo/addons/test_new_api/tests/test_new_fields.py') diff --git a/odoo/addons/test_new_api/tests/test_new_fields.py b/odoo/addons/test_new_api/tests/test_new_fields.py index c3a7005cab0..5591ce7e4c5 100644 --- a/odoo/addons/test_new_api/tests/test_new_fields.py +++ b/odoo/addons/test_new_api/tests/test_new_fields.py @@ -2947,3 +2947,39 @@ class TestComputeQueries(common.TransactionCase): record = model.create({'foo': 'Foo', 'bar': 'Bar'}) self.assertEqual(record.foo, 'Bar') self.assertEqual(record.bar, 'Bar') + +class test_shared_cache(TransactionCaseWithUserDemo): + def test_shared_cache_computed_field(self): + # Test case: Check that the shared cache is not used if a compute_sudo stored field + # is computed IF there is an ir.rule defined on this specific model. + + # Real life example: + # A user can only see its own timesheets on a task, but the field "Planned Hours", + # which is stored-compute_sudo, should take all the timesheet lines into account + # However, when adding a new line and then recomputing the value, no existing line + # from another user is binded on self, then the value is erased and saved on the + # database. + + task = self.env['test_new_api.model_shared_cache_compute_parent'].create({ + 'name': 'Shared Task'}) + self.env['test_new_api.model_shared_cache_compute_line'].create({ + 'user_id': self.env.ref('base.user_admin').id, + 'parent_id': task.id, + 'amount': 1, + }) + self.assertEqual(task.total_amount, 1) + + self.env['base'].flush() + task.invalidate_cache() # Start fresh, as it would be the case on 2 different sessions. + + task = task.with_user(self.user_demo) + with common.Form(task) as task_form: + # Use demo has no access to the already existing line + self.assertEqual(len(task_form.line_ids), 0) + # But see the real total_amount + self.assertEqual(task_form.total_amount, 1) + # Now let's add a new line (and retrigger the compute method) + with task_form.line_ids.new() as line: + line.amount = 2 + # The new value for total_amount, should be 3, not 2. + self.assertEqual(task_form.total_amount, 2) -- cgit v1.2.3