Skip to content

How to mock external authentication decorator used along with routes #11584

Discussion options

You must be logged in to vote

You can't patch decorator since Python applies decorators right after you import the module and before methods of TestRoutes are called.
So, at the moment when you call TestRoutes's methods, list_schools is already modified and patching the authentication function doesn't have any effect.

Your case is a clear example of the benefits of using dependencies instead of other mechanisms (injecting logic via middlewares, decorators, etc..).

I think you can overcome this by introducing a dependency like it's shown above:

# project_name/routes/school_routes.py
from external_library import authentication

@authentication
def authenticate(request: Request):
    pass

@router.get("/", status_code=st…

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@David-Vithayathil
Comment options

Comment options

You must be logged in to vote
1 reply
@David-Vithayathil
Comment options

Answer selected by David-Vithayathil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question or problem
3 participants