rest-framework icon indicating copy to clipboard operation
rest-framework copied to clipboard

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

Open mmequignon opened this issue 1 year ago • 1 comments

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 avatar Apr 16 '24 14:04 mmequignon

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[bot] avatar Oct 20 '24 12:10 github-actions[bot]