|
""" |
|
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...") |
|
|
|
|
|
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 |
|
|
|
|
|
hf_api = HfApi() |
|
|
|
|
|
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 |
|
|
|
|
|
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 |
|
|
|
|
|
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() |
|
|