File size: 1,732 Bytes
4a823b5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
"""
Test script for token-based authentication in Dynamic Highscores.
This script tests the token-based authentication functionality.
"""
import os
import sys
import requests
from huggingface_hub import HfApi
def test_token_auth():
"""Test token-based authentication with HuggingFace API."""
print("Testing token-based authentication...")
# Prompt for tokens
read_token = input("Enter your HuggingFace read token: ")
write_token = input("Enter your HuggingFace write token: ")
if not read_token or not write_token:
print("Error: Both read and write tokens are required.")
return False
# Initialize HuggingFace API
hf_api = HfApi()
# Test read token
print("\nTesting read token...")
try:
read_user_info = hf_api.whoami(token=read_token)
print(f"Read token valid. User: {read_user_info.get('name')}")
except Exception as e:
print(f"Error validating read token: {e}")
return False
# Test write token
print("\nTesting write token...")
try:
write_user_info = hf_api.whoami(token=write_token)
print(f"Write token valid. User: {write_user_info.get('name')}")
except Exception as e:
print(f"Error validating write token: {e}")
return False
# Check if tokens belong to the same user
if read_user_info.get("id") != write_user_info.get("id"):
print("Error: Tokens must belong to the same user.")
return False
print("\nToken authentication test successful!")
print(f"User: {read_user_info.get('name')}")
print(f"User ID: {read_user_info.get('id')}")
return True
if __name__ == "__main__":
test_token_auth()
|