2ND
This commit is contained in:
72
test_api.py
Normal file
72
test_api.py
Normal file
@@ -0,0 +1,72 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
測試 LDAP API 端點功能
|
||||
"""
|
||||
import requests
|
||||
import json
|
||||
|
||||
def test_api_search():
|
||||
"""測試 API 搜尋功能"""
|
||||
print("=== 測試 LDAP API 搜尋功能 ===")
|
||||
|
||||
# 測試不同的搜尋詞
|
||||
test_terms = ["liu", "劉", "PE", "管理", "admin"]
|
||||
|
||||
base_url = "http://127.0.0.1:5000"
|
||||
|
||||
# 先登入獲取 session cookie
|
||||
session = requests.Session()
|
||||
|
||||
print("正在登入系統...")
|
||||
login_data = {
|
||||
'username': 'ymirliu@panjit.com.tw',
|
||||
'password': input("請輸入 ymirliu@panjit.com.tw 的密碼: ")
|
||||
}
|
||||
|
||||
# 嘗試登入
|
||||
login_response = session.post(f"{base_url}/login", data=login_data)
|
||||
|
||||
if login_response.status_code == 200 and "總表檢視" in login_response.text:
|
||||
print("✅ 登入成功")
|
||||
|
||||
for term in test_terms:
|
||||
print(f"\n🔍 測試搜尋: '{term}'")
|
||||
print("-" * 40)
|
||||
|
||||
api_url = f"{base_url}/api/ldap-search"
|
||||
params = {'q': term}
|
||||
|
||||
try:
|
||||
response = session.get(api_url, params=params)
|
||||
print(f"HTTP 狀態: {response.status_code}")
|
||||
|
||||
if response.status_code == 200:
|
||||
try:
|
||||
data = response.json()
|
||||
print(f"找到 {len(data)} 個結果:")
|
||||
|
||||
for i, result in enumerate(data, 1):
|
||||
print(f" {i}. {result.get('text', 'N/A')}")
|
||||
print(f" 值: {result.get('value', 'N/A')}")
|
||||
print(f" 類型: {result.get('type', 'N/A')}")
|
||||
|
||||
except json.JSONDecodeError:
|
||||
print("❌ 回應不是有效的 JSON")
|
||||
print(f"回應內容: {response.text[:200]}")
|
||||
else:
|
||||
print(f"❌ API 請求失敗: {response.status_code}")
|
||||
print(f"回應: {response.text[:200]}")
|
||||
|
||||
except requests.RequestException as e:
|
||||
print(f"❌ 請求錯誤: {e}")
|
||||
else:
|
||||
print("❌ 登入失敗")
|
||||
print(f"HTTP 狀態: {login_response.status_code}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
test_api_search()
|
||||
except KeyboardInterrupt:
|
||||
print("\n\n測試已中斷")
|
||||
except Exception as e:
|
||||
print(f"\n測試發生錯誤: {e}")
|
Reference in New Issue
Block a user