Archived
拆掉两类垫片的最后一处:auth_system 原为同步 sqlite3 栈,web 层与 services 的
worker 只能靠 asyncio.to_thread 逐个桥接才不阻塞事件循环——每次 DB 访问都白付一次
线程池调度,且「同步调异步」的边界散落在 100+ 个调用点。B1 已把 worker 变成协程,
本批把访问层本身也迁到原生异步,两条线在此合拢。
改动(按子系统):
- auth_system:db.py 照 core/db.py 现有范式重建为原生异步访问层(aiosqlite + WAL)
——读路径 _q 每次独立连接、无锁、可并发;写路径 _x / write_transaction 用
asyncio.Lock 串行化,写者不阻塞读者。users / cards / credits / ledger / logs /
settings / mail / geo / backup 九模块随之全部 async/await;security.py 是纯计算
(pbkdf2/normalize)不动。__init__ 去掉 _DB_LOCK 与 get_conn 导出。
- web:app / deps / billing / tasks_registry 与 routers/{admin_site,auth,music,
register,user} 直接 await,拆掉包裹的 to_thread。deps.secret_key 改 async,
连带 _parse_cookie 也 async(会话解析要读密钥)。
- services:_taskcore / tasks / downloads / podcast 的账本调用与查询直接 await。
4 处 start_task 调用点(download.py)确认已 await。
设计取舍(为什么这么做,而不是直接包一层):
- 锁按**事件循环**隔离(_LOOP_LOCKS 以 id(loop) 为键):asyncio.Lock 绑定创建它的
loop,应用主 loop 与测试各自的 loop 并存,跨 loop 复用会抛 "bound to a different
event loop"。
- WAL 切换用**独立锁** _wal_lock 而非 _write_lock:execute() 已持写锁,再在锁内
connect→_configure 会自锁死(asyncio.Lock 非重入)。切换 + 标记在专用锁内串行并
二次复查,否则并发首连会同时执行 PRAGMA journal_mode=WAL 争排他锁报
database is locked(core/db.py 同款教训,实测复现)。
- force_wal 保留:journal_mode 持久化在库文件头,库文件被删重建时新文件不继承,
必须重设。exists() 判定须在 aiosqlite.connect **之前**(connect 会创建空文件)。
- connect() 用 DB_PATH.parent 兜底建目录,优于 DATA_DIR:生产两者相同,但测试
monkeypatch 只换 DB_PATH 时兜底仍能命中正确目录。
留在 to_thread 上的仅剩**正当的阻塞卸载**,不是同步门面:PBKDF2 的 10 万次迭代
(登录链路每次都要跑)、smtplib 发信、备份文件的 copy2 / os.replace、secret.key
的文件 IO。原有 aria2 / downloader / zip 侧的 to_thread 未动。
不变量逐条保持(迁移只改执行模型,不改语义):
- ledger_settle 幂等:账本非 open 直接 return 0,重启后重放不重复退款;
- 终态先到先得:_mark_task_terminal 对已 done/error/canceled 的任务直接 return,
堵住 canceled→error 的非法迁移边;
- settle 必须在任务锁**外**调用(ledger_settle 要抢 SQLite 写锁,busy 最长 15s,
持任务锁会让全站进度/提交/取消随库停摆);
- reserve_and_open 的预扣与开账同事务,消除原两步之间的崩溃丢账窗口。
路径真源红线:DATA_DIR / DB_PATH / BACKUP_DIR / SECRET_KEY_PATH 仍是 auth_system.db
的**模块全局**(用与 core.db 相同的表达式,但**不是**它的别名)。测试只 patch
auth_system.db.*,若改成 core.db 的别名会静默把测试写到生产库。已核 data/site.db
读写前后 sha256 完全一致。
顺带清掉 get_conn:迁移后它只剩「转调 connect()」一层空壳、全仓零消费者(测试
直接开裸连接、备份用 db.connect()),保留等于留一条「拿裸连接自己写」的口子。
测试:19 个文件同步迁移——10 个文件的 web.deps.secret_key 替身改 async;
test_db_conn.py 的多线程首连用例按异步语义拆成 2 条(单 loop gather(16) 覆盖批量
并发首连、WAL 就绪后 8 线程各自 loop 建连覆盖跨 loop);test_billing_scope.py 去掉
多余的 @pytest.mark.anyio;被 await 且**正常返回**的 monkeypatch 替身一律改 async
(仅 raise 的同步替身仍可用)。
门禁:ruff 全过 / pyright 0-0-0(standard 模式含 tests 与顶层模块)/ pytest
384 项全绿(0 失败 0 error 0 skipped;383→384 因 test_db_conn 拆用例)。
data/site.db 读写前后 sha256 一致(a19f5db…6487),无 -wal/-shm 边车残留。
下一步 B3:删 core/sync_http.py、core/async_bridge.py、core/http_errors.py 及
tests/test_async_bridge.py——三者在生产侧已零消费者(http_errors 仅 sync_http
自用,requests 已非声明依赖)。
207 lines
7.9 KiB
Python
207 lines
7.9 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""register 端点失败路径契约(restore_verify_code / 防刷闸 / create_user 异常映射)。
|
|
|
|
test_register.py 覆盖成功主路径;本文件钉住五条「还原验证码」失败路径与
|
|
create_user 的三种异常映射,作为 register 拆解的等价性验证器。
|
|
|
|
验证码由 check_verify_code 以 DELETE 原子消费:失败路径后 email_codes 行
|
|
仍然存在 ⇔ restore_verify_code 确实被调用。以此做实证断言,不 mock 还原。
|
|
"""
|
|
import asyncio
|
|
import sqlite3
|
|
import time
|
|
|
|
import pytest
|
|
from fastapi.testclient import TestClient
|
|
|
|
import auth_system as auth
|
|
import core.db as db
|
|
|
|
SECRET = 'register-flow-test-secret'
|
|
|
|
|
|
async def _fake_secret_key() -> str:
|
|
return SECRET
|
|
|
|
|
|
def _connect() -> sqlite3.Connection:
|
|
"""直连沙箱库做只读/播种(auth.get_conn 已异步化,测试用裸连接更直接)。"""
|
|
conn = sqlite3.connect(str(auth.db.DB_PATH))
|
|
conn.row_factory = sqlite3.Row
|
|
return conn
|
|
|
|
|
|
@pytest.fixture()
|
|
def env(tmp_path, monkeypatch):
|
|
db_path = tmp_path / 'site.db'
|
|
monkeypatch.setattr(auth.db, 'DATA_DIR', tmp_path)
|
|
monkeypatch.setattr(auth.db, 'DB_PATH', db_path)
|
|
monkeypatch.setattr(auth.db, 'BACKUP_DIR', tmp_path / 'backups')
|
|
monkeypatch.setattr(auth.db, 'SECRET_KEY_PATH', tmp_path / 'secret.key')
|
|
monkeypatch.setattr(db, 'DATA_DIR', tmp_path)
|
|
monkeypatch.setattr(db, 'DB_PATH', db_path)
|
|
asyncio.run(auth.init_db())
|
|
monkeypatch.setattr('web.deps.secret_key', _fake_secret_key)
|
|
import web.auth_state as auth_state
|
|
auth_state._SEND_CODE_IP.clear()
|
|
from web.app import create_app
|
|
return TestClient(create_app())
|
|
|
|
|
|
def _seed_code(email: str, code: str, purpose: str = 'register'):
|
|
conn = _connect()
|
|
conn.execute('DELETE FROM email_codes WHERE email = ? AND purpose = ?', (email, purpose))
|
|
conn.execute('INSERT INTO email_codes (email, code, expires_at, created_at, purpose) '
|
|
'VALUES (?, ?, ?, ?, ?)',
|
|
(email, code, int(time.time()) + 600, int(time.time()), purpose))
|
|
conn.commit()
|
|
conn.close()
|
|
|
|
|
|
def _code_rows(email: str, purpose: str = 'register') -> list[str]:
|
|
conn = _connect()
|
|
rows = conn.execute('SELECT code FROM email_codes WHERE email = ? AND purpose = ?',
|
|
(email, purpose)).fetchall()
|
|
conn.close()
|
|
return [r['code'] for r in rows]
|
|
|
|
|
|
def _register_logs() -> list[tuple[str, str]]:
|
|
conn = _connect()
|
|
rows = conn.execute("SELECT type, detail FROM logs WHERE action = '注册'").fetchall()
|
|
conn.close()
|
|
return [(r['type'], r['detail']) for r in rows]
|
|
|
|
|
|
def _post(env, username: str, email: str, code: str, **extra):
|
|
return env.post('/auth/register', json={
|
|
'username': username, 'password': 'Passw0rd12', 'email': email,
|
|
'code': code, 'agree': '1', **extra})
|
|
|
|
|
|
def test_register_input_validation_order(env):
|
|
"""四项入参校验按序:协议 → 用户名 → 密码 → 邮箱长度。"""
|
|
payload = {'username': 'okuser', 'password': 'Passw0rd12', 'email': 'v@example.com',
|
|
'code': '000000', 'agree': '1'}
|
|
r = env.post('/auth/register', json={**payload, 'agree': '0'})
|
|
assert r.json()['message'].startswith('请先阅读并同意')
|
|
|
|
r = env.post('/auth/register', json={**payload, 'username': 'ab'})
|
|
assert '用户名需 3-20 位' in r.json()['message']
|
|
|
|
r = env.post('/auth/register', json={**payload, 'password': 'short'})
|
|
assert '密码长度需在 8-128 位之间' in r.json()['message']
|
|
|
|
r = env.post('/auth/register', json={**payload, 'email': 'e' * 300})
|
|
assert r.json()['message'] == '邮箱格式不正确'
|
|
|
|
|
|
def test_dup_username_restores_code_end_to_end(env):
|
|
"""用户名重名拒绝后,同一验证码无需重发即可再次使用(restore 实证)。"""
|
|
asyncio.run(auth.create_user('taken', 'Passw0rd12'))
|
|
_seed_code('x@example.com', '123456')
|
|
|
|
r = _post(env, 'taken', 'x@example.com', '123456')
|
|
assert r.json()['message'] == '用户名已被注册'
|
|
|
|
r2 = _post(env, 'freshname', 'x@example.com', '123456')
|
|
assert r2.json()['success'] is True
|
|
|
|
|
|
def test_dup_email_keeps_code_row(env):
|
|
"""邮箱重名拒绝后验证码行仍在(被 check_verify_code 消费后又还原)。"""
|
|
asyncio.run(auth.create_user('holder', 'Passw0rd12', email='y@example.com'))
|
|
_seed_code('y@example.com', '111111')
|
|
|
|
r = _post(env, 'maildup', 'y@example.com', '111111')
|
|
assert '该邮箱已被注册' in r.json()['message']
|
|
assert _code_rows('y@example.com') == ['111111']
|
|
|
|
|
|
def test_ip_exceeded_restores_code_and_logs(env, monkeypatch):
|
|
"""IP 闸触发:还原验证码 + 写失败日志 + 专属文案。"""
|
|
async def _exceeded(ip, **k):
|
|
return True
|
|
|
|
monkeypatch.setattr(auth, 'register_ip_exceeded', _exceeded)
|
|
_seed_code('ip@example.com', '222222')
|
|
|
|
r = _post(env, 'ipuser', 'ip@example.com', '222222')
|
|
assert r.status_code == 400
|
|
assert '注册过于频繁' in r.json()['message']
|
|
assert _code_rows('ip@example.com') == ['222222']
|
|
assert ('fail', '同 IP 24h 注册次数超限') in _register_logs()
|
|
|
|
|
|
def test_device_used_grants_zero_credits(env):
|
|
"""同设备重复注册:账号照建、赠送归零;首注设备正常赠送。"""
|
|
_seed_code('d1@example.com', '333333')
|
|
r1 = _post(env, 'firstdev', 'd1@example.com', '333333', device_id='dev-9')
|
|
assert r1.json()['success'] is True
|
|
assert '已赠送' in r1.json()['message']
|
|
|
|
_seed_code('d2@example.com', '444444')
|
|
r2 = _post(env, 'seconddev', 'd2@example.com', '444444', device_id='dev-9')
|
|
assert r2.json()['success'] is True
|
|
assert r2.json()['message'] == '注册成功'
|
|
|
|
conn = _connect()
|
|
row = conn.execute("SELECT duration_seconds FROM users WHERE username = 'seconddev'").fetchone()
|
|
conn.close()
|
|
assert row['duration_seconds'] == 0
|
|
|
|
|
|
def test_integrityerror_maps_and_restores(env, monkeypatch):
|
|
"""并发撞唯一索引(建号 IntegrityError):统一文案 + 还原验证码。"""
|
|
def _boom(*a, **k):
|
|
raise sqlite3.IntegrityError('UNIQUE constraint failed: users.username')
|
|
|
|
monkeypatch.setattr(auth, 'create_user', _boom)
|
|
_seed_code('ie@example.com', '555555')
|
|
|
|
r = _post(env, 'ieuser', 'ie@example.com', '555555')
|
|
assert r.status_code == 400
|
|
assert r.json()['message'] == '用户名或邮箱已被注册,请换一个再试'
|
|
assert _code_rows('ie@example.com') == ['555555']
|
|
|
|
|
|
def test_valueerror_maps_and_restores(env, monkeypatch):
|
|
"""create_user 业务拒绝(ValueError):原文案透出 + 失败日志 + 还原验证码。"""
|
|
def _boom(*a, **k):
|
|
raise ValueError('用户名含敏感词')
|
|
|
|
monkeypatch.setattr(auth, 'create_user', _boom)
|
|
_seed_code('ve@example.com', '666666')
|
|
|
|
r = _post(env, 'veuser', 've@example.com', '666666')
|
|
assert r.status_code == 400
|
|
assert r.json()['message'] == '用户名含敏感词'
|
|
assert _code_rows('ve@example.com') == ['666666']
|
|
assert ('fail', '用户名含敏感词') in _register_logs()
|
|
|
|
|
|
def test_user_none_maps_and_restores(env, monkeypatch):
|
|
"""建号事务提交却读不回:按注册失败处理 + 还原验证码 + 失败日志。"""
|
|
async def _none(*a, **k):
|
|
return None
|
|
|
|
monkeypatch.setattr(auth, 'create_user', _none)
|
|
_seed_code('none@example.com', '777777')
|
|
|
|
r = _post(env, 'noneuser', 'none@example.com', '777777')
|
|
assert r.status_code == 400
|
|
assert r.json()['message'] == '注册失败,请稍后重试'
|
|
assert _code_rows('none@example.com') == ['777777']
|
|
assert ('fail', '建号后读取用户失败') in _register_logs()
|
|
|
|
|
|
def test_unexpected_error_is_500(env, monkeypatch):
|
|
"""非预期异常:泛化文案 + HTTP 500(不透出内部信息)。"""
|
|
def _boom(*a, **k):
|
|
raise RuntimeError('db busy')
|
|
|
|
monkeypatch.setattr(auth, 'check_verify_code', _boom)
|
|
r = _post(env, 'boomuser', 'boom@example.com', '888888')
|
|
assert r.status_code == 500
|
|
assert r.json()['message'] == '注册失败,请稍后重试'
|