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

Open
mmequignon opened this issue Apr 16, 2024 · 0 comments
Labels
bug Something isn't working

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': '[email protected]', '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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant