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:
98
backend/tests/test_drive_service.py
Normal file
98
backend/tests/test_drive_service.py
Normal file
@@ -0,0 +1,98 @@
|
||||
"""
|
||||
Drive Service HTTP Client 單元測試 (6.3)
|
||||
使用 mock 測試,不需要實際 Drive Service 上線
|
||||
"""
|
||||
import pytest
|
||||
from unittest.mock import patch, MagicMock
|
||||
from app.services.drive_service import DriveServiceClient, get_drive_quota_by_job_level
|
||||
|
||||
|
||||
class TestGetDriveQuotaByJobLevel:
|
||||
"""職級配額對應測試"""
|
||||
|
||||
def test_junior_quota(self):
|
||||
assert get_drive_quota_by_job_level("Junior") == 50
|
||||
|
||||
def test_mid_quota(self):
|
||||
assert get_drive_quota_by_job_level("Mid") == 100
|
||||
|
||||
def test_senior_quota(self):
|
||||
assert get_drive_quota_by_job_level("Senior") == 200
|
||||
|
||||
def test_manager_quota(self):
|
||||
assert get_drive_quota_by_job_level("Manager") == 500
|
||||
|
||||
def test_unknown_level_defaults_to_junior(self):
|
||||
assert get_drive_quota_by_job_level("Unknown") == 50
|
||||
|
||||
def test_none_defaults_to_junior(self):
|
||||
assert get_drive_quota_by_job_level(None) == 50
|
||||
|
||||
|
||||
class TestDriveServiceClient:
|
||||
"""Drive Service HTTP Client 測試"""
|
||||
|
||||
def setup_method(self):
|
||||
self.client = DriveServiceClient(
|
||||
base_url="https://drive-api.ease.taipei",
|
||||
timeout=5,
|
||||
)
|
||||
|
||||
def test_create_user_service_unavailable(self):
|
||||
"""Drive Service 未上線時回傳 warning 結果"""
|
||||
from requests.exceptions import ConnectionError as RequestsConnectionError
|
||||
with patch.object(self.client.session, "post") as mock_post:
|
||||
mock_post.side_effect = RequestsConnectionError("Connection refused")
|
||||
result = self.client.create_user(
|
||||
tenant_id=1,
|
||||
keycloak_user_id="kc-uuid-123",
|
||||
username="test.user",
|
||||
email="test.user@porscheworld.tw",
|
||||
display_name="Test User",
|
||||
quota_gb=100,
|
||||
)
|
||||
assert result["created"] is False
|
||||
assert result["error"] is not None
|
||||
assert "test.user" in str(result)
|
||||
|
||||
def test_create_user_success(self):
|
||||
"""Drive Service 正常回應時成功建立"""
|
||||
with patch.object(self.client.session, "post") as mock_post:
|
||||
mock_response = MagicMock()
|
||||
mock_response.status_code = 201
|
||||
mock_response.json.return_value = {
|
||||
"id": 42,
|
||||
"username": "test.user",
|
||||
"quota_gb": 100,
|
||||
"drive_url": "https://drive.ease.taipei/test.user",
|
||||
}
|
||||
mock_post.return_value = mock_response
|
||||
|
||||
result = self.client.create_user(
|
||||
tenant_id=1,
|
||||
keycloak_user_id="kc-uuid-123",
|
||||
username="test.user",
|
||||
email="test.user@porscheworld.tw",
|
||||
display_name="Test User",
|
||||
quota_gb=100,
|
||||
)
|
||||
assert result["created"] is True
|
||||
assert result["error"] is None
|
||||
assert result["quota_gb"] == 100
|
||||
|
||||
def test_get_quota_service_unavailable(self):
|
||||
"""Drive Service 未上線時查詢配額回傳 None"""
|
||||
from requests.exceptions import ConnectionError as RequestsConnectionError
|
||||
with patch.object(self.client.session, "get") as mock_get:
|
||||
mock_get.side_effect = RequestsConnectionError("Connection refused")
|
||||
result = self.client.get_quota(drive_user_id=42)
|
||||
assert result is None
|
||||
|
||||
def test_disable_user_service_unavailable(self):
|
||||
"""Drive Service 未上線時停用帳號回傳 False"""
|
||||
from requests.exceptions import ConnectionError as RequestsConnectionError
|
||||
with patch.object(self.client.session, "delete") as mock_delete:
|
||||
mock_delete.side_effect = RequestsConnectionError("Connection refused")
|
||||
result = self.client.disable_user(drive_user_id=42)
|
||||
assert result["disabled"] is False
|
||||
assert result["error"] is not None
|
||||
Reference in New Issue
Block a user