This repository has been archived by the owner on Oct 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(dependencies): handle cases where the token isn't JWT compliant (#…
…123) * fix(dependencies): catch the cases where the token isn't JWT * test(dependencies): add test cases for dependencies
- Loading branch information
Showing
2 changed files
with
35 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import pytest | ||
from fastapi import HTTPException | ||
from fastapi.security import SecurityScopes | ||
|
||
from app.api.dependencies import get_token_payload | ||
from app.core.security import create_access_token | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("scopes", "token", "expires_minutes", "error_code", "expected_payload"), | ||
[ | ||
(["admin"], "", None, 406, None), | ||
(["admin"], {"user_id": "123", "scopes": ["admin"]}, None, 422, None), | ||
(["admin"], {"sub": "123", "scopes": ["admin"]}, -1, 401, None), | ||
(["admin"], {"sub": "123", "scopes": ["admin"]}, None, None, {"user_id": 123, "scopes": ["admin"]}), | ||
(["admin"], {"sub": "123", "scopes": ["user"]}, None, 403, None), | ||
], | ||
) | ||
@pytest.mark.asyncio() | ||
async def test_get_token_payload(scopes, token, expires_minutes, error_code, expected_payload): | ||
_token = await create_access_token(token, expires_minutes) if isinstance(token, dict) else token | ||
if isinstance(error_code, int): | ||
with pytest.raises(HTTPException): | ||
await get_token_payload(SecurityScopes(scopes), _token) | ||
else: | ||
payload = await get_token_payload(SecurityScopes(scopes), _token) | ||
if expected_payload is not None: | ||
assert payload.model_dump() == expected_payload |