Auditlog Causes Unit Test Failure
Describe the Bug
Unit tests fail when there is a multi-company setup and an auditlog rule for the model.
To Reproduce
- Enable the
auditlogmodule. - Create an auditlog rule for the
res.partnermodel. - Ensure a parent partner exists with child partners, where at least one child has an assigned company_id.
- Attempt to update the is_company field of the parent partner during a unit test.
- The test will fail with an AccessError. OR
- Run the account_financial_report test cases with the
auditlogmodule enabled to reproduce the issue.
This is the Traceback when I run test cases of account_financial_report.
Traceback (most recent call last):
File "/opt/odoo/odoo/api.py", line 1023, in get
cache_value = field_cache[record._ids[0]]
KeyError: 22
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/opt/odoo/odoo/fields.py", line 1161, in __get__
value = env.cache.get(record, self)
File "/opt/odoo/odoo/api.py", line 1030, in get
raise CacheMiss(record, field)
odoo.exceptions.CacheMiss: 'res.partner(22,).is_company'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/opt/odoo/odoo/fields.py", line 1187, in __get__
recs._fetch_field(self)
File "/opt/odoo/odoo/models.py", line 3229, in _fetch_field
self._read(fnames)
File "/opt/odoo/odoo/models.py", line 3355, in _read
raise self.env['ir.rule']._make_access_error('read', forbidden)
odoo.exceptions.AccessError: Due to security restrictions, you are not allowed to access 'Contact' (res.partner) records.
Contact your administrator to request access if necessary.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/__w/sfaj-oca/sfaj-oca/account_financial_report/tests/test_open_items.py", line 42, in test_partner_filter
partner_3.write({"is_company": False})
File "/__w/sfaj-oca/sfaj-oca/auditlog/models/rule.py", line 396, in write_full
result = write_full.origin(self, vals, **kwargs)
File "/opt/odoo/odoo/addons/base/models/res_partner.py", line 734, in write
partner._fields_sync(vals)
File "/opt/odoo/odoo/addons/base/models/res_partner.py", line [639](https://github.com/qrtl/sfaj-oca/actions/runs/13688939318/job/38278429974?pr=36#step:8:640), in _fields_sync
self._children_sync(values)
File "/opt/odoo/odoo/addons/base/models/res_partner.py", line 649, in _children_sync
for child in self.child_ids.filtered(lambda c: not c.is_company):
File "/opt/odoo/odoo/models.py", line 5480, in filtered
return self.browse([rec.id for rec in self if func(rec)])
File "/opt/odoo/odoo/models.py", line 5480, in <listcomp>
return self.browse([rec.id for rec in self if func(rec)])
File "/opt/odoo/odoo/addons/base/models/res_partner.py", line [649](https://github.com/qrtl/sfaj-oca/actions/runs/13688939318/job/38278429974?pr=36#step:8:650), in <lambda>
for child in self.child_ids.filtered(lambda c: not c.is_company):
File "/opt/odoo/odoo/fields.py", line 1189, in __get__
record._fetch_field(self)
File "/opt/odoo/odoo/models.py", line 3229, in _fetch_field
self._read(fnames)
File "/opt/odoo/odoo/models.py", line 3355, in _read
raise self.env['ir.rule']._make_access_error('read', forbidden)
odoo.exceptions.AccessError: Due to security restrictions, you are not allowed to access 'Contact' (res.partner) records.
Contact your administrator to request access if necessary.
Key Findings and Behavior Differences
-
Test Behavior When auditlog is enabled and an auditlog rule is configured for the res.partner model, the test fails with an AccessError. The error occurs because auditlog tries to access child partners, including those with a restricted company_id.
-
UI Behavior Performing the same write operation via the UI (e.g., updating the is_company field) succeeds without any errors, even when the user does not have access to the company assigned to one of the child partners.
-
child_ids Behavior When inspecting
child_idsin both the UI and test environments: It includes child partners from different companies (or those without acompany_idassigned). However, theAccessErroronly occurs in the test environment, not in the UI.
Debugging Suggestion
Create a new user and assign them to a new company.
Log in with this user and try to update this parent partner(eg, partner type):
https://github.com/odoo/odoo/blob/34947c01623a9fc753197bd55d6121d9c6fa682f/odoo/addons/base/data/res_partner_demo.xml#L73-L86
You can try to debug this line in the Odoo source:
https://github.com/odoo/odoo/blob/34947c01623a9fc753197bd55d6121d9c6fa682f/odoo/addons/base/models/res_partner.py#L649
You will notice that child_ids include the partner that has a company_id different from the user's company.
However, without auditlog, this partner is not included in child_ids.
@qrtl QT5209