[Phase 0 Reset]
- 清除舊版 app/、alembic/versions/、雜亂測試腳本
- 新 requirements.txt (移除 caldav/redis/keycloak-lib,加入 apscheduler/croniter/docker/paramiko/ping3/dnspython)
[Phase 1 資料庫]
- 9 張資料表 SQLAlchemy Models:tenants / accounts / schedules / schedule_logs /
tenant_schedule_results / account_schedule_results / servers / server_status_logs / system_status_logs
- Alembic migration 001_create_all_tables (已套用到 10.1.0.20:5433/virtual_mis)
- seed.py:schedules 初始 3 筆 / servers 初始 4 筆
[Phase 2 CRUD API]
- GET/POST/PUT/DELETE: /api/v1/tenants / accounts / servers / schedules
- /api/v1/system-status
- 帳號編碼自動產生 (prefix + seq_no 4碼左補0)
- 燈號 (lights) 從最新排程結果取得
[Phase 3 Watchdog]
- APScheduler interval 3分鐘,原子 UPDATE status=Going 防重複執行
- 手動觸發 API: POST /api/v1/schedules/{id}/run
[Phase 4 Service Clients]
- KeycloakClient:vmis-admin realm,REST API (不用 python-keycloak)
- MailClient:Docker Mailserver @ 10.1.0.254:8080,含 MX DNS 驗證
- DockerClient:docker-py 本機 + paramiko SSH 遠端 compose
- NextcloudClient:OCS API user/quota
- SystemChecker:功能驗證 (traefik routers>0 / keycloak token / SMTP EHLO / DB SELECT 1 / ping)
[TDD]
- 37 tests / 37 passed (2.11s)
- SQLite in-memory + StaticPool,無需外部 DB
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
101 lines
3.0 KiB
Python
101 lines
3.0 KiB
Python
"""TDD: Tenant CRUD API tests"""
|
|
|
|
|
|
def test_create_tenant(client):
|
|
payload = {
|
|
"code": "PORS01",
|
|
"prefix": "PC",
|
|
"name": "Porsche測試公司",
|
|
"domain": "porsche.lab.taipei",
|
|
"status": "trial",
|
|
}
|
|
resp = client.post("/api/v1/tenants", json=payload)
|
|
assert resp.status_code == 201
|
|
data = resp.json()
|
|
assert data["code"] == "PORS01"
|
|
assert data["prefix"] == "PC"
|
|
assert data["domain"] == "porsche.lab.taipei"
|
|
assert data["status"] == "trial"
|
|
assert data["is_active"] is True
|
|
assert data["quota_per_user"] == 20
|
|
assert data["total_quota"] == 200
|
|
assert data["id"] is not None
|
|
|
|
|
|
def test_create_tenant_duplicate_code(client, sample_tenant):
|
|
payload = {
|
|
"code": sample_tenant["code"],
|
|
"prefix": "XX",
|
|
"name": "重複代碼",
|
|
"domain": "another.lab.taipei",
|
|
"status": "trial",
|
|
}
|
|
resp = client.post("/api/v1/tenants", json=payload)
|
|
assert resp.status_code == 409
|
|
|
|
|
|
def test_create_tenant_duplicate_domain(client, sample_tenant):
|
|
payload = {
|
|
"code": "NEWCODE",
|
|
"prefix": "XX",
|
|
"name": "重複網域",
|
|
"domain": sample_tenant["domain"],
|
|
"status": "trial",
|
|
}
|
|
resp = client.post("/api/v1/tenants", json=payload)
|
|
assert resp.status_code == 409
|
|
|
|
|
|
def test_list_tenants(client, sample_tenant):
|
|
resp = client.get("/api/v1/tenants")
|
|
assert resp.status_code == 200
|
|
tenants = resp.json()
|
|
assert len(tenants) == 1
|
|
assert tenants[0]["code"] == sample_tenant["code"]
|
|
|
|
|
|
def test_list_tenants_filter_active(client, sample_tenant):
|
|
# Deactivate
|
|
client.put(f"/api/v1/tenants/{sample_tenant['id']}", json={"is_active": False})
|
|
resp = client.get("/api/v1/tenants", params={"is_active": True})
|
|
assert resp.status_code == 200
|
|
assert len(resp.json()) == 0
|
|
|
|
|
|
def test_get_tenant(client, sample_tenant):
|
|
resp = client.get(f"/api/v1/tenants/{sample_tenant['id']}")
|
|
assert resp.status_code == 200
|
|
data = resp.json()
|
|
assert data["id"] == sample_tenant["id"]
|
|
assert data["code"] == sample_tenant["code"]
|
|
assert data["lights"] is None # No schedule runs yet
|
|
|
|
|
|
def test_get_tenant_not_found(client):
|
|
resp = client.get("/api/v1/tenants/99999")
|
|
assert resp.status_code == 404
|
|
|
|
|
|
def test_update_tenant(client, sample_tenant):
|
|
resp = client.put(
|
|
f"/api/v1/tenants/{sample_tenant['id']}",
|
|
json={"name": "更新後的名稱", "status": "active"},
|
|
)
|
|
assert resp.status_code == 200
|
|
data = resp.json()
|
|
assert data["name"] == "更新後的名稱"
|
|
assert data["status"] == "active"
|
|
|
|
|
|
def test_delete_tenant(client, sample_tenant):
|
|
resp = client.delete(f"/api/v1/tenants/{sample_tenant['id']}")
|
|
assert resp.status_code == 204
|
|
resp = client.get(f"/api/v1/tenants/{sample_tenant['id']}")
|
|
assert resp.status_code == 404
|
|
|
|
|
|
def test_health_endpoint(client):
|
|
resp = client.get("/health")
|
|
assert resp.status_code == 200
|
|
assert resp.json()["status"] == "ok"
|