feat(monitor): 超级编导账号密码 + token 失效自动续期
Deploy VitePress site to Pages / build (push) Canceled after 0s
Deploy VitePress site to Pages / Deploy (push) Canceled after 0s

设置里新增「超级编导账号」「超级编导密码」(密码是 secret,不回显)。查询/
自动追加撞上 4017/5006 等登录失效错误时,用它们调密码登录接口换新 token,
写回设置,再把整段操作重试一次。

- 续期加锁:超级编导是单登录态,新登录会顶掉旧 token;并发续期只会互相作废。
- 重试放在整段而不是单个请求:一次预览并发十几个请求,请求层重试会变成
  十几个并发登录。
- 没有 token 但配了账号密码时也直接登录,不必先粘一次 token。
- 登录失败(1002 密码错误 / 10006 未设密码)原样报出来,不吞成「token 失效」。
This commit is contained in:
2026-10-25 11:08:23 +08:00
parent 3a5b973bc2
commit d30ccb196a
3 changed files with 293 additions and 36 deletions
+110 -3
View File
@@ -228,14 +228,16 @@ class TestPreview:
assert len(result["rows"]) == 2
@pytest.mark.asyncio
async def test_without_a_token_the_message_says_where_to_put_it(self, monkeypatch):
async def test_without_a_token_the_message_says_where_to_put_it(
self, session, monkeypatch
):
async def fake_token(session, platform=superdir.adapters.PLATFORM_DY):
return ""
monkeypatch.setattr(superdir, "get_token", fake_token)
with pytest.raises(superdir.SuperDirError) as excinfo:
await superdir.preview(None, "2026-10-21", "2026-10-21")
await superdir.preview(session, "2026-10-21", "2026-10-21")
assert "设置" in str(excinfo.value)
@@ -315,7 +317,7 @@ class TestErrorHints:
error = superdir._error({"code": 4017, "message": "账号已在别处登录"})
assert "4017" in str(error)
assert "重新从超级编导客户端取一次" in str(error)
assert "自动用账号密码续期" in str(error)
@pytest_asyncio.fixture
@@ -423,6 +425,111 @@ class TestCreateTask:
await superdir.append_task(session, 99999, [AWEME_A])
class TestTokenRenewal:
"""Token 过期(4017/5006)时用设置里的账号密码自动续期,并重试整段。"""
async def _set_token(
self,
session,
value: str = "old-token",
username: str = "15655035256",
password: str = "pw",
):
await superdir.app_settings.update(
session,
{
"platform.dy.superdir_token": value,
"platform.dy.superdir_username": username,
"platform.dy.superdir_password": password,
},
"dy",
)
@pytest.mark.asyncio
async def test_an_expired_token_is_renewed_and_the_operation_retried(
self, session, monkeypatch
):
await self._set_token(session)
seen = []
async def fake_login(username, password):
seen.append((username, password))
return {"token": "new-token"}
monkeypatch.setattr(superdir, "_login", fake_login)
calls = []
async def operation(token):
calls.append(token)
if token == "old-token":
raise superdir.SuperDirError(4017, "登录已过期")
return "ok"
result = await superdir._run_with_token(session, "dy", operation)
assert result == "ok"
assert calls == ["old-token", "new-token"]
assert seen == [("15655035256", "pw")]
# 新 token 写回设置,后面的查询就不会再撞一次墙。
assert await superdir.get_token(session) == "new-token"
@pytest.mark.asyncio
async def test_a_non_auth_error_is_not_retried(self, session, monkeypatch):
await self._set_token(session)
async def must_not_login(*args, **kwargs):
raise AssertionError("非登录类错误不该去登录")
monkeypatch.setattr(superdir, "_login", must_not_login)
calls = []
async def operation(token):
calls.append(token)
raise superdir.SuperDirError(500, "服务端错误")
with pytest.raises(superdir.SuperDirError):
await superdir._run_with_token(session, "dy", operation)
assert calls == ["old-token"]
@pytest.mark.asyncio
async def test_without_credentials_the_message_says_where_to_configure(
self, session
):
await self._set_token(session, username="", password="")
async def operation(token):
raise superdir.SuperDirError(4017, "登录已过期")
with pytest.raises(superdir.SuperDirError) as excinfo:
await superdir._run_with_token(session, "dy", operation)
assert "设置" in str(excinfo.value)
@pytest.mark.asyncio
async def test_a_wrong_password_surfaces_1002(self, session, monkeypatch):
"""登录失败不能吞成「token 失效」,要把平台的原话(含剩余次数)报出来。"""
await self._set_token(session)
async def fake_login(*args, **kwargs):
raise superdir._error(
{"code": 1002, "message": "登录密码错误,今日剩余尝试次数 4 次"}
)
monkeypatch.setattr(superdir, "_login", fake_login)
async def operation(token):
raise superdir.SuperDirError(4017, "登录已过期")
with pytest.raises(superdir.SuperDirError) as excinfo:
await superdir._run_with_token(session, "dy", operation)
assert "1002" in str(excinfo.value)
assert "密码" in str(excinfo.value)
class TestAutoAppend:
"""每隔 N 分钟自动把新发布的作品补进同一个任务。"""