79 lines
2.9 KiB
Python
79 lines
2.9 KiB
Python
|
|
import requests
|
|
import json
|
|
import os
|
|
|
|
# 配置
|
|
BASE_URL = "http://localhost:18080/api"
|
|
GROUP_NAME = "TestGroup_Verify"
|
|
USER_NAME = "DuplicateTestUser"
|
|
TEST_IMG = "verify_face.jpg"
|
|
|
|
def create_test_image():
|
|
if not os.path.exists(TEST_IMG):
|
|
# 创建一个简单的dummy文件
|
|
with open(TEST_IMG, 'wb') as f:
|
|
f.write(b'\xFF\xD8\xFF\xE0' + b'\x00' * 1000) # Fake JPG header
|
|
return TEST_IMG
|
|
|
|
def run_verification():
|
|
print("=== 开始验证 ===")
|
|
|
|
# 1. 创建测试分组
|
|
print("\n1. 创建分组...")
|
|
resp = requests.post(f"{BASE_URL}/groups/add", json={"name": GROUP_NAME})
|
|
if resp.status_code == 200 and resp.json()['code'] == 200:
|
|
group_id = resp.json()['data']['id']
|
|
print(f"✅ 分组创建成功 ID: {group_id}")
|
|
else:
|
|
print(f"❌ 分组创建失败: {resp.text}")
|
|
return
|
|
|
|
# 准备图片
|
|
create_test_image()
|
|
files = {'photo': open(TEST_IMG, 'rb')}
|
|
|
|
# 2. 添加用户 (第一次)
|
|
print("\n2. 添加用户 (预期成功)...")
|
|
data = {"name": USER_NAME, "groupId": group_id}
|
|
# Re-open file for each request
|
|
resp = requests.post(f"{BASE_URL}/users/add", data=data, files={'photo': open(TEST_IMG, 'rb')})
|
|
|
|
if resp.status_code == 200 and resp.json()['code'] == 200:
|
|
print(f"✅ 用户创建成功")
|
|
else:
|
|
# 如果是因为测试图片无法提取特征而失败,也是预期的(说明代码跑到了特征提取那一步)
|
|
# 但我们主要验证重名逻辑,所以这里假设如果失败是因为重名则是有问题,如果是因为特征提取则是环境问题
|
|
print(f"⚠️ 用户创建返回: {resp.json().get('msg')}")
|
|
|
|
# 3. 添加同名用户 (预期失败)
|
|
print("\n3. 再次添加同名用户 (预期被拦截)...")
|
|
resp = requests.post(f"{BASE_URL}/users/add", data=data, files={'photo': open(TEST_IMG, 'rb')})
|
|
|
|
res_json = resp.json()
|
|
if res_json.get('code') != 200 and "已存在" in str(res_json.get('msg')):
|
|
print(f"✅ 成功拦截重名用户: {res_json.get('msg')}")
|
|
else:
|
|
print(f"❌ 拦截失败或错误信息不匹配: {resp.text}")
|
|
|
|
# 4. 测试搜索接口
|
|
print("\n4. 测试人脸搜索接口...")
|
|
try:
|
|
resp = requests.post(f"{BASE_URL}/users/search", data={"groupId": group_id}, files={'photo': open(TEST_IMG, 'rb')})
|
|
if resp.status_code == 200:
|
|
print(f"✅ 接口调用成功: {resp.json()}")
|
|
elif resp.status_code == 404:
|
|
print(f"✅ 接口调用成功 (未找到匹配 - 符合预期): {resp.json()}")
|
|
else:
|
|
print(f"❌ 接口调用异常: {resp.status_code} - {resp.text}")
|
|
except Exception as e:
|
|
print(f"❌ 请求异常: {e}")
|
|
|
|
print("\n=== 验证结束 ===")
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
run_verification()
|
|
except Exception as e:
|
|
print(f"Fatal Error: {e}")
|