test: add comprehensive unit tests for PassportService with exception handling optimization (#22268)

This commit is contained in:
Jason Young 2025-07-12 19:56:20 +08:00 • committed by GitHub
commit 253d8e5a5f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 209 additions and 2 deletions

View file

@ -14,9 +14,11 @@ class PassportService:
def verify(self, token):
try:
return jwt.decode(token, self.sk, algorithms=["HS256"])
except jwt.exceptions.ExpiredSignatureError:
raise Unauthorized("Token has expired.")
except jwt.exceptions.InvalidSignatureError:
raise Unauthorized("Invalid token signature.")
except jwt.exceptions.DecodeError:
raise Unauthorized("Invalid token.")
except jwt.exceptions.ExpiredSignatureError:
raise Unauthorized("Token has expired.")
except jwt.exceptions.PyJWTError: # Catch-all for other JWT errors
raise Unauthorized("Invalid token.")