feat(weibo): 增加协作 CK 批量复制与商品图片代理

This commit is contained in:
yml
2026-11-12 14:12:50 +08:00
parent 31045e0f2e
commit 0e29717c97
18 changed files with 604 additions and 153 deletions
-128
View File
@@ -267,131 +267,3 @@ class TestMigrationSmoke:
mig_engine.dispose()
with admin_engine.begin() as conn:
conn.execute(text(f"DROP DATABASE IF EXISTS `{mig_db}`;"))
class TestRollbackBackfillScript:
"""回滚前置脚本:把账号侧 CK 搬回 `login_tasks`。
为什么必须有它:S2 之后 CK **只写 `accounts.cookie`、不双写** `login_tasks`,
而降级会 drop 掉 `accounts.cookie` —— 不先搬回去,
**部署期间新拿到的 CK 会永久丢失**(本项目唯一不能承受的损失)。
演练验证过:跑了它再降级,部署期间新拿到的 CK 完整幸存。
"""
def test_moves_account_cookie_back_to_login_tasks(self):
import importlib.util
from pathlib import Path
import web.backend.database as db_module
from tests.support.db import make_test_session
from web.backend.models import Account, LoginTask, User
path = Path(PROJECT_ROOT) / "scripts" / "backfill_login_tasks_from_accounts.py"
spec = importlib.util.spec_from_file_location("rollback_backfill", path)
assert spec and spec.loader
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
engine = get_test_engine()
db_module.SessionLocal.configure(bind=engine)
db = make_test_session(engine)
try:
db.query(LoginTask).filter(LoginTask.batch_id == "rb-test").delete()
db.commit()
user = User(username="rb-user", password_hash="h", role="super_admin")
db.add(user)
db.commit()
account = Account(
username="rb-acc",
password="p",
email="e@e.com",
email_password="ep",
)
db.add(account)
db.commit()
# 账号侧有新 CK;登录记录侧还是旧值(模拟「部署期间新拿到」)
account.cookie = "PHPSESSID=NEW_DEPLOY_WINDOW_CK"
db.add(
LoginTask(
batch_id="rb-test",
account_id=account.id,
status="success",
message="旧行",
cookie="PHPSESSID=OLD",
created_by=user.id,
)
)
db.commit()
result = module.run(db, apply=True)
assert result["updated"] >= 1
db.expire_all()
tasks = (
db.query(LoginTask)
.filter(
LoginTask.account_id == account.id,
LoginTask.cookie.isnot(None),
LoginTask.cookie != "",
)
.all()
)
assert any(t.cookie == "PHPSESSID=NEW_DEPLOY_WINDOW_CK" for t in tasks), (
"部署期间的新 CK 没有搬回登录记录 —— 降级会丢"
)
finally:
db.close()
def test_creates_synthetic_row_when_no_history(self):
"""新代码跑期间新导入、没有任何登录记录的账号,也要能回填。
旧代码靠 `login_tasks` 找 CK,没有行就等于「这账号没 CK」。
"""
import importlib.util
from pathlib import Path
import web.backend.database as db_module
from tests.support.db import make_test_session
from web.backend.models import Account, LoginTask, User
path = Path(PROJECT_ROOT) / "scripts" / "backfill_login_tasks_from_accounts.py"
spec = importlib.util.spec_from_file_location("rollback_backfill2", path)
assert spec and spec.loader
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
engine = get_test_engine()
db_module.SessionLocal.configure(bind=engine)
db = make_test_session(engine)
try:
user = User(username="rb-user2", password_hash="h", role="super_admin")
db.add(user)
db.commit()
account = Account(
username="rb-orphan",
password="p",
email="e@e.com",
email_password="ep",
cookie="PHPSESSID=ORPHAN_CK",
)
db.add(account)
db.commit()
assert (
db.query(LoginTask).filter(LoginTask.account_id == account.id).count()
== 0
)
module.run(db, apply=True)
db.expire_all()
row = (
db.query(LoginTask)
.filter(LoginTask.account_id == account.id)
.order_by(LoginTask.id.desc())
.first()
)
assert row is not None, "没有历史行时必须补一条合成行"
assert row.cookie == "PHPSESSID=ORPHAN_CK"
assert row.batch_id == module.BACKFILL_BATCH
finally:
db.close()