124 lines
4.1 KiB
Python
124 lines
4.1 KiB
Python
|
|
#!/usr/bin/env python3
|
|||
|
|
# -*- coding: utf-8 -*-
|
|||
|
|
"""
|
|||
|
|
测试邀请码功能
|
|||
|
|
"""
|
|||
|
|
|
|||
|
|
import requests
|
|||
|
|
|
|||
|
|
# 配置
|
|||
|
|
BASE_URL = "http://localhost:30101"
|
|||
|
|
TOKEN_USER1 = "3932cd35-4238-4c3f-ad5c-ad6ce9454e79" # 用户1的 token(邀请人)
|
|||
|
|
TOKEN_USER2 = "test-token-user2" # 用户2的 token(被邀请人,需要替换为真实 token)
|
|||
|
|
|
|||
|
|
def test_get_invite_info(token):
|
|||
|
|
"""测试获取邀请信息"""
|
|||
|
|
url = f"{BASE_URL}/config/invite/info"
|
|||
|
|
headers = {
|
|||
|
|
"Authorization": f"Bearer {token}"
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
print(f"\n=== 测试获取邀请信息 ===")
|
|||
|
|
print(f"请求 URL: {url}")
|
|||
|
|
|
|||
|
|
response = requests.get(url, headers=headers)
|
|||
|
|
|
|||
|
|
print(f"状态码: {response.status_code}")
|
|||
|
|
print(f"响应内容: {response.json()}")
|
|||
|
|
|
|||
|
|
if response.status_code == 200:
|
|||
|
|
data = response.json()
|
|||
|
|
if data.get("code") == 1:
|
|||
|
|
invite_code = data.get("data", {}).get("invite_code")
|
|||
|
|
print(f"\n✅ 邀请码: {invite_code}")
|
|||
|
|
return invite_code
|
|||
|
|
else:
|
|||
|
|
print(f"\n❌ 获取失败: {data.get('message')}")
|
|||
|
|
else:
|
|||
|
|
print(f"\n❌ HTTP 错误: {response.status_code}")
|
|||
|
|
|
|||
|
|
return None
|
|||
|
|
|
|||
|
|
def test_apply_invite_code(token, invite_code):
|
|||
|
|
"""测试使用邀请码"""
|
|||
|
|
url = f"{BASE_URL}/config/invite/apply"
|
|||
|
|
headers = {
|
|||
|
|
"Authorization": f"Bearer {token}",
|
|||
|
|
"Content-Type": "application/json"
|
|||
|
|
}
|
|||
|
|
data = {
|
|||
|
|
"invite_code": invite_code
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
print(f"\n=== 测试使用邀请码 ===")
|
|||
|
|
print(f"请求 URL: {url}")
|
|||
|
|
print(f"邀请码: {invite_code}")
|
|||
|
|
|
|||
|
|
response = requests.post(url, headers=headers, json=data)
|
|||
|
|
|
|||
|
|
print(f"状态码: {response.status_code}")
|
|||
|
|
print(f"响应内容: {response.json()}")
|
|||
|
|
|
|||
|
|
if response.status_code == 200:
|
|||
|
|
data = response.json()
|
|||
|
|
if data.get("code") == 1:
|
|||
|
|
print(f"\n✅ 使用成功: {data.get('data', {}).get('message')}")
|
|||
|
|
print(f"奖励: {data.get('data', {}).get('reward')} 金币")
|
|||
|
|
print(f"余额: {data.get('data', {}).get('balance')} 金币")
|
|||
|
|
else:
|
|||
|
|
print(f"\n❌ 使用失败: {data.get('message')}")
|
|||
|
|
else:
|
|||
|
|
print(f"\n❌ HTTP 错误: {response.status_code}")
|
|||
|
|
print(f"错误详情: {response.text}")
|
|||
|
|
|
|||
|
|
def test_check_user_info(token):
|
|||
|
|
"""测试检查用户信息"""
|
|||
|
|
url = f"{BASE_URL}/user/basic"
|
|||
|
|
headers = {
|
|||
|
|
"Authorization": f"Bearer {token}"
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
print(f"\n=== 测试获取用户信息 ===")
|
|||
|
|
print(f"请求 URL: {url}")
|
|||
|
|
|
|||
|
|
response = requests.get(url, headers=headers)
|
|||
|
|
|
|||
|
|
print(f"状态码: {response.status_code}")
|
|||
|
|
if response.status_code == 200:
|
|||
|
|
data = response.json()
|
|||
|
|
print(f"响应内容: {data}")
|
|||
|
|
if data.get("code") == 1:
|
|||
|
|
user_data = data.get("data", {})
|
|||
|
|
print(f"\n✅ 用户ID: {user_data.get('user_id')}")
|
|||
|
|
print(f"金币余额: {user_data.get('money')}")
|
|||
|
|
print(f"邀请码: {user_data.get('invite_code')}")
|
|||
|
|
print(f"被邀请码: {user_data.get('invited_by')}")
|
|||
|
|
else:
|
|||
|
|
print(f"\n❌ HTTP 错误: {response.status_code}")
|
|||
|
|
|
|||
|
|
if __name__ == "__main__":
|
|||
|
|
print("=" * 60)
|
|||
|
|
print("邀请码功能测试")
|
|||
|
|
print("=" * 60)
|
|||
|
|
|
|||
|
|
# 测试1:获取用户1的邀请码
|
|||
|
|
print("\n【步骤1】获取用户1(邀请人)的邀请码")
|
|||
|
|
invite_code = test_get_invite_info(TOKEN_USER1)
|
|||
|
|
|
|||
|
|
if invite_code:
|
|||
|
|
print(f"\n【步骤2】用户1的邀请码是: {invite_code}")
|
|||
|
|
print("\n提示:请使用另一个用户的 token 来测试使用邀请码")
|
|||
|
|
print(f"修改脚本中的 TOKEN_USER2 变量,然后取消注释下面的代码")
|
|||
|
|
|
|||
|
|
# 取消注释下面的代码来测试使用邀请码
|
|||
|
|
# print("\n【步骤3】用户2使用邀请码")
|
|||
|
|
# test_apply_invite_code(TOKEN_USER2, invite_code)
|
|||
|
|
|
|||
|
|
# print("\n【步骤4】检查用户1的信息(应该增加了邀请计数和奖励)")
|
|||
|
|
# test_check_user_info(TOKEN_USER1)
|
|||
|
|
|
|||
|
|
# print("\n【步骤5】检查用户2的信息(应该有 invited_by 字段)")
|
|||
|
|
# test_check_user_info(TOKEN_USER2)
|
|||
|
|
|
|||
|
|
print("\n" + "=" * 60)
|