feat(accounts): 账号列表按归属筛选,批量分配支持按筛选全选
原来只有 assigned_only(已分配),筛不出「未分配」,也看不到账号归谁。 新增 assign_status/assigned_user_id 可与标签、CK、搜索等叠加; BatchAssign 支持 all_matching,「筛出未分配 → 一键分给某人」不必先勾选。
This commit is contained in:
@@ -0,0 +1,184 @@
|
||||
"""账号管理的「归属筛选 + 快速分配」。
|
||||
|
||||
背景:账号列表原先只能用 `assigned_only` 表达「已分配」,**没有「未分配」**,
|
||||
也看不到、筛不出「这个账号归谁」。于是「把一批还没主的账号分给客服」只能
|
||||
靠人肉翻页勾选。
|
||||
|
||||
本文件守住两件事:
|
||||
|
||||
1. `GET /accounts` 的归属筛选(unassigned / assigned / 指定客服)能生效,
|
||||
并且与标签等条件**同时**生效(综合筛选,不是互相覆盖);
|
||||
2. `POST /accounts/batch-assign` 支持 `all_matching` —— 按当前筛选解析目标,
|
||||
「筛出未分配 → 一键分给某人」不必先勾选。显式 `account_ids` 的旧路径不变。
|
||||
"""
|
||||
|
||||
from tests.support.db import get_test_engine, make_test_session
|
||||
from web.backend.models import Account, User
|
||||
from web.backend.routers.accounts import batch_assign_accounts, list_accounts
|
||||
from web.backend.schemas import BatchAssign
|
||||
|
||||
|
||||
class _Base:
|
||||
def setup_method(self):
|
||||
self.engine = get_test_engine()
|
||||
self.session = make_test_session(self.engine)
|
||||
self.admin = User(username="af-admin", password_hash="h", role="super_admin")
|
||||
self.support = User(username="af-kf", password_hash="h", role="support")
|
||||
self.other = User(username="af-kf2", password_hash="h", role="support")
|
||||
self.session.add_all([self.admin, self.support, self.other])
|
||||
self.session.commit()
|
||||
|
||||
def teardown_method(self):
|
||||
self.session.close()
|
||||
|
||||
def make_account(self, name: str, **kwargs) -> Account:
|
||||
account = Account(
|
||||
username=name,
|
||||
password="p",
|
||||
email=f"{name}@e.com",
|
||||
email_password="ep",
|
||||
# 默认持有 CK —— 分配接口只接受「还拿着 CK」的账号
|
||||
cookie=kwargs.pop("cookie", "acf_uid=1"),
|
||||
**kwargs,
|
||||
)
|
||||
self.session.add(account)
|
||||
self.session.commit()
|
||||
return account
|
||||
|
||||
def names(self, **kwargs) -> list[str]:
|
||||
"""按归属条件查列表,返回用户名(沿用路由真实调用方式)。
|
||||
|
||||
未显式传的参数保持默认值;`Query(...)` 默认对象由 `_query_str` /
|
||||
`isinstance` 兜住,这正是路由函数能被直接调用的原因。
|
||||
"""
|
||||
params = {
|
||||
"assigned_only": False,
|
||||
"tag": "",
|
||||
"has_cookie": False,
|
||||
"search": "",
|
||||
"page": 1,
|
||||
"page_size": 50,
|
||||
"include_sensitive": False,
|
||||
"db": self.session,
|
||||
"current": self.admin,
|
||||
}
|
||||
params.update(kwargs)
|
||||
result = list_accounts(**params)
|
||||
assert isinstance(result, dict)
|
||||
return [item.username for item in result["items"]]
|
||||
|
||||
|
||||
class TestAssignFilter(_Base):
|
||||
def test_unassigned_only(self):
|
||||
self.make_account("free-1")
|
||||
self.make_account("owned-1", assigned_to=self.support.id)
|
||||
|
||||
assert self.names(assign_status="unassigned") == ["free-1"]
|
||||
|
||||
def test_assigned_only(self):
|
||||
self.make_account("free-2")
|
||||
self.make_account("owned-2", assigned_to=self.support.id)
|
||||
|
||||
assert self.names(assign_status="assigned") == ["owned-2"]
|
||||
|
||||
def test_filter_by_specific_support(self):
|
||||
self.make_account("a-of-kf", assigned_to=self.support.id)
|
||||
self.make_account("a-of-kf2", assigned_to=self.other.id)
|
||||
self.make_account("a-free")
|
||||
|
||||
assert self.names(assigned_user_id=self.other.id) == ["a-of-kf2"]
|
||||
|
||||
def test_combines_with_tag(self):
|
||||
"""综合筛选:归属 + 标签要**同时**生效,而不是后一个覆盖前一个。"""
|
||||
self.make_account("tag-a-free", tag="10.1-测试")
|
||||
self.make_account("tag-b-free", tag="10.2-测试")
|
||||
self.make_account("tag-a-owned", tag="10.1-测试", assigned_to=self.support.id)
|
||||
|
||||
assert self.names(assign_status="unassigned", tag="10.1-测试") == ["tag-a-free"]
|
||||
|
||||
|
||||
class TestAllMatchingAssign(_Base):
|
||||
def test_assigns_every_matching_unassigned(self):
|
||||
free = [self.make_account(f"mf-{i}") for i in range(3)]
|
||||
# 已归别人:这一批里**不能**被动到
|
||||
taken = self.make_account("mf-taken", assigned_to=self.other.id)
|
||||
|
||||
result = batch_assign_accounts(
|
||||
BatchAssign(
|
||||
all_matching=True,
|
||||
assign_status="unassigned",
|
||||
assigned_to=self.support.id,
|
||||
),
|
||||
db=self.session,
|
||||
current=self.admin,
|
||||
)
|
||||
|
||||
assert result["count"] == 3
|
||||
for account in free:
|
||||
self.session.refresh(account)
|
||||
assert account.assigned_to == self.support.id
|
||||
self.session.refresh(taken)
|
||||
assert taken.assigned_to == self.other.id
|
||||
|
||||
def test_all_matching_respects_tag(self):
|
||||
self.make_account("mt-a", tag="A")
|
||||
self.make_account("mt-b", tag="B")
|
||||
|
||||
result = batch_assign_accounts(
|
||||
BatchAssign(all_matching=True, tag="A", assigned_to=self.support.id),
|
||||
db=self.session,
|
||||
current=self.admin,
|
||||
)
|
||||
|
||||
assert result["count"] == 1
|
||||
assert self.names(assigned_user_id=self.support.id) == ["mt-a"]
|
||||
|
||||
def test_unassign_all_matching(self):
|
||||
"""留空目标 = 取消分配,同样支持按筛选全选。"""
|
||||
a = self.make_account("mu-a", assigned_to=self.support.id)
|
||||
b = self.make_account("mu-b", assigned_to=self.support.id)
|
||||
|
||||
result = batch_assign_accounts(
|
||||
BatchAssign(all_matching=True, assign_status="assigned", assigned_to=None),
|
||||
db=self.session,
|
||||
current=self.admin,
|
||||
)
|
||||
|
||||
assert result["count"] == 2
|
||||
for account in (a, b):
|
||||
self.session.refresh(account)
|
||||
assert account.assigned_to is None
|
||||
|
||||
def test_explicit_ids_still_work(self):
|
||||
"""旧路径(显式勾选)不能被 all_matching 改动带坏。"""
|
||||
a = self.make_account("me-a")
|
||||
b = self.make_account("me-b")
|
||||
|
||||
result = batch_assign_accounts(
|
||||
BatchAssign(account_ids=[a.id], assigned_to=self.support.id),
|
||||
db=self.session,
|
||||
current=self.admin,
|
||||
)
|
||||
|
||||
assert result["count"] == 1
|
||||
self.session.refresh(a)
|
||||
self.session.refresh(b)
|
||||
assert a.assigned_to == self.support.id
|
||||
assert b.assigned_to is None
|
||||
|
||||
def test_all_matching_without_match_is_rejected(self):
|
||||
import pytest
|
||||
from fastapi import HTTPException
|
||||
|
||||
self.make_account("mn-a", assigned_to=self.other.id)
|
||||
with pytest.raises(HTTPException) as exc:
|
||||
batch_assign_accounts(
|
||||
BatchAssign(
|
||||
all_matching=True,
|
||||
assign_status="unassigned",
|
||||
assigned_to=self.support.id,
|
||||
),
|
||||
db=self.session,
|
||||
current=self.admin,
|
||||
)
|
||||
assert exc.value.status_code == 400
|
||||
Reference in New Issue
Block a user