Skip to content
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

[14.0] Fastapi: Cannot use sudo() in a dependency with _create_test_client #430

Closed
mmequignon opened this issue Apr 16, 2024 · 1 comment
Labels
bug Something isn't working stale PR/Issue without recent activity, it'll be soon closed automatically.

Comments

@mmequignon
Copy link
Member

I created a dependency returning an auth_api_key which requires sudo()

def authenticated_api_key(
    key: Annotated[str, Depends(APIKeyHeader(name="HTTP-API-KEY"))],
    env: Annotated[Environment, Depends(odoo_env)],
) -> AuthApiKey:
    [...]
    admin = env["res.users"].sudo().browse(1)
    [...]

When executed on a running instance, this is working fine.
When executed in unit tests like this:

args = {'app': <fastapi.applications.FastAPI object at 0x7fb78c915898>, 'router': <fastapi.routing.APIRouter object at 0x7fb78e74e7b8>, 'env': <odoo.api.Environment object at 0x7fb78cec58d0>, 'user': res.users(93,)}
headers = {'HTTP-PARTNER-EMAIL': 'plaffont@foo.bar', 'HTTP-API-KEY': 'KEY'}
with self._create_test_client(**args) as client:
    response = client.post(
        "/create", content=payload, headers=headers
    )
[...]

I get the following error

odoo/local-src/cosanum_api_base/dependencies.py:30: in authenticated_api_key
   admin = env["res.users"].sudo().browse(1)
odoo/src/odoo/models.py:5073: in sudo
   return self.with_env(self.env(su=flag))
odoo/src/odoo/api.py:510: in __call__
   return Environment(cr, uid, context, su)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

cls = <class 'odoo.api.Environment'>, cr = <odoo.sql_db.Cursor object at 0x7fb78e378d68>, uid = 93, context = {'tracking_disable': True}, su = True

   def __new__(cls, cr, uid, context, su=False):
       if uid == SUPERUSER_ID:
           su = True
       assert context is not None
       args = (cr, uid, context, su)
   
       # if env already exists, return it
       env, envs = None, cls.envs
       for env in envs:
           if env.args == args:
               return env
   
       # otherwise create environment, and add it in the set
       self = object.__new__(cls)
       args = (cr, uid, frozendict(context), su)
       self.cr, self.uid, self.context, self.su = self.args = args
       self.registry = Registry(cr.dbname)
>       self.cache = envs.cache
E       AttributeError: 'tuple' object has no attribute 'cache'

Of course I placed a PDB, and cls.envs in __new__ is returning an empty tuple, which makes me thing that the default env used by _create_test_client isn't fully initialized.

Any bell ringing?

@lmignon any plan to provide a std way of overriding the env, as it is done for the authenticated_partner_impl ?

I can provide more infos about the implementation, if needed.

Thanks !

ping @simahawk

@mmequignon mmequignon added the bug Something isn't working label Apr 16, 2024
Copy link

There hasn't been any activity on this issue in the past 6 months, so it has been marked as stale and it will be closed automatically if no further activity occurs in the next 30 days.
If you want this issue to never become stale, please ask a PSC member to apply the "no stale" label.

@github-actions github-actions bot added the stale PR/Issue without recent activity, it'll be soon closed automatically. label Oct 20, 2024
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Nov 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working stale PR/Issue without recent activity, it'll be soon closed automatically.
Projects
None yet
Development

No branches or pull requests

1 participant