File size: 7,180 Bytes
e657e99 | 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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | #!/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() |