11import jwt
22import pytest
3- from fastapi import Depends , FastAPI , Request
4- from fastapi .responses import JSONResponse
3+ from fastapi import Depends , FastAPI
54from fastapi .testclient import TestClient
65from pydantic import BaseSettings
76
87from fastapi_jwt_auth import AuthJWT
9- from fastapi_jwt_auth .exceptions import AuthJWTException
108
119
1210@pytest .fixture (scope = "function" )
1311def client () -> TestClient :
1412 app = FastAPI ()
1513
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-
2214 @app .get ("/protected" )
2315 def protected (Authorize : AuthJWT = Depends ()):
2416 Authorize .jwt_required ()
@@ -51,7 +43,10 @@ def test_config():
5143
5244 # Checking that created token has custom type claim
5345 access = Authorize .create_access_token (subject = "test" )
54- assert jwt .decode (access , key = "secret" , algorithms = ['HS256' ])["custom_type" ] == "access"
46+ assert (
47+ jwt .decode (access , key = "secret" , algorithms = ["HS256" ])["custom_type" ]
48+ == "access"
49+ )
5550
5651 # Checking that protected endpoint validates token correctly
5752 response = client .get ("/protected" , headers = {"Authorization" : f"Bearer { access } " })
@@ -60,22 +55,26 @@ def test_config():
6055
6156 # Checking that endpoint with optional protection validates token with
6257 # custom type claim correctly.
63- response = client .get ("/semi_protected" , headers = {"Authorization" : f"Bearer { access } " })
58+ response = client .get (
59+ "/semi_protected" , headers = {"Authorization" : f"Bearer { access } " }
60+ )
6461 assert response .status_code == 200
6562 assert response .json () == {"hello" : "world" }
6663
67- # Creating refresh token and checking if it has correct
64+ # Creating refresh token and checking if it has correct
6865 # type claim.
6966 refresh = Authorize .create_refresh_token (subject = "test" )
70- assert jwt .decode (refresh , key = "secret" , algorithms = ['HS256' ])["custom_type" ] == "refresh"
67+ assert (
68+ jwt .decode (refresh , key = "secret" , algorithms = ["HS256" ])["custom_type" ]
69+ == "refresh"
70+ )
7171
7272 # Checking that refreshing with custom claim works.
7373 response = client .get ("/refresh" , headers = {"Authorization" : f"Bearer { refresh } " })
7474 assert response .status_code == 200
7575 assert response .json () == {"hello" : "world" }
7676
7777
78-
7978def test_custom_token_type_names_validation (
8079 client : TestClient , Authorize : AuthJWT
8180) -> None :
@@ -88,33 +87,39 @@ class TestConfig(BaseSettings):
8887 def test_config ():
8988 return TestConfig ()
9089
91- # Creating access token and checking that
90+ # Creating access token and checking that
9291 # it has custom type
9392 access = Authorize .create_access_token (subject = "test" )
94- assert jwt .decode (access , key = "secret" , algorithms = ['HS256' ])["type" ] == "access_custom"
93+ assert (
94+ jwt .decode (access , key = "secret" , algorithms = ["HS256" ])["type" ]
95+ == "access_custom"
96+ )
9597
9698 # Checking that validation for custom type works as expected.
9799 response = client .get ("/protected" , headers = {"Authorization" : f"Bearer { access } " })
98100 assert response .status_code == 200
99101 assert response .json () == {"hello" : "world" }
100102
101- response = client .get ("/semi_protected" , headers = {"Authorization" : f"Bearer { access } " })
103+ response = client .get (
104+ "/semi_protected" , headers = {"Authorization" : f"Bearer { access } " }
105+ )
102106 assert response .status_code == 200
103107 assert response .json () == {"hello" : "world" }
104108
105109 # Creating refresh token and checking if it has correct type claim.
106110 refresh = Authorize .create_refresh_token (subject = "test" )
107- assert jwt .decode (refresh , key = "secret" , algorithms = ['HS256' ])["type" ] == "refresh_custom"
111+ assert (
112+ jwt .decode (refresh , key = "secret" , algorithms = ["HS256" ])["type" ]
113+ == "refresh_custom"
114+ )
108115
109116 # Checking that refreshing with custom type works.
110117 response = client .get ("/refresh" , headers = {"Authorization" : f"Bearer { refresh } " })
111118 assert response .status_code == 200
112119 assert response .json () == {"hello" : "world" }
113120
114121
115- def test_without_type_claims (
116- client : TestClient , Authorize : AuthJWT
117- ) -> None :
122+ def test_without_type_claims (client : TestClient , Authorize : AuthJWT ) -> None :
118123 class TestConfig (BaseSettings ):
119124 authjwt_secret_key : str = "secret"
120125 authjwt_token_type_claim : bool = False
@@ -125,19 +130,21 @@ def test_config():
125130
126131 # Creating access token and checking if it doesn't have type claim.
127132 access = Authorize .create_access_token (subject = "test" )
128- assert "type" not in jwt .decode (access , key = "secret" , algorithms = [' HS256' ])
133+ assert "type" not in jwt .decode (access , key = "secret" , algorithms = [" HS256" ])
129134
130135 response = client .get ("/protected" , headers = {"Authorization" : f"Bearer { access } " })
131136 assert response .status_code == 200
132137 assert response .json () == {"hello" : "world" }
133138
134- response = client .get ("/semi_protected" , headers = {"Authorization" : f"Bearer { access } " })
139+ response = client .get (
140+ "/semi_protected" , headers = {"Authorization" : f"Bearer { access } " }
141+ )
135142 assert response .status_code == 200
136143 assert response .json () == {"hello" : "world" }
137144
138145 # Creating refresh token and checking if it doesn't have type claim.
139146 refresh = Authorize .create_refresh_token (subject = "test" )
140- assert "type" not in jwt .decode (refresh , key = "secret" , algorithms = [' HS256' ])
147+ assert "type" not in jwt .decode (refresh , key = "secret" , algorithms = [" HS256" ])
141148
142149 # Checking that refreshing without type works.
143150 response = client .get ("/refresh" , headers = {"Authorization" : f"Bearer { refresh } " })
0 commit comments