feat: 退出时选择离开系统或删除账号
This commit is contained in:
@@ -10,7 +10,7 @@ import time
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from dataclasses import dataclass
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy import delete, select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from euniv.client import (
|
||||
@@ -580,15 +580,16 @@ def _live(db: Session, employee_id: str, client_factory, read):
|
||||
|
||||
|
||||
def revoke_credential(db: Session, employee_id: str) -> None:
|
||||
"""从本平台删除账号,并停掉定时任务和已经开始的学习。"""
|
||||
from euniv.models import FinishedCourse
|
||||
|
||||
with _credential_lock(employee_id):
|
||||
account = db.get(Account, employee_id)
|
||||
if account is None:
|
||||
return
|
||||
db.refresh(account)
|
||||
account.credential_cipher = ""
|
||||
account.password_cipher = ""
|
||||
account.sessions_valid_after = datetime.now(timezone.utc)
|
||||
db.commit()
|
||||
if account is not None:
|
||||
db.refresh(account)
|
||||
db.execute(delete(FinishedCourse).where(FinishedCourse.employee_id == employee_id))
|
||||
db.delete(account)
|
||||
db.commit()
|
||||
from euniv.autostudy import stop_account
|
||||
from euniv.study_push import close_account
|
||||
|
||||
|
||||
@@ -352,16 +352,32 @@ def test_browser_session_logout_and_revoke(monkeypatch) -> None:
|
||||
account = db.get(Account, "6650000146")
|
||||
assert account is not None
|
||||
assert account.credential_cipher
|
||||
again = client.post("/api/login", json={"employee_id": "6650000146", "password": "secret-pass"})
|
||||
assert again.status_code == 200
|
||||
assert calls["login"] == 1
|
||||
assert client.post("/api/revoke").status_code == 200
|
||||
assert client.get("/api/session").status_code == 401
|
||||
account.study_schedule_enabled = True
|
||||
db.commit()
|
||||
with SessionLocal() as db:
|
||||
account = db.get(Account, "6650000146")
|
||||
assert account is not None
|
||||
assert account.credential_cipher == ""
|
||||
assert account.password_cipher == ""
|
||||
assert account.study_schedule_enabled is True
|
||||
again = client.post("/api/login", json={"employee_id": "6650000146", "password": "secret-pass"})
|
||||
assert again.status_code == 200
|
||||
assert calls["login"] == 1
|
||||
from euniv.autostudy import _wanted
|
||||
from euniv.models import FinishedCourse
|
||||
|
||||
_wanted["6650000146"] = object()
|
||||
with SessionLocal() as db:
|
||||
db.add(FinishedCourse(employee_id="6650000146", knowledge_id="course-1"))
|
||||
db.commit()
|
||||
assert client.post("/api/revoke").status_code == 200
|
||||
assert client.get("/api/session").status_code == 401
|
||||
assert "6650000146" not in _wanted
|
||||
with SessionLocal() as db:
|
||||
from sqlalchemy import select
|
||||
|
||||
account = db.get(Account, "6650000146")
|
||||
assert account is None
|
||||
left = db.scalars(select(FinishedCourse).where(FinishedCourse.employee_id == "6650000146")).all()
|
||||
assert left == []
|
||||
|
||||
|
||||
def test_logout_keeps_the_other_browser_until_revoke(monkeypatch) -> None:
|
||||
|
||||
Reference in New Issue
Block a user