244 lines
6.4 KiB
Python
244 lines
6.4 KiB
Python
from datetime import UTC, datetime, timedelta
|
|
|
|
import pytest
|
|
from fastapi import HTTPException
|
|
|
|
from web.backend.models import Account, DouyuTask, User
|
|
from web.backend.routers.douyu import _prepare_task_batch, _require_batch_owner
|
|
from web.backend.schemas import DouyuTaskBatchRequest
|
|
from web.backend.services.douyu_service import (
|
|
DouyuActivePaymentError,
|
|
create_douyu_planned_tasks,
|
|
find_reusable_active_payment_task,
|
|
)
|
|
|
|
|
|
def _user(db, username: str) -> User:
|
|
user = User(username=username, password_hash="hash", role="operation")
|
|
db.add(user)
|
|
db.flush()
|
|
return user
|
|
|
|
|
|
def _account(db, username: str) -> Account:
|
|
account = Account(
|
|
username=username,
|
|
password="password",
|
|
email=f"{username}@example.com",
|
|
email_password="mail-password",
|
|
cookie=f"acf_uid={username}",
|
|
)
|
|
db.add(account)
|
|
db.flush()
|
|
return account
|
|
|
|
|
|
def test_active_payment_blocks_same_account_across_users(db):
|
|
owner = _user(db, "payment-owner")
|
|
other = _user(db, "payment-other")
|
|
account = _account(db, "payment-account")
|
|
db.commit()
|
|
|
|
first_batch, _ = create_douyu_planned_tasks(
|
|
db,
|
|
[account.id],
|
|
"create_elite_qr",
|
|
"elite",
|
|
owner.id,
|
|
)
|
|
|
|
with pytest.raises(DouyuActivePaymentError) as exc_info:
|
|
create_douyu_planned_tasks(
|
|
db,
|
|
[account.id],
|
|
"create_gold_qr",
|
|
"elite",
|
|
other.id,
|
|
)
|
|
|
|
message = str(exc_info.value)
|
|
assert first_batch in message
|
|
assert "开通精英宝典" in message
|
|
assert "已创建,等待执行" in message
|
|
assert "二维码或支付会话串单" in message
|
|
|
|
task = db.query(DouyuTask).filter(DouyuTask.batch_id == first_batch).one()
|
|
task.status = "stopped"
|
|
db.commit()
|
|
|
|
second_batch, count = create_douyu_planned_tasks(
|
|
db,
|
|
[account.id],
|
|
"create_gold_qr",
|
|
"elite",
|
|
other.id,
|
|
)
|
|
assert second_batch != first_batch
|
|
assert count == 1
|
|
|
|
|
|
def test_active_payment_does_not_block_non_payment_task(db):
|
|
owner = _user(db, "payment-owner")
|
|
account = _account(db, "payment-account")
|
|
db.commit()
|
|
|
|
create_douyu_planned_tasks(
|
|
db,
|
|
[account.id],
|
|
"create_elite_qr",
|
|
"elite",
|
|
owner.id,
|
|
)
|
|
batch_id, count = create_douyu_planned_tasks(
|
|
db,
|
|
[account.id],
|
|
"query_points",
|
|
"elite",
|
|
owner.id,
|
|
)
|
|
|
|
assert batch_id
|
|
assert count == 1
|
|
|
|
|
|
def test_active_payment_does_not_block_distinct_accounts(db):
|
|
owner = _user(db, "payment-owner")
|
|
first = _account(db, "payment-first")
|
|
second = _account(db, "payment-second")
|
|
db.commit()
|
|
|
|
create_douyu_planned_tasks(
|
|
db,
|
|
[first.id],
|
|
"create_elite_qr",
|
|
"elite",
|
|
owner.id,
|
|
)
|
|
batch_id, count = create_douyu_planned_tasks(
|
|
db,
|
|
[second.id],
|
|
"create_elite_qr",
|
|
"elite",
|
|
owner.id,
|
|
)
|
|
|
|
assert batch_id
|
|
assert count == 1
|
|
|
|
|
|
def test_operation_cannot_stop_another_users_batch(db):
|
|
owner = _user(db, "payment-owner")
|
|
other = _user(db, "payment-other")
|
|
admin = User(username="payment-admin", password_hash="hash", role="super_admin")
|
|
account = _account(db, "payment-account")
|
|
db.add(admin)
|
|
db.commit()
|
|
|
|
batch_id, _ = create_douyu_planned_tasks(
|
|
db,
|
|
[account.id],
|
|
"create_elite_qr",
|
|
"elite",
|
|
owner.id,
|
|
)
|
|
|
|
_require_batch_owner(db, owner, batch_id)
|
|
with pytest.raises(HTTPException) as exc_info:
|
|
_require_batch_owner(db, other, batch_id)
|
|
assert exc_info.value.status_code == 403
|
|
|
|
_require_batch_owner(db, admin, batch_id)
|
|
|
|
|
|
def test_reuse_active_payment_reopens_unexpired_qr_and_skips_expired(db):
|
|
owner = _user(db, "payment-reopen-owner")
|
|
other = _user(db, "payment-reopen-other")
|
|
account = _account(db, "payment-reopen-acc")
|
|
db.commit()
|
|
|
|
first = _prepare_task_batch(
|
|
DouyuTaskBatchRequest(
|
|
account_ids=[account.id],
|
|
task_type="create_elite_qr",
|
|
handbook_scope="elite",
|
|
reuse_active_payment=True,
|
|
),
|
|
db,
|
|
owner,
|
|
)
|
|
assert first["success"] is True
|
|
assert first.get("reused") is not True
|
|
|
|
task = db.query(DouyuTask).filter(DouyuTask.batch_id == first["batch_id"]).one()
|
|
task.status = "running"
|
|
task.run_deadline_at = datetime.now(UTC) + timedelta(minutes=5)
|
|
task.result = {"pay_url": "https://pay.example.test/elite-qr"}
|
|
db.commit()
|
|
|
|
reopened = _prepare_task_batch(
|
|
DouyuTaskBatchRequest(
|
|
account_ids=[account.id],
|
|
task_type="create_elite_qr",
|
|
handbook_scope="elite",
|
|
reuse_active_payment=True,
|
|
),
|
|
db,
|
|
other,
|
|
)
|
|
assert reopened["batch_id"] == first["batch_id"]
|
|
assert reopened["reused"] is True
|
|
assert reopened["count"] == 1
|
|
assert db.query(DouyuTask).filter(DouyuTask.account_id == account.id).count() == 1
|
|
|
|
# 过期后的任务不再被复用
|
|
task.run_deadline_at = datetime.now(UTC) - timedelta(seconds=10)
|
|
db.commit()
|
|
assert (
|
|
find_reusable_active_payment_task(
|
|
db,
|
|
account.id,
|
|
task_type="create_elite_qr",
|
|
handbook_scope="elite",
|
|
)
|
|
is None
|
|
)
|
|
|
|
# 鱼翅二维码按金额严格区分:同金额复用,不同金额不复用
|
|
task.status = "stopped"
|
|
db.commit()
|
|
gold_first = _prepare_task_batch(
|
|
DouyuTaskBatchRequest(
|
|
account_ids=[account.id],
|
|
task_type="create_gold_qr",
|
|
handbook_scope="elite",
|
|
payload={"amount": 30},
|
|
reuse_active_payment=True,
|
|
),
|
|
db,
|
|
owner,
|
|
)
|
|
gold_same = _prepare_task_batch(
|
|
DouyuTaskBatchRequest(
|
|
account_ids=[account.id],
|
|
task_type="create_gold_qr",
|
|
handbook_scope="elite",
|
|
payload={"amount": 30},
|
|
reuse_active_payment=True,
|
|
),
|
|
db,
|
|
owner,
|
|
)
|
|
assert gold_same["batch_id"] == gold_first["batch_id"]
|
|
assert gold_same["reused"] is True
|
|
assert (
|
|
find_reusable_active_payment_task(
|
|
db,
|
|
account.id,
|
|
task_type="create_gold_qr",
|
|
handbook_scope="elite",
|
|
recharge_channel="wechat_qr",
|
|
recharge_amount=60,
|
|
)
|
|
is None
|
|
)
|