feat: HR Portal - Complete Multi-Tenant System with Redis Session Storage
Major Features: - ✅ Multi-tenant architecture (tenant isolation) - ✅ Employee CRUD with lifecycle management (onboarding/offboarding) - ✅ Department tree structure with email domain management - ✅ Company info management (single-record editing) - ✅ System functions CRUD (permission management) - ✅ Email account management (multi-account per employee) - ✅ Keycloak SSO integration (auth.lab.taipei) - ✅ Redis session storage (10.1.0.254:6379) - Solves Cookie 4KB limitation - Cross-system session sharing - Sliding expiration (8 hours) - Automatic token refresh Technical Stack: Backend: - FastAPI + SQLAlchemy - PostgreSQL 16 (10.1.0.20:5433) - Keycloak Admin API integration - Docker Mailserver integration (SSH) - Alembic migrations Frontend: - Next.js 14 (App Router) - NextAuth 4 with Keycloak Provider - Redis session storage (ioredis) - Tailwind CSS Infrastructure: - Redis 7 (10.1.0.254:6379) - Session + Cache - Keycloak 26.1.0 (auth.lab.taipei) - Docker Mailserver (10.1.0.254) Architecture Highlights: - Session管理由 Keycloak + Redis 統一控制 - 支援多系統 (HR/WebMail/Calendar/Drive/Office) 共享 session - Token 自動刷新,異質服務整合 - 未來可無縫遷移到雲端 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
152
test_integration.py
Normal file
152
test_integration.py
Normal file
@@ -0,0 +1,152 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
HR Portal 前後端整合測試腳本
|
||||
"""
|
||||
import requests
|
||||
import time
|
||||
import sys
|
||||
|
||||
# 配置
|
||||
BACKEND_URL = "http://localhost:10181"
|
||||
FRONTEND_URL = "http://localhost:10180"
|
||||
|
||||
def test_backend_health():
|
||||
"""測試後端健康檢查"""
|
||||
print("=" * 60)
|
||||
print("測試 1: 後端健康檢查")
|
||||
print("=" * 60)
|
||||
|
||||
try:
|
||||
response = requests.get(f"{BACKEND_URL}/health", timeout=5)
|
||||
if response.status_code == 200:
|
||||
data = response.json()
|
||||
print("[OK] 後端 API 正常運行")
|
||||
print(f" 服務: {data.get('service')}")
|
||||
print(f" 版本: {data.get('version')}")
|
||||
print(f" 環境: {data.get('environment')}")
|
||||
return True
|
||||
else:
|
||||
print(f"[FAIL] 後端回應異常: {response.status_code}")
|
||||
return False
|
||||
except requests.exceptions.ConnectionError:
|
||||
print("[FAIL] 無法連接到後端 API")
|
||||
print(" 請確認後端伺服器是否在 http://localhost:10181 運行")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"[FAIL] 錯誤: {e}")
|
||||
return False
|
||||
|
||||
def test_backend_docs():
|
||||
"""測試後端 API 文件"""
|
||||
print("\n" + "=" * 60)
|
||||
print("測試 2: 後端 API 文件")
|
||||
print("=" * 60)
|
||||
|
||||
try:
|
||||
response = requests.get(f"{BACKEND_URL}/docs", timeout=5)
|
||||
if response.status_code == 200:
|
||||
print("[OK] Swagger UI 可存取")
|
||||
print(f" URL: {BACKEND_URL}/docs")
|
||||
return True
|
||||
else:
|
||||
print(f"[FAIL] Swagger UI 回應異常: {response.status_code}")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"[FAIL] 錯誤: {e}")
|
||||
return False
|
||||
|
||||
def test_backend_api_endpoints():
|
||||
"""測試後端 API 端點"""
|
||||
print("\n" + "=" * 60)
|
||||
print("測試 3: 後端 API 端點")
|
||||
print("=" * 60)
|
||||
|
||||
endpoints = [
|
||||
"/api/v1/employees",
|
||||
"/api/v1/email-accounts",
|
||||
"/api/v1/permissions",
|
||||
]
|
||||
|
||||
success_count = 0
|
||||
for endpoint in endpoints:
|
||||
try:
|
||||
response = requests.get(f"{BACKEND_URL}{endpoint}", timeout=5)
|
||||
# 401 是預期的 (需要認證)
|
||||
if response.status_code in [200, 401]:
|
||||
print(f"[OK] {endpoint}")
|
||||
success_count += 1
|
||||
else:
|
||||
print(f"[WARN] {endpoint} - 狀態: {response.status_code}")
|
||||
except Exception as e:
|
||||
print(f"[FAIL] {endpoint} - 錯誤: {e}")
|
||||
|
||||
print(f"\n端點測試結果: {success_count}/{len(endpoints)} 通過")
|
||||
return success_count == len(endpoints)
|
||||
|
||||
def test_frontend_health():
|
||||
"""測試前端健康檢查"""
|
||||
print("\n" + "=" * 60)
|
||||
print("測試 4: 前端健康檢查")
|
||||
print("=" * 60)
|
||||
|
||||
try:
|
||||
response = requests.get(FRONTEND_URL, timeout=5)
|
||||
if response.status_code == 200:
|
||||
print("[OK] 前端應用正常運行")
|
||||
print(f" URL: {FRONTEND_URL}")
|
||||
return True
|
||||
else:
|
||||
print(f"[FAIL] 前端回應異常: {response.status_code}")
|
||||
return False
|
||||
except requests.exceptions.ConnectionError:
|
||||
print("[FAIL] 無法連接到前端應用")
|
||||
print(" 請確認前端伺服器是否在 http://localhost:10180 運行")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"[FAIL] 錯誤: {e}")
|
||||
return False
|
||||
|
||||
def main():
|
||||
"""主測試流程"""
|
||||
print("\n")
|
||||
print("╔" + "=" * 58 + "╗")
|
||||
print("║" + " " * 15 + "HR Portal 整合測試" + " " * 24 + "║")
|
||||
print("╚" + "=" * 58 + "╝")
|
||||
print("\n")
|
||||
|
||||
results = []
|
||||
|
||||
# 測試後端
|
||||
results.append(("後端健康檢查", test_backend_health()))
|
||||
time.sleep(1)
|
||||
results.append(("後端 API 文件", test_backend_docs()))
|
||||
time.sleep(1)
|
||||
results.append(("後端 API 端點", test_backend_api_endpoints()))
|
||||
|
||||
# 測試前端
|
||||
print("\n[INFO] 前端測試將在後端測試完成後執行...")
|
||||
print("[INFO] 請先手動啟動前端: cd frontend && npm run dev")
|
||||
|
||||
# 總結
|
||||
print("\n" + "=" * 60)
|
||||
print("測試總結")
|
||||
print("=" * 60)
|
||||
|
||||
passed = sum(1 for _, result in results if result)
|
||||
total = len(results)
|
||||
|
||||
for name, result in results:
|
||||
status = "[PASS]" if result else "[FAIL]"
|
||||
print(f"{status} {name}")
|
||||
|
||||
print(f"\n總計: {passed}/{total} 測試通過")
|
||||
|
||||
if passed == total:
|
||||
print("\n[SUCCESS] 所有測試通過!")
|
||||
return 0
|
||||
else:
|
||||
print("\n[WARNING] 部分測試失敗,請檢查錯誤訊息")
|
||||
return 1
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Reference in New Issue
Block a user