@@ -49,6 +49,19 @@ def default_access_token():
4949 'fresh' : True ,
5050 }
5151
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+
5265@pytest .fixture (scope = 'function' )
5366def encoded_token (default_access_token ):
5467 return jwt .encode (default_access_token ,'secret-key' ,algorithm = 'HS256' ).decode ('utf-8' )
@@ -111,23 +124,23 @@ def get_settings_two():
111124 assert response .status_code == 200
112125 assert response .json () == {'hello' :'world' }
113126
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 ):
115128 response = client .get ('/raw_token' ,headers = {"Authorization" :f"Bearer { encoded_token } " })
116129 assert response .status_code == 200
117130 assert response .json () == default_access_token
118131
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 ):
120133 assert Authorize .get_raw_jwt (encoded_token ) == default_access_token
121134
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 ):
123136 assert Authorize .get_jti (encoded_token = encoded_token ) == default_access_token ['jti' ]
124137
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 ):
126139 response = client .get ('/get_subject' ,headers = {"Authorization" :f"Bearer { encoded_token } " })
127140 assert response .status_code == 200
128141 assert response .json () == default_access_token ['sub' ]
129142
130- def test_invalid_jwt_issuer (client ,Authorize ):
143+ def test_invalid_jwt_issuer (client ,Authorize , test_settings ):
131144 # No issuer claim expected or provided - OK
132145 token = Authorize .create_access_token (subject = 'test' )
133146 response = client .get ('/protected' ,headers = {'Authorization' :f"Bearer { token } " })
@@ -154,7 +167,7 @@ def test_invalid_jwt_issuer(client,Authorize):
154167 AuthJWT ._encode_issuer = None
155168
156169@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 ):
158171 AuthJWT ._decode_audience = ['foo' ,'bar' ]
159172
160173 access_token = Authorize .create_access_token (subject = 1 ,audience = token_aud )
@@ -171,7 +184,7 @@ def test_valid_aud(client,Authorize,token_aud):
171184 AuthJWT ._decode_audience = None
172185
173186@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 ):
175188 AuthJWT ._decode_audience = 'foo'
176189
177190 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):
187200 if token_aud == ['bar' ,'baz' ]:
188201 AuthJWT ._decode_audience = None
189202
190- def test_invalid_decode_algorithms (client ,Authorize ):
203+ def test_invalid_decode_algorithms (client ,Authorize , test_settings ):
191204 class SettingsAlgorithms (BaseSettings ):
192205 authjwt_secret_key : str = "secret"
193206 authjwt_decode_algorithms : list = ['HS384' ,'RS256' ]
@@ -203,7 +216,7 @@ def get_settings_algorithms():
203216
204217 AuthJWT ._decode_algorithms = None
205218
206- def test_valid_asymmetric_algorithms (client ,Authorize ):
219+ def test_valid_asymmetric_algorithms (client ,Authorize , test_settings ):
207220 hs256_token = Authorize .create_access_token (subject = 1 )
208221
209222 DIR = os .path .abspath (os .path .dirname (__file__ ))
@@ -236,7 +249,7 @@ def get_settings_asymmetric():
236249 assert response .status_code == 200
237250 assert response .json () == {'hello' :'world' }
238251
239- def test_invalid_asymmetric_algorithms (client ,Authorize ):
252+ def test_invalid_asymmetric_algorithms (client ,Authorize , test_settings ):
240253 class SettingsAsymmetricOne (BaseSettings ):
241254 authjwt_algorithm : str = "RS256"
242255
0 commit comments