import sys
from app.core.token_blacklist import token_blacklist
from app.core.security import verify_token

def test_blacklist():
    """测试令牌黑名单功能"""
    try:
        print("=== 测试令牌黑名单功能 ===")

        # 使用之前获取的令牌
        token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI3IiwidXNlcm5hbWUiOiJ0ZXN0bWUiLCJlbWFpbCI6InRlc3RtZUBleGFtcGxlLmNvbSIsImV4cCI6MTc2MDE3MjI5OCwidHlwZSI6ImFjY2VzcyJ9.Fp9cZ4rDelm6mpxc0cYqXJJ4ne-xG90QuAx1UNfJBIY"

        # 1. 检查令牌是否在黑名单中
        print("1. 检查令牌是否在黑名单中...")
        is_blacklisted = token_blacklist.is_blacklisted(token)
        print(f"   令牌在黑名单中: {is_blacklisted}")

        # 2. 验证令牌
        print("2. 验证令牌...")
        payload = verify_token(token, "access")
        if payload:
            print(f"   令牌验证成功，用户ID: {payload.get('sub')}")
        else:
            print("   令牌验证失败")

        # 3. 手动将令牌加入黑名单
        print("3. 将令牌加入黑名单...")
        token_blacklist.add_token(token)
        print("   令牌已加入黑名单")

        # 4. 再次检查
        print("4. 再次检查令牌是否在黑名单中...")
        is_blacklisted = token_blacklist.is_blacklisted(token)
        print(f"   令牌在黑名单中: {is_blacklisted}")

        # 5. 再次验证令牌
        print("5. 再次验证令牌...")
        payload = verify_token(token, "access")
        if payload:
            print(f"   令牌验证成功，用户ID: {payload.get('sub')}")
        else:
            print("   令牌验证失败（预期结果）")

        print("\n=== 黑名单功能测试完成 ===")

    except Exception as e:
        print(f"测试过程出错: {e}")
        import traceback
        traceback.print_exc()

if __name__ == "__main__":
    test_blacklist()