[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>
102 lines
3.4 KiB
Python
102 lines
3.4 KiB
Python
"""TDD: Account CRUD API tests - 含帳號編碼自動產生驗證"""
|
|
|
|
|
|
def test_create_account(client, sample_tenant):
|
|
payload = {
|
|
"tenant_id": sample_tenant["id"],
|
|
"sso_account": "alice",
|
|
"notification_email": "alice@gmail.com",
|
|
"quota_limit": 30,
|
|
}
|
|
resp = client.post("/api/v1/accounts", json=payload)
|
|
assert resp.status_code == 201
|
|
data = resp.json()
|
|
assert data["sso_account"] == "alice"
|
|
assert data["tenant_id"] == sample_tenant["id"]
|
|
assert data["quota_limit"] == 30
|
|
assert data["is_active"] is True
|
|
assert data["seq_no"] == 1
|
|
# account_code = prefix(TC) + seq_no 4碼左補0
|
|
assert data["account_code"] == "TC0001"
|
|
# email = sso_account@domain
|
|
assert data["email"] == f"alice@{sample_tenant['domain']}"
|
|
|
|
|
|
def test_account_code_auto_increment(client, sample_tenant):
|
|
"""驗證同租戶內帳號編碼流水號遞增"""
|
|
for sso in ["alice", "bob", "carol"]:
|
|
client.post("/api/v1/accounts", json={
|
|
"tenant_id": sample_tenant["id"],
|
|
"sso_account": sso,
|
|
"notification_email": f"{sso}@external.com",
|
|
})
|
|
resp = client.get("/api/v1/accounts", params={"tenant_id": sample_tenant["id"]})
|
|
accounts = resp.json()
|
|
codes = [a["account_code"] for a in accounts]
|
|
assert "TC0001" in codes
|
|
assert "TC0002" in codes
|
|
assert "TC0003" in codes
|
|
|
|
|
|
def test_create_account_tenant_not_found(client):
|
|
payload = {
|
|
"tenant_id": 99999,
|
|
"sso_account": "ghost",
|
|
"notification_email": "ghost@gmail.com",
|
|
}
|
|
resp = client.post("/api/v1/accounts", json=payload)
|
|
assert resp.status_code == 404
|
|
|
|
|
|
def test_list_accounts(client, sample_account):
|
|
resp = client.get("/api/v1/accounts")
|
|
assert resp.status_code == 200
|
|
accounts = resp.json()
|
|
assert len(accounts) == 1
|
|
assert accounts[0]["sso_account"] == sample_account["sso_account"]
|
|
assert accounts[0]["tenant_name"] is not None
|
|
|
|
|
|
def test_list_accounts_by_tenant(client, sample_tenant, sample_account):
|
|
resp = client.get("/api/v1/accounts", params={"tenant_id": sample_tenant["id"]})
|
|
assert resp.status_code == 200
|
|
assert len(resp.json()) == 1
|
|
|
|
|
|
def test_get_account(client, sample_account):
|
|
resp = client.get(f"/api/v1/accounts/{sample_account['id']}")
|
|
assert resp.status_code == 200
|
|
data = resp.json()
|
|
assert data["id"] == sample_account["id"]
|
|
assert data["lights"] is None
|
|
|
|
|
|
def test_get_account_not_found(client):
|
|
resp = client.get("/api/v1/accounts/99999")
|
|
assert resp.status_code == 404
|
|
|
|
|
|
def test_update_account(client, sample_account):
|
|
resp = client.put(
|
|
f"/api/v1/accounts/{sample_account['id']}",
|
|
json={"legal_name": "Alice Chen", "quota_limit": 50},
|
|
)
|
|
assert resp.status_code == 200
|
|
data = resp.json()
|
|
assert data["legal_name"] == "Alice Chen"
|
|
assert data["quota_limit"] == 50
|
|
|
|
|
|
def test_delete_account(client, sample_account):
|
|
resp = client.delete(f"/api/v1/accounts/{sample_account['id']}")
|
|
assert resp.status_code == 204
|
|
resp = client.get(f"/api/v1/accounts/{sample_account['id']}")
|
|
assert resp.status_code == 404
|
|
|
|
|
|
def test_delete_tenant_cascades_accounts(client, sample_tenant, sample_account):
|
|
"""刪除租戶後,帳號應該被 CASCADE 刪除"""
|
|
client.delete(f"/api/v1/tenants/{sample_tenant['id']}")
|
|
resp = client.get(f"/api/v1/accounts/{sample_account['id']}")
|
|
assert resp.status_code == 404
|