reposync/test_gitlink_token.py

119 lines
3.9 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
GitLink Token认证测试脚本
用于验证token认证是否正常工作
"""
import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from src.utils.gitlink import gitlink_auth, _is_token_format, _get_gitlink_headers
from src.utils.logger import logger
def test_token_format_detection():
"""测试token格式检测功能"""
print("=== 测试token格式检测 ===")
# 测试用例
test_cases = [
# (输入, 期望结果, 描述)
("abc123def456", True, "简单token"),
("token_with_underscore", True, "带下划线的token"),
("token-with-dash", True, "带连字符的token"),
("_session_id=abc123; csrf_token=def456", False, "cookie格式"),
("remember_user_token=xyz; session=abc", False, "包含cookie关键字"),
("a", False, "太短的字符串"),
("", False, "空字符串"),
("a" * 300, False, "太长的字符串"),
("valid_token_123", True, "有效的token格式"),
]
for input_str, expected, description in test_cases:
result = _is_token_format(input_str)
status = "" if result == expected else ""
print(f"{status} {description}: '{input_str[:50]}...' -> {result} (期望: {expected})")
print()
def test_headers_generation():
"""测试请求头生成功能"""
print("=== 测试请求头生成 ===")
# 测试token认证
token = "test_token_123"
headers = _get_gitlink_headers(token)
print(f"Token认证头: {headers}")
assert "Authorization" in headers
assert headers["Authorization"] == f"Bearer {token}"
print("✓ Token认证头生成正确")
# 测试cookie认证
cookie = "_session_id=abc123; csrf_token=def456"
headers = _get_gitlink_headers(cookie)
print(f"Cookie认证头: {headers}")
assert "Cookie" in headers
assert headers["Cookie"] == cookie
print("✓ Cookie认证头生成正确")
print()
def test_gitlink_auth_with_token():
"""测试GitLink认证功能需要真实的token"""
print("=== 测试GitLink认证功能 ===")
# 这里需要用户提供真实的token进行测试
print("请提供GitLink token进行测试按Enter跳过:")
token = input().strip()
if token:
print(f"正在测试token认证...")
try:
result = gitlink_auth(token)
if result:
print("✓ Token认证成功")
else:
print("✗ Token认证失败")
except Exception as e:
print(f"✗ Token认证异常: {str(e)}")
else:
print("跳过真实token测试")
print()
def test_mixed_auth_priority():
"""测试混合认证的优先级"""
print("=== 测试认证优先级 ===")
# 模拟项目配置
class MockProject:
def __init__(self, gitlink_token=None, gitlink_cookie=None):
self.gitlink_token = gitlink_token
self.gitlink_cookie = gitlink_cookie
# 测试用例
test_cases = [
(MockProject("token123", "cookie=abc"), "token123", "有token和cookie时优先token"),
(MockProject(None, "cookie=abc"), "cookie=abc", "只有cookie时使用cookie"),
(MockProject("token123", None), "token123", "只有token时使用token"),
(MockProject(None, None), None, "都没有时返回None"),
]
for project, expected, description in test_cases:
result = project.gitlink_token or project.gitlink_cookie
status = "" if result == expected else ""
print(f"{status} {description}: {result}")
print()
if __name__ == "__main__":
print("GitLink Token认证测试")
print("=" * 50)
test_token_format_detection()
test_headers_generation()
test_gitlink_auth_with_token()
test_mixed_auth_priority()
print("测试完成!")