# Path: craft_ai/jwt_decode.py
# def jwt_decode(jwt):
#     if isinstance(jwt, str):
#         jwt = jwt.encode("utf-8")
# 
#     if not issubclass(type(jwt), bytes):
#         raise CraftAiTokenError(
#             "Invalid token type, the token must be a {0}".format(bytes)
#         )
# 
#     try:
#         signing_input, crypto_segment = jwt.strip().rsplit(b".", 1)
#         header_segment, payload_segment = signing_input.split(b".", 1)
#     except ValueError:
#         raise CraftAiTokenError("Not enough segments")
# 
#     try:
#         header_data = base64url_decode(header_segment)
#     except (TypeError, binascii.Error):
#         raise CraftAiTokenError("Invalid header padding")
# 
#     try:
#         header = json.loads(header_data.decode("utf-8"))
#     except ValueError as e:
#         raise CraftAiTokenError("Invalid header string '%s'" % e)
# 
#     try:
#         payload_data = base64url_decode(payload_segment)
#     except (TypeError, binascii.Error):
#         raise CraftAiTokenError("Unable to decode the payload segment of the token")
# 
#     try:
#         payload = json.loads(payload_data.decode("utf-8"))
#     except ValueError:
#         # Unable to load the payload as a json
#         payload = payload_data
# 
#     try:
#         signature = base64url_decode(crypto_segment)
#     except (TypeError, binascii.Error):
#         raise CraftAiTokenError("Unable to decode the crypto segment of the token")
# 
#     return (payload, signing_input, header, signature)
# 
# Path: craft_ai/errors.py
# class CraftAiTokenError(CraftAiError):
#     """An invalid token error occured."""


import unittest
from craft_ai.jwt_decode import jwt_decode
from craft_ai.errors import CraftAiTokenError


JWT_IO_EXAMPLE = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ"  # noqa: E501


class TestJwtDecode(unittest.TestCase):
    def test_decode_works(self):
        (payload, _, header, _) = jwt_decode(JWT_IO_EXAMPLE)
        self.assertEqual(header, {"alg": "HS256", "typ": "JWT"})
        self.assertEqual(
            payload, {"sub": "1234567890", "name": "John Doe", "admin": True}
        )

    def test_decode_works_with_spaces(self):

# Path: craft_ai/jwt_decode.py
# def jwt_decode(jwt):
#     if isinstance(jwt, str):
#         jwt = jwt.encode("utf-8")
# 
#     if not issubclass(type(jwt), bytes):
#         raise CraftAiTokenError(
#             "Invalid token type, the token must be a {0}".format(bytes)
#         )
# 
#     try:
#         signing_input, crypto_segment = jwt.strip().rsplit(b".", 1)
#         header_segment, payload_segment = signing_input.split(b".", 1)
#     except ValueError:
#         raise CraftAiTokenError("Not enough segments")
# 
#     try:
#         header_data = base64url_decode(header_segment)
#     except (TypeError, binascii.Error):
#         raise CraftAiTokenError("Invalid header padding")
# 
#     try:
#         header = json.loads(header_data.decode("utf-8"))
#     except ValueError as e:
#         raise CraftAiTokenError("Invalid header string '%s'" % e)
# 
#     try:
#         payload_data = base64url_decode(payload_segment)
#     except (TypeError, binascii.Error):
#         raise CraftAiTokenError("Unable to decode the payload segment of the token")
# 
#     try:
#         payload = json.loads(payload_data.decode("utf-8"))
#     except ValueError:
#         # Unable to load the payload as a json
#         payload = payload_data
# 
#     try:
#         signature = base64url_decode(crypto_segment)
#     except (TypeError, binascii.Error):
#         raise CraftAiTokenError("Unable to decode the crypto segment of the token")
# 
#     return (payload, signing_input, header, signature)
# 
# Path: craft_ai/errors.py
# class CraftAiTokenError(CraftAiError):
#     """An invalid token error occured."""


# Path: tests/test_jwt_decode.py
import unittest
from craft_ai.jwt_decode import jwt_decode
from craft_ai.errors import CraftAiTokenError



JWT_IO_EXAMPLE = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ"  # noqa: E501


class TestJwtDecode(unittest.TestCase):
    def test_decode_works(self):
        (payload, _, header, _) = jwt_decode(JWT_IO_EXAMPLE)
        self.assertEqual(header, {"alg": "HS256", "typ": "JWT"})
        self.assertEqual(
            payload, {"sub": "1234567890", "name": "John Doe", "admin": True}
        )

    def test_decode_works_with_spaces(self):

        (payload, _, header, _) = jwt_decode("  " + JWT_IO_EXAMPLE + " ")