Download test_token_auth.py from zdy10046/RamEx: direct link, hf CLI and curl.
- Browser
- Download file 7.18 kB
-
https://huggingface.co/spaces/zdy10046/RamEx/resolve/main/test_token_auth.py
- Command line
-
hf download hf://spaces/zdy10046/RamEx/test_token_auth.py
-
curl -L -o test_token_auth.py https://huggingface.co/spaces/zdy10046/RamEx/resolve/main/test_token_auth.py
7.18 kB
| #!/usr/bin/env python3 | |
| """ | |
| 测试第三方Token认证功能 | |
| 只测试Authorization header方式的认证 | |
| """ | |
| import requests | |
| import json | |
| import sys | |
| import time | |
| def test_token_authentication(): | |
| """测试Token认证功能""" | |
| # 测试配置 | |
| BASE_URL = "http://localhost:8000" | |
| TEST_TOKEN = "0c4763d5b1aa561a5032f50e95bf069afa9a9e55" # 用户提供的token | |
| print("=" * 60) | |
| print("第三方Token认证测试 (仅Authorization Header)") | |
| print("=" * 60) | |
| # 测试用例1: 使用Token前缀的有效认证 | |
| print("\n1. 测试Token前缀认证...") | |
| try: | |
| headers = { | |
| 'Authorization': f'Token {TEST_TOKEN}', | |
| 'Content-Type': 'application/json' | |
| } | |
| response = requests.get(f"{BASE_URL}/api/projects/", headers=headers, timeout=10) | |
| if response.status_code == 200: | |
| print("✅ Token前缀认证成功") | |
| data = response.json() | |
| print(f" 响应数据: {json.dumps(data, indent=2, ensure_ascii=False)}") | |
| elif response.status_code == 401: | |
| print("❌ Token前缀认证失败 - 401 Unauthorized") | |
| print(f" 响应: {response.text}") | |
| else: | |
| print(f"⚠️ Token前缀认证 - 状态码: {response.status_code}") | |
| print(f" 响应: {response.text}") | |
| except requests.RequestException as e: | |
| print(f"❌ Token前缀认证请求失败: {e}") | |
| # 测试用例2: 使用Bearer前缀的有效认证 | |
| print("\n2. 测试Bearer前缀认证...") | |
| try: | |
| headers = { | |
| 'Authorization': f'Bearer {TEST_TOKEN}', | |
| 'Content-Type': 'application/json' | |
| } | |
| response = requests.get(f"{BASE_URL}/api/projects/", headers=headers, timeout=10) | |
| if response.status_code == 200: | |
| print("✅ Bearer前缀认证成功") | |
| data = response.json() | |
| print(f" 响应数据: {json.dumps(data, indent=2, ensure_ascii=False)}") | |
| elif response.status_code == 401: | |
| print("❌ Bearer前缀认证失败 - 401 Unauthorized") | |
| print(f" 响应: {response.text}") | |
| else: | |
| print(f"⚠️ Bearer前缀认证 - 状态码: {response.status_code}") | |
| print(f" 响应: {response.text}") | |
| except requests.RequestException as e: | |
| print(f"❌ Bearer前缀认证请求失败: {e}") | |
| # 测试用例3: 无效Token测试 | |
| print("\n3. 测试无效Token...") | |
| try: | |
| headers = { | |
| 'Authorization': 'Token invalid_token_123', | |
| 'Content-Type': 'application/json' | |
| } | |
| response = requests.get(f"{BASE_URL}/api/projects/", headers=headers, timeout=10) | |
| if response.status_code == 401: | |
| print("✅ 无效Token正确被拒绝") | |
| print(f" 响应: {response.text}") | |
| else: | |
| print(f"❌ 无效Token测试失败 - 状态码: {response.status_code}") | |
| print(f" 响应: {response.text}") | |
| except requests.RequestException as e: | |
| print(f"❌ 无效Token测试请求失败: {e}") | |
| # 测试用例4: 无Authorization header测试 | |
| print("\n4. 测试无Authorization header...") | |
| try: | |
| headers = { | |
| 'Content-Type': 'application/json' | |
| } | |
| response = requests.get(f"{BASE_URL}/api/projects/", headers=headers, timeout=10) | |
| if response.status_code == 401: | |
| print("✅ 无Authorization header正确被拒绝") | |
| print(f" 响应: {response.text}") | |
| else: | |
| print(f"❌ 无Authorization header测试失败 - 状态码: {response.status_code}") | |
| print(f" 响应: {response.text}") | |
| except requests.RequestException as e: | |
| print(f"❌ 无Authorization header测试请求失败: {e}") | |
| # 测试用例5: 错误的Authorization格式 | |
| print("\n5. 测试错误的Authorization格式...") | |
| try: | |
| headers = { | |
| 'Authorization': f'Basic {TEST_TOKEN}', # 使用Basic而不是Token/Bearer | |
| 'Content-Type': 'application/json' | |
| } | |
| response = requests.get(f"{BASE_URL}/api/projects/", headers=headers, timeout=10) | |
| if response.status_code == 401: | |
| print("✅ 错误Authorization格式正确被拒绝") | |
| print(f" 响应: {response.text}") | |
| else: | |
| print(f"❌ 错误Authorization格式测试失败 - 状态码: {response.status_code}") | |
| print(f" 响应: {response.text}") | |
| except requests.RequestException as e: | |
| print(f"❌ 错误Authorization格式测试请求失败: {e}") | |
| # 测试用例6: 不再支持的查询参数方式 (确认已禁用) | |
| print("\n6. 测试查询参数方式(应该被拒绝)...") | |
| try: | |
| response = requests.get(f"{BASE_URL}/api/projects/?token={TEST_TOKEN}", timeout=10) | |
| if response.status_code == 401: | |
| print("✅ 查询参数方式正确被拒绝") | |
| print(f" 响应: {response.text}") | |
| else: | |
| print(f"❌ 查询参数方式测试失败 - 状态码: {response.status_code}") | |
| print(f" 响应: {response.text}") | |
| except requests.RequestException as e: | |
| print(f"❌ 查询参数方式测试请求失败: {e}") | |
| def test_third_party_api(): | |
| """测试第三方API连接""" | |
| print("\n" + "=" * 60) | |
| print("第三方API连接测试") | |
| print("=" * 60) | |
| try: | |
| url = "http://27.223.62.254:10008/user/valid_token/" | |
| headers = { | |
| 'accept': 'application/json', | |
| 'X-CSRFToken': 'ARNjGjIoXRiA5I5LwmKcUDB5GGzPtMofGDJW5UcBsjWAolmvA8iZoLjWMxFepakY' | |
| } | |
| files = {'user_token': (None, "0c4763d5b1aa561a5032f50e95bf069afa9a9e55")} | |
| response = requests.post(url, headers=headers, files=files, timeout=10) | |
| print(f"第三方API响应状态码: {response.status_code}") | |
| print(f"第三方API响应内容: {response.text}") | |
| if response.status_code == 200: | |
| data = response.json() | |
| if data.get('code') == '200' and data.get('message') == 'succeed': | |
| print("✅ 第三方API验证成功") | |
| user_name = data.get('data', {}).get('user_name') | |
| print(f" 用户名: {user_name}") | |
| else: | |
| print("❌ 第三方API验证失败") | |
| else: | |
| print("❌ 第三方API连接失败") | |
| except requests.RequestException as e: | |
| print(f"❌ 第三方API连接错误: {e}") | |
| def main(): | |
| """主函数""" | |
| print("开始Token认证测试...") | |
| print("确保Django服务已启动在 http://localhost:8000") | |
| # 等待用户确认 | |
| input("按回车键开始测试...") | |
| # 测试Token认证 | |
| test_token_authentication() | |
| # 测试第三方API | |
| test_third_party_api() | |
| print("\n" + "=" * 60) | |
| print("测试完成!") | |
| print("=" * 60) | |
| if __name__ == "__main__": | |
| main() |