"""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