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
parent 7f5087c6db
commit 253d8e5a5f
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.")