refactor: 本地只保存工号和加密密码,并整理后端目录
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
import os
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
_backend = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(_backend))
|
||||
_root = _backend.parent / "Temp" / "pytest"
|
||||
_root.mkdir(parents=True, exist_ok=True)
|
||||
os.environ["EUNIV_SECRET_KEY"] = "test-secret-key"
|
||||
os.environ["EUNIV_DB_PATH"] = str(_root / "euniv.db")
|
||||
@@ -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"}
|
||||
Reference in New Issue
Block a user