302 lines
9.3 KiB
Python
302 lines
9.3 KiB
Python
"""验证 DouyuTask 的 account_assigned_to 字段与任务查询优化。"""
|
|
|
|
import asyncio
|
|
|
|
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,
|
|
PlatformFulfillmentEvent,
|
|
PlatformFulfillmentTask,
|
|
User,
|
|
)
|
|
from web.backend.routers import douyu as douyu_router
|
|
from web.backend.routers.douyu import (
|
|
_require_task_account_access,
|
|
_visible_tasks_query,
|
|
)
|
|
from web.backend.schemas import DouyuTaskBatchRequest
|
|
from web.backend.services.douyu_service import create_douyu_planned_tasks
|
|
from web.backend.services.platform_fulfillment import service as platform_service
|
|
|
|
|
|
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()
|
|
|
|
|
|
def test_platform_exchange_checks_eligibility_before_creating_task(monkeypatch):
|
|
init_test_schema()
|
|
db: Session = make_test_session()
|
|
try:
|
|
owner = User(username="elig-owner", password_hash="h", role="support")
|
|
operator = User(
|
|
username="elig-operator",
|
|
password_hash="h",
|
|
role="support",
|
|
custom_permissions=[
|
|
"douyu:task",
|
|
"account:view_assigned",
|
|
"platform_fulfillment:use",
|
|
],
|
|
)
|
|
db.add_all([owner, operator])
|
|
db.flush()
|
|
account = Account(
|
|
username="elig-account",
|
|
password="p",
|
|
email="elig@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="elig-order",
|
|
source_order_item_id="elig-item",
|
|
platform_order_id="elig-platform-order",
|
|
account_id=account.id,
|
|
commodity_id="commodity-1",
|
|
required_points=1280,
|
|
binding_status="bound",
|
|
execution_status="bound",
|
|
exchange_status="not_opened",
|
|
)
|
|
db.add(task)
|
|
db.commit()
|
|
refreshed: list[int] = []
|
|
|
|
def fake_refresh(db_session, *, task_id, current):
|
|
refreshed.append(task_id)
|
|
row = db_session.get(PlatformFulfillmentTask, task_id)
|
|
assert row is not None and current.id == operator.id
|
|
row.exchange_status = "eligible"
|
|
row.issue_code = ""
|
|
row.last_error = ""
|
|
db_session.commit()
|
|
return platform_service.get_task_detail(db_session, task_id)
|
|
|
|
monkeypatch.setattr(
|
|
platform_service,
|
|
"refresh_exchange_eligibility",
|
|
fake_refresh,
|
|
)
|
|
monkeypatch.setattr(
|
|
douyu_router,
|
|
"start_douyu_batch",
|
|
lambda **_kwargs: None,
|
|
)
|
|
|
|
result = asyncio.run(
|
|
douyu_router.create_task_batch(
|
|
DouyuTaskBatchRequest(
|
|
account_ids=[account.id],
|
|
task_type="exchange_goods",
|
|
handbook_scope="elite",
|
|
concurrency=1,
|
|
payload={"commodity_id": "commodity-1"},
|
|
platform_task_id=task.id,
|
|
),
|
|
db=db,
|
|
current=operator,
|
|
)
|
|
)
|
|
|
|
assert refreshed == [task.id]
|
|
assert result["count"] == 1
|
|
created = db.query(DouyuTask).filter(DouyuTask.batch_id == result["batch_id"]).one()
|
|
assert created.platform_task_id == task.id
|
|
event = (
|
|
db.query(PlatformFulfillmentEvent)
|
|
.filter(
|
|
PlatformFulfillmentEvent.task_id == task.id,
|
|
PlatformFulfillmentEvent.event_type == "douyu_operation_started",
|
|
)
|
|
.one()
|
|
)
|
|
assert event.detail_json["taskType"] == "exchange_goods"
|
|
finally:
|
|
db.rollback()
|
|
db.close()
|