Closed u-iandono closed 2 years ago
Hi, I figured out how to make this work.
Basically, instead of using msal_auth.scheme
directly as the dependency, you can create a custom function dependency that uses msal_auth.scheme
as the dependency. And then you can use this function as your dependency.
Here's my dependency.py file.
msal_client_config: MSALClientConfig = MSALClientConfig()
...
msal_auth = MSALAuthorization(client_config=msal_client_config, return_to_path=settings.base_url)
def authenticated_user(current_user: UserInfo = Depends(msal_auth.scheme)) -> UserInfo:
return current_user
And here is my users.py that uses the authenticated_user
function as the dependency.
@router.get('/me', response_model=UserInfo, response_model_exclude_none=True, response_model_by_alias=False)
async def get_user_me(current_user: UserInfo = Depends(authenticated_user)) -> UserInfo:
return current_user
There is not much difference in the conftest.py, only some adjustments in how to create the UserInfo object.
def override_authenticated_user() -> UserInfo:
return UserInfo(
name='Test User',
oid='11111111-1111-1111-1111-111111111111',
preferred_username='test_user@email.com',
)
@pytest.fixture(scope='module')
def client() -> Generator:
with TestClient(app) as test_client:
app.dependency_overrides[authenticated_user] = override_authenticated_user
yield test_client
First of all, thank you so much for making this great integration. You make it so easy to use AzureAD with FastAPI!
And there is a stale discussion about this topic here: https://github.com/dudil/fastapi_msal/issues/4
Since it's already closed, I thought I would create a new issue with more details because I think I'm encountering the same issue.
So, I'd like to write unit testing for the /user/me endpoint. But since it has a dependency on the msal_auth.scheme, I need to bypass the authentication process.
I've tried to follow the tutorial about overriding dependencies from the FastAPI tutorial here. But it seems to be not working as the current_user still returns
{'detail': 'Not authenticated'}
This is how I override the dependencies
If you can provide an example of how to bypass, silently override, or bypass the authentication process, that would be really helpful!