@@ -49,6 +49,19 @@ def default_access_token():
49
49
'fresh' : True ,
50
50
}
51
51
52
+ @pytest .fixture ()
53
+ def test_settings () -> None :
54
+ class TestSettings (BaseSettings ):
55
+ AUTHJWT_SECRET_KEY : str = "secret-key"
56
+ AUTHJWT_ACCESS_TOKEN_EXPIRES : int = 1
57
+ AUTHJWT_REFRESH_TOKEN_EXPIRES : int = 1
58
+ AUTHJWT_DECODE_LEEWAY : int = 2
59
+
60
+ @AuthJWT .load_config
61
+ def load ():
62
+ return TestSettings ()
63
+
64
+
52
65
@pytest .fixture (scope = 'function' )
53
66
def encoded_token (default_access_token ):
54
67
return jwt .encode (default_access_token ,'secret-key' ,algorithm = 'HS256' ).decode ('utf-8' )
@@ -111,23 +124,23 @@ def get_settings_two():
111
124
assert response .status_code == 200
112
125
assert response .json () == {'hello' :'world' }
113
126
114
- def test_get_raw_token (client ,default_access_token ,encoded_token ):
127
+ def test_get_raw_token (client ,default_access_token ,encoded_token , test_settings ):
115
128
response = client .get ('/raw_token' ,headers = {"Authorization" :f"Bearer { encoded_token } " })
116
129
assert response .status_code == 200
117
130
assert response .json () == default_access_token
118
131
119
- def test_get_raw_jwt (default_access_token ,encoded_token ,Authorize ):
132
+ def test_get_raw_jwt (default_access_token ,encoded_token ,Authorize , test_settings ):
120
133
assert Authorize .get_raw_jwt (encoded_token ) == default_access_token
121
134
122
- def test_get_jwt_jti (client ,default_access_token ,encoded_token ,Authorize ):
135
+ def test_get_jwt_jti (client ,default_access_token ,encoded_token ,Authorize , test_settings ):
123
136
assert Authorize .get_jti (encoded_token = encoded_token ) == default_access_token ['jti' ]
124
137
125
- def test_get_jwt_subject (client ,default_access_token ,encoded_token ):
138
+ def test_get_jwt_subject (client ,default_access_token ,encoded_token , test_settings ):
126
139
response = client .get ('/get_subject' ,headers = {"Authorization" :f"Bearer { encoded_token } " })
127
140
assert response .status_code == 200
128
141
assert response .json () == default_access_token ['sub' ]
129
142
130
- def test_invalid_jwt_issuer (client ,Authorize ):
143
+ def test_invalid_jwt_issuer (client ,Authorize , test_settings ):
131
144
# No issuer claim expected or provided - OK
132
145
token = Authorize .create_access_token (subject = 'test' )
133
146
response = client .get ('/protected' ,headers = {'Authorization' :f"Bearer { token } " })
@@ -154,7 +167,7 @@ def test_invalid_jwt_issuer(client,Authorize):
154
167
AuthJWT ._encode_issuer = None
155
168
156
169
@pytest .mark .parametrize ("token_aud" ,['foo' , ['bar' ], ['foo' , 'bar' , 'baz' ]])
157
- def test_valid_aud (client ,Authorize ,token_aud ):
170
+ def test_valid_aud (client ,Authorize ,token_aud , test_settings ):
158
171
AuthJWT ._decode_audience = ['foo' ,'bar' ]
159
172
160
173
access_token = Authorize .create_access_token (subject = 1 ,audience = token_aud )
@@ -171,7 +184,7 @@ def test_valid_aud(client,Authorize,token_aud):
171
184
AuthJWT ._decode_audience = None
172
185
173
186
@pytest .mark .parametrize ("token_aud" ,['bar' , ['bar' ], ['bar' , 'baz' ]])
174
- def test_invalid_aud_and_missing_aud (client ,Authorize ,token_aud ):
187
+ def test_invalid_aud_and_missing_aud (client ,Authorize ,token_aud , test_settings ):
175
188
AuthJWT ._decode_audience = 'foo'
176
189
177
190
access_token = Authorize .create_access_token (subject = 1 ,audience = token_aud )
@@ -187,7 +200,7 @@ def test_invalid_aud_and_missing_aud(client,Authorize,token_aud):
187
200
if token_aud == ['bar' ,'baz' ]:
188
201
AuthJWT ._decode_audience = None
189
202
190
- def test_invalid_decode_algorithms (client ,Authorize ):
203
+ def test_invalid_decode_algorithms (client ,Authorize , test_settings ):
191
204
class SettingsAlgorithms (BaseSettings ):
192
205
authjwt_secret_key : str = "secret"
193
206
authjwt_decode_algorithms : list = ['HS384' ,'RS256' ]
@@ -203,7 +216,7 @@ def get_settings_algorithms():
203
216
204
217
AuthJWT ._decode_algorithms = None
205
218
206
- def test_valid_asymmetric_algorithms (client ,Authorize ):
219
+ def test_valid_asymmetric_algorithms (client ,Authorize , test_settings ):
207
220
hs256_token = Authorize .create_access_token (subject = 1 )
208
221
209
222
DIR = os .path .abspath (os .path .dirname (__file__ ))
@@ -236,7 +249,7 @@ def get_settings_asymmetric():
236
249
assert response .status_code == 200
237
250
assert response .json () == {'hello' :'world' }
238
251
239
- def test_invalid_asymmetric_algorithms (client ,Authorize ):
252
+ def test_invalid_asymmetric_algorithms (client ,Authorize , test_settings ):
240
253
class SettingsAsymmetricOne (BaseSettings ):
241
254
authjwt_algorithm : str = "RS256"
242
255
0 commit comments