76 lines
3.1 KiB
Python
76 lines
3.1 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
创建一个新的测试宝箱并验证API返回的数据
|
|
"""
|
|
import requests
|
|
import json
|
|
|
|
def create_and_test_chest():
|
|
"""创建测试宝箱并测试API"""
|
|
print("=== 创建测试宝箱并验证API ===")
|
|
|
|
try:
|
|
# 先登录获取token
|
|
login_data = {
|
|
"username": "streamer", # 使用刚创建的主播用户
|
|
"password": "streamer123" # 使用默认密码
|
|
}
|
|
|
|
login_response = requests.post("http://localhost:8000/api/auth/login", data=login_data)
|
|
print(f"Login Status Code: {login_response.status_code}")
|
|
if login_response.status_code == 200:
|
|
token_data = login_response.json()
|
|
token = token_data["access_token"]
|
|
print(f"Token: {token}")
|
|
|
|
# 设置请求头
|
|
headers = {
|
|
"Authorization": f"Bearer {token}",
|
|
"Content-Type": "application/json"
|
|
}
|
|
|
|
# 设置请求体
|
|
data = {
|
|
"title": "倒计时测试宝箱",
|
|
"option_a": "选项A",
|
|
"option_b": "选项B",
|
|
"countdown_seconds": 120 # 2分钟倒计时
|
|
}
|
|
|
|
# 发送POST请求创建宝箱
|
|
response = requests.post("http://localhost:8000/api/chests", headers=headers, data=json.dumps(data))
|
|
print(f"Create Chest Status Code: {response.status_code}")
|
|
print(f"Response: {response.text}")
|
|
|
|
if response.status_code == 200:
|
|
chest = response.json()
|
|
print(f"Created chest: ID={chest['id']}, Title={chest['title']}, Time Remaining={chest.get('time_remaining', 'N/A')}")
|
|
|
|
# 测试获取单个宝箱
|
|
single_chest_response = requests.get(f"http://localhost:8000/api/chests/{chest['id']}")
|
|
if single_chest_response.status_code == 200:
|
|
single_chest = single_chest_response.json()
|
|
print(f"Single chest: ID={single_chest['id']}, Time Remaining={single_chest.get('time_remaining', 'N/A')}")
|
|
else:
|
|
print(f"Get single chest failed: {single_chest_response.status_code}")
|
|
|
|
# 测试获取宝箱列表
|
|
list_response = requests.get("http://localhost:8000/api/chests")
|
|
if list_response.status_code == 200:
|
|
chests = list_response.json()
|
|
print(f"Total chests in list: {len(chests)}")
|
|
for c in chests:
|
|
if c['id'] == chest['id']:
|
|
print(f"List chest: ID={c['id']}, Time Remaining={c.get('time_remaining', 'N/A')}")
|
|
break
|
|
else:
|
|
print(f"Get chests list failed: {list_response.status_code}")
|
|
else:
|
|
print(f"Create chest failed: {response.text}")
|
|
else:
|
|
print(f"Login failed: {login_response.text}")
|
|
except Exception as e:
|
|
print(f"Error: {e}")
|
|
|
|
if __name__ == "__main__":
|
|
create_and_test_chest() |