[FIX] queue_job: repoint default_env to SUPERUSER in runjob#923
[FIX] queue_job: repoint default_env to SUPERUSER in runjob#923dannyadair wants to merge 2 commits into
Conversation
runjob is declared auth="none", so _auth_method_none pins request.env and transaction.default_env to a uid=None env. `env = http.request.env( user=SUPERUSER_ID)` only creates a local superuser env, leaving the default_env as uid=None. Any flush that goes through Transaction.flush() -> default_env.flush_all() then recomputes stored fields as uid=None and fails on anything dereferencing self.env.user. `http.request.update_env(user=SUPERUSER_ID)` additionally sets transaction.default_env to the superuser env. Closes OCA#922
4a36fc9 to
415420f
Compare
|
Rebased after #924 was merged. |
There was a problem hiding this comment.
Seems reasonable after reading the source here and https://github.com/odoo/odoo/blob/19.0/odoo/http.py#L1856-L1867:
def update_env(self, user=None, context=None, su=None):
""" Update the environment of the current request.
:param user: optional user/user id to change the current user
:type user: int or :class:`res.users record<~odoo.addons.base.models.res_users.ResUsers>`
:param dict context: optional context dictionary to change the current context
:param bool su: optional boolean to change the superuser mode
"""
cr = None # None is a sentinel, it keeps the same cursor
self.env = self.env(cr, user, context, su)
self.env.transaction.default_env = self.env
threading.current_thread().uid = self.env.uidThat said, why make the change? What problem does this avoid? ☕ Unit test that problem?
|
The problem is described in #922 and solved by this PR. |
Yep, sorry! Struck through much of my comment already! Still desire a unit test. |
ok with the existing tests this one would be weak and almost meaningless. (test_run_job_controller just calling class methods so we'd be mocking and verify that our two line change calls the two-line change), I 'll add something using HttpCase. |
588919a to
eeea3ee
Compare
|
ugh, sorry for the noise. _acquire_job commits which is forbidden in tests. Have to get tricky. |
Adds test_run_job_controller_http.py covering OCA#922: - test_runjob_pins_default_env_to_superuser asserts that when _acquire_job is entered, env.transaction.default_env.uid is SUPERUSER_ID -- the state a job would inherit at perform time. - test_runjob_without_updating_default_env_leaves_uid_none patches Request.update_env to a no-op and verifies default_env.uid stays None, isolating default_env repointing as the load-bearing behavior of runjob. Tests use HttpCase so _auth_method_none, session handling, and update_env run for real. _acquire_job is patched to return None so the endpoint exits cleanly without performing DB work (its internal commit is forbidden inside tests).
eeea3ee to
fd214cb
Compare
|
added test |
runjob is declared auth="none", so _auth_method_none pins request.env and transaction.default_env to a uid=None env.
env = http.request.env( user=SUPERUSER_ID)only creates a local superuser env, leaving the default_env as uid=None. Any flush that goes through Transaction.flush() -> default_env.flush_all() then recomputes stored fields as uid=None and fails on anything dereferencing self.env.user.http.request.update_env(user=SUPERUSER_ID)additionally sets transaction.default_env to the superuser env.Closes #922