Skip to content

Auditlog Causes Unit Test Failure #3224

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
AungKoKoLin1997 opened this issue Mar 12, 2025 · 0 comments
Open

Auditlog Causes Unit Test Failure #3224

AungKoKoLin1997 opened this issue Mar 12, 2025 · 0 comments
Labels

Comments

@AungKoKoLin1997
Copy link
Contributor

AungKoKoLin1997 commented Mar 12, 2025

Describe the Bug

Unit tests fail when there is a multi-company setup and an auditlog rule for the model.

To Reproduce

  • Enable the auditlog module.
  • Create an auditlog rule for the res.partner model.
  • 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 auditlog module 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

  1. 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.

  2. 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.

  3. child_ids Behavior
    When inspecting child_ids in both the UI and test environments:
    It includes child partners from different companies (or those without a company_id assigned).
    However, the AccessError only 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

@AungKoKoLin1997 AungKoKoLin1997 changed the title auditlog causes unit test failed Auditlog Causes Unit Test Failure Mar 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant