refactor: 本地只保存工号和加密密码,并整理后端目录

This commit is contained in:
2026-10-26 16:52:29 +08:00
parent c2b0a0f2e2
commit ce3b924b92
26 changed files with 237 additions and 442 deletions
+46
View File
@@ -0,0 +1,46 @@
from fastapi.testclient import TestClient
from sqlalchemy import text
from euniv.crypto import decrypt_secret
from euniv.db import SessionLocal, engine, init_db
from euniv.models import Account
class FakeClient:
def __init__(self, login_name: str, password: str) -> None:
self.login_name = login_name
self.password = password
def close(self) -> None:
return None
def login(self) -> dict:
return {"accountId": self.login_name}
def setup_function() -> None:
with engine.begin() as connection:
connection.execute(text("DROP TABLE IF EXISTS accounts"))
connection.execute(text("DROP TABLE IF EXISTS schedules"))
connection.execute(text("DROP TABLE IF EXISTS alembic_version"))
init_db()
def test_login_stores_only_employee_id_and_password() -> None:
import euniv.api as api_module
import euniv.service as service_module
api_module.login_and_store = lambda db, employee_id, password: service_module.login_and_store(
db, employee_id, password, FakeClient
)
client = TestClient(api_module.app)
response = client.post("/api/login", json={"employee_id": "6650000146", "password": "secret-pass"})
assert response.status_code == 200
assert response.json() == {"employee_id": "6650000146"}
listed = client.get("/api/accounts").json()
assert listed == [{"employee_id": "6650000146"}]
with SessionLocal() as db:
account = db.get(Account, "6650000146")
assert account is not None
assert decrypt_secret(account.password_cipher) == "secret-pass"
assert set(account.__table__.columns.keys()) == {"employee_id", "password_cipher"}