Files
Yiqun/gitea-shim/python/test_shim.py

189 lines
5.3 KiB
Python

#!/usr/bin/env python3
"""Test script for the Gitea GitHub Shim."""
import os
import sys
from pathlib import Path
# Add current directory to path
current_dir = Path(__file__).parent
sys.path.insert(0, str(current_dir))
try:
from gitea_github_shim import GiteaGitHubShim
from config import get_github_client, is_gitea_mode, get_api_info
from utils import setup_api_environment, log_api_configuration
except ImportError as e:
print(f"Import error: {e}")
print("Make sure you're running from the correct directory")
sys.exit(1)
def test_shim_basic():
"""Test basic shim functionality."""
print("=== Testing Gitea GitHub Shim ===")
# Set up environment
setup_api_environment()
log_api_configuration()
# Get API info
api_info = get_api_info()
print(f"API Info: {api_info}")
# Test client creation
try:
client = get_github_client()
print(f"✓ Successfully created client: {type(client).__name__}")
# Test API status
try:
status = client.get_api_status()
print(f"✓ API Status: {status}")
except Exception as e:
print(f"⚠ API Status failed: {e}")
# Test rate limit
try:
rate_limit = client.get_rate_limit()
print(f"✓ Rate Limit: {rate_limit}")
except Exception as e:
print(f"⚠ Rate Limit failed: {e}")
except ValueError as e:
print(f"✗ Configuration error: {e}")
print(" Please set GITHUB_TOKEN or GITEA_TOKEN environment variable")
return False
except Exception as e:
print(f"✗ Failed to create client: {e}")
return False
return True
def test_repository_access():
"""Test repository access functionality."""
print("\n=== Testing Repository Access ===")
try:
client = get_github_client()
# Test with a known repository (GitHub's hello-world)
test_repo = "octocat/Hello-World"
try:
repo = client.get_repo(test_repo)
print(f"✓ Successfully accessed repository: {repo.full_name}")
print(f" - Default branch: {repo.default_branch}")
print(f" - Description: {repo.description}")
print(f" - Private: {repo.private}")
except Exception as e:
print(f"⚠ Repository access failed: {e}")
print(" This might be expected if using Gitea without the test repo")
except ValueError as e:
print(f"✗ Configuration error: {e}")
return False
except Exception as e:
print(f"✗ Repository test failed: {e}")
return False
return True
def test_user_access():
"""Test user access functionality."""
print("\n=== Testing User Access ===")
try:
client = get_github_client()
# Test getting authenticated user
try:
user = client.get_user()
print(f"✓ Successfully got authenticated user: {user.login}")
except Exception as e:
print(f"⚠ User access failed: {e}")
except ValueError as e:
print(f"✗ Configuration error: {e}")
return False
except Exception as e:
print(f"✗ User test failed: {e}")
return False
return True
def test_configuration():
"""Test configuration functionality."""
print("\n=== Testing Configuration ===")
try:
# Test API info
api_info = get_api_info()
print(f"✓ API Info: {api_info}")
# Test mode detection
mode = "Gitea" if is_gitea_mode() else "GitHub"
print(f"✓ API Mode: {mode}")
# Test client class detection
from config import config
client_class = config.get_client_class()
print(f"✓ Client Class: {client_class.__name__}")
return True
except Exception as e:
print(f"✗ Configuration test failed: {e}")
return False
def main():
"""Main test function."""
print("Gitea GitHub Shim Test Suite")
print("=" * 40)
# Check environment
print(f"USE_GITEA: {os.getenv('USE_GITEA', 'false')}")
print(f"GITEA_URL: {os.getenv('GITEA_URL', 'not set')}")
print(f"GITHUB_TOKEN: {'set' if os.getenv('GITHUB_TOKEN') else 'not set'}")
print(f"GITEA_TOKEN: {'set' if os.getenv('GITEA_TOKEN') else 'not set'}")
print()
# Run tests
tests = [
test_configuration,
test_shim_basic,
test_repository_access,
test_user_access,
]
passed = 0
total = len(tests)
for test in tests:
try:
if test():
passed += 1
except Exception as e:
print(f"✗ Test {test.__name__} failed with exception: {e}")
print(f"\n=== Test Results ===")
print(f"Passed: {passed}/{total}")
if passed == total:
print("🎉 All tests passed!")
return 0
else:
print("⚠ Some tests failed or had warnings")
if not os.getenv('GITHUB_TOKEN') and not os.getenv('GITEA_TOKEN'):
print("\n💡 To test with real API access, set GITHUB_TOKEN or GITEA_TOKEN")
return 1
if __name__ == "__main__":
sys.exit(main())