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

Mock auth0 for tests #49

Open
amitrahav opened this issue Jun 20, 2021 · 1 comment
Open

Mock auth0 for tests #49

amitrahav opened this issue Jun 20, 2021 · 1 comment
Labels
answered This issue is answered. wait for issuer's response documentation Improvements or additions to documentation

Comments

@amitrahav
Copy link

Request: adding documentation of mocking auth for tests.

I tried:

"""tests/test_templates.py"""
from fastapi.testclient import TestClient
from tests import auth_with_mocked_auth

client = TestClient(auth_with_mocked_auth())

def test_create_template():
    response = client.get("/templates/")
    assert response.status_code == 200
"""tests/__init__.py"""
def auth_with_mocked_auth():
    def override_auth_dependency():
        return {
             'iss': '<auth0 endoint>',
             'sub': 'auth0|<auth0 id>',
             'aud': [
                 '<endpoints>',
             ],
             'iat': <...>,
             'exp': <...>,
             'azp': '<...>,
             'scope': 'openid profile email read:current_user update:current_user_metadata',
             'permissions': [<...>]
        }

    app.dependency_overrides[auth] = override_auth_dependency()
    return app

Yet, getting 403 with details: "Not authenticated"

@niekbruins
Copy link

niekbruins commented Sep 30, 2021

I had the same issue and i found this workaround:

def get_current_user(
    current_user: AccessUser = Depends(auth.claim(AccessUser)),
):
    return current_user


def get_read_scopes(
    scopes=Depends(auth.scope(["admin:read"])),
):
    return scopes

And then in the route

 @r.get(
    "/test",
    responses={404: {"description": "Not found"}},
    dependencies=[Depends(get_read_scopes)],
)
def get_contact_moment(
    current_user: AccessUser = Depends(get_current_user),
):

and then use app.dependency_overrides

@tokusumi tokusumi added answered This issue is answered. wait for issuer's response documentation Improvements or additions to documentation labels Jan 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
answered This issue is answered. wait for issuer's response documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

3 participants