1
1
import jwt
2
2
import pytest
3
- from fastapi import Depends , FastAPI , Request
4
- from fastapi .responses import JSONResponse
3
+ from fastapi import Depends , FastAPI
5
4
from fastapi .testclient import TestClient
6
5
from pydantic import BaseSettings
7
6
8
7
from fastapi_jwt_auth import AuthJWT
9
- from fastapi_jwt_auth .exceptions import AuthJWTException
10
8
11
9
12
10
@pytest .fixture (scope = "function" )
13
11
def client () -> TestClient :
14
12
app = FastAPI ()
15
13
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
14
@app .get ("/protected" )
23
15
def protected (Authorize : AuthJWT = Depends ()):
24
16
Authorize .jwt_required ()
@@ -51,7 +43,10 @@ def test_config():
51
43
52
44
# Checking that created token has custom type claim
53
45
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
+ )
55
50
56
51
# Checking that protected endpoint validates token correctly
57
52
response = client .get ("/protected" , headers = {"Authorization" : f"Bearer { access } " })
@@ -60,22 +55,26 @@ def test_config():
60
55
61
56
# Checking that endpoint with optional protection validates token with
62
57
# 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
+ )
64
61
assert response .status_code == 200
65
62
assert response .json () == {"hello" : "world" }
66
63
67
- # Creating refresh token and checking if it has correct
64
+ # Creating refresh token and checking if it has correct
68
65
# type claim.
69
66
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
+ )
71
71
72
72
# Checking that refreshing with custom claim works.
73
73
response = client .get ("/refresh" , headers = {"Authorization" : f"Bearer { refresh } " })
74
74
assert response .status_code == 200
75
75
assert response .json () == {"hello" : "world" }
76
76
77
77
78
-
79
78
def test_custom_token_type_names_validation (
80
79
client : TestClient , Authorize : AuthJWT
81
80
) -> None :
@@ -88,33 +87,39 @@ class TestConfig(BaseSettings):
88
87
def test_config ():
89
88
return TestConfig ()
90
89
91
- # Creating access token and checking that
90
+ # Creating access token and checking that
92
91
# it has custom type
93
92
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
+ )
95
97
96
98
# Checking that validation for custom type works as expected.
97
99
response = client .get ("/protected" , headers = {"Authorization" : f"Bearer { access } " })
98
100
assert response .status_code == 200
99
101
assert response .json () == {"hello" : "world" }
100
102
101
- response = client .get ("/semi_protected" , headers = {"Authorization" : f"Bearer { access } " })
103
+ response = client .get (
104
+ "/semi_protected" , headers = {"Authorization" : f"Bearer { access } " }
105
+ )
102
106
assert response .status_code == 200
103
107
assert response .json () == {"hello" : "world" }
104
108
105
109
# Creating refresh token and checking if it has correct type claim.
106
110
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
+ )
108
115
109
116
# Checking that refreshing with custom type works.
110
117
response = client .get ("/refresh" , headers = {"Authorization" : f"Bearer { refresh } " })
111
118
assert response .status_code == 200
112
119
assert response .json () == {"hello" : "world" }
113
120
114
121
115
- def test_without_type_claims (
116
- client : TestClient , Authorize : AuthJWT
117
- ) -> None :
122
+ def test_without_type_claims (client : TestClient , Authorize : AuthJWT ) -> None :
118
123
class TestConfig (BaseSettings ):
119
124
authjwt_secret_key : str = "secret"
120
125
authjwt_token_type_claim : bool = False
@@ -125,19 +130,21 @@ def test_config():
125
130
126
131
# Creating access token and checking if it doesn't have type claim.
127
132
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" ])
129
134
130
135
response = client .get ("/protected" , headers = {"Authorization" : f"Bearer { access } " })
131
136
assert response .status_code == 200
132
137
assert response .json () == {"hello" : "world" }
133
138
134
- response = client .get ("/semi_protected" , headers = {"Authorization" : f"Bearer { access } " })
139
+ response = client .get (
140
+ "/semi_protected" , headers = {"Authorization" : f"Bearer { access } " }
141
+ )
135
142
assert response .status_code == 200
136
143
assert response .json () == {"hello" : "world" }
137
144
138
145
# Creating refresh token and checking if it doesn't have type claim.
139
146
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" ])
141
148
142
149
# Checking that refreshing without type works.
143
150
response = client .get ("/refresh" , headers = {"Authorization" : f"Bearer { refresh } " })
0 commit comments