-
-
Notifications
You must be signed in to change notification settings - Fork 582
[FIX] queue_job: repoint default_env to SUPERUSER in runjob #923
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
dannyadair
wants to merge
2
commits into
OCA:19.0
Choose a base branch
from
dannyadair:19.0-fix-runjob-env-user
base: 19.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+88
−1
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). | ||
|
|
||
| from unittest import mock | ||
|
|
||
| from odoo import SUPERUSER_ID, http | ||
| from odoo.tests.common import HttpCase, tagged | ||
|
|
||
| from ..controllers.main import RunJobController | ||
|
|
||
|
|
||
| @tagged("-at_install", "post_install") | ||
| class TestRunJobHttp(HttpCase): | ||
| """End-to-end tests for the ``/queue_job/runjob`` endpoint. | ||
|
|
||
| ``runjob`` is declared ``auth="none"``, so ``_auth_method_none`` pins | ||
| ``transaction.default_env`` to an env with ``uid=None``. The controller | ||
| must additionally repoint ``default_env`` to a superuser env, otherwise | ||
| any flush during ``job.perform()`` that goes through | ||
| ``Transaction.flush() -> default_env.flush_all()`` recomputes stored | ||
| fields with ``self.env.user`` as an empty recordset. See #922. | ||
| """ | ||
|
|
||
| def _capture_default_env_uid_at_acquire(self): | ||
| """Hit ``/queue_job/runjob`` and observe ``transaction.default_env.uid`` | ||
| at the moment ``_acquire_job`` is entered. | ||
|
|
||
| ``_acquire_job`` is patched to return ``None`` so the endpoint exits | ||
| without performing the job. This avoids the commit inside | ||
| ``_acquire_job``, which is forbidden inside tests, and is sufficient | ||
| to observe the env state established by the request-handling | ||
| machinery: nothing between ``update_env`` and ``_acquire_job`` | ||
| touches ``transaction.default_env``, so the value seen here is the | ||
| same value a job would see at ``perform()`` time. | ||
|
|
||
| HttpCase runs the HTTP server in the same process, so class-level | ||
| ``mock.patch`` calls take effect on the server side as well. | ||
| """ | ||
| captured = {} | ||
|
|
||
| def spy(cls, env, job_uuid): | ||
| captured["default_env_uid"] = env.transaction.default_env.uid | ||
| return None | ||
|
|
||
| with mock.patch.object(RunJobController, "_acquire_job", classmethod(spy)): | ||
| response = self.url_open( | ||
| f"/queue_job/runjob?db={self.env.cr.dbname}&job_uuid=stub" | ||
| ) | ||
| response.raise_for_status() | ||
|
|
||
| return captured.get("default_env_uid") | ||
|
|
||
| def test_runjob_pins_default_env_to_superuser(self): | ||
| """``runjob`` must set ``transaction.default_env`` to a superuser env. | ||
|
|
||
| Verifies that by the time ``_acquire_job`` is entered, | ||
| ``env.transaction.default_env.uid`` is ``SUPERUSER_ID``. This is the | ||
| state that lets flushes triggered by the job recompute stored fields | ||
| with a real ``self.env.user`` rather than an empty recordset. | ||
| """ | ||
| default_env_uid = self._capture_default_env_uid_at_acquire() | ||
| self.assertEqual(default_env_uid, SUPERUSER_ID) | ||
|
|
||
| def test_runjob_without_updating_default_env_leaves_uid_none(self): | ||
| """Repointing ``default_env`` is the specific mechanism this relies on. | ||
|
|
||
| Neutralises ``http.request.update_env`` for the duration of the | ||
| request, matching what happens if a ``runjob`` implementation only | ||
| obtains a local superuser env via ``http.request.env(user=...)`` | ||
| without invoking ``update_env``. The observable ``default_env.uid`` | ||
| then remains at the ``None`` value installed by | ||
| ``_auth_method_none``. This isolates ``default_env`` repointing as | ||
| the load-bearing behavior of ``runjob``: any future refactor that | ||
| drops it will trip this test. | ||
| """ | ||
|
|
||
| def no_op_update_env(self_, user=None, context=None, su=None): | ||
| """Alternative ``Request.update_env`` that intentionally does | ||
| nothing. Simulates a controller that never asks the request to | ||
| update its environment. | ||
| """ | ||
|
|
||
| with mock.patch.object(http.Request, "update_env", no_op_update_env): | ||
| default_env_uid = self._capture_default_env_uid_at_acquire() | ||
|
|
||
| self.assertIsNone(default_env_uid) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.