|
| 1 | +import jwt |
| 2 | +import pytest |
| 3 | +from fastapi import Depends, FastAPI, Request |
| 4 | +from fastapi.responses import JSONResponse |
| 5 | +from fastapi.testclient import TestClient |
| 6 | +from pydantic import BaseSettings |
| 7 | + |
| 8 | +from fastapi_jwt_auth import AuthJWT |
| 9 | +from fastapi_jwt_auth.exceptions import AuthJWTException |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture(scope="function") |
| 13 | +def client() -> TestClient: |
| 14 | + app = FastAPI() |
| 15 | + |
| 16 | + @app.exception_handler(AuthJWTException) |
| 17 | + def authjwt_exception_handler(request: Request, exc: AuthJWTException): |
| 18 | + return JSONResponse( |
| 19 | + status_code=exc.status_code, content={"detail": exc.message} |
| 20 | + ) |
| 21 | + |
| 22 | + @app.get("/protected") |
| 23 | + def protected(Authorize: AuthJWT = Depends()): |
| 24 | + Authorize.jwt_required() |
| 25 | + return {"hello": "world"} |
| 26 | + |
| 27 | + @app.get("/semi_protected") |
| 28 | + def protected(Authorize: AuthJWT = Depends()): |
| 29 | + Authorize.jwt_optional() |
| 30 | + return {"hello": "world"} |
| 31 | + |
| 32 | + @app.get("/refresh") |
| 33 | + def refresher(Authorize: AuthJWT = Depends()): |
| 34 | + Authorize.jwt_refresh_token_required() |
| 35 | + return {"hello": "world"} |
| 36 | + |
| 37 | + client = TestClient(app) |
| 38 | + return client |
| 39 | + |
| 40 | + |
| 41 | +def test_custom_token_type_claim_validation( |
| 42 | + client: TestClient, Authorize: AuthJWT |
| 43 | +) -> None: |
| 44 | + class TestConfig(BaseSettings): |
| 45 | + authjwt_secret_key: str = "secret" |
| 46 | + authjwt_token_type_claim_name: str = "custom_type" |
| 47 | + |
| 48 | + @AuthJWT.load_config |
| 49 | + def test_config(): |
| 50 | + return TestConfig() |
| 51 | + |
| 52 | + # Checking that created token has custom type claim |
| 53 | + access = Authorize.create_access_token(subject="test") |
| 54 | + assert jwt.decode(access, key="secret", algorithms=['HS256'])["custom_type"] == "access" |
| 55 | + |
| 56 | + # Checking that protected endpoint validates token correctly |
| 57 | + response = client.get("/protected", headers={"Authorization": f"Bearer {access}"}) |
| 58 | + assert response.status_code == 200 |
| 59 | + assert response.json() == {"hello": "world"} |
| 60 | + |
| 61 | + # Checking that endpoint with optional protection validates token with |
| 62 | + # custom type claim correctly. |
| 63 | + response = client.get("/semi_protected", headers={"Authorization": f"Bearer {access}"}) |
| 64 | + assert response.status_code == 200 |
| 65 | + assert response.json() == {"hello": "world"} |
| 66 | + |
| 67 | + # Creating refresh token and checking if it has correct |
| 68 | + # type claim. |
| 69 | + refresh = Authorize.create_refresh_token(subject="test") |
| 70 | + assert jwt.decode(refresh, key="secret", algorithms=['HS256'])["custom_type"] == "refresh" |
| 71 | + |
| 72 | + # Checking that refreshing with custom claim works. |
| 73 | + response = client.get("/refresh", headers={"Authorization": f"Bearer {refresh}"}) |
| 74 | + assert response.status_code == 200 |
| 75 | + assert response.json() == {"hello": "world"} |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | +def test_custom_token_type_names_validation( |
| 80 | + client: TestClient, Authorize: AuthJWT |
| 81 | +) -> None: |
| 82 | + class TestConfig(BaseSettings): |
| 83 | + authjwt_secret_key: str = "secret" |
| 84 | + authjwt_refresh_token_type: str = "refresh_custom" |
| 85 | + authjwt_access_token_type: str = "access_custom" |
| 86 | + |
| 87 | + @AuthJWT.load_config |
| 88 | + def test_config(): |
| 89 | + return TestConfig() |
| 90 | + |
| 91 | + # Creating access token and checking that |
| 92 | + # it has custom type |
| 93 | + access = Authorize.create_access_token(subject="test") |
| 94 | + assert jwt.decode(access, key="secret", algorithms=['HS256'])["type"] == "access_custom" |
| 95 | + |
| 96 | + # Checking that validation for custom type works as expected. |
| 97 | + response = client.get("/protected", headers={"Authorization": f"Bearer {access}"}) |
| 98 | + assert response.status_code == 200 |
| 99 | + assert response.json() == {"hello": "world"} |
| 100 | + |
| 101 | + response = client.get("/semi_protected", headers={"Authorization": f"Bearer {access}"}) |
| 102 | + assert response.status_code == 200 |
| 103 | + assert response.json() == {"hello": "world"} |
| 104 | + |
| 105 | + # Creating refresh token and checking if it has correct type claim. |
| 106 | + refresh = Authorize.create_refresh_token(subject="test") |
| 107 | + assert jwt.decode(refresh, key="secret", algorithms=['HS256'])["type"] == "refresh_custom" |
| 108 | + |
| 109 | + # Checking that refreshing with custom type works. |
| 110 | + response = client.get("/refresh", headers={"Authorization": f"Bearer {refresh}"}) |
| 111 | + assert response.status_code == 200 |
| 112 | + assert response.json() == {"hello": "world"} |
| 113 | + |
| 114 | + |
| 115 | +def test_without_type_claims( |
| 116 | + client: TestClient, Authorize: AuthJWT |
| 117 | +) -> None: |
| 118 | + class TestConfig(BaseSettings): |
| 119 | + authjwt_secret_key: str = "secret" |
| 120 | + authjwt_token_type_claim: bool = False |
| 121 | + |
| 122 | + @AuthJWT.load_config |
| 123 | + def test_config(): |
| 124 | + return TestConfig() |
| 125 | + |
| 126 | + # Creating access token and checking if it doesn't have type claim. |
| 127 | + access = Authorize.create_access_token(subject="test") |
| 128 | + assert "type" not in jwt.decode(access, key="secret", algorithms=['HS256']) |
| 129 | + |
| 130 | + response = client.get("/protected", headers={"Authorization": f"Bearer {access}"}) |
| 131 | + assert response.status_code == 200 |
| 132 | + assert response.json() == {"hello": "world"} |
| 133 | + |
| 134 | + response = client.get("/semi_protected", headers={"Authorization": f"Bearer {access}"}) |
| 135 | + assert response.status_code == 200 |
| 136 | + assert response.json() == {"hello": "world"} |
| 137 | + |
| 138 | + # Creating refresh token and checking if it doesn't have type claim. |
| 139 | + refresh = Authorize.create_refresh_token(subject="test") |
| 140 | + assert "type" not in jwt.decode(refresh, key="secret", algorithms=['HS256']) |
| 141 | + |
| 142 | + # Checking that refreshing without type works. |
| 143 | + response = client.get("/refresh", headers={"Authorization": f"Bearer {refresh}"}) |
| 144 | + assert response.status_code == 200 |
| 145 | + assert response.json() == {"hello": "world"} |
0 commit comments