@@ -25,7 +25,7 @@ def test_missing_credentials_with_secret_raises_401(self):
2525 with patch .dict (os .environ , {"AB_JWT_SIGNATURE_SECRET" : "test-secret" }):
2626 with pytest .raises (HTTPException ) as exc_info :
2727 verify_jwt_token (None )
28-
28+
2929 assert exc_info .value .status_code == 401
3030 assert exc_info .value .detail == "Bearer token required"
3131 assert exc_info .value .headers == {"WWW-Authenticate" : "Bearer" }
@@ -34,13 +34,12 @@ def test_invalid_token_raises_401(self):
3434 """Test that invalid JWT tokens raise 401."""
3535 with patch .dict (os .environ , {"AB_JWT_SIGNATURE_SECRET" : "test-secret" }):
3636 invalid_credentials = HTTPAuthorizationCredentials (
37- scheme = "Bearer" ,
38- credentials = "invalid.jwt.token"
37+ scheme = "Bearer" , credentials = "invalid.jwt.token"
3938 )
40-
39+
4140 with pytest .raises (HTTPException ) as exc_info :
4241 verify_jwt_token (invalid_credentials )
43-
42+
4443 assert exc_info .value .status_code == 401
4544 assert exc_info .value .detail == "Invalid token"
4645 assert exc_info .value .headers == {"WWW-Authenticate" : "Bearer" }
@@ -49,13 +48,12 @@ def test_malformed_token_raises_401(self):
4948 """Test that malformed tokens raise 401."""
5049 with patch .dict (os .environ , {"AB_JWT_SIGNATURE_SECRET" : "test-secret" }):
5150 malformed_credentials = HTTPAuthorizationCredentials (
52- scheme = "Bearer" ,
53- credentials = "not-a-jwt-token"
51+ scheme = "Bearer" , credentials = "not-a-jwt-token"
5452 )
55-
53+
5654 with pytest .raises (HTTPException ) as exc_info :
5755 verify_jwt_token (malformed_credentials )
58-
56+
5957 assert exc_info .value .status_code == 401
6058 assert exc_info .value .detail == "Invalid token"
6159
@@ -65,16 +63,15 @@ def test_valid_token_passes(self):
6563 payload = {
6664 "exp" : datetime .now (timezone .utc ) + timedelta (hours = 1 ),
6765 "iat" : datetime .now (timezone .utc ),
68- "sub" : "test-user"
66+ "sub" : "test-user" ,
6967 }
7068 valid_token = jwt .encode (payload , secret , algorithm = "HS256" )
71-
69+
7270 with patch .dict (os .environ , {"AB_JWT_SIGNATURE_SECRET" : secret }):
7371 valid_credentials = HTTPAuthorizationCredentials (
74- scheme = "Bearer" ,
75- credentials = valid_token
72+ scheme = "Bearer" , credentials = valid_token
7673 )
77-
74+
7875 # Should not raise any exception
7976 verify_jwt_token (valid_credentials )
8077
@@ -84,43 +81,41 @@ def test_expired_token_raises_401(self):
8481 expired_payload = {
8582 "exp" : datetime .now (timezone .utc ) - timedelta (hours = 1 ),
8683 "iat" : datetime .now (timezone .utc ) - timedelta (hours = 2 ),
87- "sub" : "test-user"
84+ "sub" : "test-user" ,
8885 }
8986 expired_token = jwt .encode (expired_payload , secret , algorithm = "HS256" )
90-
87+
9188 with patch .dict (os .environ , {"AB_JWT_SIGNATURE_SECRET" : secret }):
9289 expired_credentials = HTTPAuthorizationCredentials (
93- scheme = "Bearer" ,
94- credentials = expired_token
90+ scheme = "Bearer" , credentials = expired_token
9591 )
96-
92+
9793 with pytest .raises (HTTPException ) as exc_info :
9894 verify_jwt_token (expired_credentials )
99-
95+
10096 assert exc_info .value .status_code == 401
10197 assert exc_info .value .detail == "Invalid token"
10298
10399 def test_wrong_secret_raises_401 (self ):
104100 """Test that tokens signed with wrong secret raise 401."""
105101 correct_secret = "correct-secret"
106102 wrong_secret = "wrong-secret"
107-
103+
108104 payload = {
109105 "exp" : datetime .now (timezone .utc ) + timedelta (hours = 1 ),
110106 "iat" : datetime .now (timezone .utc ),
111- "sub" : "test-user"
107+ "sub" : "test-user" ,
112108 }
113109 token_with_wrong_secret = jwt .encode (payload , wrong_secret , algorithm = "HS256" )
114-
110+
115111 with patch .dict (os .environ , {"AB_JWT_SIGNATURE_SECRET" : correct_secret }):
116112 wrong_credentials = HTTPAuthorizationCredentials (
117- scheme = "Bearer" ,
118- credentials = token_with_wrong_secret
113+ scheme = "Bearer" , credentials = token_with_wrong_secret
119114 )
120-
115+
121116 with pytest .raises (HTTPException ) as exc_info :
122117 verify_jwt_token (wrong_credentials )
123-
118+
124119 assert exc_info .value .status_code == 401
125120 assert exc_info .value .detail == "Invalid token"
126121
@@ -136,12 +131,11 @@ def test_token_without_required_claims_passes(self):
136131 secret = "test-secret"
137132 minimal_payload = {"custom" : "data" } # No exp, iat, sub etc.
138133 minimal_token = jwt .encode (minimal_payload , secret , algorithm = "HS256" )
139-
134+
140135 with patch .dict (os .environ , {"AB_JWT_SIGNATURE_SECRET" : secret }):
141136 minimal_credentials = HTTPAuthorizationCredentials (
142- scheme = "Bearer" ,
143- credentials = minimal_token
137+ scheme = "Bearer" , credentials = minimal_token
144138 )
145-
139+
146140 # Should not raise any exception - we only verify signature
147- verify_jwt_token (minimal_credentials )
141+ verify_jwt_token (minimal_credentials )
0 commit comments