44
55import jwt
66import pytest
7+ from jwt import PyJWS , __version__ as jwt_version , algorithms
78
8- from jwt import PyJWS
9- from jwt import __version__ as jwt_version
10- from jwt import algorithms
119from ninja_jwt .backends import JWK_CLIENT_AVAILABLE , TokenBackend
1210from ninja_jwt .exceptions import TokenBackendError
1311from ninja_jwt .utils import aware_utcnow , datetime_to_epoch , make_utc
@@ -53,7 +51,6 @@ def setUp(self):
5351 )
5452 self .payload = {"foo" : "bar" }
5553
56-
5754 def test_init (self ):
5855 # Should reject unknown algorithms
5956 with pytest .raises (TokenBackendError ):
@@ -65,8 +62,8 @@ def test_init(self):
6562 def test_init_fails_for_rs_algorithms_when_crypto_not_installed (self ):
6663 for algo in ("RS256" , "RS384" , "RS512" , "ES256" ):
6764 with pytest .raises (
68- TokenBackendError ,
69- match = rf"You must have cryptography installed to use { algo } ." ,
65+ TokenBackendError ,
66+ match = rf"You must have cryptography installed to use { algo } ." ,
7067 ):
7168 TokenBackend (algo , "not_secret" )
7269
@@ -122,7 +119,10 @@ def test_encode_aud_iss(self):
122119
123120 @pytest .mark .parametrize (
124121 "title, backend" ,
125- [(f"Test decode with no expiry for { backend .algorithm } " , backend )for backend in backends ]
122+ [
123+ (f"Test decode with no expiry for { backend .algorithm } " , backend )
124+ for backend in backends
125+ ],
126126 )
127127 def test_decode_with_no_expiry (self , title , backend ):
128128 # for backend in self.backends:
@@ -134,19 +134,26 @@ def test_decode_with_no_expiry(self, title, backend):
134134
135135 @pytest .mark .parametrize (
136136 "title, backend" ,
137- [(f"Test decode with no expiry and no verify for { backend .algorithm } " , backend )for backend in backends ]
137+ [
138+ (
139+ f"Test decode with no expiry and no verify for { backend .algorithm } " ,
140+ backend ,
141+ )
142+ for backend in backends
143+ ],
138144 )
139145 def test_decode_with_no_expiry_no_verify (self , title , backend ):
140146 no_exp_token = jwt .encode (
141147 self .payload , backend .signing_key , algorithm = backend .algorithm
142148 )
143- assert (
144- backend .decode (no_exp_token , verify = False ) == self .payload
145- )
149+ assert backend .decode (no_exp_token , verify = False ) == self .payload
146150
147151 @pytest .mark .parametrize (
148152 "title, backend" ,
149- [(f"Test decode with expiry for { backend .algorithm } " , backend ) for backend in backends ]
153+ [
154+ (f"Test decode with expiry for { backend .algorithm } " , backend )
155+ for backend in backends
156+ ],
150157 )
151158 def test_decode_with_expiry (self , title , backend ):
152159 self .payload ["exp" ] = aware_utcnow () - timedelta (seconds = 1 )
@@ -158,7 +165,10 @@ def test_decode_with_expiry(self, title, backend):
158165
159166 @pytest .mark .parametrize (
160167 "title, backend" ,
161- [(f"Test decode with invalid sig for { backend .algorithm } " , backend ) for backend in backends ]
168+ [
169+ (f"Test decode with invalid sig for { backend .algorithm } " , backend )
170+ for backend in backends
171+ ],
162172 )
163173 def test_decode_with_invalid_sig (self , title , backend ):
164174 self .payload ["exp" ] = aware_utcnow () - timedelta (seconds = 1 )
@@ -167,13 +177,9 @@ def test_decode_with_invalid_sig(self, title, backend):
167177 # with self.subTest(f"Test decode with invalid sig for {backend.algorithm}"):
168178 payload = self .payload .copy ()
169179 payload ["exp" ] = aware_utcnow () + timedelta (days = 1 )
170- token_1 = jwt .encode (
171- payload , backend .signing_key , algorithm = backend .algorithm
172- )
180+ token_1 = jwt .encode (payload , backend .signing_key , algorithm = backend .algorithm )
173181 payload ["foo" ] = "baz"
174- token_2 = jwt .encode (
175- payload , backend .signing_key , algorithm = backend .algorithm
176- )
182+ token_2 = jwt .encode (payload , backend .signing_key , algorithm = backend .algorithm )
177183
178184 if IS_OLD_JWT :
179185 token_1 = token_1 .decode ("utf-8" )
@@ -188,19 +194,18 @@ def test_decode_with_invalid_sig(self, title, backend):
188194
189195 @pytest .mark .parametrize (
190196 "title, backend" ,
191- [(f"Test decode with invalid sig for { backend .algorithm } " , backend ) for backend in backends ]
197+ [
198+ (f"Test decode with invalid sig for { backend .algorithm } " , backend )
199+ for backend in backends
200+ ],
192201 )
193202 def test_decode_with_invalid_sig_no_verify (self , title , backend ):
194203 self .payload ["exp" ] = aware_utcnow () + timedelta (days = 1 )
195204
196205 payload = self .payload .copy ()
197- token_1 = jwt .encode (
198- payload , backend .signing_key , algorithm = backend .algorithm
199- )
206+ token_1 = jwt .encode (payload , backend .signing_key , algorithm = backend .algorithm )
200207 payload ["foo" ] = "baz"
201- token_2 = jwt .encode (
202- payload , backend .signing_key , algorithm = backend .algorithm
203- )
208+ token_2 = jwt .encode (payload , backend .signing_key , algorithm = backend .algorithm )
204209 if IS_OLD_JWT :
205210 token_1 = token_1 .decode ("utf-8" )
206211 token_2 = token_2 .decode ("utf-8" )
@@ -212,13 +217,14 @@ def test_decode_with_invalid_sig_no_verify(self, title, backend):
212217 token_1_sig = token_1 .rsplit ("." , 1 )[- 1 ]
213218 invalid_token = token_2_payload + "." + token_1_sig
214219
215- assert (
216- backend .decode (invalid_token , verify = False ) == payload
217- )
220+ assert backend .decode (invalid_token , verify = False ) == payload
218221
219222 @pytest .mark .parametrize (
220223 "title, backend" ,
221- [(f"est decode success for { backend .algorithm } " , backend ) for backend in backends ]
224+ [
225+ (f"est decode success for { backend .algorithm } " , backend )
226+ for backend in backends
227+ ],
222228 )
223229 def test_decode_success (self , title , backend ):
224230 self .payload ["exp" ] = aware_utcnow () + timedelta (days = 1 )
0 commit comments