195 lines
6.0 KiB
Python
195 lines
6.0 KiB
Python
"""验证 DouyuTask 的 account_assigned_to 字段与任务查询优化。"""
|
|
|
|
import pytest
|
|
from fastapi import HTTPException
|
|
from sqlalchemy.orm import Session
|
|
|
|
from tests.support.db import init_test_schema, make_test_session
|
|
from web.backend.models import Account, DouyuTask, PlatformFulfillmentTask, User
|
|
from web.backend.routers.douyu import (
|
|
_require_task_account_access,
|
|
_visible_tasks_query,
|
|
)
|
|
from web.backend.services.douyu_service import create_douyu_planned_tasks
|
|
|
|
|
|
def test_douyu_task_assigned_to_flow():
|
|
init_test_schema()
|
|
db: Session = make_test_session()
|
|
try:
|
|
db.query(DouyuTask).delete()
|
|
db.commit()
|
|
# 创建测试客服用户与账号
|
|
user_a = User(
|
|
username="test_kefu_a",
|
|
password_hash="fake",
|
|
role="support",
|
|
custom_permissions=["douyu:task", "account:view_assigned"],
|
|
is_active=True,
|
|
)
|
|
user_b = User(
|
|
username="test_kefu_b",
|
|
password_hash="fake",
|
|
role="support",
|
|
custom_permissions=["douyu:task", "account:view_assigned"],
|
|
is_active=True,
|
|
)
|
|
db.add_all([user_a, user_b])
|
|
db.flush()
|
|
|
|
acc_1 = Account(
|
|
username="dy_user_1",
|
|
password="pwd",
|
|
email="em1@test.com",
|
|
email_password="pwd",
|
|
assigned_to=user_a.id,
|
|
# CK 挂在账号上(2026-10-04 CK 解耦)
|
|
cookie="acf_uid=101; acf_auth=fake",
|
|
)
|
|
acc_2 = Account(
|
|
username="dy_user_2",
|
|
password="pwd",
|
|
email="em2@test.com",
|
|
email_password="pwd",
|
|
assigned_to=user_b.id,
|
|
cookie="acf_uid=102; acf_auth=fake",
|
|
)
|
|
db.add_all([acc_1, acc_2])
|
|
db.flush()
|
|
|
|
# 1. 验证创建任务时,自动继承 account.assigned_to
|
|
batch_id, count = create_douyu_planned_tasks(
|
|
db=db,
|
|
account_ids=[acc_1.id, acc_2.id],
|
|
task_type="query_points",
|
|
handbook_scope="elite",
|
|
created_by=user_a.id,
|
|
)
|
|
assert count == 2
|
|
|
|
tasks = db.query(DouyuTask).filter(DouyuTask.batch_id == batch_id).all()
|
|
task_map = {t.account_id: t for t in tasks}
|
|
assert task_map[acc_1.id].account_assigned_to == user_a.id
|
|
assert task_map[acc_2.id].account_assigned_to == user_b.id
|
|
|
|
# 2. 客服能看到自己创建的任务;被分配账号的客服也能看到。
|
|
query_a = _visible_tasks_query(db, user_a).filter(
|
|
DouyuTask.batch_id == batch_id
|
|
)
|
|
visible_a = query_a.all()
|
|
assert {row.account_id for row in visible_a} == {acc_1.id, acc_2.id}
|
|
|
|
query_b = _visible_tasks_query(db, user_b).filter(
|
|
DouyuTask.batch_id == batch_id
|
|
)
|
|
visible_b = query_b.all()
|
|
assert len(visible_b) == 1
|
|
assert visible_b[0].account_id == acc_2.id
|
|
|
|
# 3. 验证账号重新分配后,关联任务同步变更
|
|
db.query(DouyuTask).filter(DouyuTask.account_id == acc_1.id).update(
|
|
{DouyuTask.account_assigned_to: user_b.id}, synchronize_session=False
|
|
)
|
|
acc_1.assigned_to = user_b.id
|
|
db.commit()
|
|
|
|
# 此时 user_b 可以看到 acc_1 的任务
|
|
visible_b_after = (
|
|
_visible_tasks_query(db, user_b)
|
|
.filter(DouyuTask.batch_id == batch_id)
|
|
.all()
|
|
)
|
|
assert len(visible_b_after) == 2
|
|
finally:
|
|
db.rollback()
|
|
db.close()
|
|
|
|
|
|
def test_platform_task_context_allows_shared_account_operation():
|
|
init_test_schema()
|
|
db: Session = make_test_session()
|
|
try:
|
|
owner = User(
|
|
username="ctx-owner",
|
|
password_hash="h",
|
|
role="support",
|
|
is_active=True,
|
|
)
|
|
operator = User(
|
|
username="ctx-operator",
|
|
password_hash="h",
|
|
role="support",
|
|
custom_permissions=[
|
|
"douyu:task",
|
|
"account:view_assigned",
|
|
"platform_fulfillment:use",
|
|
],
|
|
is_active=True,
|
|
)
|
|
db.add_all([owner, operator])
|
|
db.flush()
|
|
account = Account(
|
|
username="ctx-shared-account",
|
|
password="p",
|
|
email="ctx@test.com",
|
|
email_password="p",
|
|
cookie="acf_uid=1; acf_auth=x",
|
|
assigned_to=owner.id,
|
|
)
|
|
db.add(account)
|
|
db.flush()
|
|
task = PlatformFulfillmentTask(
|
|
source_order_id="ctx-order",
|
|
source_order_item_id="ctx-item",
|
|
platform_order_id="ctx-platform-order",
|
|
account_id=account.id,
|
|
execution_status="allocated",
|
|
)
|
|
db.add(task)
|
|
db.commit()
|
|
|
|
with pytest.raises(HTTPException) as exc_info:
|
|
_require_task_account_access(db, operator, [account.id])
|
|
assert exc_info.value.status_code == 403
|
|
|
|
_require_task_account_access(
|
|
db,
|
|
operator,
|
|
[account.id],
|
|
platform_task_id=task.id,
|
|
)
|
|
batch_id, count = create_douyu_planned_tasks(
|
|
db,
|
|
[account.id],
|
|
"query_elite_status",
|
|
"elite",
|
|
operator.id,
|
|
platform_task_id=task.id,
|
|
)
|
|
assert count == 1
|
|
assert (
|
|
db.query(DouyuTask).filter(DouyuTask.batch_id == batch_id).one().platform_task_id
|
|
== task.id
|
|
)
|
|
|
|
other = Account(
|
|
username="ctx-other-account",
|
|
password="p",
|
|
email="ctx-other@test.com",
|
|
email_password="p",
|
|
cookie="acf_uid=2; acf_auth=x",
|
|
assigned_to=owner.id,
|
|
)
|
|
db.add(other)
|
|
db.commit()
|
|
with pytest.raises(HTTPException):
|
|
_require_task_account_access(
|
|
db,
|
|
operator,
|
|
[other.id],
|
|
platform_task_id=task.id,
|
|
)
|
|
finally:
|
|
db.rollback()
|
|
db.close()
|