Skip to content

Commit

Permalink
Add test for cusotm auth0user model
Browse files Browse the repository at this point in the history
  • Loading branch information
dorinclisu committed Mar 2, 2021
1 parent be901a1 commit 66578c4
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion tests/test_auth.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import base64
import json
import os
from typing import Dict
from typing import Dict, Optional

import pytest
import requests
from fastapi import FastAPI, Depends, Security
from fastapi.testclient import TestClient
from pydantic import Field

#from fastapi_auth0 import Auth0, Auth0User, security_responses
from src.fastapi_auth0 import Auth0, Auth0User, security_responses
Expand All @@ -29,9 +30,13 @@

auth0_test_permission = os.getenv('AUTH0_TEST_PERMISSION', '')

###############################################################################
class CustomAuth0User(Auth0User):
grant_type: Optional[str] = Field(None, alias='gty')

###############################################################################
auth = Auth0(domain=auth0_domain, api_audience=auth0_api_audience)
auth_custom = Auth0(domain=auth0_domain, api_audience=auth0_api_audience, auth0user_model=CustomAuth0User)
app = FastAPI()

@app.get('/public')
Expand All @@ -58,6 +63,9 @@ def get_also_secure_2():
def get_secure_scoped(user: Auth0User = Security(auth.get_user, scopes=[auth0_test_permission])):
return user

@app.get('/secure-custom-user')
def get_secure_custom_user(user: CustomAuth0User = Security(auth_custom.get_user)):
return user

###############################################################################
client = TestClient(app)
Expand Down Expand Up @@ -133,6 +141,11 @@ def test_m2m_app():
resp = client.get('/secure-scoped', headers=get_bearer_header(access_token))
assert resp.status_code == 200, resp.text

resp = client.get('/secure-custom-user', headers=get_bearer_header(access_token))
assert resp.status_code == 200, resp.text
user = CustomAuth0User(**resp.json())
assert user.grant_type in ['client-credentials', 'client_credentials']


def test_spa_app_noscope():
resp = requests.post(
Expand Down

0 comments on commit 66578c4

Please sign in to comment.